Horde_Date_Recurrence::fromRRule20 PHP Method

fromRRule20() public method

Parses an iCalendar 2.0 recurrence rule.
public fromRRule20 ( string $rrule )
$rrule string An iCalendar 2.0 conform RRULE value.
    public function fromRRule20($rrule)
    {
        $this->reset();
        // Parse the recurrence rule into keys and values.
        $rdata = array();
        $parts = explode(';', $rrule);
        foreach ($parts as $part) {
            list($key, $value) = explode('=', $part, 2);
            $rdata[Horde_String::upper($key)] = $value;
        }
        if (isset($rdata['FREQ'])) {
            // Always default the recurInterval to 1.
            $this->setRecurInterval(isset($rdata['INTERVAL']) ? $rdata['INTERVAL'] : 1);
            switch (Horde_String::upper($rdata['FREQ'])) {
                case 'DAILY':
                    $this->setRecurType(self::RECUR_DAILY);
                    break;
                case 'WEEKLY':
                    $this->setRecurType(self::RECUR_WEEKLY);
                    if (isset($rdata['BYDAY'])) {
                        $maskdays = array('SU' => Horde_Date::MASK_SUNDAY, 'MO' => Horde_Date::MASK_MONDAY, 'TU' => Horde_Date::MASK_TUESDAY, 'WE' => Horde_Date::MASK_WEDNESDAY, 'TH' => Horde_Date::MASK_THURSDAY, 'FR' => Horde_Date::MASK_FRIDAY, 'SA' => Horde_Date::MASK_SATURDAY);
                        $days = explode(',', $rdata['BYDAY']);
                        $mask = 0;
                        foreach ($days as $day) {
                            $mask |= $maskdays[$day];
                        }
                        $this->setRecurOnDay($mask);
                    } else {
                        // Recur on the day of the week of the original
                        // recurrence.
                        $maskdays = array(Horde_Date::DATE_SUNDAY => Horde_Date::MASK_SUNDAY, Horde_Date::DATE_MONDAY => Horde_Date::MASK_MONDAY, Horde_Date::DATE_TUESDAY => Horde_Date::MASK_TUESDAY, Horde_Date::DATE_WEDNESDAY => Horde_Date::MASK_WEDNESDAY, Horde_Date::DATE_THURSDAY => Horde_Date::MASK_THURSDAY, Horde_Date::DATE_FRIDAY => Horde_Date::MASK_FRIDAY, Horde_Date::DATE_SATURDAY => Horde_Date::MASK_SATURDAY);
                        $this->setRecurOnDay($maskdays[$this->start->dayOfWeek()]);
                    }
                    break;
                case 'MONTHLY':
                    if (isset($rdata['BYDAY'])) {
                        if (strpos($rdata['BYDAY'], '-') === false) {
                            $this->setRecurType(self::RECUR_MONTHLY_WEEKDAY);
                        } else {
                            $this->setRecurType(self::RECUR_MONTHLY_LAST_WEEKDAY);
                        }
                    } else {
                        $this->setRecurType(self::RECUR_MONTHLY_DATE);
                    }
                    break;
                case 'YEARLY':
                    if (isset($rdata['BYYEARDAY'])) {
                        $this->setRecurType(self::RECUR_YEARLY_DAY);
                    } elseif (isset($rdata['BYDAY'])) {
                        $this->setRecurType(self::RECUR_YEARLY_WEEKDAY);
                    } else {
                        $this->setRecurType(self::RECUR_YEARLY_DATE);
                    }
                    break;
            }
            // MUST take into account the time portion if it is present.
            // See Bug: 12869 and Bug: 2813
            if (isset($rdata['UNTIL'])) {
                if (preg_match('/^(\\d{4})-?(\\d{2})-?(\\d{2})T? ?(\\d{2}):?(\\d{2}):?(\\d{2})(?:\\.\\d+)?(Z?)$/', $rdata['UNTIL'], $parts)) {
                    $until = new Horde_Date($rdata['UNTIL'], 'UTC');
                    $until->setTimezone($this->start->timezone);
                } else {
                    list($year, $month, $mday) = sscanf($rdata['UNTIL'], '%04d%02d%02d');
                    $until = new Horde_Date(array('year' => $year, 'month' => $month, 'mday' => $mday + 1), $this->start->timezone);
                }
                $this->setRecurEnd($until);
            }
            if (isset($rdata['COUNT'])) {
                $this->setRecurCount($rdata['COUNT']);
            }
        } else {
            // No recurrence data - event does not recur.
            $this->setRecurType(self::RECUR_NONE);
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Handle parsing recurrence related fields.
  *
  * @param Horde_Icalendar $vEvent
  * @throws Kronolith_Exception
  */
 protected function _handlevEventRecurrence($vEvent)
 {
     // Recurrence.
     try {
         $rrule = $vEvent->getAttribute('RRULE');
         if (!is_array($rrule)) {
             $this->recurrence = new Horde_Date_Recurrence($this->start);
             if (strpos($rrule, '=') !== false) {
                 $this->recurrence->fromRRule20($rrule);
             } else {
                 $this->recurrence->fromRRule10($rrule);
             }
             /* Delete all existing exceptions to this event if it
              * already exists */
             if (!empty($this->uid)) {
                 $kronolith_driver = Kronolith::getDriver(null, $this->calendar);
                 $search = new StdClass();
                 $search->start = $this->recurrence->getRecurStart();
                 $search->end = $this->recurrence->getRecurEnd();
                 $search->baseid = $this->uid;
                 $results = $kronolith_driver->search($search);
                 foreach ($results as $days) {
                     foreach ($days as $exception) {
                         $kronolith_driver->deleteEvent($exception->id);
                     }
                 }
             }
             // Exceptions. EXDATE represents deleted events, just add the
             // exception, no new event is needed.
             $exdates = $vEvent->getAttributeValues('EXDATE');
             if (is_array($exdates)) {
                 foreach ($exdates as $exdate) {
                     if (is_array($exdate)) {
                         $this->recurrence->addException((int) $exdate['year'], (int) $exdate['month'], (int) $exdate['mday']);
                     }
                 }
             }
         }
     } catch (Horde_Icalendar_Exception $e) {
     }
     // RECURRENCE-ID indicates that this event represents an exception
     try {
         $recurrenceid = $vEvent->getAttribute('RECURRENCE-ID');
         $kronolith_driver = Kronolith::getDriver(null, $this->calendar);
         $originaldt = new Horde_Date($recurrenceid);
         $this->exceptionoriginaldate = $originaldt;
         $this->baseid = $this->uid;
         $this->uid = null;
         try {
             $originalEvent = $kronolith_driver->getByUID($this->baseid);
             $originalEvent->recurrence->addException($originaldt->format('Y'), $originaldt->format('m'), $originaldt->format('d'));
             $originalEvent->save();
         } catch (Horde_Exception_NotFound $e) {
             throw new Kronolith_Exception(_("Unable to locate original event series."));
         }
     } catch (Horde_Icalendar_Exception $e) {
     }
 }
All Usage Examples Of Horde_Date_Recurrence::fromRRule20