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