Esempio n. 1
0
 public function setTimezone($timezone)
 {
     $this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
     $this->TZID = $this->timezone->getName();
     list($standardTransition, $daylightTransition) = $transitions = $this->_getTransitionsForTimezoneAndYear($this->timezone, date('Y'));
     $dtstart = new Sabre_VObject_Property_DateTime('DTSTART');
     $dtstart->setDateTime(new DateTime(), Sabre_VObject_Element_DateTime::LOCAL);
     if ($daylightTransition !== null) {
         $offsetTo = ($daylightTransition['offset'] < 0 ? '-' : '+') . strftime('%H%M', abs($daylightTransition['offset']));
         $offsetFrom = ($standardTransition['offset'] < 0 ? '-' : '+') . strftime('%H%M', abs($standardTransition['offset']));
         $daylight = new Sabre_VObject_Component('DAYLIGHT');
         $daylight->TZOFFSETFROM = $offsetFrom;
         $daylight->TZOFFSETTO = $offsetTo;
         $daylight->TZNAME = $daylightTransition['abbr'];
         $daylight->DTSTART = $dtstart;
         #$daylight->RRULE       = 'FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3';
         $this->add($daylight);
     }
     if ($standardTransition !== null) {
         $offsetTo = ($standardTransition['offset'] < 0 ? '-' : '+') . strftime('%H%M', abs($standardTransition['offset']));
         if ($daylightTransition !== null) {
             $offsetFrom = ($daylightTransition['offset'] < 0 ? '-' : '+') . strftime('%H%M', abs($daylightTransition['offset']));
         } else {
             $offsetFrom = $offsetTo;
         }
         $standard = new Sabre_VObject_Component('STANDARD');
         $standard->TZOFFSETFROM = $offsetFrom;
         $standard->TZOFFSETTO = $offsetTo;
         $standard->TZNAME = $standardTransition['abbr'];
         $standard->DTSTART = $dtstart;
         #$standard->RRULE         = 'FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10';
         $this->add($standard);
     }
 }
 /**
  * Parses the input data and returns a correct VFREEBUSY object, wrapped in
  * a VCALENDAR.
  *
  * @return Sabre_VObject_Component
  */
 public function getResult()
 {
     $busyTimes = array();
     foreach ($this->objects as $object) {
         foreach ($object->getBaseComponents() as $component) {
             switch ($component->name) {
                 case 'VEVENT':
                     $FBTYPE = 'BUSY';
                     if (isset($component->TRANSP) && strtoupper($component->TRANSP) === 'TRANSPARENT') {
                         break;
                     }
                     if (isset($component->STATUS)) {
                         $status = strtoupper($component->STATUS);
                         if ($status === 'CANCELLED') {
                             break;
                         }
                         if ($status === 'TENTATIVE') {
                             $FBTYPE = 'BUSY-TENTATIVE';
                         }
                     }
                     $times = array();
                     if ($component->RRULE) {
                         $iterator = new Sabre_VObject_RecurrenceIterator($object, (string) $component->uid);
                         if ($this->start) {
                             $iterator->fastForward($this->start);
                         }
                         $maxRecurrences = 200;
                         while ($iterator->valid() && --$maxRecurrences) {
                             $startTime = $iterator->getDTStart();
                             if ($this->end && $startTime > $this->end) {
                                 break;
                             }
                             $times[] = array($iterator->getDTStart(), $iterator->getDTEnd());
                             $iterator->next();
                         }
                     } else {
                         $startTime = $component->DTSTART->getDateTime();
                         if ($this->end && $startTime > $this->end) {
                             break;
                         }
                         $endTime = null;
                         if (isset($component->DTEND)) {
                             $endTime = $component->DTEND->getDateTime();
                         } elseif (isset($component->DURATION)) {
                             $duration = Sabre_VObject_DateTimeParser::parseDuration((string) $component->DURATION);
                             $endTime = clone $startTime;
                             $endTime->add($duration);
                         } elseif ($component->DTSTART->getDateType() === Sabre_VObject_Property_DateTime::DATE) {
                             $endTime = clone $startTime;
                             $endTime->modify('+1 day');
                         } else {
                             // The event had no duration (0 seconds)
                             break;
                         }
                         $times[] = array($startTime, $endTime);
                     }
                     foreach ($times as $time) {
                         if ($this->end && $time[0] > $this->end) {
                             break;
                         }
                         if ($this->start && $time[1] < $this->start) {
                             break;
                         }
                         $busyTimes[] = array($time[0], $time[1], $FBTYPE);
                     }
                     break;
                 case 'VFREEBUSY':
                     foreach ($component->FREEBUSY as $freebusy) {
                         $fbType = isset($freebusy['FBTYPE']) ? strtoupper($freebusy['FBTYPE']) : 'BUSY';
                         // Skipping intervals marked as 'free'
                         if ($fbType === 'FREE') {
                             continue;
                         }
                         $values = explode(',', $freebusy);
                         foreach ($values as $value) {
                             list($startTime, $endTime) = explode('/', $value);
                             $startTime = Sabre_VObject_DateTimeParser::parseDateTime($startTime);
                             if (substr($endTime, 0, 1) === 'P' || substr($endTime, 0, 2) === '-P') {
                                 $duration = Sabre_VObject_DateTimeParser::parseDuration($endTime);
                                 $endTime = clone $startTime;
                                 $endTime->add($duration);
                             } else {
                                 $endTime = Sabre_VObject_DateTimeParser::parseDateTime($endTime);
                             }
                             if ($this->start && $this->start > $endTime) {
                                 continue;
                             }
                             if ($this->end && $this->end < $startTime) {
                                 continue;
                             }
                             $busyTimes[] = array($startTime, $endTime, $fbType);
                         }
                     }
                     break;
             }
         }
     }
     if ($this->baseObject) {
         $calendar = $this->baseObject;
     } else {
         $calendar = new Sabre_VObject_Component('VCALENDAR');
         $calendar->version = '2.0';
         $calendar->prodid = '-//SabreDAV//Sabre VObject ' . Sabre_VObject_Version::VERSION . '//EN';
         $calendar->calscale = 'GREGORIAN';
     }
     $vfreebusy = new Sabre_VObject_Component('VFREEBUSY');
     $calendar->add($vfreebusy);
     if ($this->start) {
         $dtstart = new Sabre_VObject_Property_DateTime('DTSTART');
         $dtstart->setDateTime($this->start, Sabre_VObject_Property_DateTime::UTC);
         $vfreebusy->add($dtstart);
     }
     if ($this->end) {
         $dtend = new Sabre_VObject_Property_DateTime('DTEND');
         $dtend->setDateTime($this->start, Sabre_VObject_Property_DateTime::UTC);
         $vfreebusy->add($dtend);
     }
     $dtstamp = new Sabre_VObject_Property_DateTime('DTSTAMP');
     $dtstamp->setDateTime(new DateTime('now'), Sabre_VObject_Property_DateTime::UTC);
     $vfreebusy->add($dtstamp);
     foreach ($busyTimes as $busyTime) {
         $busyTime[0]->setTimeZone(new DateTimeZone('UTC'));
         $busyTime[1]->setTimeZone(new DateTimeZone('UTC'));
         $prop = new Sabre_VObject_Property('FREEBUSY', $busyTime[0]->format('Ymd\\THis\\Z') . '/' . $busyTime[1]->format('Ymd\\THis\\Z'));
         $prop['FBTYPE'] = $busyTime[2];
         $vfreebusy->add($prop);
     }
     return $calendar;
 }
 /**
  * @expectedException InvalidArgumentException
  */
 function testGetDateTimeDateInvalid()
 {
     $elem = new Sabre_VObject_Property_DateTime('DTSTART', 'bla');
     $dt = $elem->getDateTime();
 }
