1<?php
2/**
3 * Plugin Data Search Form: Inserts a search form for the data plugin in any page
4
5 * @author     Hans-Juergen Schuemmer
6 * adapted from the original plugin "Searchform"
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     Adolfo González Blázquez <code@infinicode.org>
10 */
11
12/**
13 * Syntax:  {datasearchform flt=<filter>}
14 */
15
16// must be run within Dokuwiki
17if(!defined('DOKU_INC')) die();
18
19/**
20 * All DokuWiki plugins to extend the parser/rendering mechanism
21 * need to inherit from this class
22 */
23class syntax_plugin_datasearchform extends DokuWiki_Syntax_Plugin {
24
25    /**
26     * Syntax Type
27     *
28     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
29     * @return string
30     */
31    public function getType() {
32        return 'substition';
33    }
34
35    /**
36     * Sort order when overlapping syntax
37     * @return int
38     */
39    public function getSort() {
40        return 138;
41    }
42
43    /**
44     * @param $mode
45     */
46    public function connectTo($mode) {
47        $this->Lexer->addSpecialPattern('\{datasearchform\b.*?\}', $mode, 'plugin_datasearchform');
48    }
49
50    /**
51     * Handler to prepare matched data for the rendering process
52     *
53     * @param   string $match   The text matched by the patterns
54     * @param   int $state   The lexer state for the match
55     * @param   int $pos     The character position of the matched text
56     * @param   Doku_Handler $handler Reference to the Doku_Handler object
57     * @return  array Return an array with all data you want to use in render
58     */
59    public function handle($match, $state, $pos, Doku_Handler $handler) {
60        $match = trim(substr($match,15,-1)); //strip {datasearchform (length 15) from start and } from end
61        list($key,$value) = explode('=', $match, 2);
62
63        $options['filter'] = null;
64
65        if(isset($key) && $key == 'flt') {
66            $options['filter'] = cleanID($value);
67        }
68        return array($options, $state, $pos);
69    }
70
71    /**
72     * The actual output creation.
73     *
74     * @param   $format   string        output format being rendered
75     * @param   $renderer Doku_Renderer reference to the current renderer object
76     * @param   $data     array         data created by handler()
77     * @return  boolean                 rendered correctly?
78     */
79    public function render($format, Doku_Renderer $renderer, $data) {
80        global $lang, $INFO, $ACT, $QUERY;
81
82        if($format == 'xhtml') {
83            list($options,,) = $data;
84
85            // don't print the search form if search action has been disabled
86            if(!actionOK('search')) return true;
87
88			$flt = $options['filter'];
89			$flt = "dataflt[" . $flt . "*~]";
90
91            $ns = $INFO['namespace'];
92
93            /** based on tpl_datasearchform() 	*/
94            $renderer->doc .= '<div class="datasearchform__form">' . "\n";
95            $renderer->doc .= '<form action="' . wl() . '" accept-charset="utf-8" class="search" id="datasearchform__search" method="get" role="search"><div class="no">' . "\n";
96            $renderer->doc .= '<input type="hidden" name="id" value="' . $ns . ':datatable" />' . "\n";
97            $renderer->doc .= '<input type="text" ';
98            if($ACT == 'search') $renderer->doc .= 'value="' . htmlspecialchars($QUERY) . '" ';
99            $renderer->doc .= 'name="'. $flt .'" class="edit datasearchform__qsearch_in" />' . "\n";
100            $renderer->doc .= '<input type="submit" value="' . $lang['btn_search'] . '" class="button" title="' . $lang['btn_search'] . '" />' . "\n";
101            $renderer->doc .= '<div class="ajax_qsearch JSpopup datasearchform__qsearch_out"></div>' . "\n";
102            $renderer->doc .= '</div></form>' . "\n";
103            $renderer->doc .= '</div>' . "\n";
104
105            return true;
106        }
107        return false;
108    }
109}
110