1<?php
2/**
3 * Filterform
4 *
5 * Inserts a form which allows searching and filtering in a list
6 * generated by a datatable (data plugin) or a datatemplatelist
7 * (datatemplate plugin).
8 *
9 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 * @author     Christoph Clausen <christoph.clausen@gmail.com>
11 */
12
13// must be run within Dokuwiki
14if(!defined('DOKU_INC')) die();
15require_once(DOKU_PLUGIN.'syntax.php');
16
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21class syntax_plugin_datatemplate_filterform extends DokuWiki_Syntax_Plugin {
22
23    /**
24     * Get the type of syntax this plugin defines.
25     */
26    function getType(){
27        return 'substition';
28    }
29
30    /**
31     * Define how this plugin is handled regarding paragraphs.
32     */
33    function getPType(){
34        return 'block';
35    }
36
37    /**
38     * Where to sort in?
39     */
40    function getSort(){
41        return 150;
42    }
43
44
45    /**
46     * Connect lookup pattern to lexer.
47     */
48    function connectTo($mode) {
49        $this->Lexer->addSpecialPattern('----+ *filterform(?: [ a-zA-Z0-9_]*)?-+\n.*?\n----+',$mode,'plugin_datatemplate_filterform');
50    }
51
52    /**
53     * Handler to prepare matched data for the rendering process.
54     */
55    function handle($match, $state, $pos, Doku_Handler $handler){
56        $data = array();
57        $lines = explode("\n",$match);
58        foreach ( $lines as $num => $line ) {
59            // ignore comments
60            $line = preg_replace('/(?<![&\\\\])#.*$/','',$line);
61            $line = str_replace('\\#','#',$line);
62            $line = trim($line);
63            if(empty($line)) continue;
64            $line = preg_split('/\s*:\s*/',$line,2);
65            if(strtolower($line[0]) == 'fields') {
66                $data['fields'] = preg_split("/[\s,]+/", $line[1]);
67            }
68        }
69        return $data;
70    }
71
72    /**
73     * Handle the actual output creation.
74     */
75    function render($mode, Doku_Renderer $R, $data) {
76        if($mode == 'xhtml'){
77            /** @var $R Doku_Renderer_xhtml */
78            $R->info['cache'] = false;
79            if (isset($_POST['filterform']) && checkSecurityToken()) {
80                $new_flt = '';
81                if(!empty($_POST['contains'])) {
82                    $new_flt = $_POST['field'] . '*~' . $_POST['contains'];
83                    if(!isset($_REQUEST['dataflt'])){
84                        $flt = array();
85                    } elseif (!is_array($_REQUEST['dataflt'])){
86                        $flt = (array) $_REQUEST['dataflt'];
87                    } else {
88                        $flt = $_REQUEST['dataflt'];
89                    }
90
91                    if(!empty($new_flt)) $flt[] = $new_flt;
92                    $_REQUEST['dataflt'] = $flt;
93                }
94            }
95            $R->doc .= $this->_htmlform($data);
96            return true;
97        }
98        return false;
99    }
100
101    function _htmlform($data){
102        global $ID;
103
104        $form = new Doku_Form(array('class' => 'filterform_plugin', 'action' => wl($ID)));
105        $form->addHidden('filterform', $ID);
106        $form->addElement(form_openfieldset(array('_legend' => $this->getLang('searchfilter'), 'class' => 'filterform')));
107        $form->addElement(form_makeMenuField('field', $data['fields'], '', '', '', 'cell menu'));
108        $form->addElement(form_makeTextField('contains', '', '', '', 'cell text'));
109        if(count($_REQUEST['dataflt']) > 0) {
110            $form->addElement('<div class="group">'.$this->getLang('prevfilters').':</div>');
111            foreach($_REQUEST['dataflt'] as $num=>$flt) {
112                list($key, $value) = explode('*~', $flt);
113                $value = trim($value, '*');
114                $txt = '<i>' . $key . ':</i> ' . $value;
115                $form->addElement(form_checkboxField(
116                    array(
117                        '_text' => $txt, 'name' => 'dataflt[]','value' => $flt,
118                        'checked'=>'true', '_class' => 'row')
119                    ));
120            }
121        }
122        $form->addElement(form_makeButton('submit', '', $this->getLang('submitfilter')));
123        $form->endFieldset();
124
125        return $form->getForm();
126    }
127}
128