<?php
namespace App\Cron\Job; class DeleteOldCalendarEvents extends \App\Cron\Job\Utils\ShortRunningCronJobAbstract { protected $category = 'operational'; private $retentionPeriod = ['1month', '2months', '3months', '4months', '5months', '6months', '9months', '1year', '2years', '3years']; public function getSettingsDialog(array $fields) : \Engine\Form\General { $form = parent::getSettingsDialog($fields); $_LANG = \App\Loader_Lang::getInstance()->getGlobal(); $lang = \App\Loader_Lang::getInstance()->getSection("Admin/CronJobs/Management/DeleteOldCalendarEvents"); $fieldset = \Engine\Form\Container\Fieldset::create($_LANG['Admin']['CronJobs']['Management']['formFields']['legend']); $form->addContainer($fieldset); $possibleValues = []; foreach ($this->retentionPeriod as $rp) { $possibleValues[$rp] = $lang['options']['retentionPeriod_values_'.$rp]; } $field = new \Engine\Form\Input\Select($lang['options']['retentionMonths'], 'retentionMonths', $fields['settings']['retentionMonths']); $field->setPossibleValues($possibleValues); $field->showDefaultOption(); $field->setTooltip($lang['options']['retentionMonthsTooltip']); $fieldset->addElement($field); $fieldset->groupInputsInArray('settings'); return $form; } public function validateSettings(\App\Entity\Messages $msgs, array $fields) : void { if ($fields['scheduleOptions']['type'] != 'scheduled') { $msgs->addWarningText('It is recommended to run this cronjob on a scheduled basis (e.g., daily).'); } if ($fields['settings']['retentionMonths'] && !in_array($fields['settings']['retentionMonths'], $this->retentionPeriod, true)) { $msgs->addWarningText('Invalid retention period configured.'); } } protected function executeJob() : void { if (!$this->settings['retentionMonths']) { $this->logger->idle('No retention period configured.'); return; } if (!in_array($this->settings['retentionMonths'], $this->retentionPeriod, true)) { $this->logger->error('Invalid retention period configured.'); return; } $retention = $this->settings['retentionMonths']; $cutoff = \Engine\DateTime::create("-{$retention}")->formatDatetimeIso(); $this->logger->info(sprintf('Purging calendar events with end date older than %s.', $cutoff)); $list = new \App\EntityList\CalendarEvents(); $list->removeAllFilters(); $list->setFilter('endDateTime', '<', $cutoff); $events = $list->exportRecords(); if (!count($events)) { $this->logger->idle('No events found to purge.'); return; } $deleted = 0; $failed = 0; foreach ($events as $ev) { \DB::startTransaction(); try { $ent = \Entity\CalendarEvent::purge($ev['id']); $ent->save(); \DB::commitTransaction(); $deleted++; } catch (\App\Entity\Exception $ex) { \DB::rollbackTransaction(); $failed++; $this->logger->error(sprintf('Failed to purge calendar event %s (ended at %s): %s', $ev['id'], $ev['endDateTime'], $ex->getMessage())); } } if ($deleted) { $this->logger->success(sprintf('%d calendar event(s) purged.', $deleted)); } if ($failed) { $this->logger->error(sprintf('%d calendar event(s) could not be purged due to errors.', $failed)); } } } 