<?php if (!defined('PmWiki')) exit();
/*
    forumstyled.php
    Copyright 2005 Hans Bracker,

    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 (:newforumtopic:) markup and a (:pagelist fmt=forum:) option.
    Use in conjunction with the commentboxstyled.php script.
*/
# Version date
$RecipeInfo['SimpleForum']['Version'] = '2019-06-02';

# apply some styling to Forum topics table,
# most styling is done via styles of commentboxstyled.
# styles could also, as an alternative, be added to pub/css/local.css
$group = PageVar($pagename, '$Group');
if(strpos($group,"Forum")!==false) {
     $HTMLStylesFmt['forum'] = "
          table.forum { border-left:1px solid #aaa;
                border-top:1px solid #aaa; }
          table.forum td.messagehead { padding:3px; }
          table.forum td.messageitem, table.forum td.messagehead {
                border:none;
                border-right:1px solid #aaa;
                border-bottom:1px solid #aaa; }
     ";
 }


# defines text to be displayed when new topic page gets created
# you can add a forum to any group with the word 'Forum' in it,
# like ChessForum, ChessForumForAll.
if(strstr($group,'Forum'))
$DefaultPageTextFmt = '$[Compose message, add author name, and click post]';

# markup (:newforumtopic:) for forum HomePage
function sf_NewTopic($m) {
    extract($GLOBALS["MarkupToHTML"]);
    return NewForumTopic($pagename, $m[1]);
}
Markup('newforumtopic', '>links',
  '/\\(:newforumtopic\\s*(.*?):\\)/i',
  "sf_NewTopic");

# creates a "Create New Topic" box (adaptation of newpagebox)
function NewForumTopic($pagename, $opt) {
  global $ScriptUrl, $PageUrl;
  $defaults = array(
        'base' => '',
        'template' => '',
        'label' => FmtPageName(' $[Go] ', $pagename),
        'button' => 'right',
        'size'   => '30',
        'value' => FmtPageName(' $[Create New Topic] ', $pagename),
        'focus' => 'true',
        );
  $opt = array_merge($defaults, ParseArgs($opt));
  $buttonHTML = "<input class='inputbutton newpagebutton' type='submit' value='{$opt['label']}' />";
  $onfocusHTML = "onfocus=\"if(this.value=='{$opt['value']}') {this.value=''}\"
                  onblur=\"if(this.value=='') {this.value='{$opt['value']}'}\" ";
  $out = "\n <form class='newpage' action='{$PageUrl}' method='post'>
    <input type='hidden' name='n' value='$pagename' />
    <input type='hidden' name='action' value='newtopic' />
    <input type='hidden' name='value' value='{$opt['value']}' />
    <input type='hidden' name='focus' value='{$opt['focus']}' />
    <input type='hidden' name='base' value='{$opt['base']}' />
    <input type='hidden' name='template' value='{$opt['template']}' />" .
    ($opt['button']=="left" ? $buttonHTML : "") .
    "<input class='inputbox newpagetext' type='text' name='name' value='{$opt['value']}' size='{$opt['size']}'" .
    ($opt['focus']=="true" ? $onfocusHTML : "") .
    "/>" .
    ($opt['button']=="right" ? $buttonHTML : "") .
    "</form>";
    return Keep($out);
 }

$HandleActions['newtopic'] = 'HandleNewTopic';

# modified HandleNewPage function from new
function HandleNewTopic($pagename) { echo "NEWTOPIC";
  #strip any group from name, restricts topic pages to be in the same group
  $_REQUEST['name'] = preg_replace('/^.*[\\/.]/', '', $_REQUEST['name']);
  $name = @$_REQUEST['name'];
  if (!$name) Redirect($pagename);
  if (@$_REQUEST['focus'] && $name==$_REQUEST['value']) Redirect($pagename);
  $base = MakePageName($pagename, $_REQUEST['base']);
  $basegroup = PageVar($base, '$Group');
  if (@$_REQUEST['base']=='') $newpage = MakePageName($pagename, $name);
  else $newpage = MakePageName($base, "$basegroup.$name");
  $urlfmt = '$PageUrl?action=browse';
  if (@$_REQUEST['template'])
    $urlfmt .= '&template=' . MakePageName($base, $_REQUEST['template']);
  Redirect($newpage, $urlfmt);
}

/* The format of the pagelist displaying the topics on the forum homepage
   can be defined either by (:pagelist ... fmt=forum :) using the
   FPLFormatOpt function below, or by using a pagelist format template
   from a wiki page. For instance by adding the following to
   Site.PageListTemplate, and using (:pagelist ... fmt=#forum :). Note the # !!
   Sample format template, which may be easier to modify than the function below:

!!!fmt=#forum
forum topics in table format
[@
[[#forum]]
(:if equal {<$Group}:)
(:table class=forum width=100% cellpadding=0 cellspacing=0:)
(:cell class=messagehead :)'''Topic'''
(:cell class=messagehead :)'''Posts'''
(:cell class=messagehead :)'''Last Posted'''
(:cell class=messagehead :)'''By''' (:if:)
(:cellnr class=messageitem :)[[{=$FullName}|+]]
(:cell class=messageitem :){=$PostCount}
(:cell class=messageitem :){=$LastModified}
(:cell class=messageitem :){=$LastModifiedBy}
(:if equal {>$Group}:)
(:tableend:)(:if:)
[[#forumend]]
@]

*/

# adds fmt=forum to pagelist options.
# FPLForum provides table listing Page Titles, PostCount, LastModified time
# and LastModifiedBy author
# usage: (:pagelist group={$Group} list=normal fmt=forum:)
$FPLFormatOpt['forum']['fn'] = 'FPLForum';
$FPLFunctions['forum'] = 'FPLForum';    # for pmwiki pre 2.1.beta15
function FPLForum($pagename, &$matches, $opt) {
 global $FPLForumStartFmt, $FPLForumEndFmt, $FPLForumGFmt,
   $FPLForumIFmt, $FPLForumOpt;
 SDV($FPLForumStartFmt,"<div class='fplforum'>");
 SDV($FPLForumEndFmt,'</table></div>');
 SDV($FPLForumGFmt,"\n<h3>Topic Index </h3>
     <table class='forum' width='100%' cellpadding=0 cellspacing=0>
     <tr><td class='messagehead'>topic</td><td class='messagehead'>posts</td>
     <td class='messagehead'>last posted</td><td class='messagehead'>by</td></tr>\n");
 SDV($FPLForumIFmt,"<tr class='messageitem'>
     <td class='messageitem'><a href='\$PageUrl'>\$Titlespaced</a></td>
     <td class='messageitem' align=right>\$PostCount &nbsp;</td>
     <td class='messageitem'>\$LastModified</td>
     <td class='messageitem'> \$LastModifiedBy</td></tr>\n");
 SDVA($FPLForumOpt, array('readf' => 0, 'order' => '-time'));
 $matches = MakePageList($pagename, array_merge($FPLForumOpt, $opt));
 if (@$opt['count']) array_splice($matches, $opt['count']);
 $out = array();
 foreach($matches as $pc) {
   $pgroup = FmtPageName($FPLForumGFmt, $pc['pagename']);
   if ($pgroup != @$lgroup) { $out[] = $pgroup; $lgroup = $pgroup; }
   $out[] = FmtPageName($FPLForumIFmt, $pc['pagename']);
 }
 return FmtPageName($FPLForumStartFmt, $pagename) . implode('', $out) .
            FmtPageName($FPLForumEndFmt, $pagename);
}