Skip to content

Commit

Permalink
Use RRULE[WKST] and/or a default first day of week (USA=SU, Europe=MO)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromecombes committed Jun 20, 2016
1 parent bb20646 commit 1770648
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions class.iCalReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ 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
*
* @param {mixed} $filename The path to the iCal-file or an array of lines from an iCal file
*
* @return Object The iCal Object
*/
public function __construct($filename=false)
public function __construct($filename=false, $weekStart=false)
{
if (!$filename) {
return false;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -490,6 +508,7 @@ public function process_recurrences()

// Move forwards $interval weeks
$week_recurring_timestamp = strtotime($offset, $week_recurring_timestamp);

}
break;
case 'MONTHLY':
Expand Down

0 comments on commit 1770648

Please sign in to comment.