Created
August 13, 2011 22:41
-
-
Save mattsah/1144327 to your computer and use it in GitHub Desktop.
Generates Event Occurr for Recurring Events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function generateEventOccurrences() | |
{ | |
// Flush all existing occurrences | |
foreach ($this->buildEventOccurrences() as $event_occurrence) { | |
$event_occurrence->delete(); | |
} | |
// Rebuild based on the new date information | |
// Determine start timestamp | |
$start_timestamp = $this->prepareStartDate('Y-m-d'); | |
if ($this->getStartTime() == NULL) { | |
$start_timestamp .= ' 00:00:00'; | |
} else { | |
$start_timestamp .= $this->prepareStartTime(' H:i:s'); | |
} | |
$start_timestamp = new fTimestamp($start_timestamp); | |
// Determine end timestamp | |
if ($this->getEndDate() == NULL) { | |
$end_timestamp = $this->prepareStartDate('Y-m-d'); | |
} | |
if ($this->getEndTime() == NULL) { | |
$end_timestamp .= ' 11:59:59'; | |
} else { | |
$end_timestamp .= $this->prepareEndTime(' H:i:s'); | |
} | |
$end_timestamp = new fTimestamp($end_timestamp); | |
// Determine recurrence until | |
if ($this->getRecurrenceUntil() == NULL) { | |
$recurrence_until = strtotime('+5 years'); | |
} else { | |
$recurrence_until = $this->prepareRecurrenceUntil('Y-m-d'); | |
} | |
$recurrence_until = new fTimestamp($recurrence_until); | |
$recurrence_until->adjust('+1 day')->adjust('-1 second'); | |
switch ($this->getRecurrence()) { | |
case 'Never': | |
EventOccurrence::create($this, $start_timestamp, $end_timestamp); | |
break; | |
case 'Daily': | |
while ($start_timestamp->lt($recurrence_until)) { | |
EventOccurrence::create($this, $start_timestamp, $end_timestamp); | |
$start_timestamp = $start_timestamp->adjust("+{$this->getRecurrenceFrequency()} days"); | |
$end_timestamp = $end_timestamp->adjust("+{$this->getRecurrenceFrequency()} days"); | |
} | |
break; | |
case 'Weekly': | |
// Add the first occurance and set week to week 0 | |
EventOccurrence::create($this, $start_timestamp, $end_timestamp); | |
$week = 0; | |
while ($start_timestamp->lt($recurrence_until)) { | |
$start_timestamp = $start_timestamp->adjust('+1 day'); | |
$end_timestamp = $end_timestamp->adjust('+1 day'); | |
$current_day = pow(2, $start_timestamp->format('w')); | |
// If we've hit sunday, increment the week | |
if ($current_day == 1) { | |
$week++; | |
} | |
// If We're in an active week | |
if (($week % $this->getRecurrenceFrequency()) == 0) { | |
// If We're on an active day | |
if (($current_day & $this->getRecurrenceDays()) == $current_day) { | |
EventOccurrence::create($this, $start_timestamp, $end_timestamp); | |
} | |
} | |
} | |
break; | |
case 'Monthly': | |
while ($start_timestamp->lt($recurrence_until)) { | |
EventOccurrence::create($this, $start_timestamp, $end_timestamp); | |
$start_timestamp = $start_timestamp->adjust("+{$this->getRecurrenceFrequency()} months"); | |
$end_timestamp = $end_timestamp->adjust("+{$this->getRecurrenceFrequency()} months"); | |
} | |
break; | |
case 'Yearly': | |
while ($start_timestapm->lt($recurrence_until)) { | |
EventOccurrence::create($this, $start_timestamp, $end_timestamp); | |
// fix leapyear | |
if ($start_timestamp->format('L') && $start_timestamp->format('m-d') == '02-29') { | |
$start_timestamp->adjust("-1 day"); | |
} | |
$start_timestamp = $start_timestamp->adjust("+{$this->getRecurrenceFrequency()} years"); | |
$end_timestamp = $end_timestamp->adjust("+{$this->getRecurrenceFrequency()} years"); | |
} | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment