1<?php
2/**
3 * DokuWiki Plugin datepicker (Syntax Component)
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  lisps
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12/*
13 * All DokuWiki plugins to extend the parser/rendering mechanism
14 * need to inherit from this class
15 */
16class syntax_plugin_datepicker extends DokuWiki_Syntax_Plugin
17{
18    var $idwcount = 0;
19	var $iddcount = 0;
20
21    /*
22     * What kind of syntax are we?
23     */
24    function getType() {return 'substition';}
25
26    /*
27     * Where to sort in?
28     */
29    function getSort() {return 155;}
30
31    /*
32     * Paragraph Type
33     */
34    function getPType() {return 'normal';}
35
36    /*
37     * Connect pattern to lexer
38     */
39    function connectTo($mode){
40		$this->Lexer->addSpecialPattern("<datepicker[^>]*>",$mode,'plugin_datepicker');
41		$this->Lexer->addSpecialPattern("<weekpicker[^>]*>",$mode,'plugin_datepicker');
42	}
43
44    /*
45     * Handle the matches
46     */
47    function handle($match, $state, $pos, Doku_Handler $handler) {
48		$mode=trim(substr($match,1,10));
49		$option = trim(substr($match,11,1));
50		//echo $break;
51		$match=trim(substr($match,12,strlen($match)-12-1));
52		if($option === '\\' || $option === '#')
53			$opts["option"] = $option;
54		else
55			$opts["option"] = false;
56
57		if($mode === "datepicker"){
58			$opts["id"]=$this->idwcount++;
59		}
60		else if($mode === "weekpicker"){
61			$opts["id"]=$this->iddcount++;
62		}
63
64		$opts["mode"]=$mode;
65		$opts["date"]=$match;
66		return ($opts);
67    }
68
69    function iswriter(){
70		global $conf;
71		global $INFO;
72
73		return(!$conf['useacl'] || $INFO['perm'] > AUTH_READ);
74	}
75
76    /*
77     * Create output
78     */
79    function render($mode, Doku_Renderer $renderer, $opt) {
80		global $INFO;
81
82		if($mode == 'metadata') return false;
83		if($mode == 'xhtml') {
84			$renderer->nocache();
85			$Hajax = plugin_load('helper', 'ajaxedit');
86			if(!$Hajax){
87				msg('Plugin ajaxedit is missing');
88			}
89
90			if($opt["mode"] == 'weekpicker'){
91				$mode ='week';
92				$empty = hsc($this->getConf('emptyStringWeek'));
93			}
94			else if ($opt["mode"] == 'datepicker'){
95				$mode ='date';
96				$empty = hsc($this->getConf('emptyStringDate'));
97			}
98
99			if($opt['date'] === ""){
100				$opt['date']=$empty;
101			}
102
103			//insert selector if writable
104			if ($this->iswriter()==TRUE && $Hajax) {
105				$id = $opt["id"];
106				$renderer->cdata("\n");
107				$image = DOKU_URL."lib/plugins/datepicker/images/".$mode.".gif";
108				switch($opt['option']){
109					case '#':
110						$renderer->doc .="<a class='".$mode."picker' style='cursor:pointer;' id='".$mode."picker__button__".$id."'>";
111						$renderer->doc .="<img  src='$image' alt='Kalender' onload='".$mode."pickerInit(".$id.",\"".$empty."\")' style='display:none;' />";
112						$renderer->doc .="<span class='".$mode."picker' id='".$mode."picker__show__".$id."' data-plugin-datepicker-idx='".$id."'>";
113						$renderer->doc .= hsc($opt['date']);
114						$renderer->doc .= "</span></a>";
115						break;
116					case '\\':
117						$renderer->doc .="<span class='".$mode."picker' id='".$mode."picker__show__".$id."' data-plugin-datepicker-idx='".$id."'>";
118						$renderer->doc .= hsc($opt['date'])."</span><br>";
119						$renderer->doc .="<img class='".$mode."picker' src='$image' alt='Kalender' style='cursor:pointer;' id='".$mode."picker__button__".$id."' onload='".$mode."pickerInit(".$id.",\"".$empty."\")' />";
120						break;
121					case false:
122						$renderer->doc .="<span class='".$mode."picker' id='".$mode."picker__show__".$id."' data-plugin-datepicker-idx='".$id."'>";
123						$renderer->doc .= hsc($opt['date'])."</span>";
124						$renderer->doc .="<img class='".$mode."picker' src='$image' alt='Kalender' style='cursor:pointer;' id='".$mode."picker__button__".$id."' onload='".$mode."pickerInit(".$id.",\"".$empty."\")' />";
125						break;
126
127				}
128			} else {
129				$renderer->doc .= hsc($opt['date']);
130			}
131		}
132		else {
133			$renderer->doc .= hsc($opt['date']);
134		}
135		return true;
136	}
137
138}
139
140//Setup VIM: ex: et ts=4 enc=utf-8 :
141