/** * Updates the corresponding Plancake task. * If there is not a corresponding Plancake task, * we create a new one. * * @param -Google Calendar Event- $event */ private function updatePlancakeTask($event) { $eventId = $this->getEventId($event); // retrieving the Plancake task: we use this method, rather than the EVENT_EXTENDED_PROPERTY_TASK_ID // extended property, because it should be more reliable $task = PcTaskPeer::getTaskByGoogleCalendarEventId($eventId); // {{{ PATCH: we have this problem something Google was trying to send us back some events that // had been either completed or deleted on the Plancake side. // This is the reason why we have decided to use an extended property to keep a record of the link // CalendarEvent<-->PlancakeTask $taskIdFromExtendedProperty = $this->getExtendedProperty($event, self::EVENT_EXTENDED_PROPERTY_TASK_ID); if ($taskIdFromExtendedProperty) { $taskFromExtendedProperty = PcTaskPeer::retrieveByPK($taskIdFromExtendedProperty); if ($taskFromExtendedProperty && $taskFromExtendedProperty->isCompleted()) { $taskFromExtendedProperty->removeGoogleCalendarEventId(); return; } if (PcTrashbinTaskPeer::retrieveByPK($taskIdFromExtendedProperty)) { return; } } // }}} // end PATCH // {{{ checking whether the event has been deleted if ($this->hasEventBeenDeleted($event)) { // event has been deleted if (is_object($task)) { if (SfConfig::get('app_gcal_debug')) { error_log('Deleting Plancake task ' . $task->getId() . ' ' . $task->getDescription()); } $task->removeGoogleCalendarEventId(); // In order to avoid potential disasters, we disable the possibility // for GCal to delete tasks // $task->delete(); return; } } // }}} $eventDescription = $event->title->text; $eventNote = $event->content->text; $eventDueDate = ''; $eventDueTime = ''; $eventRepetitionId = 0; $eventRepetitionParam = 0; $eventIsRecurrent = true; foreach ($event->when as $when) { $startTime = $when->startTime; $eventIsRecurrent = false; if (strlen($startTime) == 10) { $eventDueDate = $startTime; $eventDueTime = ''; } else { preg_match('!([^T]+)T([0-9]{2}):([0-9]{2}):.*!', $startTime, $matches); $eventDueDate = $matches[1]; $eventDueTime = $matches[2] . $matches[3]; $eventDueTime = (int) $eventDueTime; } break; // getting just the first due date } if ($eventIsRecurrent) { $recurrentData = $event->recurrence->text; // $recurrentData is something like: // DTSTART;VALUE=DATE:20110215 DTEND;VALUE=DATE:20110216 RRULE:FREQ=WEEKLY;INTERVAL=2;BYDAY=TU // DTSTART:20110228T110000Z DTEND:20110228T120000Z RRULE:FREQ=WEEKLY;BYDAY=MO list($eventDueDate, $eventDueTime, $eventRepetitionId, $eventRepetitionParam) = DateFormat::fromICalRecurrentStringToInternalParams($recurrentData); } foreach ($event->when as $when) { break; // we only consider the first 'when' parameter } list($eventLocalDueDate, $eventLocalDueTime) = DateFormat::fromGmtDateAndTime2LocalDateAndTime($eventDueDate, $eventDueTime); if (is_object($task)) { $task->edit($eventDescription, null, null, null, $eventNote, $eventLocalDueDate, $eventLocalDueTime, null, $eventRepetitionId, $eventRepetitionParam, 'gcal'); if (SfConfig::get('app_gcal_debug')) { error_log('Updated Plancake task ' . $task->getId() . ' ' . $task->getDescription()); } } else { $dbEntry = PcGoogleCalendarPeer::retrieveByUser($this->user); if (!is_object($dbEntry)) { throw new Exception("You need a session token in order to create a new task."); } // we have to create a new Plancake task $task = PcTaskPeer::createOrEdit($eventDescription, null, 0, '', false, $eventNote, $eventLocalDueDate, $eventLocalDueTime, 0, $eventRepetitionId, $eventRepetitionParam, 0, 'gcal', 'Y-m-d'); if (SfConfig::get('app_gcal_debug')) { error_log('Created Plancake task ' . $task->getId() . ' ' . $task->getDescription()); } } $task->setGoogleCalendarEventId($eventId); }