Esempio n. 4
0
 /**
  * Returns the type of Date format.
  *
  * This method returns one of the format constants. If no date was set,
  * this method will return null.
  *
  * @return int|null
  */
 public function getDateType()
 {
     if ($this->dateType) {
         return $this->dateType;
     }
     if (!$this->value) {
         $this->dateTimes = null;
         $this->dateType = null;
         return null;
     }
     $dts = array();
     foreach (explode(',', $this->value) as $val) {
         list($type, $dt) = Sabre_VObject_Property_DateTime::parseData($val, $this);
         $dts[] = $dt;
         $this->dateType = $type;
     }
     $this->dateTimes = $dts;
     return $this->dateType;
 }
Esempio n. 5
0
 /**
  * Sets or unsets the Date and Time for a property.
  * When $datetime is set to 'now', use the current time
  * When $datetime is null, unset the property
  *
  * @param string property name
  * @param DateTime $datetime
  * @param int $dateType
  * @return void
  */
 public function setDateTime($name, $datetime, $dateType = Sabre_VObject_Property_DateTime::LOCALTZ)
 {
     if ($datetime == 'now') {
         $datetime = new DateTime();
     }
     if ($datetime instanceof DateTime) {
         $datetime_element = new Sabre_VObject_Property_DateTime($name);
         $datetime_element->setDateTime($datetime, $dateType);
         $this->vobject->__set($name, $datetime_element);
     } else {
         $this->vobject->__unset($name);
     }
 }
Esempio n. 6
0
 /**
  * converts Addressbook_Model_Contact to vcard
  * 
  * @todo return all supported fields in correct format see http://forge.tine20.org/mantisbt/view.php?id=5346
  * @param  Addressbook_Model_Contact  $_record
  * @return string
  */
 public function fromTine20Model(Tinebase_Record_Abstract $_record)
 {
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' contact ' . print_r($_record->toArray(), true));
     }
     $card = new Sabre_VObject_Component('VCARD');
     // required vcard fields
     $card->add(new Sabre_VObject_Property('VERSION', '3.0'));
     $card->add(new Sabre_VObject_Property('FN', $_record->n_fileas));
     $card->add(new Sabre_VObject_Element_MultiValue('N', array($_record->n_family, $_record->n_given, $_record->n_middle, $_record->n_prefix, $_record->n_suffix)));
     $card->add(new Sabre_VObject_Property('PRODID', '-//tine20.org//Tine 2.0//EN'));
     $card->add(new Sabre_VObject_Property('UID', $_record->getId()));
     // optional fields
     $card->add(new Sabre_VObject_Element_MultiValue('ORG', array($_record->org_name, $_record->org_unit)));
     $card->add(new Sabre_VObject_Property('TITLE', $_record->title));
     $tel = new Sabre_VObject_Property('TEL', $_record->tel_work);
     $tel->add('TYPE', 'WORK');
     $card->add($tel);
     $tel = new Sabre_VObject_Property('TEL', $_record->tel_home);
     $tel->add('TYPE', 'HOME');
     $card->add($tel);
     $tel = new Sabre_VObject_Property('TEL', $_record->tel_cell);
     $tel->add('TYPE', 'CELL');
     $card->add($tel);
     $tel = new Sabre_VObject_Property('TEL', $_record->tel_pager);
     $tel->add('TYPE', 'PAGER');
     $card->add($tel);
     $tel = new Sabre_VObject_Property('TEL', $_record->tel_fax);
     $tel->add('TYPE', 'FAX');
     $card->add($tel);
     $tel = new Sabre_VObject_Property('TEL', $_record->tel_fax_home);
     $tel->add('TYPE', 'FAX');
     $tel->add('TYPE', 'HOME');
     $card->add($tel);
     $tel = new Sabre_VObject_Property('TEL', $_record->tel_cell_private);
     $tel->add('TYPE', 'CELL');
     $tel->add('TYPE', 'HOME');
     $card->add($tel);
     $adr = new Sabre_VObject_Element_MultiValue('ADR', array(null, $_record->adr_one_street2, $_record->adr_one_street, $_record->adr_one_locality, $_record->adr_one_region, $_record->adr_one_postalcode, $_record->adr_one_countryname));
     $adr->add('TYPE', 'WORK');
     $card->add($adr);
     $adr = new Sabre_VObject_Element_MultiValue('ADR', array(null, $_record->adr_two_street2, $_record->adr_two_street, $_record->adr_two_locality, $_record->adr_two_region, $_record->adr_two_postalcode, $_record->adr_two_countryname));
     $adr->add('TYPE', 'HOME');
     $card->add($adr);
     $email = new Sabre_VObject_Property('EMAIL', $_record->email);
     $adr->add('TYPE', 'PREF');
     $card->add($email);
     $email = new Sabre_VObject_Property('EMAIL', $_record->email_home);
     $card->add($email);
     $card->add(new Sabre_VObject_Property('URL', $_record->url));
     $card->add(new Sabre_VObject_Property('NOTE', $_record->note));
     if ($_record->bday instanceof Tinebase_DateTime) {
         $bday = new Sabre_VObject_Property_DateTime('BDAY');
         $bday->setDateTime($_record->bday, Sabre_VObject_Property_DateTime::LOCAL);
         $card->add($bday);
     }
     if (!empty($_record->jpegphoto)) {
         try {
             $image = Tinebase_Controller::getInstance()->getImage('Addressbook', $_record->getId());
             $jpegData = $image->getBlob('image/jpeg');
             $photo = new Sabre_VObject_Property('PHOTO', $jpegData);
             $photo->add('ENCODING', 'b');
             $photo->add('TYPE', 'JPEG');
             $card->add($photo);
         } catch (Exception $e) {
             Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . " Image for contact {$_record->getId()} not found or invalid");
         }
     }
     if (isset($_record->tags) && count($_record->tags) > 0) {
         $card->add(new Sabre_VObject_Property('CATEGORIES', Sabre_VObject_Element_List((array) $_record->tags->name)));
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' card ' . $card->serialize());
     }
     return $card;
 }
 /**
  * @depends testValues
  */
 function testComplexExclusions()
 {
     $ev = new Sabre_VObject_Component('VEVENT');
     $ev->UID = 'bla';
     $ev->RRULE = 'FREQ=YEARLY;COUNT=10';
     $dtStart = new Sabre_VObject_Property_DateTime('DTSTART');
     $tz = new DateTimeZone('Canada/Eastern');
     $dtStart->setDateTime(new DateTime('2011-01-01 13:50:20', $tz), Sabre_VObject_Property_DateTime::LOCALTZ);
     $exDate1 = new Sabre_VObject_Property_MultiDateTime('EXDATE');
     $exDate1->setDateTimes(array(new DateTime('2012-01-01 13:50:20', $tz), new DateTime('2014-01-01 13:50:20', $tz)), Sabre_VObject_Property_DateTime::LOCALTZ);
     $exDate2 = new Sabre_VObject_Property_MultiDateTime('EXDATE');
     $exDate2->setDateTimes(array(new DateTime('2016-01-01 13:50:20', $tz)), Sabre_VObject_Property_DateTime::LOCALTZ);
     $ev->add($dtStart);
     $ev->add($exDate1);
     $ev->add($exDate2);
     $vcal = Sabre_VObject_Component::create('VCALENDAR');
     $vcal->add($ev);
     $it = new Sabre_VObject_RecurrenceIterator($vcal, (string) $ev->uid);
     $this->assertEquals('yearly', $it->frequency);
     $this->assertEquals(1, $it->interval);
     $this->assertEquals(10, $it->count);
     $max = 20;
     $result = array();
     foreach ($it as $k => $item) {
         $result[] = $item;
         $max--;
         if (!$max) {
             break;
         }
     }
     $this->assertEquals(array(new DateTime('2011-01-01 13:50:20', $tz), new DateTime('2013-01-01 13:50:20', $tz), new DateTime('2015-01-01 13:50:20', $tz), new DateTime('2017-01-01 13:50:20', $tz), new DateTime('2018-01-01 13:50:20', $tz), new DateTime('2019-01-01 13:50:20', $tz), new DateTime('2020-01-01 13:50:20', $tz)), $result);
 }