Ejemplo n.º 1
0
 public static function backup($user, $taskId = 0, $syncTaskId = 0, $ignoreUpdate = false)
 {
     // Stats
     $stats = array('calendars' => 0, 'events' => 0);
     // Calendars
     $calendars = \Rest::get(static::$endpoints['calendars'], array('maxResults' => static::$limit, 'showHidden' => 'true'), $user);
     if ($calendars && $calendars['items']) {
         foreach ($calendars['items'] as $calendar) {
             // Save Calendar
             $newCalendar = CalendarsModel::create();
             $newCalendar->user_id = $user['username']['id'];
             $newCalendar->task_id = $taskId;
             $newCalendar->sync_task_id = $syncTaskId;
             $newCalendar->kind = static::$kind['calendar'];
             $newCalendar->calendar_id = $calendar['id'];
             $newCalendar->name = $calendar['summary'];
             $newCalendar->timezone = $calendar['timeZone'];
             $newCalendar->color_id = $calendar['colorId'];
             $newCalendar->background = $calendar['backgroundColor'];
             $newCalendar->foreground = $calendar['foregroundColor'];
             $newCalendar->selected = isset($calendar['selected']) ? $calendar['selected'] : '';
             $newCalendar->status = CalendarsModel::STATUS_ACTIVE;
             $newCalendar->save();
             $stats['calendars']++;
             // Save events
             $newEvents = array();
             do {
                 $payload = array('maxResults' => static::$limit);
                 if (isset($events['nextPageToken'])) {
                     $payload['pageToken'] = $events['nextPageToken'];
                 }
                 $events = \Rest::get(sprintf(static::$endpoints['events'], $newCalendar->calendar_id), $payload, $user);
                 if (isset($events['result']['error'])) {
                     d($events);
                 }
                 if (isset($events['items'])) {
                     foreach ($events['items'] as $event) {
                         if ($event['status'] != 'cancelled') {
                             $formatedData = array('user_id' => $user['username']['id'], 'task_id' => $taskId, 'sync_task_id' => $syncTaskId, 'kind' => static::$kind['event'], 'calendar_id' => $newCalendar->id, 'google_calendar_id' => $newCalendar->calendar_id, 'event_id' => $event['id'], 'name' => isset($event['summary']) ? $event['summary'] : '', 'description' => isset($event['description']) ? $event['description'] : '', 'location' => isset($event['location']) ? $event['location'] : '', 'event_status' => $event['status'], 'url' => isset($event['htmlLink']) ? $event['htmlLink'] : '', 'color_id' => isset($event['colorId']) ? $event['colorId'] : '', 'created_at' => date(DATE_TIME, strtotime($event['created'])), 'creator' => json_encode($event['creator']), 'start' => json_encode($event['start']), 'end' => json_encode($event['end']), 'recurrence' => isset($event['recurrence']) ? json_encode($event['recurrence']) : '', 'status' => EventsModel::STATUS_ACTIVE);
                             $newEvents[] = $formatedData;
                             $stats['events']++;
                         }
                     }
                 }
             } while (isset($events['nextPageToken']) && $events['nextPageToken']);
             if ($newEvents) {
                 EventsModel::insertBatch($newEvents);
             }
         }
     }
     return $stats;
 }