1<?php
2// must be run within DokuWiki
3if (!defined('DOKU_INC')) die();
4
5/**
6 * All DokuWiki plugins to extend the parser/rendering mechanism
7 * need to inherit from this class
8 */
9class action_plugin_ireadit_notification extends DokuWiki_Action_Plugin
10{
11    public function register(Doku_Event_Handler $controller)
12    {
13        $controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'add_notifications_source');
14        $controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'add_notifications');
15        $controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'add_notification_cache_dependencies');
16    }
17
18    public function add_notifications_source(Doku_Event $event)
19    {
20        $event->data[] = 'ireadit';
21    }
22
23    public function add_notification_cache_dependencies(Doku_Event $event)
24    {
25        if (!in_array('ireadit', $event->data['plugins'])) return;
26        $event->data['_nocache'] = true; // TODO: notification cache mechanism should be updated to "Igor" dokuwiki
27    }
28
29    public function add_notifications(Doku_Event $event)
30    {
31        if (!in_array('ireadit', $event->data['plugins'])) return;
32
33        $user = $event->data['user'];
34
35        /** @var helper_plugin_ireadit $helper */
36        $helper = $this->loadHelper('ireadit');
37        $pages = $helper->get_list($user);
38
39        foreach ($pages as $page => $row) {
40            if ($row['state'] == 'read') continue;
41
42            $urlParameters = [];
43            // in case of approve integration current_rev may be last approved revision
44            if ($this->getConf('approve_integration') &&
45                $row['current_rev'] != p_get_metadata($page, 'last_change date')) {
46                $urlParameters['rev'] = $row['current_rev'];
47            }
48
49            $link = '<a class="wikilink1" href="' . wl($page, $urlParameters, true) . '">';
50            if (useHeading('content')) {
51                $heading = p_get_first_heading($page);
52                if (!blank($heading)) {
53                    $link .= $heading;
54                } else {
55                    $link .= noNSorNS($page);
56                }
57            } else {
58                $link .= noNSorNS($page);
59            }
60            $link .= '</a>';
61            $full = sprintf($this->getLang('notification full'), $link);
62            $event->data['notifications'][] = [
63                'plugin' => 'ireadit',
64                'id' => $page . ':' . $row['current_rev'],
65                'full' => $full,
66                'brief' => $link,
67                'timestamp' => (int) $row['current_rev']
68            ];
69        }
70    }
71}
72