Horde_Date_Recurrence::toKolab PHP Method

toKolab() public method

Export this object into a Kolab hash.
public toKolab ( ) : array
return array The recurrence hash.
    public function toKolab()
    {
        if ($this->getRecurType() == self::RECUR_NONE) {
            return array();
        }
        $day2number = array(0 => 'sunday', 1 => 'monday', 2 => 'tuesday', 3 => 'wednesday', 4 => 'thursday', 5 => 'friday', 6 => 'saturday');
        $month2number = array(1 => 'january', 2 => 'february', 3 => 'march', 4 => 'april', 5 => 'may', 6 => 'june', 7 => 'july', 8 => 'august', 9 => 'september', 10 => 'october', 11 => 'november', 12 => 'december');
        $hash = array('interval' => $this->getRecurInterval());
        $start = $this->getRecurStart();
        switch ($this->getRecurType()) {
            case self::RECUR_DAILY:
                $hash['cycle'] = 'daily';
                break;
            case self::RECUR_WEEKLY:
                $hash['cycle'] = 'weekly';
                $bits = array('monday' => Horde_Date::MASK_MONDAY, 'tuesday' => Horde_Date::MASK_TUESDAY, 'wednesday' => Horde_Date::MASK_WEDNESDAY, 'thursday' => Horde_Date::MASK_THURSDAY, 'friday' => Horde_Date::MASK_FRIDAY, 'saturday' => Horde_Date::MASK_SATURDAY, 'sunday' => Horde_Date::MASK_SUNDAY);
                $days = array();
                foreach ($bits as $name => $bit) {
                    if ($this->recurOnDay($bit)) {
                        $days[] = $name;
                    }
                }
                $hash['day'] = $days;
                break;
            case self::RECUR_MONTHLY_DATE:
                $hash['cycle'] = 'monthly';
                $hash['type'] = 'daynumber';
                $hash['daynumber'] = $start->mday;
                break;
            case self::RECUR_MONTHLY_WEEKDAY:
            case self::RECUR_MONTHLY_LAST_WEEKDAY:
                $hash['cycle'] = 'monthly';
                $hash['type'] = 'weekday';
                if ($this->recurType == self::RECUR_MONTHLY_LAST_WEEKDAY) {
                    // This is not officially part of the Kolab 2.0 specs.
                    $hash['daynumber'] = '-1';
                } else {
                    $hash['daynumber'] = $start->weekOfMonth();
                }
                $hash['day'] = array($day2number[$start->dayOfWeek()]);
                break;
            case self::RECUR_YEARLY_DATE:
                $hash['cycle'] = 'yearly';
                $hash['type'] = 'monthday';
                $hash['daynumber'] = $start->mday;
                $hash['month'] = $month2number[$start->month];
                break;
            case self::RECUR_YEARLY_DAY:
                $hash['cycle'] = 'yearly';
                $hash['type'] = 'yearday';
                $hash['daynumber'] = $start->dayOfYear();
                break;
            case self::RECUR_YEARLY_WEEKDAY:
                $hash['cycle'] = 'yearly';
                $hash['type'] = 'weekday';
                $hash['daynumber'] = $start->weekOfMonth();
                $hash['day'] = array($day2number[$start->dayOfWeek()]);
                $hash['month'] = $month2number[$start->month];
        }
        if ($this->hasRecurCount()) {
            $hash['range-type'] = 'number';
            $hash['range'] = $this->getRecurCount();
        } elseif ($this->hasRecurEnd()) {
            $date = $this->getRecurEnd();
            $hash['range-type'] = 'date';
            $hash['range'] = $date->toDateTime();
        } else {
            $hash['range-type'] = 'none';
            $hash['range'] = '';
        }
        // Recurrence exceptions
        $hash['exclusion'] = $hash['complete'] = array();
        foreach ($this->exceptions as $exception) {
            $hash['exclusion'][] = new DateTime($exception);
        }
        foreach ($this->completions as $completionexception) {
            $hash['complete'][] = new DateTime($completionexception);
        }
        return $hash;
    }

Usage Example

Exemplo n.º 1
0
 /**
  */
 public function testCompletions()
 {
     $r = new Horde_Date_Recurrence(new Horde_Date(1970, 1, 1));
     $r->setRecurType(Horde_Date_Recurrence::RECUR_DAILY);
     $r->addCompletion(1970, 1, 2);
     $this->assertTrue($r->hasCompletion(1970, 1, 2));
     $this->assertEquals(1, count($r->getCompletions()));
     $r->addCompletion(1970, 1, 4);
     $this->assertEquals(2, count($r->getCompletions()));
     $r->deleteCompletion(1970, 1, 2);
     $this->assertEquals(1, count($r->getCompletions()));
     $this->assertFalse($r->hasCompletion(1970, 1, 2));
     $r->addCompletion(1970, 1, 2);
     $r->addException(1970, 1, 1);
     $r->addException(1970, 1, 3);
     $next = $r->nextRecurrence(new Horde_Date($r->start));
     $this->assertEquals(1, $next->mday);
     $this->assertTrue($r->hasException($next->year, $next->month, $next->mday));
     $next->mday++;
     $next = $r->nextRecurrence($next);
     $this->assertTrue($r->hasCompletion($next->year, $next->month, $next->mday));
     $next->mday++;
     $next = $r->nextRecurrence($next);
     $this->assertTrue($r->hasException($next->year, $next->month, $next->mday));
     $next->mday++;
     $next = $r->nextRecurrence($next);
     $this->assertTrue($r->hasCompletion($next->year, $next->month, $next->mday));
     $r->setRecurEnd(new Horde_Date(1970, 1, 4));
     $this->assertTrue($r->hasRecurEnd());
     $this->assertFalse($r->hasActiveRecurrence());
     $s = new Horde_Date_Recurrence(new Horde_Date(1970, 1, 1));
     $s->fromKolab($r->toKolab());
     $this->assertTrue($s->hasRecurEnd());
     $next = $s->nextRecurrence(new Horde_Date($s->start));
     $this->assertEquals(1, $next->mday);
     $this->assertTrue($s->hasException($next->year, $next->month, $next->mday));
     $next->mday++;
     $next = $s->nextRecurrence($next);
     $this->assertTrue($s->hasCompletion($next->year, $next->month, $next->mday));
     $next->mday++;
     $next = $s->nextRecurrence($next);
     $this->assertTrue($s->hasException($next->year, $next->month, $next->mday));
     $next->mday++;
     $next = $s->nextRecurrence($next);
     $this->assertTrue($s->hasCompletion($next->year, $next->month, $next->mday));
     $this->assertEquals(2, count($s->getCompletions()));
     $this->assertEquals(2, count($s->getExceptions()));
     $this->assertFalse($s->hasActiveRecurrence());
     $this->assertEquals(2, count($s->getCompletions()));
     $s->deleteCompletion(1970, 1, 2);
     $this->assertEquals(1, count($s->getCompletions()));
     $s->deleteCompletion(1970, 1, 4);
     $this->assertEquals(0, count($s->getCompletions()));
 }