1<?php
2/**
3 * Info rtmchecklist: Show a button to send a Checklist to RTM.
4 *
5 * @license     GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author      Fabien Lisiecki
7 *
8 */
9
10if(!defined('DOKU_INC')) die();
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13if(!defined('RTMCHECKLIST_IMG_ABSDIR')) define('RTMCHECKLIST_IMG_ABSDIR', DOKU_PLUGIN."rtmchecklist/images");
14
15require_once(DOKU_INC.'inc/search.php');
16
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21class syntax_plugin_rtmchecklist extends DokuWiki_Syntax_Plugin {
22
23   /**
24    * Get the type of syntax this plugin defines.
25    *
26    * @param none
27    * @return String <tt>'substition'</tt> (i.e. 'substitution').
28    * @public
29    * @static
30    */
31    function getType() {
32        return 'substition';
33    }
34
35   /**
36    * Where to sort in?
37    *
38    * @param none
39    * @return Integer <tt>999</tt>.
40    * @public
41    * @static
42    */
43    function getSort(){
44        return 999;
45    }
46
47
48   /**
49    * Connect lookup pattern to lexer.
50    *
51    * @param $aMode String The desired rendermode.
52    * @return none
53    * @public
54    * @see render()
55    */
56    function connectTo($mode) {
57      $this->Lexer->addSpecialPattern('<rtmchecklist>.*?</rtmchecklist>',$mode,'plugin_rtmchecklist');
58    }
59
60
61   /**
62    * Handler to prepare matched data for the rendering process.
63    *
64    * <p>
65    * The <tt>$aState</tt> parameter gives the type of pattern
66    * which triggered the call to this method:
67    * </p>
68    * <dl>
69    * <dt>DOKU_LEXER_ENTER</dt>
70    * <dd>a pattern set by <tt>addEntryPattern()</tt></dd>
71    * <dt>DOKU_LEXER_MATCHED</dt>
72    * <dd>a pattern set by <tt>addPattern()</tt></dd>
73    * <dt>DOKU_LEXER_EXIT</dt>
74    * <dd> a pattern set by <tt>addExitPattern()</tt></dd>
75    * <dt>DOKU_LEXER_SPECIAL</dt>
76    * <dd>a pattern set by <tt>addSpecialPattern()</tt></dd>
77    * <dt>DOKU_LEXER_UNMATCHED</dt>
78    * <dd>ordinary text encountered within the plugin's syntax mode
79    * which doesn't match any pattern.</dd>
80    * </dl>
81    * @param $aMatch String The text matched by the patterns.
82    * @param $aState Integer The lexer state for the match.
83    * @param $aPos Integer The character position of the matched text.
84    * @param $aHandler Object Reference to the Doku_Handler object.
85    * @return Integer The current lexer state for the match.
86    * @public
87    * @see render()
88    * @static
89    */
90    function handle($match, $state, $pos, Doku_Handler $handler){
91        $tasks = array();
92        $start = false;
93        $end   = false;
94        switch ($state) {
95          case DOKU_LEXER_ENTER :
96            $start = true;
97            break;
98          case DOKU_LEXER_MATCHED :
99            break;
100          case DOKU_LEXER_UNMATCHED :
101            break;
102          case DOKU_LEXER_EXIT :
103            $end = true;
104            break;
105          case DOKU_LEXER_SPECIAL :
106            $start = true;
107            $end   = true;
108            $match    = substr($match, 14, -15);
109            $tasks = array();
110            //split tasks in lines
111            $tasks = preg_split('/[\|\n]/u', $match);
112            break;
113          default:
114        }
115        return array('tasks' => $tasks, 'start' => $start, 'end' => $end);
116    }
117
118
119   /**
120    * Handle the actual output creation.
121    *
122    * <p>
123    * The method checks for the given <tt>$aFormat</tt> and returns
124    * <tt>FALSE</tt> when a format isn't supported. <tt>$aRenderer</tt>
125    * contains a reference to the renderer object which is currently
126    * handling the rendering. The contents of <tt>$aData</tt> is the
127    * return value of the <tt>handle()</tt> method.
128    * </p>
129    * @param $aFormat String The output format to generate.
130    * @param $aRenderer Object A reference to the renderer object.
131    * @param $aData Array The data created by the <tt>handle()</tt>
132    * method.
133    * @return Boolean <tt>TRUE</tt> if rendered successfully, or
134    * <tt>FALSE</tt> otherwise.
135    * @public
136    * @see handle()
137    */
138    function render($mode, Doku_Renderer $renderer, $data) {
139        if($mode == 'xhtml'){
140            if($data['start'])
141            {
142                global $ID;
143                $renderer->doc .= '<form action="' . $_SERVER['PHP_SELF'] .'" accept-charset="utf-8" id="form_rtmchecklist" method="post" enctype="multipart/form-data"><lu>';
144                $renderer->doc .= '<input type="hidden" name="id" value="' . $ID . '"/>';
145                $renderer->doc .= '<input type="hidden" name="do" value="plugin_rtmsend"/>';
146                $renderer->doc .= '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />';
147            }
148            $full_list = "";
149            foreach($data['tasks'] as $i=> $item) {
150                if(strlen(preg_replace('/[\s\n\r]+/', '', $item))>0) {
151                    $renderer->doc .= '<li>'.htmlspecialchars($item).'</li>';
152                    $full_list.=htmlspecialchars($item)."\n";
153                }
154            }
155            $renderer->doc .= '<input type="hidden" name="taskslist" value="'.$full_list.'"/>';
156            if($data['end'])
157            {
158                $renderer->doc .= '</lu>';
159                //  onclick=\"sendRtmChecklist()\" ?
160                $renderer->doc .= '<input type="submit" class="button" value="'.$this->getLang('send').'" ></submit></form><br/ >';
161            }
162            return true;
163        }
164        return false;
165    }
166
167} //rtmchecklist class end
168