From 17706489dce50603696b555cefa4179f93002029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Combes?= Date: Tue, 21 Jun 2016 00:08:58 +0200 Subject: [PATCH] Use RRULE[WKST] and/or a default first day of week (USA=SU, Europe=MO) --- class.iCalReader.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/class.iCalReader.php b/class.iCalReader.php index 4b88b21..b3fc642 100644 --- a/class.iCalReader.php +++ b/class.iCalReader.php @@ -39,6 +39,8 @@ class ICal /* The value in years to use for indefinite, recurring events */ public /** @type {int} */ $default_span = 2; + /* The default first day of weeks */ + public /** @type {string} */ $default_weekStart = "SU"; /** * Creates the iCal Object * @@ -46,7 +48,7 @@ class ICal * * @return Object The iCal Object */ - public function __construct($filename=false) + public function __construct($filename=false, $weekStart=false) { if (!$filename) { return false; @@ -58,6 +60,10 @@ public function __construct($filename=false) $lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); } + if ($weekStart) { + $this->default_weekStart = $weekStart; + } + return $this->initLines($lines); } @@ -395,6 +401,7 @@ public function process_recurrences() $count_orig = (is_numeric($rrules['COUNT']) && $rrules['COUNT'] > 1) ? $rrules['COUNT'] : 0; $count = ($count_orig - 1); // Remove one to exclude the occurrence that initialises the rule $count += ($count > 0) ? $count * ($interval - 1) : 0; + $offset = "+$count " . $frequency_conversion[$frequency]; $until = strtotime($offset, $start_timestamp); @@ -449,19 +456,30 @@ public function process_recurrences() case 'WEEKLY': // Create offset $offset = "+$interval week"; + + // Use RRULE['WKST'] setting or a default week start (USA = SU, Europe = MO) + $weeks = array( + 'SA' => array('SA', 'SU', 'MO', 'TU', 'WE', 'TH', 'FR'), + 'SU' => array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'), + 'MO' => array('MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU')); + + $wkst = (isset($rrules['WKST']) and in_array($rrules['WKST'], array("SA","SU","MO"))) ? $rrules['WKST'] : $this->default_weekStart; + $aWeek = $weeks[$wkst]; + $days = array("SA"=>"Saturday", "SU"=>"Sunday", "MO"=> "Monday"); + // Build list of days of week to add events - $weekdays = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'); + $weekdays = $aWeek; if (isset($rrules['BYDAY']) && $rrules['BYDAY'] != '') { $bydays = explode(',', $rrules['BYDAY']); } else { - $weekTemp = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'); + $weekTemp = $aWeek; $findDay = $weekTemp[date('w', $start_timestamp)]; $bydays = array($findDay); } // Get timestamp of first day of start week - $week_recurring_timestamp = (date('w', $start_timestamp) == 0) ? $start_timestamp : strtotime('last Sunday ' . date('H:i:s', $start_timestamp), $start_timestamp); + $week_recurring_timestamp = (date('w', $start_timestamp) == 0) ? $start_timestamp : strtotime("last {$days[$wkst]} " . date('H:i:s', $start_timestamp), $start_timestamp); // Step through weeks while ($week_recurring_timestamp <= $until) { @@ -490,6 +508,7 @@ public function process_recurrences() // Move forwards $interval weeks $week_recurring_timestamp = strtotime($offset, $week_recurring_timestamp); + } break; case 'MONTHLY':