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