1<?php
2/**
3 * DokuWiki Plugin multiselect (Ajax Component)
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  lisps
7 * @author peterfromearth
8 */
9class action_plugin_multiselect extends DokuWiki_Action_Plugin {
10
11    /**
12     * Register the eventhandlers
13     */
14    function register(Doku_Event_Handler $controller) {
15        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());
16        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE',  $this, '_ajax_call');
17    }
18
19    /**
20     * Inserts the toolbar button
21     */
22    function insert_button(Doku_Event $event, $param) {
23        $event->data[] = array(
24            'type'   => 'format',
25            'title' => 'Multiselect',
26            'icon'   => '../../plugins/multiselect/images/toolicon.png',
27            'sample' => 'CHECK HELP',
28            'open' => '<multiselect ',
29            'close'=>'>',
30            'insert'=>'',
31        );
32    }
33
34    function _ajax_call(Doku_Event $event, $param) {
35        if ($event->data !== 'plugin_multiselect') {
36            return;
37        }
38        //no other ajax call handlers needed
39        $event->stopPropagation();
40        $event->preventDefault();
41
42        /* @var $INPUT \Input */
43        global $INPUT;
44
45        #Variables
46        $selectcount = $INPUT->int('index');
47        $smileycount = $INPUT->int('count');
48        $token       = $INPUT->str('token');
49
50        $token= cleanText($token);
51
52        /* @var $Hajax \helper_plugin_ajaxedit */
53        $Hajax = $this->loadHelper('ajaxedit');
54
55        $data=$Hajax->getWikiPage();
56
57        $range_delemiters = array();
58        //remove pagemod area - no changes here
59        $ranges  = preg_split('$<pagemod[\w\W]*?</pagemod>$', $data);
60        $count = preg_match_all('$<pagemod[\w\W]*?</pagemod>$', $data, $range_delemiters);
61
62        if($count) {
63            $range_delemiters = $range_delemiters[0];
64        } else {
65            $range_delemiters = array();
66        }
67
68        //will be set in loop to detect if change has already happened
69        $found_flag = false;
70
71        //will count the <multiselect - need for calculation
72        $found_counter = 0;
73
74        $temp = ''; //old selected icon
75        foreach($ranges as $range_index=>&$range_part){
76            //find "our" multiselect
77            $found=explode("<multiselect",$range_part);
78
79            //selectcount for the specific range
80            $selectcount_range = $selectcount-$found_counter;
81
82            //overall found counter
83            $found_counter += count($found)-1;
84
85            if (!$found_flag && $selectcount < $found_counter) {
86                $found_flag = true;
87                //get smiley collection
88                $stop=strpos($found[$selectcount_range+1],">");
89                if ($stop !== FALSE) {
90                    $oldsmileys=substr($found[$selectcount_range+1],0,$stop);
91
92                    //move selected smiley to front
93                    $ret = preg_match_all('/[\w\[\]\(\)\{\}\|\?\+\-\*\^\$\\\.:!\/;,+#~&%]+|"[\w\[\]\(\)\{\}\|\?\+\-\*\^\$\\\.:!\/;,+#~&%\s]+"/u',trim($oldsmileys),$matches);
94                    $newsmileys=str_replace('"','',$matches[0]);
95                    //$newsmileys=explode(" ",trim($oldsmileys));
96
97                    if ($smileycount < count($newsmileys)) {
98                        $temp=$newsmileys[0];
99                        $newsmileys[0]=$newsmileys[$smileycount];
100                        $newsmileys[$smileycount]=$temp;
101                    }
102                    foreach($newsmileys as $key=>$sm){
103                        if(strpos($sm,' ') !== false){
104                            $newsmileys[$key] = '"'.$sm.'"';
105                        }
106                    }
107
108                    $newsmileys=implode(' ',$newsmileys);
109
110                    //create new pagesource
111                    $found[$selectcount_range+1]=str_replace($oldsmileys," ".$newsmileys." ",$found[$selectcount_range+1]);
112                    $range_part=implode("<multiselect",$found) . (isset($range_delemiters[$range_index])?$range_delemiters[$range_index]:'');
113                }
114            } else {
115                $range_part .= isset($range_delemiters[$range_index])?$range_delemiters[$range_index]:'';
116            }
117        }
118
119        $data = implode($ranges);
120        //Save data and create log
121        $summary = "Multiselect ".$selectcount." changed from \"".hsc($temp)."\" to \"".hsc($token)."\"";
122        $param = array();
123
124        $param['msg'] = sprintf($Hajax->getLang('changed_from_to'),'Multiselect',hsc($temp),hsc($token));
125
126        $Hajax->saveWikiPage($data,$summary,false,$param);
127    }
128}
129