1<?php
2// must be run within DokuWiki
3use dokuwiki\plugin\approve\meta\ApproveMetadata;
4
5if (!defined('DOKU_INC')) die();
6
7if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
8require_once DOKU_PLUGIN . 'syntax.php';
9
10/**
11 * All DokuWiki plugins to extend the parser/rendering mechanism
12 * need to inherit from this class
13 */
14class action_plugin_approve_notification extends DokuWiki_Action_Plugin
15{
16    public function register(Doku_Event_Handler $controller)
17    {
18        $controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'add_notifications_source');
19        $controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'add_notifications');
20        $controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'add_notification_cache_dependencies');
21
22
23    }
24
25    public function add_notifications_source(Doku_Event $event)
26    {
27        $event->data[] = 'approve';
28    }
29
30    public function add_notification_cache_dependencies(Doku_Event $event)
31    {
32        if (!in_array('approve', $event->data['plugins'])) return;
33
34        try {
35            /** @var \helper_plugin_approve_db $db_helper */
36            $db_helper = plugin_load('helper', 'approve_db');
37            $sqlite = $db_helper->getDB();
38            $event->data['dependencies'][] = $sqlite->getAdapter()->getDbFile();
39        } catch (Exception $e) {
40            msg($e->getMessage(), -1);
41            return;
42        }
43    }
44
45    public function add_notifications(Doku_Event $event)
46    {
47        if (!in_array('approve', $event->data['plugins'])) return;
48
49        $user = $event->data['user'];
50        try {
51            $approveMetadata = new ApproveMetadata();
52        } catch (Exception $e) {
53            msg($e->getMessage(), -1);
54            return;
55        }
56
57        $states = ['draft', 'ready_for_approval'];
58        if ($this->getConf('ready_for_approval_notification')) {
59            $states = ['ready_for_approval'];
60        }
61
62        $notifications = $approveMetadata->getPages($user, $states);
63
64        foreach ($notifications as $notification) {
65            $page = $notification['page'];
66            $rev = $notification['rev'];
67
68            $link = '<a class="wikilink1" href="' . wl($page, '', true) . '">';
69            if (useHeading('content')) {
70                $heading = p_get_first_heading($page);
71                if (!blank($heading)) {
72                    $link .= $heading;
73                } else {
74                    $link .= noNSorNS($page);
75                }
76            } else {
77                $link .= noNSorNS($page);
78            }
79            $link .= '</a>';
80            $full = sprintf($this->getLang('notification full'), $link);
81            $event->data['notifications'][] = [
82                'plugin' => 'approve',
83                'id' => $page.':'.$rev,
84                'full' => $full,
85                'brief' => $link,
86                'timestamp' => (int)$rev
87            ];
88        }
89    }
90}
91