1<?php
2/**
3 * DokuWiki Plugin datepicker (Action Component)
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author lisps
7 * @author peterfromearth
8 */
9
10if (!defined('DOKU_INC')) die();
11class action_plugin_datepicker extends DokuWiki_Action_Plugin {
12	/**
13	 * Register the eventhandlers
14	 */
15	public function register(Doku_Event_Handler $controller) {
16		$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());
17		$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE',  $this, '_ajax_call');
18	}
19
20	/**
21	 * Inserts the toolbar button
22	 */
23	public function insert_button(Doku_Event $event, $param) {
24		$event->data[] = array(
25			'type'   => 'picker',
26			'list' =>array('<datepicker>','<datepicker\>','<datepicker#>','<weekpicker>','<weekpicker\>','<weekpicker#>'),
27			'title' => 'Datepicker',
28			'icon'   => '../../plugins/datepicker/images/date.gif',
29			'sample' => '',
30			//'open' => '<datepicker>',
31			'close'=>'',
32			'insert'=>'',
33		);
34	}
35
36	public function _ajax_call(Doku_Event $event, $param) {
37	    if ($event->data !== 'plugin_datepicker') {
38	        return;
39	    }
40	    //no other ajax call handlers needed
41	    $event->stopPropagation();
42	    $event->preventDefault();
43
44	    /* @var $INPUT \Input */
45	    global $INPUT;
46
47	    #Variables
48	    $datecount = $INPUT->int('id');
49	    $datestr   = $INPUT->str('datestr');
50	    $mode 	   = $INPUT->str('mode');
51
52	    /* @var $Hajax \helper_plugin_ajaxedit */
53	    $Hajax = $this->loadHelper('ajaxedit');
54
55	    if($mode !== "datepicker" && $mode !== "weekpicker"){
56	        $Hajax->error('unknown mode');
57	        return;
58	    }
59
60	    $data=$Hajax->getWikiPage();
61
62	    $range_delemiters = array();
63	    //remove pagemod area - no changes here
64	    $ranges  = preg_split('$<pagemod[\w\W]*?</pagemod>$',$data);
65	    $count = preg_match_all('$<pagemod[\w\W]*?</pagemod>$',$data,$range_delemiters);
66
67	    if($count) {
68	        $range_delemiters = $range_delemiters[0];
69	    } else {
70	        $range_delemiters = array();
71	    }
72
73	    //will be set in loop to detect if change has already happened
74	    $found_flag = false;
75
76	    //will count the <multiselect - need for calculation
77	    $found_counter = 0;
78
79	    foreach($ranges as $range_index=>&$range_part){
80	        //find "our" datepicker
81	        $found=explode('<'.$mode,$range_part);
82
83	        //selectcount for the specific range
84	        $datecount_range = $datecount-$found_counter;
85
86	        //overall found counter
87	        $found_counter += count($found)-1;
88
89	        if (!$found_flag && $datecount < $found_counter) {
90	            $found_flag = true;
91
92	            $olddatestr = "none";
93	            $option= '';
94	            if($found[$datecount_range+1][0] === '\\'){
95	                $option = '\\';
96	                $found[$datecount_range+1]=substr($found[$datecount_range+1],1);
97	            }
98	            else if($found[$datecount_range+1][0] === '#'){
99	                $option = '#';
100	                $found[$datecount_range+1]=substr($found[$datecount_range+1],1);
101	            }
102	            $found[$datecount_range+1] = ltrim($found[$datecount_range+1]);
103	            $stop=strpos($found[$datecount_range+1],">");
104	            if ($stop === FALSE) {
105	                $Hajax->error('Cannot find object, please contact your admin!');
106	            }
107	            else if ($stop > 0) {
108	                $olddatestr=substr($found[$datecount_range+1],0,$stop);
109	                $found[$datecount_range+1]=str_replace($olddatestr,$option." ".$datestr." ",$found[$datecount_range+1]);
110	            }
111	            else if ($stop == 0) {
112	                $found[$datecount_range+1]= $option." ".$datestr . $found[$datecount_range+1];
113	            }
114	            //create new pagesource
115	            $range_part=implode('<'.$mode,$found). (isset($range_delemiters[$range_index])?$range_delemiters[$range_index]:'');
116
117
118	        } else {
119	            $range_part .= isset($range_delemiters[$range_index])?$range_delemiters[$range_index]:'';
120	        }
121	    }
122
123	    $data = implode($ranges);
124
125	    $param = array(
126	        'mode'	=> $mode,
127	        'index'	=> $datecount
128	    );
129	    $summary= $mode.' '.$datecount." changed from ".$olddatestr." to ".$datestr;
130
131	    $param['msg'] = sprintf($Hajax->getLang('changed_from_to'),hsc($mode),hsc($olddatestr),hsc($datestr));
132
133	    $Hajax->saveWikiPage($data,$summary,true,$param);
134	}
135}
136