1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/**
6 * DokuWiki Plugin acknowledge (Syntax Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Andreas Gohr, Anna Dabrowska <dokuwiki@cosmocode.de>
10 */
11class syntax_plugin_acknowledge_listing extends SyntaxPlugin
12{
13    /** @inheritDoc */
14    public function getType()
15    {
16        return 'substition';
17    }
18
19    /** @inheritDoc */
20    public function getPType()
21    {
22        return 'block';
23    }
24
25    /** @inheritDoc */
26    public function getSort()
27    {
28        return 155;
29    }
30
31    /** @inheritDoc */
32    public function connectTo($mode)
33    {
34        $this->Lexer->addSpecialPattern('~~ACKNOWLEDGE.*?~~', $mode, 'plugin_acknowledge_listing');
35    }
36
37    /** @inheritDoc */
38    public function handle($match, $state, $pos, Doku_Handler $handler)
39    {
40        // check for 'all' parameter
41        $includeDone = strtolower(substr($match, strlen('~~ACKNOWLEDGE '), -2)) === 'all';
42        return ['includeDone' => $includeDone];
43    }
44
45    /** @inheritDoc */
46    public function render($mode, Doku_Renderer $renderer, $data)
47    {
48        if ($mode !== 'xhtml') {
49            return false;
50        }
51
52        $renderer->info['cache'] = false;
53
54        $renderer->doc .= '<div class="plugin-acknowledge-listing">';
55        $renderer->doc .= $this->getListing($data['includeDone']);
56        $renderer->doc .= '</div>';
57        return true;
58    }
59
60    /**
61     * Returns the list of pages to be acknowledged by the user,
62     * optionally including past acknowledgments.
63     *
64     * @param bool $includeDone
65     *
66     * @return string
67     */
68    protected function getListing($includeDone)
69    {
70        global $INPUT;
71        global $USERINFO;
72
73        $user = $INPUT->server->str('REMOTE_USER');
74        if ($user === '') return '';
75
76        $groups = $USERINFO['grps'];
77
78        /** @var helper_plugin_acknowledge $helper */
79        $helper = plugin_load('helper', 'acknowledge');
80        $items = $helper->getUserAssignments($user, $groups, $includeDone);
81
82        $html =  $this->getLang('ackNotFound');
83
84        if (!empty($items)) {
85            $html = '<ul>';
86            foreach ($items as $item) {
87                $done = $item['ack'] ?
88                    ' <span title="'
89                    . sprintf($this->getLang('ackGranted'), dformat($item['ack']))
90                    . '">&#x2714;</span>'
91                    : '';
92                $html .= '<li>' . html_wikilink(':' . $item['page']) . $done . '</li>';
93            }
94            $html .= '</ul>';
95        }
96
97        return $html;
98    }
99}
100