1<?php
2/**
3 * @file       clock/syntax.php
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author     Luis Machuca Bezzaza <luis.machuca [at] gulix.cl>
6 * @version    2.0
7 *
8 */
9
10if(!defined('DW_LF')) define('DW_LF',"\n");
11
12if(!defined('DOKU_INC'))
13define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
14if(!defined('DOKU_PLUGIN'))
15define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16require_once(DOKU_PLUGIN.'syntax.php');
17
18class syntax_plugin_clock extends DokuWiki_Syntax_Plugin {
19function getType(){
20	return 'substition';
21}
22
23function getAllowedTypes() {
24	return array();
25}
26
27function getSort(){
28	return 290;
29}
30
31function getPType(){
32	return 'block';
33}
34
35function connectTo($mode) {
36	$this->Lexer->addSpecialPattern (
37	'^\{\{clock\}\}$', $mode, 'plugin_clock');
38}
39
40function handle($match, $state, $pos, Doku_Handler $handler){
41	$data= array();
42	$theJS= $this->getConf('nojs_fallback');
43	// if 'nojs_fallback' is set, we get the time from the server
44	// if 'clock_infopage' contains a link, we convert it
45
46	/* compose the data array */
47	$data['type']    = $this->getConf('clock_type');
48	$data['style']   = $this->getConf('clock_style');
49	$data['text']    = $theJS ? date('H:i:s') : 'clock';
50
51	/* Are we ready yet? */
52	return $data;
53}
54
55function render($mode, Doku_Renderer $renderer, $data) {
56	static $wasrendered= false;
57	if ($wasrendered===true) {return true; }
58	if ($mode == 'xhtml') {
59		$nl   = DW_LF;
60		$cty  = $this->getConf('clock_type');
61		$cid  = $this->getConf('clock_id');
62		$html = $this->_clock_createblock_html($data, $cty);
63		$hbar = ($this->getConf('helpbar')) ? $this->_get_clock_helpbar() : '';
64		$renderer->doc .= <<<EOF
65<div id="clock_wrapper">$html
66$hbar </div><!-- end clock-->
67EOF;
68		$wasrendered= true;
69	} else if ($mode == 'odt') {
70		return false; // may be implemented in the future
71	}
72	else if ($mode == 'text') {
73		if ($wasrendered) return true;
74		$text= ' ['. $c. ' '. date('H:i'). '] ';
75		$renderer->doc .= $text;
76		$wasrendered= true;
77		return true;
78	}
79	/* That's all about rendering that needs to be done, at the moment */
80	return false;
81}
82
83/**
84 * From this point onwards, local (helper) functions are implemented.
85 */
86
87	/**
88     * @fn          dw_clock_helpbar
89     * @brief       Returns the contents of the help bar
90     * The helpbar is displayed only when $conf['helpbar'] is set.
91     */
92private function _get_clock_helpbar () {
93	$p.= '<p class="helpbar" >';
94	$link= $this->getConf('clock_infopage');
95	if (empty($link) ) { // point to plugin page by default
96		$info= '[[doku>plugin:clock|Clock Plugin]]';
97	} else {
98		$info= "[[$link|Info]]";
99	}
100	$info= p_render('xhtml', p_get_instructions($info), $info);
101	$info= trim(substr($info, 4, -5)); // remove <p>, </p>
102	$p.= $info;
103	$p.= '</p>';
104	return $p;
105}
106
107private function _clock_createblock_html($data, $cty='text') {
108	$theType    = $data['type'] ?? 'text';
109	$theStyle   = $data['style'] ?? 'plain';
110	$theText    = $data['text'];
111	$id= $this->getConf('clock_id');
112	$codetext= <<<EOF
113<div class="${theType}"><div id="dw_clock_object" class="${theStyle}"> {1} </div>
114</div>
115EOF;
116	if ($cty=== 'text' || $cty=== 'beats') {
117		$s1= 'default';
118	} else if ($cty === 'binary') {
119		$s1= <<<EOF
120<span class="hh">
121<span></span><span></span><span></span><span></span><span></span>
122</span>
123<span class="mm">
124<span></span><span></span><span></span><span></span><span></span><span></span>
125</span>
126<span class="ss">
127<span></span><span></span><span></span><span></span><span></span><span></span>
128</span>
129EOF;
130	}
131	$codetext= str_replace('{1}', $s1, $codetext);
132	return $codetext;
133}
134
135}
136