<?php if (!defined('PmWiki')) exit();
/*
    commentboxstyled.php
    Copyright 2005 Hans Bracker, an adaptation of commentbox.php by 
    John Rankin, copyright 2004, 2005 John Rankin john.rankin@affinity.co.nz
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    Adds (:commentbox:) and (:commentboxchrono:) markups.
    
    Some styling mods for message type postings by Jefferson 01-01-06

*/
# you may wish to put the style declarations into pub/css/local.css
# and set $CommentStyles = 0; or delete $HTMLStylesFmt definitions below.
SDV($CommentStyles, 1);
if($CommentStyles == 1) {
$HTMLStylesFmt[commentbox] = "
/* styling of commentbox entries */
#message table {border-collapse: collapse; }
div.amessage h5 { 
            margin:0;
            padding:3px 0 0 3px;
            border:1px solid #999;
            background:#e5e5ff;
            }
div.amessage p {
            margin:0;
            padding:3px;
            border-left:1px solid #999;
            border-right:1px solid #999;
            background:#eef;
            }
div.amessage {border-bottom:1px solid #999;
            }
.journalhead { 
            margin:0;
            padding:3px 0 0 3px;
            border:1px solid #999;
            border-top-width:0;
            background:#ffb;
            }
.journalitem, .diaryitem {
            margin:0;
            padding:3px;
            border:1px solid #999;
            border-top-width:0;
            background:#ffc;
            }

.commentbutton { margin:0 0 0 5px; 
                padding:0 3px }
.commenttext { width:100%; 
               margin:0 0 3px 0 }
";}

SDV($DiaryBoxFmt,"<div id='diary'><form action='\$PageUrl' method='post'>
    <input type='hidden' name='n' value='\$FullName' />
    <input type='hidden' name='action' value='comment' />
    <input type='hidden' name='author' value='\$Author' />
    <table width='90%'><tr>
    <th class='prompt' align='right' valign='top'>$[New entry]&nbsp;</th>
    <td><textarea class='inputtext commenttext' name='text' rows='6' cols='50'></textarea><br />
    <input class='inputbutton commentbutton' type='submit' name='post' value=' $[Post] ' />
    <input class='inputbutton commentbutton' type='reset' value='$[Reset]' /></td></tr></table></form></div>");
SDV($CommentBoxFmt,"<div id='message'><form action='\$PageUrl' method='post'>
    <input type='hidden' name='n' value='\$FullName' />
    <input type='hidden' name='action' value='comment' />
    <input type='hidden' name='order' value='\$Chrono' />
    <table width='90%'><tr>
    <th class='prompt' align='right' valign='top'>$[Comment]&nbsp;</th>
    <td><textarea class='inputtext commenttext' name='text' rows='6' cols='50'></textarea>
    </td></tr><tr><th class='prompt' align='right' valign='top'>$[Author]&nbsp;</th>
    <td><input class='inputbox commentauthorbox' type='text' name='author' value='\$Author' size='32' />
    <input class='inputbutton commentbutton' type='submit' name='post' value=' $[Post] ' />
    <input class='inputbutton commentbutton' type='reset' value='$[Reset]' /></td></tr></table></form>
    </div>\n");
SDV($JournalDateFmt,'%d %B %Y');
SDV($JournalTimeFmt,'%H:%M');
SDV($JournalPattern,'/Journal$/');
SDV($DiaryPattern,'/Diary$/');

if ($action == 'comment') 
    SDV($HandleActions['comment'],'HandleCommentPost');
else if ($action=='print' || $action=='publish') 
    Markup('cbox','<block','/\(:commentbox(chrono)?:\)/','');
else {  
    Markup('cbox','<block','/\(:commentbox(chrono)?:\)/e',
        "'<:block>'.str_replace('\$Chrono','$1',
        FmtPageName(\$GLOBALS['CommentBoxFmt'],\$pagename))");
    Markup('dbox','<block','/\(:diarybox:\)/e',
        "'<:block>'.FmtPageName(\$GLOBALS['DiaryBoxFmt'],\$pagename)");
    if (preg_match($JournalPattern,$pagename) ||
        preg_match($DiaryPattern,$pagename)) {
            $GroupHeaderFmt .= '(:if auth edit:)(:diarybox:)(:if:)(:nl:)';
            if (!PageExists($pagename)) $DefaultPageTextFmt = '';
    }
}

function HandleCommentPost($pagename) {
  global $_POST,$JournalPattern,$DiaryPattern;
  global $AuthFunction, $oAuthFunction, $Author, $EnableBypassAuth, $HTMLStylesFmt;
  SDV($AuthFunction,'PmWikiAuth');
  $oAuthFunction = $AuthFunction;
  $AuthFunction = 'BypassAuth';
  if (preg_match($JournalPattern,$pagename)) HandleJournalPost($pagename);
  elseif (preg_match($DiaryPattern,$pagename)) HandleDiaryPost($pagename);
  else HandleMessagePost($pagename);
  HandleEdit($pagename);
  exit;
}

function FormatDateHeading($txt,$datefmt,$fmt) {
  return str_replace($txt,strftime($datefmt,time()),$fmt);
}

## Journal entry
function HandleJournalPost($pagename) {
   global $_POST, $JournalDateFmt, $JournalTimeFmt, $JPDateFmt, $JPTimeFmt,
            $Author;
   SDV($JPDateFmt,'!!!!$Date');
   SDV($JPTimeFmt,"\n>>journalhead<<\n!!!!!&ndash; \$Time &ndash;\n");
   SDV($JPItemStartFmt,">>journalitem<<\n");
   SDV($JPItemEndFmt,"");
   $page = RetrieveAuthPage($pagename,"read");
   if (@$_POST['author']=='') $Author = 'anon';  
   $date = FormatDateHeading('$Date',$JournalDateFmt,$JPDateFmt);
   $time = $date . FormatDateHeading('$Time',$JournalTimeFmt,$JPTimeFmt);
   $entry = $time.$JPItemStartFmt.$_POST['text'].$JPItemEndFmt;  
   $_POST['text'] = (strstr(@$page['text'], $date)) ?
        str_replace($date, $entry, $page['text']) :
        "$entry\n>><<\n\n" . @$page['text'];
}

## Diary entry
function HandleDiaryPost($pagename) {
   global $_POST, $JournalDateFmt, $DPDateFmt, $DPItemFmt, $Author;
   SDV($DPDateFmt,'!!!!$Date'); 
   SDV($DPItemStartFmt,"\n>>diaryitem<<\n");
   SDV($DPItemEndFmt,"");
   $page = RetrieveAuthPage($pagename, "read");
   if (@$_POST['author']=='') $Author = 'anon';  
   $now  = time();
   $date = FormatDateHeading('$Date',$JournalDateFmt,$DPDateFmt);
   $entry = $date.$DPItemStartFmt.$_POST['text'].$DPItemEndFmt;
   $_POST['text'] = (strstr(@$page['text'], $date)) ?
        str_replace($date, $entry, $page['text']) :
        "$entry\n>><<\n\n" . @$page['text'];
}

##  Comment entry
function HandleMessagePost($pagename) {
   global $_POST,$JournalDateFmt,$JournalTimeFmt,$MPDateFmt,$MPTimeFmt,
        $MPItemFmt,$MPDateTimeFmt,$MultipleItemsPerDay,$Author;
   SDV($MPDateFmt,'!!!!$Date');
   SDV($MPTimeFmt,"\n>>amessage<<\n!!!!!\$Author &ndash; [-at \$Time-] \n");
   SDV($MPItemStartFmt,"");			# was >>messageitem<<\n
   SDV($MPItemEndFmt,"\n>><<\n");
   SDV($MPDateTimeFmt,"!!!!\$Date at \$Time by '''\$Author'''\n\n");
   SDV($MultipleItemsPerDay,true);
   $name = @$_POST['author'];
   if (@$_POST['author']=='') $Author = 'anon';
#   $name = ($name=='') ? 'anonymous' : '[[~' . $name . ']]'; # author links
   if ($name=='') $name = 'anonymous'; # no author links
   if ($MultipleItemsPerDay) {
        $date = FormatDateHeading('$Date',$JournalDateFmt,$MPDateFmt);
        $entry = str_replace('$Author',$name,
            FormatDateHeading('$Time',$JournalTimeFmt,$MPTimeFmt));
   } else {
        $date = '';
        $entry = FormatDateHeading('$Date',$JournalDateFmt,
            str_replace('$Author',$name,
            FormatDateHeading('$Time',$JournalTimeFmt,$MPDateTimeFmt)));
   }
   $entry.= $MPItemStartFmt.$_POST['text'].$MPItemEndFmt;
   $page = RetrieveAuthPage($pagename, "read");
   $order= @$_POST['order'];
   if ($order=='') {
      if (strstr($page['text'],'(:commentbox:)')) {
         $pos = strpos($page['text'],'(:commentbox:)');
         $len = strlen('(:commentbox:)');
         $before = substr($page['text'],0,$pos+$len);
         $after  = substr($page['text'],$pos+$len);
      } else {
         $before = '';
         $after  = $page['text'];
      }
      $entry = "$date$entry";
      $after = ($MultipleItemsPerDay && strstr($after, $date)) ? 
            str_replace($date, $entry, $after) : "\n$entry\n$after";
   } else {
      $entry .= "\n";
      if (strstr($page['text'],'(:commentboxchrono:)')) {
         $pos = strpos($page['text'],'(:commentboxchrono:)');
         $before = substr($page['text'],0,$pos);
         $after  = substr($page['text'],$pos);
      } else {
         $before = $page['text'];
         if ($before[strlen($before)-1]!='\n') $before .="\n";
         $after  = '';
      }
      $before .= ($MultipleItemsPerDay && strstr($before, $date)) ? 
            substr($entry,1) : "\n$date$entry";
   }
   $_POST['text'] = "$before$after";
}

function BypassAuth($pagename,$level,$authprompt=true) {
    global $AuthFunction,$oAuthFunction;
    if ($level=='edit') $AuthFunction = $oAuthFunction;
    return $oAuthFunction($pagename,"read",$authprompt);
}

?>