1<?php
2if(!defined('DOKU_INC')) die();
3
4class action_plugin_fancysearch extends DokuWiki_Action_Plugin {
5
6    /**
7     * Register its handlers with the DokuWiki's event controller
8     */
9    function register(Doku_Event_Handler $controller) {
10        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER',  $this, '_fixquery');
11    }
12
13    /**
14     * Put namespace into search
15     */
16    function _fixquery(Doku_Event &$event, $param) {
17        global $QUERY;
18        global $ACT;
19
20        if($ACT != 'search'){
21            $QUERY = '';
22            return;
23        }
24
25        if(trim($_REQUEST['namespace'])){
26            $QUERY .= ' @'.trim($_REQUEST['namespace']);
27        }
28    }
29
30    function tpl_searchform($namespaces) {
31        global $QUERY;
32        $cur_val = isset($_REQUEST['namespace']) ? $_REQUEST['namespace'] : '';
33        $lang = preg_quote($this->getLangCode(), '/');
34        $cur_val = preg_replace('/^'.$lang.':/', '', $cur_val);
35
36        echo '<form method="post" action="" accept-charset="utf-8">';
37        echo '<select class="fancysearch_namespace" name="namespace">';
38        foreach ($namespaces as $ns => $class){
39            echo '<option value="'.hsc($ns).'"'.($cur_val === $ns ? ' selected="selected"' : '').'>'.hsc($ns).'</option>';
40        }
41        echo '</select>';
42
43        echo '<div id="fancysearch__ns_custom" class="closed" style="display: none;">';
44        echo '<ul>';
45        foreach ($namespaces as $ns => $class) {
46            echo '<li class="fancysearch_ns_'.hsc($class).'">'.hsc($this->translatedNamespace($ns)).'</li>';
47        }
48        echo '</ul>';
49        echo '</div>';
50
51        echo '<input type="hidden" name="do" value="search" />';
52        echo '<input type="hidden" id="qsearch__in" value="" />';
53        echo '<input class="query" id="fancysearch__input" type="text" name="id" autocomplete="off" value="'.hsc(preg_replace('/ ?@\S+/','',$QUERY)).'" accesskey="f" />';
54        echo '<input class="submit" type="submit" name="submit" value="Search" />';
55        echo '</form>';
56        echo '<div id="qsearch__out" class="ajax_qsearch JSpopup"></div>';
57    }
58
59    function translatedNamespace($id) {
60        global $conf;
61
62        if ($id === '') return '';
63        static $lang = null;
64        if ($lang === null) {
65            $lang = $this->getLangCode();
66            if ($lang !== '') {
67                $lang .= ':';
68            }
69        }
70
71        if (page_exists($lang . $id . ':' . $conf['start'])) return $lang . $id;
72        return $id;
73    }
74
75    private function getLangCode() {
76        if (!isset($_SESSION[DOKU_COOKIE]['translationlc']) || empty($_SESSION[DOKU_COOKIE]['translationlc'])) {
77            return '';
78        }
79        return $_SESSION[DOKU_COOKIE]['translationlc'];
80    }
81}
82