1<?php
2
3use dokuwiki\Extension\CLIPlugin;
4use splitbrain\phpcli\Exception;
5use splitbrain\phpcli\Options;
6
7class cli_plugin_notification extends CLIPlugin
8{
9    /** @var helper_plugin_notification_db */
10    protected $db_helper;
11
12    /** @var helper_plugin_notification_cron */
13    protected $cron_helper;
14
15    /**
16     * Initialize helper plugin
17     */
18    public function __construct()
19    {
20        parent::__construct();
21        $this->db_helper = plugin_load('helper', 'notification_db');
22        $this->cron_helper = plugin_load('helper', 'notification_cron');
23    }
24
25    /**
26     * Register options and arguments on the given $options object
27     *
28     * @param Options $options
29     * @return void
30     * @throws Exception
31     */
32    protected function setup(Options $options)
33    {
34        $options->setHelp('Bulk notification dispatcher');
35        $options->registerCommand('send', 'Send all due notifications');
36    }
37
38    /**
39     * Your main program
40     *
41     * Arguments and options have been parsed when this is run
42     *
43     * @param Options $options
44     * @return void
45     */
46    protected function main(Options $options)
47    {
48        auth_setup(); // make sure ACLs are initialized
49        $cmd = $options->getCmd();
50        switch ($cmd) {
51            case 'send':
52                $this->sendNotifications();
53                break;
54            default:
55                $this->error('No command provided');
56                exit(1);
57        }
58    }
59
60    /**
61     * Check and send notifications
62     */
63    protected function sendNotifications()
64    {
65        $sqlite = $this->db_helper->getDB();
66
67        // get users from DB
68        $sql = 'SELECT user FROM cron_check';
69        $result = $sqlite->query($sql);
70        $rows = $sqlite->res2arr($result);
71        if (!is_array($rows)) {
72            $this->info('Exiting: no users to notify found.');
73            return;
74        }
75        // gather new notifications per user
76        foreach ($rows as $row) {
77            $user = $row['user'];
78
79            // update timestamp of cron check
80            $sqlite->query('UPDATE cron_check SET timestamp=? WHERE user=?', date('c'), $user);
81
82            $notification_data = $this->cron_helper->getNotificationData($user);
83
84            //no notifications - nothing to send
85            if (empty($notification_data['notifications'])) {
86                $this->info('No notifications at all for user ' . $user);
87                continue;
88            }
89
90            $new_notifications = $this->cron_helper->getNewNotifications($user, $notification_data);
91            //  send email
92            // no notifications left - nothing to send
93            if (!$new_notifications) {
94                $this->info('No new notifications for user ' . $user);
95                continue;
96            }
97
98            list($text, $html) = $this->cron_helper->composeEmail($new_notifications);
99            if ($this->cron_helper->sendMail($user, $text, $html)) {
100                $this->cron_helper->storeSentNotifications($user, $new_notifications);
101                $this->info('Sent notification to ' . $user);
102            } else {
103                $this->error('Failed sending notification to ' . $user);
104            }
105        }
106    }
107}
108