xref: /plugin/approve/action/notification.php (revision 2ce523c6fdc141946a4630f6c73b585854f5c8e8)
1<?php
2// must be run within DokuWiki
3if (!defined('DOKU_INC')) die();
4
5if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
6require_once DOKU_PLUGIN . 'syntax.php';
7
8/**
9 * All DokuWiki plugins to extend the parser/rendering mechanism
10 * need to inherit from this class
11 */
12class action_plugin_approve_notification extends DokuWiki_Action_Plugin
13{
14    public function register(Doku_Event_Handler $controller)
15    {
16        $controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'add_notifications_source');
17        $controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'add_notifications');
18        $controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'add_notification_cache_dependencies');
19
20
21    }
22
23    public function add_notifications_source(Doku_Event $event)
24    {
25        $event->data[] = 'approve';
26    }
27
28    public function add_notification_cache_dependencies(Doku_Event $event)
29    {
30        if (!in_array('approve', $event->data['plugins'])) return;
31
32        /** @var \helper_plugin_ireadit_db $db_helper */
33        $db_helper = plugin_load('helper', 'approve_db');
34        $event->data['dependencies'][] = $db_helper->getDB()->getAdapter()->getDbFile();
35    }
36
37    public function add_notifications(Doku_Event $event)
38    {
39        if (!in_array('approve', $event->data['plugins'])) return;
40
41        /** @var \helper_plugin_ireadit_db $db_helper */
42        $db_helper = plugin_load('helper', 'approve_db');
43        $sqlite = $db_helper->getDB();
44
45        $user = $event->data['user'];
46
47        $q = 'SELECT page.page, revision.rev
48                    FROM page INNER JOIN revision ON page.page = revision.page
49                    WHERE page.hidden = 0 AND page.approver=?
50                      AND revision.current=1 AND revision.approved IS NULL';
51        $res = $sqlite->query($q, $user);
52
53        $notifications = $sqlite->res2arr($res);
54
55        foreach ($notifications as $notification) {
56            $page = $notification['page'];
57            $rev = $notification['rev'];
58
59            $link = '<a class="wikilink1" href="' . wl($page) . '">';
60            if (useHeading('content')) {
61                $heading = p_get_first_heading($page);
62                if (!blank($heading)) {
63                    $link .= $heading;
64                } else {
65                    $link .= noNSorNS($page);
66                }
67            } else {
68                $link .= noNSorNS($page);
69            }
70            $link .= '</a>';
71            $full = sprintf($this->getLang('notification full'), $link);
72            $event->data['notifications'][] = [
73                'plugin' => 'ireadit',
74                'full' => $full,
75                'brief' => $link,
76                'timestamp' => (int)$rev
77            ];
78        }
79    }
80}
81