xref: /plugin/ireadit/action/notification.php (revision ce9be9e9c1ec866d658e8f1bd79fef3f5ed26fc0)
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
27        /** @var \helper_plugin_ireadit_db $db_helper */
28        $db_helper = plugin_load('helper', 'ireadit_db');
29        $event->data['dependencies'][] = $db_helper->getDB()->getAdapter()->getDbFile();
30    }
31
32    public function add_notifications(Doku_Event $event)
33    {
34        if (!in_array('ireadit', $event->data['plugins'])) return;
35
36        /** @var \helper_plugin_ireadit_db $db_helper */
37        $db_helper = plugin_load('helper', 'ireadit_db');
38        $sqlite = $db_helper->getDB();
39
40        $user = $event->data['user'];
41
42        $res = $sqlite->query('SELECT page, rev FROM ireadit
43                                        WHERE timestamp IS NULL
44                                        AND user = ?
45                                        ORDER BY timestamp', $user);
46
47        $notifications = $sqlite->res2arr($res);
48
49        foreach ($notifications as $notification) {
50            $page = $notification['page'];
51            $rev = $notification['rev'];
52
53            $link = '<a class="wikilink1" href="' . wl($page) . '">';
54            if (useHeading('content')) {
55                $heading = p_get_first_heading($page);
56                if (!blank($heading)) {
57                    $link .= $heading;
58                } else {
59                    $link .= noNSorNS($page);
60                }
61            } else {
62                $link .= noNSorNS($page);
63            }
64            $link .= '</a>';
65            $full = sprintf($this->getLang('notification full'), $link);
66            $event->data['notifications'][] = [
67                'plugin' => 'ireadit',
68                'full' => $full,
69                'brief' => $link,
70                'timestamp' => (int)$rev
71            ];
72        }
73    }
74}
75