1<?php
2/**
3 * DokuWiki task box plugin
4 * @license    MIT (https://opensource.org/licenses/MIT)
5 * @author     Sherri W. (http://syntaxseed.com)
6 *
7 * Usage:
8 *
9 * <task>
10 * TITLE: A test task
11 * PRIORITY: High
12 * ESTIMATE: 4h
13 * PROGRESS: 10%
14 * ASSIGNED: Sherri
15 * DESCRIPTION: Some stuff for you. You can have newlines in this part. Description must be the last item.
16 * </task>
17 */
18
19if (!defined('DOKU_INC')) {
20    define('DOKU_INC', realpath(dirname(__FILE__).'/../../').'/');
21}
22if (!defined('DOKU_PLUGIN')) {
23    define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
24}
25require_once(DOKU_PLUGIN.'syntax.php');
26
27class syntax_plugin_avtaskbox extends DokuWiki_Syntax_Plugin
28{
29
30    /**
31     * return some info
32     */
33    public function getInfo()
34    {
35        return array(
36            'author' => 'Sherri Wheeler',
37            'email'  => 'Use my website: http://syntaxseed.com',
38            'date'   => '2013-02-25',
39            'name'   => 'AV Task Box',
40            'desc'   => 'Creates task/user story table boxes.',
41            'url'	=> 'http://syntaxseed.com/project/avtaskbox/',
42        );
43    }
44
45    /**
46     * What kind of syntax are we?
47     */
48    public function getType()
49    {
50        return 'substition';
51    }
52
53    /**
54     * Where to sort in?
55     */
56    public function getSort()
57    {
58        return 999;
59    }
60
61
62    /**
63        * Connect pattern to lexer
64        */
65    public function connectTo($mode)
66    {
67        $this->Lexer->addEntryPattern('\<task\>', $mode, 'plugin_avtaskbox');
68    }
69
70    public function postConnect()
71    {
72        $this->Lexer->addExitPattern('\</task\>', 'plugin_avtaskbox');
73    }
74
75
76    /**
77     * Handle the match
78     */
79    public function handle($match, $state, $pos, Doku_Handler $handler)
80    {
81        switch ($state) {
82          case DOKU_LEXER_ENTER:
83            return array($state, '');
84          case DOKU_LEXER_MATCHED:
85            break;
86          case DOKU_LEXER_UNMATCHED:
87
88            $resultStr = '<table class="inline" width="500">';
89
90            preg_match('/^Title:(.*?)$/isxm', $match, $matches);
91            $title = (!empty($matches[1]) && strlen(trim($matches[1]))>0) ? trim($matches[1]) : '&nbsp;';
92
93            preg_match('/^Priority:(.*?)$/isxm', $match, $matches);
94            $priority =(!empty($matches[1]) && strlen(trim($matches[1]))>0) ? 'Priority: '.trim($matches[1]) : '&nbsp;';
95
96            preg_match('/^Estimate:(.*?)$/isxm', $match, $matches);
97            $estimate = (!empty($matches[1]) && strlen(trim($matches[1]))>0) ? ' of '.trim($matches[1]) : '&nbsp;';
98
99            preg_match('/^Assigned:(.*?)$/isxm', $match, $matches);
100            $assigned = (!empty($matches[1]) && strlen(trim($matches[1]))>0) ? '('.trim($matches[1]).')' : '&nbsp;';
101
102            preg_match('/^Progress:(.*?)$/isxm', $match, $matches);
103            $progress = (!empty($matches[1]) && strlen(trim($matches[1]))>0) ? intval(preg_replace('[^0-9]', '', $matches[1])) : '0';
104
105            preg_match('/Description:(.*)/isx', $match, $matches);
106            $description = (!empty($matches[1]) && strlen(trim($matches[1]))>0) ? trim($matches[1]) : '&nbsp;';
107
108            if ($progress<0) {
109                $progress=0;
110            }
111            if ($progress>100) {
112                $progress=100;
113            }
114            $sizeLeft = 100-$progress;
115
116            $progbar .= '<span style="margin-top:3px;padding:0;height:8px;width: 100px;">'.($progress<=0 ? '' : '<span style="margin:0;padding:0;background-color:#74a6c9; height:8px; width:'.$progress.'"><img src="'.rtrim(dirname($_SERVER['PHP_SELF']), "/").'/lib/images/blank.gif" height="8" width="'.$progress.'" border="0" title="'.$progress.'%" alt="'.$progress.'%" hspace="0" vspace="0" style="height:8px;" /></span>') . ($progress>=100 ? '' : '<span style="margin:0;padding:0;background-color: #dee7ec;height:8px;width:'.$sizeLeft.'"><img src="'.rtrim(dirname($_SERVER['PHP_SELF']), "/").'/lib/images/blank.gif" height="8" width="'.$sizeLeft.'" border="0" title="'.$progress.'%" alt="'.$progress.'%" hspace="0" vspace="0" style="height:8px;" /></span>') .'</span>';
117
118            $resultStr .= '<tr class="row0"><th><b>'.$title.'</b><span style="float:right;font-weight:normal;">'.$assigned.'</span></th></tr>';
119            $resultStr .= '<tr><td>'.nl2br($description).'</td></tr>';
120            $resultStr .= '<tr><td><span style="float:right;font-size:0.9em;">('.$progress.'%'.$estimate.') '.$progbar.'</span>'.$priority.'</td></tr>';
121
122
123            $resultStr .= '</table>';
124
125            $match = $resultStr;
126            return array($state, $match);
127
128
129          case DOKU_LEXER_EXIT:
130            return array($state, '');
131          case DOKU_LEXER_SPECIAL:
132            break;
133        }
134        return array();
135    }
136
137
138    /**
139     * Create output
140     */
141    public function render($mode, Doku_Renderer $renderer, $data)
142    {
143        if ($mode == 'xhtml') {
144            list($state, $match) = $data;
145
146            switch ($state) {
147          case DOKU_LEXER_ENTER:
148            $renderer->doc .= "<span class='avtaskbox'>";
149            break;
150
151          case DOKU_LEXER_MATCHED:
152            break;
153
154          case DOKU_LEXER_UNMATCHED:
155
156            $renderer->doc .= $match; break;
157
158          case DOKU_LEXER_EXIT:
159            $renderer->doc .= "</span>";
160            break;
161
162          case DOKU_LEXER_SPECIAL:
163            break;
164        }
165            return true;
166        }
167        return false;
168    }
169}
170