1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6
7class action_plugin_approve_notification extends ActionPlugin
8{
9    /**
10     * @inheritDoc
11     */
12    public function register(EventHandler $controller)
13    {
14        $controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'addNotificationsSource');
15        $controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'addNotifications');
16        $controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'addNotificationCacheDependencies');
17    }
18
19    public function addNotificationsSource(Event $event)
20    {
21        $event->data[] = 'approve';
22    }
23
24    public function addNotificationCacheDependencies(Event $event)
25    {
26        if (!in_array('approve', $event->data['plugins'])) return;
27
28        /** @var \helper_plugin_approve_db $db */
29        $db = $this->loadHelper('approve_db');
30        $event->data['dependencies'][] = $db->getDbFile();
31    }
32
33    public function addNotifications(Event $event)
34    {
35        if (!in_array('approve', $event->data['plugins'])) return;
36
37        $user = $event->data['user'];
38
39        /** @var \helper_plugin_approve_db $db */
40        $db = $this->loadHelper('approve_db');
41
42        $states = ['draft', 'ready_for_approval'];
43        if ($this->getConf('ready_for_approval_notification')) {
44            $states = ['ready_for_approval'];
45        }
46
47        $notifications = $db->getPages($user, $states);
48
49        foreach ($notifications as $notification) {
50            $id = $notification['id'];
51            $rev = $notification['rev'];
52
53            $link = '<a class="wikilink1" href="' . wl($id, '', true) . '">';
54            if (useHeading('content')) {
55                $heading = p_get_first_heading($id);
56                if (!blank($heading)) {
57                    $link .= $heading;
58                } else {
59                    $link .= noNSorNS($id);
60                }
61            } else {
62                $link .= noNSorNS($id);
63            }
64            $link .= '</a>';
65            $full = sprintf($this->getLang('notification full'), $link);
66            $event->data['notifications'][] = [
67                'plugin' => 'approve',
68                'id' => $id.':'.$rev,
69                'full' => $full,
70                'brief' => $link,
71                'timestamp' => (int)$rev
72            ];
73        }
74    }
75}
76