1<?php
2
3// Search form in a navbar
4
5
6// must be run within Dokuwiki
7if(!defined('DOKU_INC')) die();
8
9
10class syntax_plugin_webcomponent_search extends DokuWiki_Syntax_Plugin {
11
12    function getType() {
13        return 'substition';
14    }
15
16    function getPType() {
17        return 'block';
18    }
19
20    function getAllowedTypes() {
21        return array();
22    }
23
24    function getSort() {
25        return 201;
26    }
27
28
29
30    function connectTo($mode) {
31
32        $this->Lexer->addSpecialPattern('<' . self::getTag() . '[^>]*>',$mode,'plugin_' . webcomponent::PLUGIN_NAME . '_' . $this->getPluginComponent());
33
34    }
35
36    function handle($match, $state, $pos, Doku_Handler $handler) {
37
38        switch ($state) {
39
40            case DOKU_LEXER_SPECIAL :
41                $init = array(
42                    'ajax' => true,
43                    'autocomplete' => true
44                );
45                $match = utf8_substr($match, strlen($this->getPluginComponent()) + 1, -1);
46                $parameters = array_merge($init, webcomponent::parseMatch($match));
47                return array($state, $parameters);
48
49        }
50        return array();
51
52    }
53
54    function render($mode, Doku_Renderer $renderer, $data) {
55
56        if ($mode == 'xhtml') {
57
58            /** @var Doku_Renderer_xhtml $renderer */
59            list($state,$parameters)=$data;
60            switch ($state) {
61                case DOKU_LEXER_SPECIAL :
62
63                    global $lang;
64                    global $ACT;
65                    global $QUERY;
66
67                    // don't print the search form if search action has been disabled
68                    if (!actionOK('search')) return false;
69
70                    $renderer->doc .= '<form action="' . wl() . '" accept-charset="utf-8" id="dw__search" method="get" role="search" class="search form-inline ';
71                    if (array_key_exists("class", $parameters)) {
72                        $renderer->doc .= ' '.$parameters["class"];
73                    }
74                    $renderer->doc .= '">' . DOKU_LF;
75                    $renderer->doc .= '<input type="hidden" name="do" value="search" />';
76                    $renderer->doc .=  '<label class="sr-only" for="search">Search Term</label>';
77                    $renderer->doc .=  '<input type="text" tabindex="1"';
78                    if ($ACT == 'search') $renderer->doc .= 'value="' . htmlspecialchars($QUERY) . '" ';
79                    $renderer->doc .= 'placeholder="' . $lang['btn_search'] . '..." ';
80                    if (!$parameters['autocomplete']) $renderer->doc .= 'autocomplete="off" ';
81                    $renderer->doc .= 'id="qsearch__in" accesskey="f" name="id" class="edit form-control" title="[F]"/>';
82                    if ($parameters['ajax']) $renderer->doc .= '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>';
83                    $renderer->doc .= '</form>';
84                    break;
85            }
86            return true;
87        }
88
89        // unsupported $mode
90        return false;
91    }
92
93    public static function getTag()
94    {
95        list(/* $t */, /* $p */, /* $n */, $c) = explode('_', get_called_class(), 4);
96        return (isset($c) ? $c : '');
97    }
98
99}
100
101