xref: /plugin/bez/cron/functions.php (revision 4da807da68245bfa1e9764160638087fcd9ff6df)
1<?php
2
3use PHPMailer\PHPMailer\PHPMailer;
4
5require_once DOKU_PLUGIN . 'bez/vendor/phpmailer/phpmailer/src/Exception.php';
6require_once DOKU_PLUGIN . 'bez/vendor/phpmailer/phpmailer/src/PHPMailer.php';
7require_once DOKU_PLUGIN . 'bez/vendor/phpmailer/phpmailer/src/SMTP.php';
8
9$action = new action_plugin_bez_base();
10$action->createObjects(true);
11
12function send_inactive_issue() {
13    global $action;
14
15    $threads = $action->get_model()->threadFactory->get_all(array(
16        'last_activity_date' => array('<=', date('c', strtotime('-26 days'))),
17        'state' => 'opened'
18    ));
19
20    foreach ($threads as $thread) {
21        //send reminder once a month
22        $day_of_issue_last_activity = date('d', strtotime($thread->last_activity_date));
23        if ($day_of_issue_last_activity == date('d')) {
24            //send message to all
25            $thread->mail_notify_inactive($thread->get_participants('subscribent'));
26        }
27    }
28}
29
30function send_task_reminder() {
31    global $action;
32
33    $filters = array('state' => 'opened', 'plan_date' => array('OR', array()));
34
35    $days_before = $action->getConf('task_remaind_days_before');
36    $days_before = array_map('trim', explode(',', $days_before));
37
38    if (count($days_before) == 0) {
39        return;
40    }
41
42    foreach($days_before as $day) {
43        $filters['plan_date'][1][] = date('Y-m-d', strtotime("+$day day"));
44    }
45
46
47    $tasks = $action->get_model()->taskFactory->get_all($filters);
48
49    $now = new DateTime(date('Y-m-d'));
50    foreach ($tasks as $task) {
51        $plan_date = new DateTime($task->plan_date);
52        $task->mail_notify_remind($task->get_participants('subscribent'), $plan_date->diff($now)->format('%a'));
53    }
54}
55
56function send_weekly_message() {
57    global $action;
58    global $auth;
59    global $conf;
60
61    //email => array('user' => array('issues' => array(), 'tasks' => array()))
62    $msg = array();
63    $output = array();
64
65    $threads = $action->get_model()->threadFactory->get_all(array(
66          'type' => 'issue',
67          'priority' => array('OR', array('2', '1'))
68      ));
69
70    foreach ($threads as $thread) {
71        $key = $thread->coordinator;
72        if (!isset($msg[$key])) {
73            $msg[$key] = array(
74                'issues'          => array(),
75                'coming_tasks'    => array(),
76                'outdated_tasks'  => array()
77            );
78        }
79        $msg[$key]['issues'][] = $thread;
80    }
81
82    $tasks  = $action->get_model()->taskFactory->get_all(array(
83        'priority' => array('OR', array('2', '1'))
84    ));
85
86    foreach ($tasks as $task) {
87        $key = $task->assignee;
88        if (!isset($msg[$key])) {
89            $msg[$key] = array(
90                'issues'          => array(),
91                'coming_tasks'    => array(),
92                'outdated_tasks'  => array()
93            );
94        }
95
96        if ($task->priority == '1') {
97            $msg[$key]['coming_tasks'][] = $task;
98        } else {
99            $msg[$key]['outdated_tasks'][] = $task;
100        }
101    }
102
103    //outdated_tasks, coming_tasks, open_tasks
104    $muted_users = $action->get_model()->factory('subscription')->getMutedUsers();
105
106    foreach ($msg as $user => $data) {
107        $udata = $auth->getUserData($user);
108        if (!$udata) continue;
109
110        //omit muted users
111        if (in_array($user, $muted_users)) continue;
112
113        $issues = $data['issues'];
114        $outdated_tasks = $data['outdated_tasks'];
115        $coming_tasks = $data['coming_tasks'];
116
117
118        if (count($issues) + count($outdated_tasks) + count($coming_tasks) == 0)
119            continue;
120
121        $tpl = $action->get_tpl();
122        $tpl->set('issues', $issues);
123        $tpl->set('outdated_tasks', $outdated_tasks);
124        $tpl->set('coming_tasks', $coming_tasks);
125        $body = $action->bez_tpl_include('cron/weekly-message', true);
126
127        $mailer = new PHPMailer;
128        $mailer->CharSet = 'utf-8';
129        $mailer->isHTML(true);
130
131        $mailer->setFrom($conf['mailfrom']);
132        $mailer->addReplyTo($conf['mailfrom']);
133
134        $token = $action->get_model()->factory('subscription')->getUserToken($user);
135        $resign_link = $action->url('unsubscribe', array('GET' => array( 't' => $token)));
136        $mailer->Body = str_replace('%%resign_link%%', $resign_link, $body);
137
138        $mailer->addAddress($udata['mail'], $udata['name']);
139        $subject = $conf['title'] . ' Nadchodzące zadania';
140        $mailer->Subject = $subject;
141
142        $mailer->send();
143        $mailer->clearAddresses();
144        $mailer->clearCustomHeaders();
145        $output[] = array($udata['name'].' <'.$udata['mail'].'>', $subject, $body, array());
146    }
147
148    return $output;
149}
150