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