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; }
public static function detail() { $taskId = (int) $_POST['id']; $task = TasksModel::details($taskId); if ($task['services']) { foreach ($task['services'] as $service) { if ($service['id'] == $_POST['type']) { $_service = $service; } } } $kinds = array(); $graphData = array(); switch ($_service['name']) { case 'Youtube': $kinds = array(Youtube::$kind['playlist'], Youtube::$kind['subscription']); // Graph data $graphData['source']['playlists'] = PlaylistsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_id']))->toArray(); $graphData['source']['subscriptions'] = SubscriptionsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_id']))->toArray(); $graphData['destination']['playlists'] = PlaylistsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_affected_id']))->toArray(); $graphData['destination']['subscriptions'] = SubscriptionsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_affected_id']))->toArray(); break; case 'Contacts': $kinds = array(Contacts::$kind['contact']); // Graph data $graphData['source']['contacts'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_id'], 'entity_type' => Contacts::$kind['contact']))->toArray(); $graphData['destination']['contacts'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_affected_id'], 'entity_type' => Contacts::$kind['contact']))->toArray(); break; case 'Tasks': $kinds = array(Tasks::$kind['list']); // Graph data $graphData['source']['lists'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_id'], 'entity_type' => Tasks::$kind['list']))->toArray(); $graphData['destination']['lists'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_affected_id'], 'entity_type' => Tasks::$kind['list']))->toArray(); break; case 'Calendar': $kinds = array(Calendar::$kind['calendar']); // Graph data $graphData['source']['calendars'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_id'], 'entity_type' => Calendar::$kind['calendar']))->toArray(); $graphData['destination']['calendars'] = BackupsModel::all(array('sync_task_id' => $taskId, 'user_id' => $task['user_affected_id'], 'entity_type' => Calendar::$kind['calendar']))->toArray(); break; } // Migrated Data $migratedData = array(); $migrated = MigratedDataModel::all(array('sync_task_id' => $taskId, 'kind' => $kinds))->toArray(); if ($migrated) { foreach ($migrated as $migrate) { switch ($migrate['kind']) { case Youtube::$kind['playlist']: $play = PlaylistsModel::first($migrate['table_id'])->toArray(); $migratedData['playlists'][] = $play; $migratedData['playlistsGraph'][$migrate['source_id']][] = $play; $migratedData['playlistsIds'][] = $play['id']; break; case Youtube::$kind['subscription']: $subs = SubscriptionsModel::first($migrate['table_id'])->toArray(); $migratedData['subscriptions'][] = $subs; $migratedData['subscriptionsGraph'][$migrate['source_id']][] = $subs; $migratedData['subscriptionsIds'][] = $subs['id']; break; case Contacts::$kind['contact']: $contact = BackupsModel::first($migrate['table_id'])->toArray(); $migratedData['contacts'][] = $contact; $migratedData['contactsGraph'][$migrate['source_id']][] = $contact; $migratedData['contactsIds'][] = $contact['id']; break; case Tasks::$kind['list']: $contact = BackupsModel::first($migrate['table_id'])->toArray(); $migratedData['lists'][] = $contact; $migratedData['listsGraph'][$migrate['source_id']][] = $contact; $migratedData['listsIds'][] = $contact['id']; break; case Calendar::$kind['calendar']: $calendar = CalendarsModel::first($migrate['table_id'])->toArray(); $migratedData['calendars'][] = $calendar; $migratedData['calendarsGraph'][$migrate['source_id']][] = $calendar; $migratedData['calendarsIds'][] = $calendar['id']; break; } } } echo \Render::view('migrate/details/' . strtolower($_service['name']), array('task' => $task, 'service' => $_service, 'migratedData' => $migratedData, 'graphData' => $graphData), 'return'); }