xref: /plugin/acknowledge/action/notification.php (revision 4bf952c9005c7028e75b1ab7185e9d7f7b566d80)
1<?php
2
3/**
4 * DokuWiki Plugin acknowledge (Notification Action Component)
5 *
6 * Integration with the notification plugin.
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Anna Dabrowska <dokuwiki@cosmocode.de>
10 */
11
12use dokuwiki\Extension\ActionPlugin;
13use dokuwiki\Extension\EventHandler;
14use dokuwiki\Extension\Event;
15use dokuwiki\Extension\AuthPlugin;
16
17class action_plugin_acknowledge_notification extends ActionPlugin
18{
19    /** @inheritDoc */
20    public function register(EventHandler $controller)
21    {
22        $controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'registerSource');
23        $controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'gatherNotifications');
24        $controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'cacheDependencies');
25    }
26
27    /**
28     * Announce acknowledge as a notification source.
29     *
30     * @param Event $event PLUGIN_NOTIFICATION_REGISTER_SOURCE
31     * @return void
32     */
33    public function registerSource(Event $event)
34    {
35        if (!$this->getConf('notification_integration')) return;
36        $event->data[] = 'acknowledge';
37    }
38
39    /**
40     * Gather pending acknowledgements for the given user.
41     *
42     * @param Event $event PLUGIN_NOTIFICATION_GATHER
43     * @return void
44     */
45    public function gatherNotifications(Event $event)
46    {
47        if (!$this->getConf('notification_integration')) return;
48        if (!in_array('acknowledge', $event->data['plugins'])) return;
49
50        /** @var AuthPlugin $auth */
51        global $auth;
52
53        // resolve the target user's groups from auth (cron runs need this)
54        $user = $event->data['user'];
55        $userData = $auth->getUserData($user);
56        if ($userData === false) return;
57        $groups = $userData['grps'] ?? [];
58
59        /** @var helper_plugin_acknowledge $helper */
60        $helper = plugin_load('helper', 'acknowledge');
61
62        $rows = $helper->getUserAcknowledgements($user, $groups, 'due');
63        if (!$rows) return;
64
65        foreach ($rows as $row) {
66            $page = $row['page'];
67
68            // don't notify about pages that cannot be acknowledged yet (approve check)
69            if ($helper->isBlockedByApprove($page)) continue;
70
71            $event->data['notifications'][] = [
72                'plugin' => 'acknowledge',
73                'id' => $page . ':' . $row['lastmod'], // notification is bound to id and rev
74                'full' => sprintf($this->getLang('notification'), $this->buildPageLink($page)),
75                'brief' => $this->buildPageLink($page),
76                'timestamp' => (int) $row['lastmod'],
77            ];
78        }
79    }
80
81    /**
82     * @param Event $event PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES
83     * @return void
84     */
85    public function cacheDependencies(Event $event)
86    {
87        if (!$this->getConf('notification_integration')) return;
88        if (!in_array('acknowledge', $event->data['plugins'])) return;
89        $event->data['_nocache'] = true;
90    }
91
92    /**
93     * Build the wiki link
94     *
95     * @param string $page Page ID
96     * @return string HTML anchor
97     */
98    protected function buildPageLink($page)
99    {
100        if (useHeading('content')) {
101            $heading = p_get_first_heading($page);
102            $title = blank($heading) ? noNSorNS($page) : $heading;
103        } else {
104            $title = noNSorNS($page);
105        }
106
107        return '<a class="wikilink1" href="' . wl($page, '', true) . '">' . hsc($title) . '</a>';
108    }
109}
110