1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6
7/**
8 * DokuWiki Plugin notification (Action Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Szymon Olewniczak <it@rid.pl>
12 */
13class action_plugin_notification_cron extends ActionPlugin
14{
15    /**
16     * Registers a callback function for a given event
17     *
18     * @param EventHandler $controller DokuWiki's event controller object
19     *
20     * @return void
21     */
22    public function register(EventHandler $controller)
23    {
24        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'handleIndexerTasksRun');
25    }
26
27    /**
28     *
29     * @param Event $event event object by reference
30     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
31     *                           handler was registered]
32     *
33     * @return void
34     */
35    public function handleIndexerTasksRun(Event $event, $param)
36    {
37        /** @var DokuWiki_Auth_Plugin $auth */
38        global $auth;
39
40        /** @var \helper_plugin_notification_db $db_helper */
41        $db_helper = plugin_load('helper', 'notification_db');
42        $sqlite = $db_helper->getDB();
43
44        // insert new users first
45        /** @var \helper_plugin_notification_cron $cron_helper */
46        $cron_helper = plugin_load('helper', 'notification_cron');
47        $cron_helper->addUsersToCron();
48
49        //get the oldest check
50        $res = $sqlite->query('SELECT user, MIN(timestamp) FROM cron_check');
51        $user = $sqlite->res2single($res);
52        //no user to send notifications to
53        if (!$user) return;
54
55        //update user last check
56        $sqlite->query('UPDATE cron_check SET timestamp=? WHERE user=?', date('c'), $user);
57
58        // get new notifications from plugins
59        $notification_data = $cron_helper->getNotificationData($user);
60
61        //no notifications - nothing to send
62        if (empty($notification_data['notifications'])) return;
63
64        $new_notifications = $cron_helper->getNewNotifications($user, $notification_data);
65
66        // no notifications left - nothing to send
67        if (!$new_notifications) return;
68
69        list($text, $html) = $cron_helper->composeEmail($new_notifications);
70        if ($cron_helper->sendMail($user, $text, $html)) {
71            //mark notifications as sent
72            $cron_helper->storeSentNotifications($user, $new_notifications);
73        }
74
75        $event->stopPropagation();
76        $event->preventDefault();
77    }
78}
79