From c4a8515d99b9410b84a5acea819a526c9e587770 Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Thu, 22 Apr 2010 20:27:14 +0000 Subject: [PATCH] Add feature to regularly remove old calendar events that are more than x years in the past. Set in Admin -> Calendar -> Site configuration --- calendar/inc/class.calendar_boupdate.inc.php | 37 ++++++++++++++++++++ calendar/inc/class.calendar_hooks.inc.php | 23 ++++++++++++ calendar/setup/setup.inc.php | 1 + calendar/templates/default/config.tpl | 14 ++++++++ 4 files changed, 75 insertions(+) diff --git a/calendar/inc/class.calendar_boupdate.inc.php b/calendar/inc/class.calendar_boupdate.inc.php index 570fcc860a..b286ad58ae 100644 --- a/calendar/inc/class.calendar_boupdate.inc.php +++ b/calendar/inc/class.calendar_boupdate.inc.php @@ -2160,4 +2160,41 @@ class calendar_boupdate extends calendar_bo } } } + /** + * Delete events that are more than $age years old + * + * Purges old events from the database + * + * @param int $age How many years old the event must be before it is deleted + */ + function purge($age) + { + $query = array( + 'end' => strtotime("-$age years", time()), + 'enum_recuring' => false + ); + $events = $this->search($query); + foreach($events as $event) + { + // Delete single events or recurring events where all ocurrences are old enough + if(!$event['recur_type'] || $event['recur_type'] && $event['recur_enddate'] && $event['recur_enddate'] <= $query['end']) + { + $this->delete($event['id'], 0, true); + } + } + + // If preserve history is on, we'll need to do this again to completely remove it + $config = config::read('phpgwapi'); + if($config['calendar_delete_history']) { + $query['filter'] = 'deleted'; + $events = $this->search($query); + foreach($events as $event) + { + if(!$event['recur_type'] || $event['recur_type'] && $event['recur_enddate'] && $event['recur_enddate'] <= $query['end']) + { + $this->delete($event['id'], 0, true); + } + } + } + } } diff --git a/calendar/inc/class.calendar_hooks.inc.php b/calendar/inc/class.calendar_hooks.inc.php index d972580260..5449b851a4 100644 --- a/calendar/inc/class.calendar_hooks.inc.php +++ b/calendar/inc/class.calendar_hooks.inc.php @@ -531,4 +531,27 @@ class calendar_hooks ) ); } + + public static function config_validate() { + $GLOBALS['egw_info']['server']['found_validation_hook'] = True; + } +} + +// Not part of the class, since config hooks are still using the old style +function calendar_purge_old($config) { + $id = 'calendar_purge'; + + // Cancel old purge + ExecMethod('phpgwapi.asyncservice.cancel_timer', $id); + + $result = ExecMethod2('phpgwapi.asyncservice.set_timer', + array('month' => '*', 'day' => 1), + $id, + 'calendar.calendar_boupdate.purge', + (int)$config + ); + if(!$result) + { + $GLOBALS['config_error'] = 'Unable to schedule purge'; + } } diff --git a/calendar/setup/setup.inc.php b/calendar/setup/setup.inc.php index c5b40d4cc8..bb12a45d07 100755 --- a/calendar/setup/setup.inc.php +++ b/calendar/setup/setup.inc.php @@ -41,6 +41,7 @@ $setup_info['calendar']['hooks']['preferences'] = 'calendar_hooks::preferences'; $setup_info['calendar']['hooks']['settings'] = 'calendar_hooks::settings'; $setup_info['calendar']['hooks']['sidebox_menu'] = 'calendar.calendar_ui.sidebox_menu'; $setup_info['calendar']['hooks']['search_link'] = 'calendar_hooks::search_link'; +$setup_info['calendar']['hooks']['config_validate'] = 'calendar_hooks::config_validate'; /* Dependencies for this app to work */ $setup_info['calendar']['depends'][] = array( diff --git a/calendar/templates/default/config.tpl b/calendar/templates/default/config.tpl index 79105dfe57..c38a27a316 100644 --- a/calendar/templates/default/config.tpl +++ b/calendar/templates/default/config.tpl @@ -68,6 +68,20 @@ + +  {lang_Automatically_purge_old_events_after} + + + + {lang_Birthdays}