<?php if (!defined('PmWiki')) exit();
/* 
   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.

   This file adds an action=search capability that displays search results
   in the current page. It also turns the search box into a jump box.
*/

$HTMLStylesFmt[] = "
div#wikihead { padding-top: 10px; }
dl.fplbygroup { margin-left: 40px; }
dl.fplbygroup dd { margin-bottom: 0; }
input.publish { margin-left: 40px; }";
SDV($HandleActions['search'],'HandleSearch');
SDV($PageSearchFmt,array("<h1 class='wikiaction'>$[Search Results]</h1>",
                         'markup:(:searchresults fmt=custom list=normal:)',
                         'wiki:$[PmWiki.SearchQuickReference]'));
$FPLFunctions['custom'] = 'FPLCustom';
SDV($NoMatchesFmt,"<div class='indent'><em>No matches.</em></div>");
SDV($SearchResultsFmt,"\$[SearchFor]
  </p>$HTMLVSpace\$MatchList
  $HTMLVSpace\$[SearchFound]$HTMLVSpace<p>");
$SearchTagFmt = ($action=='search' || preg_match('/SearchWiki$/',$pagename)) ? 
    'Searching Wiki' :
    "<form action='\$PageUrl' method='get'>
  <a href='$ScriptUrl/$[Main/SearchWiki]'>$[Search Wiki]</a>:
  <input type='hidden' name='n' value='\$FullName' />
  <input type='hidden' name='action' value='search' />
  <input class='searchbox' type='text' name='q' value='\$Group/' />
  <input class='searchbutton' type='submit' value='$[Go]' /></form>";
Markup('searchbox','>links','/\(:searchbox:\)/',
  FmtPageName("<form class='wikisearch' action='\$PageUrl' method='get'>
    <input type='hidden' name='n' value='\$FullName' />
    <input type='hidden' name='action' value='search' />
    <input class='wikisearchbox' type='text' name='q' value='' size='40' />
    <input class='wikisearchbutton' type='submit' value='$[Search]' /></form>",
    $pagename));

SDV($ActDescription,'last modified');
if ($action=='search') {
  $ActDescription = 'search performed';
  $CurrentTime = strftime($TimeFmt,$Now);
  $text = stripmagic($HTTP_GET_VARS['q']); $jumpto='';
  if (preg_match("/^$GroupPattern([\\/.])$NamePattern$/",$text))
    $jumpto=$text;
  elseif (preg_match("/^$NamePattern$/",$text)) 
    $jumpto = (isset($HTTP_GET_VARS['group'])) ?
        stripmagic($HTTP_GET_VARS['group']).".$text" :
        FmtPageName("\$Group.$text",$pagename);
  if ($jumpto && PageExists($jumpto)) Redirect($jumpto);
}

SDV($PublishSearch,false);
SDV($PublishSearchChecked,false);
SDV($EnablePDF,false);
XLSDV('en',array(
  'SearchFor' => 'Results of searching <em>$Haystack</em> for <em>$Needle</em>.',
  'SearchFound' => '$MatchCount pages found out of $MatchSearched pages searched.'));

if ($PublishSearch) {
    if ($action=='search' || FmtPageName('$Group',$pagename)=='Category') {
        $checked = ($PublishSearchChecked) ? "checked='checked'" : '';
        $FPLByGroupStartFmt =
            "<form target='_blank' action='\$PageUrl' method='get'>
    <dl class='fplbygroup'>";
        $FPLByGroupEndFmt = "</dl>$HTMLVSpace
    <input type='hidden' name='action' value='publish' />";
        $FPLByGroupEndFmt .= ($action=='search') ?
            "<input type='hidden' name='needle' value='\$Needle' />
    <input type='hidden' name='haystack' value='\$Haystack' />" :
            "<input type='hidden' name='category' value='\$FullName' />";
        $FPLByGroupEndFmt .= ($EnablePDF) ?
    "<input type='checkbox' name='format' value='pdf' checked='checked' class='publish'/>
    <img src='\$PubDirUrl/images/pdficonsmall.gif' align='top' alt='PDF' />
    <input type='submit' value='$[Publish]' /></form>" :
            "<input type='submit' value='$[Publish]' class='publish'/></form>";
        $FPLByGroupGFmt = "<dt><a href='\$ScriptUrl/\$Group'>\$Group</a></dt>";
        $FPLByGroupIFmt = 
    "<dd><input type='checkbox' name='pagearray[]' value='\$Group/\$Name' $checked/>
     <a href='\$PageUrl'>\$Title</a></dd>";
     }
}

function Needle($incl,$excl) {
  $n = str_replace('$LQ$RQ','','$LQ'.join('$RQ and $LQ',$incl).'$RQ');
  foreach($excl as $w) { $n .= " not \$LQ$w\$RQ"; }
  if ($n) return str_replace('$LQ','&lsquo;',
                 str_replace('$RQ','&rsquo;',htmlspecialchars(trim($n))));
  else return 'all';
}

function FPLCustom($pagename,&$pagelist,$opt) {
  global $NoMatchesFmt,$FmtV,$FmtP;
  $FmtV['$Haystack'] = ($GLOBALS['SearchGroup']) ? 
    '&lsquo;'.$GLOBALS['SearchGroup'].'&rsquo; group' : 'all groups';
  $FmtV['$Needle'] = Needle($GLOBALS['SearchIncl'],$GLOBALS['SearchExcl']);
  return ($FmtV['$MatchCount']==0) ? $NoMatchesFmt :
     FPLByGroup($pagename,$pagelist,$opt);
}

function HandleSearch($pagename) {
  global $HandleSearchFmt,$PageStartFmt,$PageSearchFmt,$PageEndFmt,$FmtP;
  $FmtP['/\\$LastModified/e'] = '$GLOBALS[\'CurrentTime\']';
  SDV($HandleSearchFmt,array(&$PageStartFmt, &$PageSearchFmt, &$PageEndFmt));
  PrintFmt($pagename,$HandleSearchFmt);
}

?>