<?php if (!defined('PmWiki')) exit();
/*
  Copyright
  ---------
  Copyright June 2004, Steven Leite (steven_leite@kitimat.net). The same
  terms  and  conditions that apply to PmWiki also apply to this script.

  Description
  -----------
  This add-on allows your PmWiki users to append comments to  the end of
  any page via an html form.  The comments can be formatted according to
  the admin's preferences.  Useful for guestbooks, or adding comments to
  certain pages.

  See also:  http://www.pmwiki.org/wiki/Cookbook/XComment
  
  Installation Instructions
  -------------------------
  1. Save this file to your /local/scripts directory 
     (create the directory if it doesn't exist).
  2. Add this line to your config.php file:
     include_once('local/scripts/x-comment.php');

  Usage Instructions  
  ------------------
  Enter the  [[x-comment:]]  directive on any WikiPage and save it.  Now
  when you view  the page, the directive will be replaced with a comment
  form.  You can use any standard WikiMarkup in form's fields.

  Configuration
  -------------
  It's possible to configure the layout of both the form and the comment
  templates, see the xCommentInit() function below.

  History
  -------
  Jun 2, 2004 - Beta release.  Please report any bugs.                 */
 
function xCommentInit(& $xCommentParams, & $xCommentVars) {
  global $pagename;
  $PageName = FmtPageName('$PageName', $pagename);
  $PageUrl  = FmtPageName('$PageUrl', $pagename);  
  SDVA($xCommentParams, array(
    'width'  => '50', 
	'height' => '2', 
	'wrap'   => 'virtual'));
  $xCommentVars['date_format']      = '%a, %d %b %Y';
  $xCommentVars['time_format']      = '%I:%M%p';
  $xCommentVars['comment_template'] = "\$Comment[[<<]]\n[+-\$Author+] [-(\$Date, \$Time)-]\n----\n[[x-comment:]]";
  $xCommentVars['form_template']    = "<table align=center style='border:1px solid black; background-color:#FFC000;' width='100%'>
  <form action='$PageUrl' method='post'>
   <tr>
     <td class='x_comment' align='right'>Put your comments here</td>
     <td><textarea name='text' rows='" 
	   . $xCommentParams['height'] 
	   . "' cols='" . $xCommentParams['width'] 
	   . "' wrap='" . $xCommentParams['wrap'] . 
	   "'></textarea></td></tr>
   <tr><td class='x_comment' align='right'>Sign your name here</td>
     <td><input type='text' name='author' value='' size='32' />
       <input type='submit' value='  Post  ' />
       <input type='reset' value='Reset' />
       <input type='hidden' name='pagename' value='$PageName' />
       <input type='hidden' name='action' value='post_x_comment' /></td></tr>
    </form>	   
  </table>"; 
}

function ShowXCommentForm($user_input) {
  xCommentInit($xCommentParams, $xCommentVars);
  return Keep("<!--xComment-->\n" 
    . $xCommentVars['form_template'] 
	. "\n<!--/xComment-->");
}  

function HandleXCommentPost($pagename) {
  global $pagename;
  xCommentInit($xCommentParams, $xCommentVars);
  $comment = stripmagic($_POST['text']);
  $author = stripmagic($_POST['author']);
  if ($author=='') 
    {$author = 'Anonymous';}
  $current_epoc_time  = time();
  $date = strftime($xCommentVars['date_format'],$current_epoc_time);
  $time = strftime($xCommentVars['time_format'],$current_epoc_time);
  $template = $xCommentVars['comment_template'];
  $template = str_replace("\$Date", $date, $template);
  $template = str_replace("\$Time", $time, $template);
  $template = str_replace("\$Author", $author, $template);
  $template = str_replace("\$Comment", $comment, $template);
  $existing_page = RetrieveAuthPage($pagename, "edit");
  $text = $existing_page['text'];
  $text = preg_replace("/\\[\\[x-comment:(.*?)\\]\\]/", $template, $text);
  //$text = preg_replace("/^\\[\\[x-comment:(.*?)\\]\\]/", $template, $text);
  $_POST['text'] = $text;
  HandlePost($pagename);
  exit;
}
$DoubleBrackets["/^\\[\\[x-comment:(.*?)\\]\\]/e"] = 'ShowXCommentForm("$1");';
$HandleActions['post_x_comment'] = 'HandleXCommentPost';
?>