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        try {
33            /** @var \helper_plugin_approve_db $db_helper */
34            $db_helper = plugin_load('helper', 'approve_db');
35            $sqlite = $db_helper->getDB();
36            $event->data['dependencies'][] = $sqlite->getAdapter()->getDbFile();
37        } catch (Exception $e) {
38            msg($e->getMessage(), -1);
39            return;
40        }
41    }
42
43    public function add_notifications(Doku_Event $event)
44    {
45        if (!in_array('approve', $event->data['plugins'])) return;
46
47        try {
48            /** @var \helper_plugin_approve_db $db_helper */
49            $db_helper = plugin_load('helper', 'approve_db');
50            $sqlite = $db_helper->getDB();
51        } catch (Exception $e) {
52            msg($e->getMessage(), -1);
53            return;
54        }
55
56        $user = $event->data['user'];
57
58        $q = 'SELECT page.page, revision.rev
59                    FROM page INNER JOIN revision ON page.page = revision.page
60                    WHERE page.hidden = 0 AND page.approver=?
61                      AND revision.current=1 AND revision.approved IS NULL';
62        $res = $sqlite->query($q, $user);
63
64        $notifications = $sqlite->res2arr($res);
65
66        foreach ($notifications as $notification) {
67            $page = $notification['page'];
68            $rev = $notification['rev'];
69
70            $link = '<a class="wikilink1" href="' . wl($page, '', true) . '">';
71            if (useHeading('content')) {
72                $heading = p_get_first_heading($page);
73                if (!blank($heading)) {
74                    $link .= $heading;
75                } else {
76                    $link .= noNSorNS($page);
77                }
78            } else {
79                $link .= noNSorNS($page);
80            }
81            $link .= '</a>';
82            $full = sprintf($this->getLang('notification full'), $link);
83            $event->data['notifications'][] = [
84                'plugin' => 'approve',
85                'id' => $page.':'.$rev,
86                'full' => $full,
87                'brief' => $link,
88                'timestamp' => (int)$rev
89            ];
90        }
91    }
92}
93