1<?php
2use dokuwiki\Form\Form;
3
4/**
5 * DokuWiki Plugin tagfilter (Syntax Component)
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author  lisps
9 */
10
11/*
12 * All DokuWiki plugins to extend the parser/rendering mechanism
13 * need to inherit from this class
14 */
15class syntax_plugin_tagfilter_compare extends DokuWiki_Syntax_Plugin {
16
17    private $_itemPos = array();
18    function incItemPos() {
19        global $ID;
20        if(array_key_exists($ID,$this->_itemPos)) {
21            return $this->_itemPos[$ID]++;
22        } else {
23            $this->_itemPos[$ID] = 1;
24            return 0;
25        }
26    }
27    function getItemPos(){
28        global $ID;
29        if(array_key_exists($ID,$this->_itemPos)) {
30            $this->_itemPos[$ID];
31        } else {
32            return 0;
33        }
34    }
35
36    /*
37     * What kind of syntax are we?
38     */
39    function getType() {return 'substition';}
40
41    /*
42     * Where to sort in?
43     */
44    function getSort() {return 155;}
45
46    /*
47     * Paragraph Type
48     */
49    function getPType(){return 'block';}
50
51    /*
52     * Connect pattern to lexer
53     */
54    function connectTo($mode) {
55		$this->Lexer->addSpecialPattern("\{\{tagcompare>.*?\}\}",$mode,'plugin_tagfilter_compare');
56	}
57
58    /*
59     * Handle the matches
60     */
61    function handle($match, $state, $pos, Doku_Handler $handler) {
62		global $ID;
63		$HtagfilterSyntax = $this->loadHelper('tagfilter_syntax');
64		$opts['id']=$this->incItemPos();
65
66		$match=trim(substr($match,13,-2));
67
68        list($match, $flags) = explode('&', $match, 2);
69        $flags = explode('&', $flags);
70        list($ns, $tag) = explode('?', $match);
71
72        if (!$tag) {
73            $tag = $ns;
74            $ns   = '';
75        }
76
77        if (($ns == '*') || ($ns == ':')) $ns = '';
78        elseif ($ns == '.') $ns = getNS($ID);
79        else $ns = cleanID($ns);
80
81		$opts['ns']=$ns;
82
83		//only flags for tagfilter
84		$opts['tfFlags'] = $HtagfilterSyntax->parseFlags($flags);
85
86		//all flags for pagelist plugin
87		$opts['flags']=array_map('trim',$flags);
88
89		//read and parse tag
90		$tagselect_r = array();
91		$select_expr_r = array_map('trim',explode('|',$tag));
92		foreach($select_expr_r as $key=>$usr_syntax){
93			$usr_syntax = explode("=",$usr_syntax);//aufsplitten in Label,RegExp,DefaultWert
94
95			$tagselect_r['label'][$key]  = trim($usr_syntax[0]);
96			$tagselect_r['tag_expr'][$key]  = trim($usr_syntax[1]);
97			$tagselect_r['selectedTags'][$key] = isset($usr_syntax[2])?explode(' ',$usr_syntax[2]):array();
98		}
99
100		$opts['tagselect_r'] = $tagselect_r;
101
102		return ($opts);
103    }
104
105    /*
106     * Create output
107     */
108    function render($mode, Doku_Renderer $renderer, $opt)
109	{
110		global $INFO;
111		global $ID;
112		global $INPUT;
113		global $USERINFO;
114		global $conf;
115
116		$flags = $opt['tfFlags'];
117		if($mode === 'metadata') return false;
118		if($mode === 'xhtml') {
119			$renderer->info['cache'] = false;
120
121			/* @var $Htagfilter helper_plugin_tagfilter */
122			$Htagfilter = $this->loadHelper('tagfilter');
123			$HtagfilterSyntax = $this->loadHelper('tagfilter_syntax');
124			$renderer->cdata("\n");
125
126
127			$cachedata = $HtagfilterSyntax->getTagPageRelations($opt);
128			$cachedata[] = $HtagfilterSyntax->prepareList($cachedata[1],$flags);
129
130			list($tagselect_r, $pageids, $preparedPages) = $cachedata;
131
132			//check for read access
133			foreach($pageids as $key=>$pageid) {
134				if(! $Htagfilter->canRead($pageid)) {
135					unset($pageids[$key]);
136				}
137			}
138
139			//check tags for visibility
140			foreach($tagselect_r['tagPages'] as &$select_r) {
141				foreach($select_r as $tag=>$pageid_r) {
142					if(count(array_intersect(($pageid_r), $pageids)) == 0) {
143						unset($select_r[$tag]);
144					}
145				}
146			}
147
148			$dropdownValues = array(''=>'');
149			foreach($preparedPages as $key=>$page) {
150				if(!in_array($page['id'],$pageids)) {
151					unset($preparedPages[$key]);
152				}
153				$dropdownValues[$page['id']] = $page['title'];
154			}
155
156			//dbg($INPUT->arr('tagcompare_page'));
157			$selectedValues = $INPUT->arr('tagcompare_page');
158			echo '<div class="table plugin_tagcompare">';
159			$form = new Doku_Form(array(
160			    'id'=>'tagcomparedd_'.$ii,
161			    'data-plugin'=>'tagcompare',
162			    'method' => 'GET',
163			));
164			$form->addHidden('id', $ID);
165			$form->addElement('<table>');
166			$form->addElement('<thead>');
167			$form->addElement('<tr>');
168			$form->addElement('<th>');
169			$form->addElement(hsc('Tags'));
170			$form->addElement('</th>');
171
172            for($ii = 0; $ii < 4; $ii++) {
173                $form->addElement('<th>');
174                $form->addElement(form_makeListboxField('tagcompare_page['.$ii.']', $dropdownValues, isset($selectedValues[$ii])?$selectedValues[$ii]:null , '', '', 'tagcompare' , array()));
175                $form->addElement('</th>');
176            }
177            $form->addElement('</tr>');
178            $form->addElement('</thead>');
179
180			$form->addElement('<tbody>');
181
182			if($flags['images']) {
183			    /** @var $HPageimage helper_plugin_pageimage */
184			    $HPageimage = $this->loadHelper('pageimage');
185    			$form->addElement('<tr>');
186    			$form->addElement('<th></th>');
187    			for($ii = 0; $ii < 4; $ii++) {
188    			    $form->addElement('<td>');
189    			    if(isset($selectedValues[$ii]) && !empty($selectedValues[$ii])) {
190    			        $form->addElement($HPageimage->td($selectedValues[$ii],array('firstimage' => true)));
191    			    }
192    			    $form->addElement('</td>');
193    			}
194    			$form->addElement('</tr>');
195			}
196			foreach($tagselect_r['tagPages'] as $idx => $tag_r) {
197			    $form->addElement('<tr>');
198			    $form->addElement('<th>');
199			    $form->addElement(hsc($tagselect_r['label'][$idx]));
200			    $form->addElement('</th>');
201
202			    for($ii = 0; $ii < 4; $ii++) {
203			        $form->addElement('<td>');
204			        foreach($tag_r as $tagName => $tags) {
205    			        if(in_array($selectedValues[$ii],$tags)) {
206    			             $form->addElement(hsc($Htagfilter->getTagLabel($tagName)). '<br>');
207    			        }
208			        }
209			        $form->addElement('</td>');
210			    }
211
212			    $form->addElement('</tr>');
213			}
214
215			$form->addElement('<tr>');
216			$form->addElement('<th>');
217			$form->addElement('Link');
218			$form->addElement('</th>');
219
220			for($ii = 0; $ii < 4; $ii++) {
221
222			    $form->addElement('<th>');
223
224			    if(isset($selectedValues[$ii]) && !empty($selectedValues[$ii])) {
225			     $form->addElement('<a href="'.wl($selectedValues[$ii]).'" class="wikilink1">Link</a>');
226			    }
227
228			    $form->addElement('</th>');
229
230			}
231			$form->addElement('</tr>');
232
233			$form->addElement('</tbody>');
234			$form->addElement('</table>');
235			$form->addElement('</div>');
236
237
238			$renderer->doc .= $form->getForm();
239		}
240		return true;
241	}
242
243
244
245}
246
247//Setup VIM: ex: et ts=4 enc=utf-8 :
248