コード例 #1
0
 public static function withEvents($userId, $taskId)
 {
     $calendars = static::all(array('user_id' => $userId, 'sync_task_id' => $taskId))->toArray();
     if ($calendars) {
         foreach ($calendars as $key => $calendar) {
             $calendars[$key]['events'] = EventsModel::all(array('calendar_id' => $calendar['id']))->toArray();
         }
     }
     return $calendars;
 }
コード例 #2
0
ファイル: Calendar.php プロジェクト: unDemian/gcdc-migrate
 public static function _transfer($source, $destination, $syncTaskId = 0, $ignoreUpdate = false, $whitelist = array())
 {
     // Stats
     $stats = array('calendars' => 0, 'events' => 0);
     // Get source data
     $calendars = CalendarsModel::withEvents($source['username']['id'], $syncTaskId);
     $destinationCalendars = CalendarsModel::all(array('user_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId))->toArray();
     $destinationNames = static::_getColumn($destinationCalendars, 'name');
     $syncedEvents = EventsModel::identifiers(array('user_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId));
     if ($calendars) {
         foreach ($calendars as $calendar) {
             // Create calendar
             $payload = array();
             // Whitelisting used for share feature
             $whiteListed = true;
             if ($whitelist) {
                 if (isset($whitelist['calendars']) && $whitelist['calendars']) {
                     if (!in_array($calendar['calendar_id'], $whitelist['calendars'])) {
                         $whiteListed = false;
                     }
                 }
             }
             // New
             if (!in_array($calendar['name'], array_keys($destinationNames)) && $whiteListed || $whiteListed && $ignoreUpdate) {
                 // Create Calendar
                 $payload['summary'] = $calendar['name'];
                 $newCalendar = \Rest::postJSON(static::$endpoints['calendar'], $payload, $destination);
                 $stats['calendars']++;
                 if (isset($newCalendar['result']['error'])) {
                     d($newCalendar);
                 }
                 // Create Calendar List
                 $payloadList = array();
                 $payloadList['id'] = $newCalendar['id'];
                 $payloadList['timezone'] = $calendar['timezone'];
                 $payloadList['colorId'] = $calendar['color_id'];
                 $payloadList['backgroundColor'] = $calendar['background'];
                 $payloadList['foregroundColor'] = $calendar['foreground'];
                 $payloadList['selected'] = true;
                 \Rest::postJSON(static::$endpoints['calendars'], $payloadList, $destination);
                 $syncedCalendar = MigratedDataModel::create();
                 $syncedCalendar->source_id = $source['username']['id'];
                 $syncedCalendar->destination_id = $destination['username']['id'];
                 $syncedCalendar->task_id = 0;
                 $syncedCalendar->sync_task_id = $syncTaskId;
                 $syncedCalendar->table = CalendarsModel::$schema['table'];
                 $syncedCalendar->table_id = $calendar['id'];
                 $syncedCalendar->kind = static::$kind['calendar'];
                 $syncedCalendar->identifier = $calendar['calendar_id'];
                 $syncedCalendar->name = $calendar['name'];
                 $syncedCalendar->status = MigratedDataModel::STATUS_ACTIVE;
                 $syncedCalendar->created = date(DATE_TIME);
                 $syncedCalendar->save();
                 // Update calendar with new id
                 if (!$ignoreUpdate) {
                     $oldCalendar = CalendarsModel::first($calendar['id']);
                     $oldCalendar->calendar_new_id = $newCalendar['id'];
                     $oldCalendar->save();
                 }
                 // Existing
             } else {
                 $cal = CalendarsModel::first(array('user_id' => $destination['username']['id'], 'name' => $calendar['name']))->toArray();
                 $newCalendar['id'] = $cal['calendar_id'];
             }
             // Add events
             if ($calendar['events']) {
                 foreach ($calendar['events'] as $event) {
                     $combination = static::identifier($event, $calendar);
                     // Whitelisting used for share feature
                     $whiteListed = true;
                     if ($whitelist) {
                         if (isset($whitelist['calendars']) && $whitelist['calendars']) {
                             if (!in_array($event['google_calendar_id'], $whitelist['calendars'])) {
                                 $whiteListed = false;
                             }
                         }
                     }
                     if (!in_array($combination, $syncedEvents) && $whiteListed || $whiteListed && $ignoreUpdate) {
                         // Create Event
                         $payload = array();
                         $payload['summary'] = $event['name'];
                         $payload['description'] = $event['description'];
                         $payload['location'] = $event['location'];
                         $payload['status'] = $event['event_status'];
                         if ($event['color_id']) {
                             $payload['colorId'] = $event['color_id'];
                         }
                         $payload['creator'] = json_decode($event['creator'], true);
                         $payload['start'] = json_decode($event['start'], true);
                         $payload['end'] = json_decode($event['end'], true);
                         if (json_decode($event['recurrence'], true)) {
                             $payload['recurrence'] = json_decode($event['recurrence'], true);
                         }
                         $newEvent = \Rest::postJSON(sprintf(static::$endpoints['events'], $newCalendar['id']), $payload, $destination);
                         if (isset($newEvent['result']['error'])) {
                             d($newEvent);
                         }
                         $stats['events']++;
                         $syncedEvent = MigratedDataModel::create();
                         $syncedEvent->source_id = $source['username']['id'];
                         $syncedEvent->destination_id = $destination['username']['id'];
                         $syncedEvent->task_id = 0;
                         $syncedEvent->sync_task_id = $syncTaskId;
                         $syncedEvent->table = EventsModel::$schema['table'];
                         $syncedEvent->table_id = $event['id'];
                         $syncedEvent->kind = static::$kind['event'];
                         $syncedEvent->identifier = $event['event_id'];
                         $syncedEvent->unique = $combination;
                         $syncedEvent->name = $event['name'];
                         $syncedEvent->created = date(DATE_TIME);
                         $syncedEvent->status = MigratedDataModel::STATUS_ACTIVE;
                         $syncedEvent->save();
                     }
                 }
             }
         }
     }
     return $stats;
 }