1<?php
2/**
3 * @file       clock/syntax.php
4 * @brief      Show a clock in wikipage
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Luis Machuca Bezzaza <luis.machuca [at] gulix.cl>
7 * @version    1.5
8 * @date       2010-10-15
9 * @link       http://www.dokuwiki.org/plugin:clock
10 *
11 *  This plugin's purpose is to display a clock using both
12 *  CSS and JavaScript techniques normally available.
13 *  For a live test point a decent web browser to my wiki.
14 *  http://ryan.gulix.cl/dw/desarrollo/
15 *
16 *  Greetings.
17 *        - Luis Machuca Bezzaza a.k.a. 'ryan.chappelle'
18 */
19
20if(!defined('DW_LF')) define('DW_LF',"\n");
21
22if(!defined('DOKU_INC'))
23define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
24if(!defined('DOKU_PLUGIN'))
25define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
26require_once(DOKU_PLUGIN.'syntax.php');
27
28/**
29 * All DokuWiki plugins to extend the parser/rendering mechanism
30 * need to inherit from this class
31 */
32class syntax_plugin_clock extends DokuWiki_Syntax_Plugin {
33
34    /**
35     * return some info
36     */
37    function getInfo() {
38        return array(
39            'author' => 'Luis Machuca Bezzaza',
40            'email'  => 'luis.machuca [at] gulix.cl',
41            'date'   => '2010-10-15',
42            'name'   => 'Clock Plugin',
43            'desc'   => $this->getLang('descr'),
44            'url'    => 'http://www.dokuwiki.org/plugin:clock',
45        );
46    }
47
48    /**
49     * What kind of syntax are we?
50     */
51    function getType(){
52        return 'substition';
53    }
54
55    /**
56     * What can we Do?
57     */
58    function getAllowedTypes() {
59        return array();
60    }
61
62    /**
63     * Where to sort in?
64     */
65    function getSort(){
66        return 290;
67    }
68
69    /**
70     * What's our code layout?
71     */
72    function getPType(){
73        return 'block';
74    }
75
76    /**
77     * How do we get to the lexer?
78     */
79    function connectTo($mode) {
80         $this->Lexer->addSpecialPattern (
81         '^\{\{clock\}\}$', $mode, 'plugin_clock');
82
83    }
84
85    /**
86     * Handle the match
87     */
88    function handle($match, $state, $pos, &$handler){
89        $data= array();
90        $theCS= $this->getConf('clock_style');
91        $theJS= $this->getConf('nojs_fallback');
92        // if 'nojs_fallback' is set, we get the time from the server
93        // if 'clock_infopage' contains a link, we convert it
94
95        /* compose the data array */
96        $data['style']   = $theCS;
97        $data['text']    = $theJS ? date('H:i:s') : $theCS;
98
99        /* Are we ready yet? */
100        return $data;
101    }
102
103    /**
104     * Create output
105     */
106    function render($mode, &$renderer, $data) {
107      static $wasrendered= false;
108
109      if ($mode == 'xhtml') {
110        if (!$wasrendered) {
111          $nl   = DW_LF;
112          $cid  = $this->_get_clock_object_ID();
113          $html = $this->_clock_createblock_html($data);
114          $hbar = ($this->getConf('helpbar')) ? $this->_get_clock_helpbar() : '';
115          $renderer->doc .= <<<EOF
116<div id="$cid">$nl    $html $hbar$nl    </div><!-- end clock-->
117EOF;
118          $wasrendered= true;
119          }
120        else {
121          $renderer->doc.= <<<EOF
122<p><a href="#$cid" class="wikilink" title="Go To Clock">&#x231a; clock</a></p>
123EOF;
124          }
125        return true;
126        }
127      else if ($mode == 'odt') {
128        return false; // may be implemented in the future
129        }
130      else if ($mode == 'text') {
131        if ($wasrendered) return true;
132        $text= ' ['. $this->_get_clock_object_ID(). ' '. date('H:i'). '] ';
133        $renderer->doc .= $text;
134        $wasrendered= true;
135        return true;
136        }
137      /* That's all about rendering that needs to be done, at the moment */
138      return false;
139    }
140
141    /**
142     * From this point onwards, local (helper) functions are implemented
143     */
144
145    /**
146     * @fn          dw_clock_object_ID
147     * @brief       returns ID for the clock object
148     *  This function sets the name used for the ID of the clock object.
149     *  If you change it, you must also update the #id tags in 'style.css'.
150     */
151    function _get_clock_object_ID () {
152      return $this->getConf('clock_id');
153      }
154
155    /**
156     * @fn          dw_clock_helpbar
157     * @brief       Returns the contents of the help bar
158     * The helpbar is displayed only when $conf['helpbar'] is set.
159     */
160    private function _get_clock_helpbar () {
161      $p.= '<p class="helpbar" >';
162      $link= $this->getConf('clock_infopage');
163      if (empty($link) ) { // point to plugin page by default
164        $info= '[[doku>plugin:clock|Clock Plugin]]';
165        }
166      else {
167        $info= "[[$link|Info]]";
168        }
169      $info= p_render('xhtml', p_get_instructions($info), $info);
170      $info= trim(substr($info, 4, -5)); // remove <p>, </p>
171      $p.= $info;
172      $p.= '</p>';
173      return $p;
174      }
175
176    /**
177     * @fn          _clock_createblock_html
178     * @brief       Creates the HTML code for the clock object.
179     */
180    private function _clock_createblock_html($data) {
181        $theStyle   = $data['style'];
182        $theText    = $data['text'];
183        $theDoLink  = $data['dolink'];
184        $theTarget  = $data['target'];
185        $codetext.= '<div id="clock_face" class="'. $theStyle. ' face">'.
186                    $theText. '</div>';
187        return $codetext;
188     }
189
190}
191