/**
  * Inserts or updates an event to the specific calendar.
  * If we try to edit a task without due date, it deletes it
  *
  * @param PcTask $task
  * @param string $calendarUrl
  * @param bool $updateEmailAddress (=false)
  * @return int|bool the id of the event - false if the event we try to add/edit hasn't got due date
  */
 public function createOrUpdateEvent(PcTask $task, $updateEmailAddress = false)
 {
     $event = null;
     $googleCalendarEventId = $task->getGoogleCalendarEventId();
     $mode = self::EVENT_CREATE_MODE;
     if (strlen($googleCalendarEventId)) {
         $event = $this->getEvent($googleCalendarEventId);
         if (is_object($event)) {
             $mode = self::EVENT_UPDATE_MODE;
         }
         if ($this->hasEventBeenDeleted($event)) {
             return;
         }
     }
     if (SfConfig::get('app_gcal_debug')) {
         if ($mode == self::EVENT_CREATE_MODE) {
             error_log('Creating task on GCal for the Plancake task ' . $task->getId() . ' ' . $task->getDescription());
         } else {
             error_log('Editing task on GCal for the Plancake task ' . $task->getId() . ' ' . $task->getDescription());
         }
     }
     if ($mode == self::EVENT_CREATE_MODE) {
         $event = $this->service->newEventEntry();
     }
     if (!$task->getDueDate() || $task->isCompleted()) {
         // A task without due date or completed shouldn't be on Google Calendar
         $this->deleteEventById($googleCalendarEventId);
         $task->removeGoogleCalendarEventId();
         return false;
     }
     // {{{ Adding extended property to patch a problem we have this the integration:
     // search 'EVENT_EXTENDED_PROPERTY_TASK_ID' in this file
     $this->setExtendedProperty($event, self::EVENT_EXTENDED_PROPERTY_TASK_ID, $task->getId());
     // }}}
     $event->title = $this->service->newTitle($task->getDescription());
     $event->where = array($this->service->newWhere(""));
     $event->content = $this->service->newContent($task->getNote());
     // Setting the date using RFC 3339 format
     list($startDate, $startTime) = DateFormat::fromLocalDateAndTime2GMTDateAndTime($task->getDueDate("Y-m-d"), $task->getDueTime());
     $timeObject = new PcTime();
     if ($startTime > 0) {
         $startTime = $timeObject->createFromIntegerValue($startTime)->get5CharsRepresentation();
     } else {
         $startTime = null;
     }
     //$endDate = "2011-01-20";
     //$endTime = "16:00";
     //$userTimezone = $this->user->getPcTimezone();
     //$tzOffset = ($userTimezone->getOffset()/60);
     // right now, $tzOffset can be -8, but we need -08
     //$tzOffset = "-08";
     if ($task->getRepetitionId() > 0) {
         //            $recurrence = "DTSTART;VALUE=DATE:20110501\r\n" .
         //                    "DTEND;VALUE=DATE:20110502\r\n" .
         //                    "RRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20110904\r\n";
         $dtStart = $task->getDueDate("Ymd");
         if ($startTime !== null) {
             $dtStart = $dtStart . 'T' . str_replace(':', '', $startTime) . '00Z';
         }
         $recurrence = "DTSTART:{$dtStart}\r\n" . "RRULE:{$task->getRepetitionICalRrule()}\r\n";
         $event->recurrence = $this->service->newRecurrence($recurrence);
     } else {
         $when = $this->service->newWhen();
         if ($startTime !== null) {
             $when->startTime = "{$startDate}T{$startTime}:00.000Z";
         } else {
             $when->startTime = $startDate;
         }
         // $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
         $event->when = array($when);
     }
     if ($mode == self::EVENT_CREATE_MODE) {
         $newEvent = null;
         try {
             $newEvent = $this->service->insertEvent($event, $this->getCalendarUrl());
         } catch (Exception $e) {
             $newEvent = $this->service->insertEvent($event, $this->getCalendarUrl());
         }
     } else {
         // {{{ rather then running just $event->save() ,
         // we try many times, as sometimes the updating is a bit
         //quirky on Google Calendar end
         try {
             $event->save();
         } catch (Exception $e) {
             try {
                 $event->save();
             } catch (Exception $e) {
                 $event->save();
             }
         }
         // }}}
         $newEvent = $event;
     }
     if ($updateEmailAddress) {
         $dbEntry = PcGoogleCalendarPeer::retrieveByUser($this->user);
         $dbEntry->setEmailAddress($newEvent->author[0]->email->text)->save();
     }
     $eventId = $this->getEventId($newEvent);
     if ($mode == self::EVENT_CREATE_MODE) {
         $task->setGoogleCalendarEventId($eventId);
     }
     return $eventId;
 }
Exemple #2
0
 /**
  * @return string
  */
 public function getHumanFriendlyDueTime()
 {
     $dueTime = new PcTime();
     $dueTime->createFromIntegerValue($this->getDueTime());
     return $dueTime->getHumanFriendlyTime(PcUserPeer::getLoggedInUser());
 }