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