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 105 106 foreach ($msg as $user => $data) { 107 $udata = $auth->getUserData($user); 108 if (!$udata) continue; 109 110 $issues = $data['issues']; 111 $outdated_tasks = $data['outdated_tasks']; 112 $coming_tasks = $data['coming_tasks']; 113 114 115 if (count($issues) + count($outdated_tasks) + count($coming_tasks) == 0) 116 continue; 117 118 $tpl = $action->get_tpl(); 119 $tpl->set('issues', $issues); 120 $tpl->set('outdated_tasks', $outdated_tasks); 121 $tpl->set('coming_tasks', $coming_tasks); 122 $body = $action->bez_tpl_include('cron/weekly-message', true); 123 124 $mailer = new PHPMailer; 125 $mailer->CharSet = 'utf-8'; 126 $mailer->isHTML(true); 127 128 $mailer->setFrom($conf['mailfrom']); 129 $mailer->addReplyTo($conf['mailfrom']); 130 131 $token = $action->get_model()->factory('subscription')->getUserToken($user); 132 $resign_link = $action->url('unsubscribe', array('GET' => array( 't' => $token))); 133 $oneClickUnsubscribe = $action->url('unsubscribe', array('GET' => array( 't' => $token, 'oneclick' => '1'))); 134 $mailer->AddCustomHeader("List-Unsubscribe: <$oneClickUnsubscribe>"); 135 $mailer->Body = str_replace('%%resign_link%%', $resign_link, $body); 136 137 $mailer->addAddress($udata['mail'], $udata['name']); 138 $subject = $conf['title'] . ' Nadchodzące zadania'; 139 $mailer->Subject = $subject; 140 141 $mailer->send(); 142 $mailer->clearAddresses(); 143 $mailer->clearCustomHeaders(); 144 $output[] = array($udata['name'].' <'.$udata['mail'].'>', $subject, $body, array()); 145 } 146 147 return $output; 148} 149