Horde_Date_Recurrence::toRRule20 PHP Method

toRRule20() public method

Creates an iCalendar 2.0 recurrence rule.
public toRRule20 ( Horde_Icalendar $calendar ) : string
$calendar Horde_Icalendar A Horde_Icalendar object instance.
return string An iCalendar 2.0 conform RRULE value.
    public function toRRule20($calendar)
    {
        switch ($this->recurType) {
            case self::RECUR_NONE:
                return '';
            case self::RECUR_DAILY:
                $rrule = 'FREQ=DAILY;INTERVAL=' . $this->recurInterval;
                break;
            case self::RECUR_WEEKLY:
                $rrule = 'FREQ=WEEKLY;INTERVAL=' . $this->recurInterval;
                $vcaldays = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
                for ($i = $flag = 0; $i <= 7; ++$i) {
                    if ($this->recurOnDay(pow(2, $i))) {
                        if ($flag == 0) {
                            $rrule .= ';BYDAY=';
                            $flag = 1;
                        } else {
                            $rrule .= ',';
                        }
                        $rrule .= $vcaldays[$i];
                    }
                }
                break;
            case self::RECUR_MONTHLY_DATE:
                $rrule = 'FREQ=MONTHLY;INTERVAL=' . $this->recurInterval;
                break;
            case self::RECUR_MONTHLY_WEEKDAY:
            case self::RECUR_MONTHLY_LAST_WEEKDAY:
                if ($this->recurType == self::RECUR_MONTHLY_LAST_WEEKDAY) {
                    $nth_weekday = -1;
                } else {
                    $nth_weekday = (int) ($this->start->mday / 7);
                    if ($this->start->mday % 7 > 0) {
                        $nth_weekday++;
                    }
                }
                $vcaldays = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
                $rrule = 'FREQ=MONTHLY;INTERVAL=' . $this->recurInterval . ';BYDAY=' . $nth_weekday . $vcaldays[$this->start->dayOfWeek()];
                break;
            case self::RECUR_YEARLY_DATE:
                $rrule = 'FREQ=YEARLY;INTERVAL=' . $this->recurInterval;
                break;
            case self::RECUR_YEARLY_DAY:
                $rrule = 'FREQ=YEARLY;INTERVAL=' . $this->recurInterval . ';BYYEARDAY=' . $this->start->dayOfYear();
                break;
            case self::RECUR_YEARLY_WEEKDAY:
                $nth_weekday = (int) ($this->start->mday / 7);
                if ($this->start->mday % 7 > 0) {
                    $nth_weekday++;
                }
                $vcaldays = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
                $rrule = 'FREQ=YEARLY;INTERVAL=' . $this->recurInterval . ';BYDAY=' . $nth_weekday . $vcaldays[$this->start->dayOfWeek()] . ';BYMONTH=' . $this->start->month;
                break;
        }
        if ($this->hasRecurEnd()) {
            $recurEnd = clone $this->recurEnd;
            $rrule .= ';UNTIL=' . $calendar->_exportDateTime($recurEnd);
        }
        if ($count = $this->getRecurCount()) {
            $rrule .= ';COUNT=' . $count;
        }
        return $rrule;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Exports this task in iCalendar format.
  *
  * @param Horde_Icalendar $calendar  A Horde_Icalendar object that acts as
  *                                   the container.
  *
  * @return Horde_Icalendar_Vtodo  A vtodo component of this task.
  */
 public function toiCalendar(Horde_Icalendar $calendar)
 {
     $vTodo = Horde_Icalendar::newComponent('vtodo', $calendar);
     $v1 = $calendar->getAttribute('VERSION') == '1.0';
     $vTodo->setAttribute('UID', $this->uid);
     if (!empty($this->assignee)) {
         $vTodo->setAttribute('ATTENDEE', Nag::getUserEmail($this->assignee), array('ROLE' => 'REQ-PARTICIPANT'));
     }
     $vTodo->setAttribute('ORGANIZER', !empty($this->organizer) ? Nag::getUserEmail($this->organizer) : Nag::getUserEmail($this->owner));
     if (!empty($this->name)) {
         $vTodo->setAttribute('SUMMARY', $this->name);
     }
     if (!empty($this->desc)) {
         $vTodo->setAttribute('DESCRIPTION', $this->desc);
     }
     if (isset($this->priority)) {
         $vTodo->setAttribute('PRIORITY', $this->priority);
     }
     if (!empty($this->parent_id) && !empty($this->parent)) {
         $vTodo->setAttribute('RELATED-TO', $this->parent->uid);
     }
     if ($this->private) {
         $vTodo->setAttribute('CLASS', 'PRIVATE');
     }
     if (!empty($this->start)) {
         $vTodo->setAttribute('DTSTART', $this->start);
     }
     if ($this->due) {
         $vTodo->setAttribute('DUE', $this->due);
         if ($this->alarm) {
             if ($v1) {
                 $vTodo->setAttribute('AALARM', $this->due - $this->alarm * 60);
             } else {
                 $vAlarm = Horde_Icalendar::newComponent('valarm', $vTodo);
                 $vAlarm->setAttribute('ACTION', 'DISPLAY');
                 $vAlarm->setAttribute('TRIGGER;VALUE=DURATION', '-PT' . $this->alarm . 'M');
                 $vTodo->addComponent($vAlarm);
             }
         }
     }
     if ($this->completed) {
         $vTodo->setAttribute('STATUS', 'COMPLETED');
         $vTodo->setAttribute('COMPLETED', $this->completed_date ? $this->completed_date : $_SERVER['REQUEST_TIME']);
         $vTodo->setAttribute('PERCENT-COMPLETE', '100');
     } else {
         if (!empty($this->estimate)) {
             $vTodo->setAttribute('PERCENT-COMPLETE', $this->actual / $this->estimate * 100);
         }
         if ($v1) {
             $vTodo->setAttribute('STATUS', 'NEEDS ACTION');
         } else {
             $vTodo->setAttribute('STATUS', 'NEEDS-ACTION');
         }
     }
     // Recurrence.
     // We may have to implicitely set DTSTART if not set explicitely, may
     // some clients choke on missing DTSTART attributes while RRULE exists.
     if ($this->recurs()) {
         if ($v1) {
             $rrule = $this->recurrence->toRRule10($calendar);
         } else {
             $rrule = $this->recurrence->toRRule20($calendar);
         }
         if (!empty($rrule)) {
             $vTodo->setAttribute('RRULE', $rrule);
         }
         /* The completions represent deleted recurrences */
         foreach ($this->recurrence->getCompletions() as $exception) {
             if (!empty($exception)) {
                 // Use multiple EXDATE attributes instead of EXDATE
                 // attributes with multiple values to make Apple iCal
                 // happy.
                 list($year, $month, $mday) = sscanf($exception, '%04d%02d%02d');
                 $vTodo->setAttribute('EXDATE', array(new Horde_Date($year, $month, $mday)), array('VALUE' => 'DATE'));
             }
         }
     }
     if ($this->tags) {
         $vTodo->setAttribute('CATEGORIES', implode(', ', $this->tags));
     }
     /* Get the task's history. */
     $created = $modified = null;
     try {
         $log = $GLOBALS['injector']->getInstance('Horde_History')->getHistory('nag:' . $this->tasklist . ':' . $this->uid);
         foreach ($log as $entry) {
             switch ($entry['action']) {
                 case 'add':
                     $created = $entry['ts'];
                     break;
                 case 'modify':
                     $modified = $entry['ts'];
                     break;
             }
         }
     } catch (Exception $e) {
     }
     if (!empty($created)) {
         $vTodo->setAttribute($v1 ? 'DCREATED' : 'CREATED', $created);
         if (empty($modified)) {
             $modified = $created;
         }
     }
     if (!empty($modified)) {
         $vTodo->setAttribute('LAST-MODIFIED', $modified);
     }
     return $vTodo;
 }
All Usage Examples Of Horde_Date_Recurrence::toRRule20