1<?php 2 3/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */ 4 5include_once "./Services/Cron/classes/class.ilCronJob.php"; 6include_once "./Services/Cron/classes/class.ilCronJobResult.php"; 7require_once './Services/Logging/classes/public/class.ilLoggerFactory.php'; 8 9/** 10 * Delete orphaned mails 11 * 12 * @author Nadia Matuschek <nmatuschek@databay.de> 13 */ 14class ilMailCronOrphanedMails extends ilCronJob 15{ 16 /** 17 * @var \ilLanguage 18 */ 19 protected $lng; 20 21 /** 22 * @var \ilSetting 23 */ 24 protected $settings; 25 26 /** 27 * @var \ilDBInterface 28 */ 29 protected $db; 30 31 /** 32 * @var \ilObjUser 33 */ 34 protected $user; 35 36 /** 37 * @var bool 38 */ 39 protected $initDone = false; 40 41 /** 42 * 43 */ 44 protected function init() 45 { 46 global $DIC; 47 48 if (!$this->initDone) { 49 $this->settings = $DIC->settings(); 50 $this->lng = $DIC->language(); 51 $this->db = $DIC->database(); 52 $this->user = $DIC->user(); 53 54 $this->lng->loadLanguageModule('mail'); 55 $this->initDone = true; 56 } 57 } 58 59 /** 60 * Get id 61 * @return string 62 */ 63 public function getId() 64 { 65 return "mail_orphaned_mails"; 66 } 67 68 /** 69 * @return string 70 */ 71 public function getTitle() 72 { 73 $this->init(); 74 return $this->lng->txt("mail_orphaned_mails"); 75 } 76 77 /** 78 * @return string 79 */ 80 public function getDescription() 81 { 82 $this->init(); 83 return $this->lng->txt("mail_orphaned_mails_desc"); 84 } 85 86 /** 87 * Is to be activated on "installation" 88 * @return boolean 89 */ 90 public function hasAutoActivation() 91 { 92 return false; 93 } 94 95 /** 96 * Can the schedule be configured? 97 * @return boolean 98 */ 99 public function hasFlexibleSchedule() 100 { 101 return true; 102 } 103 104 /** 105 * @inheritdoc 106 */ 107 public function getValidScheduleTypes() 108 { 109 return array( 110 self::SCHEDULE_TYPE_DAILY, 111 self::SCHEDULE_TYPE_WEEKLY, 112 self::SCHEDULE_TYPE_MONTHLY, 113 self::SCHEDULE_TYPE_QUARTERLY, 114 self::SCHEDULE_TYPE_YEARLY, 115 self::SCHEDULE_TYPE_IN_DAYS 116 ); 117 } 118 119 /** 120 * Get schedule type 121 * @return int 122 */ 123 public function getDefaultScheduleType() 124 { 125 return self::SCHEDULE_TYPE_DAILY; 126 } 127 128 /** 129 * Get schedule value 130 * @return int|array 131 */ 132 public function getDefaultScheduleValue() 133 { 134 return 1; 135 } 136 137 /** 138 * @return bool 139 */ 140 public function hasCustomSettings() 141 { 142 return true; 143 } 144 145 /** 146 * @param ilPropertyFormGUI $a_form 147 */ 148 public function addCustomSettingsToForm(ilPropertyFormGUI $a_form) 149 { 150 $this->init(); 151 parent::addCustomSettingsToForm($a_form); 152 153 $threshold = new ilNumberInputGUI($this->lng->txt('mail_threshold'), 'mail_threshold'); 154 $threshold->setInfo($this->lng->txt('mail_threshold_info')); 155 $threshold->allowDecimals(false); 156 $threshold->setSuffix($this->lng->txt('days')); 157 $threshold->setMinValue(1); 158 $threshold->setValue($this->settings->get('mail_threshold')); 159 160 $a_form->addItem($threshold); 161 162 $mail_folder = new ilCheckboxInputGUI($this->lng->txt('only_inbox_trash'), 'mail_only_inbox_trash'); 163 $mail_folder->setInfo($this->lng->txt('only_inbox_trash_info')); 164 $mail_folder->setChecked($this->settings->get('mail_only_inbox_trash')); 165 $a_form->addItem($mail_folder); 166 167 $notification = new ilNumberInputGUI($this->lng->txt('mail_notify_orphaned'), 'mail_notify_orphaned'); 168 $notification->setInfo($this->lng->txt('mail_notify_orphaned_info')); 169 $notification->allowDecimals(false); 170 $notification->setSuffix($this->lng->txt('days')); 171 $notification->setMinValue(0); 172 173 $mail_threshold = isset($_POST['mail_threshold']) ? (int) $_POST['mail_threshold'] : $this->settings->get('mail_threshold'); 174 $maxvalue = $mail_threshold - 1; 175 $notification->setMaxValue($maxvalue); 176 $notification->setValue($this->settings->get('mail_notify_orphaned')); 177 $a_form->addItem($notification); 178 } 179 180 /** 181 * @param ilPropertyFormGUI $a_form 182 * @return bool 183 */ 184 public function saveCustomSettings(ilPropertyFormGUI $a_form) 185 { 186 $this->init(); 187 $this->settings->set('mail_threshold', (int) $a_form->getInput('mail_threshold')); 188 $this->settings->set('mail_only_inbox_trash', (int) $a_form->getInput('mail_only_inbox_trash')); 189 $this->settings->set('mail_notify_orphaned', (int) $a_form->getInput('mail_notify_orphaned')); 190 191 if ($this->settings->get('mail_notify_orphaned') == 0) { 192 //delete all mail_cron_orphaned-table entries! 193 $this->db->manipulate('DELETE FROM mail_cron_orphaned'); 194 195 ilLoggerFactory::getLogger('mail')->info(sprintf( 196 "Deleted all scheduled mail deletions because a reminder should't be sent (login: %s|usr_id: %s) anymore!", 197 $this->user->getLogin(), 198 $this->user->getId() 199 )); 200 } 201 202 return true; 203 } 204 205 /** 206 * Run job 207 * @return ilCronJobResult 208 */ 209 public function run() 210 { 211 $this->init(); 212 $mail_threshold = (int) $this->settings->get('mail_threshold'); 213 214 ilLoggerFactory::getLogger('mail')->info(sprintf( 215 'Started mail deletion job with threshold: %s day(s)', 216 var_export($mail_threshold, 1) 217 )); 218 219 if ((int) $this->settings->get('mail_notify_orphaned') >= 1 && $mail_threshold >= 1) { 220 $this->processNotification(); 221 } 222 223 if ((int) $this->settings->get('last_cronjob_start_ts', time()) && $mail_threshold >= 1) { 224 $this->processDeletion(); 225 } 226 227 $result = new ilCronJobResult(); 228 $status = ilCronJobResult::STATUS_OK; 229 $result->setStatus($status); 230 231 ilLoggerFactory::getLogger('mail')->info(sprintf( 232 'Finished mail deletion job with threshold: %s day(s)', 233 var_export($mail_threshold, 1) 234 )); 235 236 return $result; 237 } 238 239 private function processNotification() 240 { 241 $this->init(); 242 include_once './Services/Mail/classes/class.ilMailCronOrphanedMailsNotificationCollector.php'; 243 $collector = new ilMailCronOrphanedMailsNotificationCollector(); 244 245 include_once'./Services/Mail/classes/class.ilMailCronOrphanedMailsNotifier.php'; 246 $notifier = new ilMailCronOrphanedMailsNotifier( 247 $collector, 248 (int) $this->settings->get('mail_threshold'), 249 (int) $this->settings->get('mail_notify_orphaned') 250 ); 251 $notifier->processNotification(); 252 } 253 254 private function processDeletion() 255 { 256 $this->init(); 257 include_once './Services/Mail/classes/class.ilMailCronOrphanedMailsDeletionCollector.php'; 258 $collector = new ilMailCronOrphanedMailsDeletionCollector(); 259 260 include_once './Services/Mail/classes/class.ilMailCronOrphanedMailsDeletionProcessor.php'; 261 $processor = new ilMailCronOrphanedMailsDeletionProcessor($collector); 262 $processor->processDeletion(); 263 } 264} 265