1<?php
2
3use dokuwiki\Form\Form;
4
5/**
6 * DokuWiki Plugin tagfilter (Syntax Component)
7 *
8 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author  lisps
10 */
11
12class syntax_plugin_tagfilter_compare extends syntax_plugin_tagfilter_filter
13{
14
15    /*
16     * What kind of syntax are we?
17     */
18    function getType()
19    {
20        return 'substition';
21    }
22
23    /*
24     * Where to sort in?
25     */
26    function getSort()
27    {
28        return 155;
29    }
30
31    /*
32     * Paragraph Type
33     */
34    function getPType()
35    {
36        return 'block';
37    }
38
39    /*
40     * Connect pattern to lexer
41     */
42    function connectTo($mode)
43    {
44        $this->Lexer->addSpecialPattern("\{\{tagcompare>.*?\}\}", $mode, 'plugin_tagfilter_compare');
45    }
46
47    /*
48     * Handle the matches
49     */
50    function handle($match, $state, $pos, Doku_Handler $handler)
51    {
52        $match = trim(substr($match, 13, -2));
53
54        return $this->getOpts($match);
55    }
56
57    /*
58     * Create output
59     */
60    function render($format, Doku_Renderer $renderer, $opt)
61    {
62        global $ID;
63        global $INPUT;
64
65        $flags = $opt['tagfilterFlags'];
66        if ($format === 'metadata') return false;
67        if ($format === 'xhtml') {
68            $renderer->nocache();
69
70            /* @var helper_plugin_tagfilter $Htagfilter */
71            $Htagfilter = $this->loadHelper('tagfilter');
72            /* @var helper_plugin_tagfilter_syntax $HtagfilterSyntax */
73            $HtagfilterSyntax = $this->loadHelper('tagfilter_syntax');
74            $renderer->cdata("\n");
75
76
77            list($tagFilters, $allPageids) = $HtagfilterSyntax->getTagPageRelations($opt);
78            $preparedPages = $HtagfilterSyntax->prepareList($allPageids, $flags);
79
80            //check for read access
81            foreach ($allPageids as $key => $pageid) {
82                if (!$Htagfilter->canRead($pageid)) {
83                    unset($allPageids[$key]);
84                }
85            }
86
87            //check tags for visibility
88            foreach ($tagFilters['pagesPerMatchedTags'] as $pagesPerMatchedTag) {
89                foreach ($pagesPerMatchedTag as $tag => $pageidsPerTag) {
90                    if (count(array_intersect($pageidsPerTag, $allPageids)) == 0) {
91                        unset($pagesPerMatchedTag[$tag]);
92                    }
93                }
94            }
95
96            $dropdownValues = ['' => ''];
97            foreach ($preparedPages as $key => $page) {
98                if (!in_array($page['id'], $allPageids)) {
99                    unset($preparedPages[$key]);
100                }
101                $dropdownValues[$page['id']] = $page['title'];
102            }
103
104            //dbg($INPUT->arr('tagcompare_page'));
105            $selectedValues = $INPUT->arr('tagcompare_page');
106            var_dump($selectedValues);
107            echo '<div class="table plugin_tagcompare">';
108            $form = new Doku_Form([
109                'id' => 'tagcomparedd_' . $opt['id'],
110                'data-plugin' => 'tagcompare',
111                'method' => 'GET',
112            ]);
113            $form->addHidden('id', $ID);
114            $form->addElement('<table>');
115            $form->addElement('<thead>');
116            $form->addElement('<tr>');
117            $form->addElement('<th>');
118            $form->addElement(hsc('Tags'));
119            $form->addElement('</th>');
120
121            for ($ii = 0; $ii < 4; $ii++) {
122                $form->addElement('<th>');
123                $form->addElement(form_makeListboxField('tagcompare_page[' . $ii . ']', $dropdownValues, $selectedValues[$ii] ?? null, '', '', 'tagcompare', []));
124                $form->addElement('</th>');
125            }
126            $form->addElement('</tr>');
127            $form->addElement('</thead>');
128
129            $form->addElement('<tbody>');
130
131            if ($flags['images']) {
132                /** @var helper_plugin_pageimage $HPageimage */
133                $HPageimage = $this->loadHelper('pageimage');
134                $form->addElement('<tr>');
135                $form->addElement('<th></th>');
136                for ($ii = 0; $ii < 4; $ii++) {
137                    $form->addElement('<td>');
138                    if (!empty($selectedValues[$ii])) {
139                        $form->addElement($HPageimage->td($selectedValues[$ii], ['firstimage' => true])); //fixme pageimage->td() does not accept flags as 2nd arg.
140                    }
141                    $form->addElement('</td>');
142                }
143                $form->addElement('</tr>');
144            }
145            // for each tagexpression a row is added, where tags that match the tagexpression are shown if they are also
146            // used on the page selected for that table column
147            foreach ($tagFilters['pagesPerMatchedTags'] as $idx => $pagesPerMatchedTag) {
148                $form->addElement('<tr>');
149                $form->addElement('<th>');
150                $form->addElement(hsc($tagFilters['label'][$idx]));
151                $form->addElement('</th>');
152
153                for ($ii = 0; $ii < 4; $ii++) {
154                    $form->addElement('<td>');
155                    //more tags per cell(=per page) possible
156                    foreach ($pagesPerMatchedTag as $tagName => $pageidsPerTag) {
157                        if (isset($selectedValues[$ii]) & in_array($selectedValues[$ii], $pageidsPerTag)) {
158                            $form->addElement(hsc($Htagfilter->getTagLabel($tagName)) . '<br>');
159                        }
160                    }
161                    $form->addElement('</td>');
162                }
163
164                $form->addElement('</tr>');
165            }
166
167            $form->addElement('<tr>');
168            $form->addElement('<th>');
169            $form->addElement('Link');
170            $form->addElement('</th>');
171
172            for ($ii = 0; $ii < 4; $ii++) {
173
174                $form->addElement('<th>');
175
176                if (!empty($selectedValues[$ii])) {
177                    $form->addElement('<a href="' . wl($selectedValues[$ii]) . '" class="wikilink1">Link</a>');
178                }
179
180                $form->addElement('</th>');
181
182            }
183            $form->addElement('</tr>');
184
185            $form->addElement('</tbody>');
186            $form->addElement('</table>');
187            $form->addElement('</div>');
188
189
190            $renderer->doc .= $form->getForm();
191        }
192        return true;
193    }
194
195}
196