Add/View Comments
Version |
Date |
Changes |
1.0 |
2006-12-01 |
Initial version. |
Want to build an Online Help Manual "livedoc" via PHP
At the bottom of each help page there should be an option to add/view comments button.
Getting Started
So there are a lot of instances where you want to add something dynamic to output created in ePublisher. In many cases you can do this with Javascript but this is a problem when you want to do something that persists across different clients. A great example would comments. It would be really nice if an API you just documented and published via ePublisher could be commented on. This way developers or engineers could add code snippets and the like to small aspects of the documentation. This could be really difficult, but with a little PHP we can make a quick comment system in no time at all.
Step One: Google PHP
There are tons of great PHP tutorials that you can look into. Also the PHP website has very helpful documentation that has great comments! In the comment section of the PHP website documentation you will often find code snippets that you can use as well as helpful tips that can get you past any gotchas.
Also, PHP is not the only language you can use. Virtually any language can be used to make CGI scripts or accept requests. Ask around your developers/engineers and see if they have a half a day to help out making a slick comment system for the output.
Modifying the Page Template
The first step is to modify the Page.asp with the code shown in the attached ExamplePageAsp.zip
At the very top you will notice we have removed the XML declaration (<?xml version="1.0" ?>) because this interferes with the PHP parsing of the page. At the very top you will also see
This is a simple PHP session that we will use to prevent against someone refreshing the page and reposting the same comment. You may also need this if you are using PHP for something such as using a user login.
At the bottom of the Page Template you will see more php code:
1 <?php
2 // Helpful Functions
3
4 /*
5 * Simple function to display input forms for comments.
6 *
7 * We are just printing/echo'ing some forms. You could change them here.
8 */
9 function display_forms() {
10 echo "<form action='" . $_SERVER['PHP_SELF'] . "' method='post'>\n";
11 echo " <fieldset name='comment-forms'>\n";
12 echo " <legend>Add a Comment</legend>\n";
13 echo " <label for='comment-title'>Title:</label>\n";
14 echo " <input type='text' id='comment-title' name='title' maxlength='255' value='" . $_POST['title'] . "'/><br />\n";
15 echo " <label for='comment-content'>Comment:</label><br />\n";
16 echo " <textarea name='content' id='comment-content' rows='10' cols='40'>" . $_POST['content'] . "</textarea><br />\n";
17 echo " <input type='submit' name='submit' value='Add Comment' />\n";
18 echo " </fieldset>\n";
19 echo "</form>\n";
20 }
21
22 /*
23 * This displays the comments by reading the comments file for the
24 * the specific file and prints it out.
25 */
26 function display_comments() {
27 $comment_file = rtrim(__FILE__, '.php') . '.comments.txt';
28 if (is_file($comment_file)) {
29 echo file_get_contents($comment_file);
30 }
31 }
32
33
34 /*
35 * Adds a comment to the comment file.
36 * The comment file is a simple text file in the same directory as the file
37 *
38 * So, if the filename is MyFrameDoc.1.4.html
39 * In ePublisher we change the extension to .php -> MyFrameDoc.1.4.php
40 * Which makes the comment file -> MyFrameDoc.1.4.comments.txt
41 */
42 function add_comment($title, $content) {
43
44 // Check the session to see if this is a repost by the same user.
45 //
46 if ($_SESSION['comment-title'] == $title && $_SESSION['comment-content'] == $content) {
47 return "";
48 }
49
50 /*
51 * Create a quick bit of HTML to save in the comment file.
52 * There should probably be some more checking here but for now it should be ok
53 *
54 * WARNING: Your sys-admin will say this is a security hole. Please listen to him/her
55 * and try to improve things.
56 */
57 $new_comment_string = "<div class='comment-block>\n";
58 $new_comment_string .= " <h4>" . htmlentities($title) . "</h4>\n";
59 // making new lines actual line breaks
60 $new_comment_string .= " <div class='comment-content'>" . nl2br(htmlentities($content)) . "</div>\n";
61 $new_comment_string .= "</div>\n";
62
63 // Comment filename is the same as the requested file except with an extension
64 // of 'comments.txt'
65 $comment_file = rtrim(__FILE__, '.php') . '.comments.txt';
66
67 // open the comment file for writing only at the beginning, if it is not there create it.
68 $comment_file_handle = fopen($comment_file, "a+");
69 if (fwrite($comment_file_handle, $new_comment_string)) {
70 fclose($comment_file_handle);
71 $_SESSION['comment-title'] = $title;
72 $_SESSION['comment-content'] = $content;
73 return "Comment Added!";
74 } else {
75 fclose($comment_file_handle);
76 return "Error writing comment file. Please contact myemail@somedomain.com.";
77 }
78 }
79
80
81 /*
82 * This is the actual logic of the script.
83 */
84 if ($_POST['submit'] == "Add Comment") {
85 if ($_POST['title'] != '' && $_POST['content'] != '') {
86 echo add_comment($_POST['title'], $_POST['content']);
87 display_forms();
88 } else {
89 echo "Please fill in the title and the content fields.";
90 display_forms();
91 }
92 } else {
93 display_forms();
94 }
95 display_comments();
96 ?>
Essentially all this code does is displays some forms and allows the user to post the values back to the page. The result will be a file in the same directory as the file with a filename in the format of:
- MY_OUTPUT_FILE.1.5.comments.txt
In order to use PHP in the output, the server must support PHP and the file might need a ".php" extension to tell the server that the file should be parsed by the PHP parser. If you have any questions regarding this aspect, just ask your sys-admin.
More Options
This example uses PHP but any server language could be used just as easily. For example, ASP .NET also allows similar embedding of code within the output. JSPs or Java Server Pages also work in a similar fashion. The point being is that your system administrator can most likely easily help in adding simple additions that improve the user experience.
Online resources
Ajax – This allows someone to post or “ask the server” for some information without reloading the browser page
Ajax Apple Tutorial - http://developer.apple.com/internet/webcontent/xmlhttpreq.html
Simple Ajax Example - http://www.degraeve.com/reference/simple-ajax-example.php
Ajax with Prototype (A javascript library used in Ruby on Rails http://rubonrails.com ) - http://24ways.org/advent/easy-ajax-with-prototype
The Complete Message Box Tutorial http://biorust.com/tutorials/detail/110/us/