Horde_Date_Recurrence::fromRRule10 PHP Method

fromRRule10() public method

Parses a vCalendar 1.0 recurrence rule.
public fromRRule10 ( string $rrule )
$rrule string A vCalendar 1.0 conform RRULE value.
    public function fromRRule10($rrule)
    {
        $this->reset();
        if (!$rrule) {
            return;
        }
        if (!preg_match('/([A-Z]+)(\\d+)?(.*)/', $rrule, $matches)) {
            // No recurrence data - event does not recur.
            $this->setRecurType(self::RECUR_NONE);
        }
        // Always default the recurInterval to 1.
        $this->setRecurInterval(!empty($matches[2]) ? $matches[2] : 1);
        $remainder = trim($matches[3]);
        switch ($matches[1]) {
            case 'D':
                $this->setRecurType(self::RECUR_DAILY);
                break;
            case 'W':
                $this->setRecurType(self::RECUR_WEEKLY);
                $mask = 0;
                if (!empty($remainder)) {
                    $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);
                    while (preg_match('/^ ?(' . implode('|', array_keys($maskdays)) . ') ?/', $remainder, $matches)) {
                        $day = trim($matches[0]);
                        $remainder = substr($remainder, strlen($matches[0]));
                        $mask |= $maskdays[$day];
                    }
                    $this->setRecurOnDay($mask);
                }
                if (!$mask) {
                    // 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 'MP':
                $this->setRecurType(self::RECUR_MONTHLY_WEEKDAY);
                if (preg_match('/^ \\d([+-])/', $remainder, $matches) && $matches[1] == '-') {
                    $this->setRecurType(self::RECUR_MONTHLY_LAST_WEEKDAY);
                }
                break;
            case 'MD':
                $this->setRecurType(self::RECUR_MONTHLY_DATE);
                break;
            case 'YM':
                $this->setRecurType(self::RECUR_YEARLY_DATE);
                break;
            case 'YD':
                $this->setRecurType(self::RECUR_YEARLY_DAY);
                break;
        }
        // Strip further modifiers.
        while ($remainder && !preg_match('/^(#\\d+|\\d{8})($| |T\\d{6})/', $remainder)) {
            $remainder = substr($remainder, 1);
        }
        if (!empty($remainder)) {
            if (strpos($remainder, '#') === 0) {
                $this->setRecurCount(substr($remainder, 1));
            } else {
                list($year, $month, $mday, $hour, $min, $sec, $tz) = sscanf($remainder, '%04d%02d%02dT%02d%02d%02d%s');
                $this->setRecurEnd(new Horde_Date(array('year' => $year, 'month' => $month, 'mday' => $mday, 'hour' => $hour, 'min' => $min, 'sec' => $sec), $tz == 'Z' ? 'UTC' : $this->start->timezone));
            }
        }
    }

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::fromRRule10