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