1<?php
2/**
3 * DokuWiki Plugin structtasks
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Chris MacMackin <cmacmackin@gmail.com>
7 *
8 */
9
10namespace dokuwiki\plugin\structtasks\meta;
11
12/**
13 * Notifies assignees before a task comes due
14 *
15 * @package dokuwiki\plugin\structtasks\meta
16 */
17class ReminderNotifier extends AbstractNotifier
18{
19    const lang_key_prefix = 'reminder';
20
21    private $days_before;
22
23    /**
24     * Constructor allows you to specify how many days before the
25     * deadline the reminder should be sent. This is done by passing
26     * an array with the days on which to provide notifications.
27     */
28    public function __construct(callable $getConf, callable $getLang,
29                                array $days_before = [1]) {
30        parent::__construct($getConf, $getLang);
31        $this->days_before = $days_before;
32    }
33
34    /**
35     * Returns a copy of the array with the list of the days before
36     * the due-date on which to send a reminder.
37     */
38    public function getDaysBefore() : array {
39        return $this->days_before;
40    }
41
42    public function getNotifiableUsers($page, $editor_email, $new_data, $old_data) {
43        if (is_null($new_data['duedate'])) return [];
44        if ($this->isCompleted($new_data['status'])) return [];
45        // FIXME: if $days_before is more than one month then this
46        // won't be very accurate
47        $time_remaining = $this->timeFromLastMidnight($new_data['duedate']);
48        $days = $time_remaining[0] * 365 + $time_remaining[1] * 31 + $time_remaining[2];
49        if (in_array($days, $this->days_before)) {
50            return $new_data['assignees'];
51        }
52        return [];
53    }
54}
55