1<?php
2/**
3 * DokuWiki Plugin CountdownV2 (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Frank Kosanke <digger@error-404.de>
7 */
8class syntax_plugin_countdownv2 extends \dokuwiki\Extension\SyntaxPlugin
9{
10    /** @inheritDoc */
11    public function getType()
12    {
13        return 'substition';
14    }
15
16    /** @inheritDoc */
17    public function getPType()
18    {
19        return 'normal';
20    }
21
22    /** @inheritDoc */
23    public function getSort()
24    {
25        return 999;
26    }
27
28    /** @inheritDoc */
29    public function connectTo($mode)
30    {
31        $this->Lexer->addSpecialPattern('<countdown=.+?>', $mode, 'plugin_countdownv2');
32        // $this->Lexer->addEntryPattern('<FIXME>', $mode, 'plugin_countdown');
33    }
34
35    // /** @inheritDoc */
36    // public function postConnect()
37    // {
38    //     $this->Lexer->addExitPattern('</FIXME>', 'plugin_countdownv2');
39    // }
40
41    /** @inheritDoc */
42    public function handle($match, $state, $pos, Doku_Handler $handler)
43    {
44        return array(substr($match, 11, -1));
45    }
46
47    /** @inheritDoc */
48    public function render($mode, Doku_Renderer $renderer, $data)
49    {
50        // if ($mode !== 'xhtml') {
51        //    return false;
52        // }
53        // return true;
54        $color = $this->getConf('colordefault'); // Standardfarbe
55        $unit = "";                              // Einheit
56        $result = "";                            // Zeitstring Ergebnis
57        $comment = "";                           // Kommentarstring
58        $commentstr = "";                        // Kommentarstring ohne Datum und Zeit
59
60        // String zerlegen
61        $parts = explode("|", $data[0]);
62        if (isset($parts[1])) {
63            $commentstr = trim($parts[1]);               // Kommentarstring
64        }
65        // Type ermitteln und Zeitstring ermitteln
66        $typestr = substr(trim($parts[0]),0,1);          // Zeichen für den Typ
67        $timestr = substr(trim($parts[0]),2);            // Zeitstring
68
69        $starttime = time();
70        $endtime = strtotime($timestr);
71        $difftime = $endtime - $starttime;
72
73        // verbleibende Zeit ermitteln und formatieren
74        switch ($typestr) {
75        case "Y":
76            $result = ceil($difftime / (60*60*24*365));
77            $unit = $this->getLang('year');
78            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('years'); }
79            if ($difftime <= $this->getConf('yearwarning')) { $color = $this->getConf('colorwarning'); }
80            break;
81        case "M":
82            $result = ceil($difftime / (60*60*24*31));
83            $unit = $this->getLang('month');
84            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('months'); }
85            if ($difftime <= $this->getConf('monthwarning')) { $color = $this->getConf('colorwarning'); }
86            break;
87        case "W":
88            $result = ceil($difftime / (60*60*24*7));
89            $unit = $this->getLang('week');
90            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('weeks'); }
91            if ($difftime <= $this->getConf('weekwarning')) { $color = $this->getConf('colorwarning'); }
92            break;
93        case "D":
94            $result = ceil($difftime / (60*60*24));
95            $unit = $this->getLang('day');
96            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('days'); }
97            if ($difftime <= $this->getConf('daywarning')) { $color = $this->getConf('colorwarning'); }
98            break;
99        case "h":
100            $result = ceil($difftime / (60*60));
101            $unit = $this->getLang('hour');
102            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('hours'); }
103            if ($difftime <= $this->getConf('hourwarning')) { $color = $this->getConf('colorwarning'); }
104            break;
105        case "m":
106            $result = ceil($difftime / 60);
107            $unit = $this->getLang('minute');
108            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('minutes'); }
109            if ($difftime <= $this->getConf('minutewarning')) { $color = $this->getConf('colorwarning'); }
110            break;
111        }
112
113        // Kommentar hinzufügen
114        if (strlen($commentstr) > 0) {
115            // Datum einfügen
116            $comment = str_replace("##DATE##", strftime($this->getConf('dateformat'), $endtime), $commentstr);
117            // Zeit einfügen
118            $comment = str_replace("##TIME##", strftime($this->getConf('timeformat'), $endtime), $comment);
119            // Datum und Zeit einfügen
120            $comment = str_replace("##DATETIME##", strftime($this->getConf('datetimeformat'), $endtime), $comment);
121            // Leerzeichen einfügen
122        }
123
124        // Wenn Zeit abgelaufen dann Farbe auf kritisch setzen
125        if ($endtime < $starttime) {
126            $color = $this->getConf('colorcritical');
127        }
128
129        // Ausgabe erstellen
130        $renderer->doc .= "<span style=\"color:" . $color . ";\">" . $result . "&nbsp;" . $unit . "</span>&nbsp;" . $comment;
131        return true;
132    }
133}
134
135