ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMailCronOrphanedMails.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 
36 {
39  private ilLanguage $lng;
41  private ilDBInterface $db;
42  private ilObjUser $user;
43  private bool $initDone = false;
45 
46  private function init(): void
47  {
48  global $DIC;
49 
50  if (!$this->initDone) {
51  $this->settings = $DIC->settings();
52  $this->lng = $DIC->language();
53  $this->db = $DIC->database();
54  $this->user = $DIC->user();
55  $this->http = $DIC->http();
56  $this->refinery = $DIC->refinery();
57  $this->cron_manager = $DIC->cron()->manager();
58 
59  $this->lng->loadLanguageModule('mail');
60  $this->initDone = true;
61  }
62  }
63 
65  {
66  $empty_string_or_null_to_stirng_trafo = $this->refinery->custom()->transformation(static function ($value): string {
67  if ($value === '' || null === $value) {
68  return '';
69  }
70 
71  throw new Exception('The value to be transformed is not an empty string');
72  });
73 
74  return $this->refinery->in()->series([
75  $this->refinery->byTrying([
76  $empty_string_or_null_to_stirng_trafo,
77  $this->refinery->kindlyTo()->int(),
78  $this->refinery->in()->series([
79  $this->refinery->kindlyTo()->float(),
80  $this->refinery->kindlyTo()->int()
81  ])
82  ]),
83  $this->refinery->kindlyTo()->string()
84  ]);
85  }
86 
87  public function getId(): string
88  {
89  return 'mail_orphaned_mails';
90  }
91 
92  public function getTitle(): string
93  {
94  $this->init();
95  return $this->lng->txt('mail_orphaned_mails');
96  }
97 
98  public function getDescription(): string
99  {
100  $this->init();
101  return $this->lng->txt('mail_orphaned_mails_desc');
102  }
103 
104  public function hasAutoActivation(): bool
105  {
106  return false;
107  }
108 
109  public function hasFlexibleSchedule(): bool
110  {
111  return true;
112  }
113 
114  public function getValidScheduleTypes(): array
115  {
116  return [
117  CronJobScheduleType::SCHEDULE_TYPE_DAILY,
118  CronJobScheduleType::SCHEDULE_TYPE_WEEKLY,
119  CronJobScheduleType::SCHEDULE_TYPE_MONTHLY,
120  CronJobScheduleType::SCHEDULE_TYPE_QUARTERLY,
122  CronJobScheduleType::SCHEDULE_TYPE_IN_DAYS
123  ];
124  }
125 
127  {
128  return CronJobScheduleType::SCHEDULE_TYPE_DAILY;
129  }
130 
131  public function getDefaultScheduleValue(): ?int
132  {
133  return 1;
134  }
135 
136  public function hasCustomSettings(): bool
137  {
138  return true;
139  }
140 
141  public function addCustomSettingsToForm(ilPropertyFormGUI $a_form): void
142  {
143  $this->init();
144 
145  parent::addCustomSettingsToForm($a_form);
146 
147  $threshold = new ilNumberInputGUI($this->lng->txt('mail_threshold'), 'mail_threshold');
148  $threshold->setInfo($this->lng->txt('mail_threshold_info'));
149  $threshold->allowDecimals(false);
150  $threshold->setSuffix($this->lng->txt('days'));
151  $threshold->setMinValue(1);
152  $threshold->setSize(4);
153  $threshold->setValue($this->settings->get('mail_threshold', ''));
154 
155  $a_form->addItem($threshold);
156 
157  $mail_folder = new ilCheckboxInputGUI(
158  $this->lng->txt('only_inbox_trash'),
159  'mail_only_inbox_trash'
160  );
161  $mail_folder->setValue('1');
162  $mail_folder->setInfo($this->lng->txt('only_inbox_trash_info'));
163  $mail_folder->setChecked((bool) $this->settings->get('mail_only_inbox_trash', '0'));
164  $a_form->addItem($mail_folder);
165 
166  $notification = new ilNumberInputGUI(
167  $this->lng->txt('mail_notify_orphaned'),
168  'mail_notify_orphaned'
169  );
170  $notification->setInfo($this->lng->txt('mail_notify_orphaned_info'));
171  $notification->allowDecimals(false);
172  $notification->setSize(4);
173  $notification->setSuffix($this->lng->txt('days'));
174  $notification->setMinValue(0);
175 
176  if ($this->http->wrapper()->post()->has('mail_threshold')) {
177  $mail_threshold = (int) $this->http->wrapper()->post()->retrieve(
178  'mail_threshold',
180  );
181  } else {
182  $mail_threshold = (int) $this->settings->get('mail_threshold');
183  }
184  $maxvalue = $mail_threshold - 1;
185  $notification->setMaxValue($maxvalue);
186  $notification->setValue($this->settings->get('mail_notify_orphaned', ''));
187  $a_form->addItem($notification);
188  }
189 
190  public function saveCustomSettings(ilPropertyFormGUI $a_form): bool
191  {
192  $this->init();
193 
194  $this->settings->set('mail_only_inbox_trash', (string) ((int) $a_form->getInput('mail_only_inbox_trash')));
195  $this->settings->set(
196  'mail_threshold',
197  $this->emptyStringOrFloatOrIntToEmptyOrIntegerString()->transform($a_form->getInput('mail_threshold'))
198  );
199  $this->settings->set(
200  'mail_notify_orphaned',
201  $this->emptyStringOrFloatOrIntToEmptyOrIntegerString()->transform($a_form->getInput('mail_notify_orphaned'))
202  );
203 
204  if ((int) $this->settings->get('mail_notify_orphaned', '0') === 0) {
205  //delete all mail_cron_orphaned-table entries!
206  $this->db->manipulate('DELETE FROM mail_cron_orphaned');
207 
208  ilLoggerFactory::getLogger('mail')->info(sprintf(
209  "Deleted all scheduled mail deletions " .
210  "because a reminder shouldn't be sent (login: %s|usr_id: %s) anymore!",
211  $this->user->getLogin(),
212  $this->user->getId()
213  ));
214  }
215 
216  return true;
217  }
218 
219  public function ping(): void
220  {
221  $this->cron_manager->ping($this->getId());
222  }
223 
224  public function run(): ilCronJobResult
225  {
226  $this->init();
227 
228  $mail_expiration_days = (int) $this->settings->get('mail_threshold', '0');
229 
230  ilLoggerFactory::getLogger('mail')->info(sprintf(
231  'Started mail deletion job with threshold: %s day(s)',
232  var_export($mail_expiration_days, true)
233  ));
234 
235  if ($mail_expiration_days >= 1 && (int) $this->settings->get('mail_notify_orphaned', '0') >= 1) {
236  $this->processNotification();
237  }
238 
239  if ($mail_expiration_days >= 1 && (int) $this->settings->get('last_cronjob_start_ts', (string) time())) {
240  $this->processDeletion();
241  }
242 
243  $result = new ilCronJobResult();
244  $status = ilCronJobResult::STATUS_OK;
245  $result->setStatus($status);
246 
247  ilLoggerFactory::getLogger('mail')->info(sprintf(
248  'Finished mail deletion job with threshold: %s day(s)',
249  var_export($mail_expiration_days, true)
250  ));
251 
252  return $result;
253  }
254 
255  private function processNotification(): void
256  {
257  $this->init();
258 
259  $notifier = new Notifier(
260  $this,
261  new NotificationsCollector($this),
262  (int) $this->settings->get('mail_threshold', '0'),
263  (int) $this->settings->get('mail_notify_orphaned', '0')
264  );
265  $notifier->send();
266  }
267 
268  private function processDeletion(): void
269  {
270  $this->init();
271 
272  $processor = new MailDeletionHandler($this, new ExpiredOrOrphanedMailsCollector($this));
273  $processor->delete();
274  }
275 }
Interface GlobalHttpState.
static getLogger(string $a_component_id)
Get component logger.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
addCustomSettingsToForm(ilPropertyFormGUI $a_form)
global $DIC
Definition: feed.php:28
static http()
Fetches the global http state from ILIAS.
saveCustomSettings(ilPropertyFormGUI $a_form)
A transformation is a function from one datatype to another.