1<?php
2/**
3 * Translation Plugin: Simple multilanguage plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13class helper_plugin_namespacesearch extends DokuWiki_Plugin {
14
15    function tpl_searchform($namespaces, $return = false) {
16        global $QUERY;
17        $cur_val = isset($_REQUEST['namespace']) ? $_REQUEST['namespace'] : '';
18        $lang = preg_quote($this->getLangCode(), '/');
19        $cur_val = preg_replace('/^'.$lang.':/', '', $cur_val);
20
21        ob_start();
22        $QUERY = hsc(preg_replace('/ ?@\S+/','',$QUERY));
23        tpl_searchform(true, false);
24        $searchForm = ob_get_contents();
25        ob_end_clean();
26
27        // Default Select
28        if ( count($namespaces) == 1 ) {
29            // Only one of them. All of them.
30                list($ns, $name, $class) = $namespaces[0];
31            $namespaceSelect =  '<input name="namespace" value="'.hsc($ns).'" type="hidden"/>';
32        } else {
33            $namespaceSelect =  '<select class="namespacesearch_namespace" name="namespace">';
34            foreach ($namespaces as $element) {
35                list($ns, $name, $class) = $element;
36                $namespaceSelect .= '<option class="namespacesearch_ns_'.hsc($class).'" value="'.hsc($ns).'"'.($cur_val === $ns ? ' selected="selected"' : '').'>'.$name.'</option>';
37            }
38            $namespaceSelect .= '</select>';
39        }
40
41        // Insert reight at the beginning.
42        $searchForm = substr_replace($searchForm, $namespaceSelect, strpos($searchForm, '<input'), 0);
43
44        if ( $return ) {
45            return '<div id="dokuwiki__sitetools" class="namespacesearch__container">'.$searchForm.'</div>';
46        } else {
47            print '<div class="namespacesearch__container">'.$searchForm.'</div>';
48        }
49    }
50
51    private function translatedNamespace($id) {
52        global $conf;
53
54        if ($id === '') return '';
55        static $lang = null;
56        if ($lang === null) {
57            $lang = $this->getLangCode();
58            if ($lang !== '') {
59                $lang .= ':';
60            }
61        }
62
63        if (page_exists($lang . $id . ':' . $conf['start'])) return $lang . $id;
64        return $id;
65    }
66
67    private function getLangCode() {
68        if (!isset($_SESSION[DOKU_COOKIE]['translationlc']) || empty($_SESSION[DOKU_COOKIE]['translationlc'])) {
69            return '';
70        }
71        return $_SESSION[DOKU_COOKIE]['translationlc'];
72    }
73}
74
75