1<?php
2/**
3 * DokuWiki select plugin (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Ikuo Obataya <I.Obataya@gmail.com>
7 *
8 */
9if(!defined('DOKU_INC')) die();
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13class syntax_plugin_select extends DokuWiki_Syntax_Plugin {
14    function getType() { return 'substition';}
15    function getPType() { return 'block';}
16    function getSort(){return 168;}
17    function connectTo($mode) {
18        $this->Lexer->addSpecialPattern('<select.+?</select>', $mode, 'plugin_select');
19    }
20
21    /**
22     * Handle the match
23     */
24    function handle($match, $state, $pos, Doku_Handler $handler) {
25        global $ID;
26
27        $match = substr($match, 7, -9);  // strip markup
28        list($title, $match) = explode('>', $match, 2);
29        $items = explode("\n", $match);
30
31        return array($title,$items);
32    }
33
34    /**
35     * Create output
36     */
37    function render($mode, Doku_Renderer $renderer, $data) {
38        global $conf;
39        if($mode == 'xhtml'){
40            list($title,$items) = $data;
41            $s  = '<form class="dw_pl_select">'.NL;
42            $s .= '<select onChange="location.href = this.options[this.selectedIndex].value">'.NL;
43            $s .= '<option selected>'.hsc($title).'</option>'.NL;
44
45            // loop for each option item
46            $link_items = array();
47            foreach($items as $item){
48
49                // split title
50                $link = preg_split('/\|/u',$item,2);
51                $arg1 = trim($link[0]);
52
53                if (empty($arg1)){continue;}
54                $name = trim($link[1]);
55                if (empty($name)){$name = $arg1;}
56
57                // The following code is partly identical to
58                // internallink function in /inc/parser/handler.php
59
60                //decide which kind of link it is
61                if ( preg_match('/^[a-zA-Z\.]+>{1}.*$/u',$arg1) ) {
62                // Interwiki
63                    $interwiki = preg_split('/>/u',$arg1);
64                    // $renderer = new Doku_Renderer();
65                    $url = $renderer->_resolveInterWiki($interwiki[0],$interwiki[1]);
66                }elseif ( preg_match('/^\\\\\\\\[\w.:?\-;,]+?\\\\/u',$arg1) ) {
67                // Windows Share
68                    $url = $arg1;
69                }elseif ( preg_match('#^([a-z0-9\-\.+]+?)://#i',$arg1) ) {
70                // External
71                    $url = $arg1;
72                }elseif ( preg_match('!^#.+!',$arg1) ){
73                // local link
74                    $url = substr($arg1,1);
75                }else{
76                // internal link
77                    $url = wl($arg1);
78                }
79                $s .= '<option value="'.hsc($url).'">'.hsc($name).'</option>'.NL;
80
81                $link_items[] = '<a href="'.hsc($url).'" title="'.hsc($name).'">'.hsc($name).'</a>';
82            }
83            $s .= '</select>'.NL;
84
85            $s .= '<noscript>|';
86            foreach ($link_items as $value){ $s .= $value.'|'; }
87            $s .= '</noscript>'.NL;
88
89            $s .= '</form>'.NL;
90
91            $renderer->doc.=$s;
92            return true;
93        }
94        return false;
95    }
96
97}
98