public static function sync($source, $destination, $syncTaskId = 0) { // Stats $stats = array('links' => 0); // Get source data $links = BackupsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId, 'entity_type' => static::$kind['link']))->toArray(); $syncedLinks = MigratedDataModel::all(array('source_id' => $source['username']['id'], 'destination_id' => $destination['username']['id'], 'kind' => static::$kind['link']))->column('identifier'); if ($links) { foreach ($links as $link) { if (!in_array($link['entity_id'], $syncedLinks)) { $link['entity'] = json_decode($link['entity'], true); $newLink = array_diff_key($link['entity'], array_flip(static::$skip['link'])); $newLink = \Rest::postJSON(static::$endpoints['link'], $newLink, $destination); if (isset($newLink['result']['error'])) { d($newLink); } $stats['links']++; $syncedCalendar = MigratedDataModel::create(); $syncedCalendar->source_id = $source['username']['id']; $syncedCalendar->destination_id = $destination['username']['id']; $syncedCalendar->kind = static::$kind['link']; $syncedCalendar->identifier = $link['entity_id']; $syncedCalendar->created = date(DATE_TIME); $syncedCalendar->save(); } } } return $stats; }
public static function _clean($destination, $source, $syncTaskId = 0, $whitelist = array()) { $service = ServicesModel::first(array('library' => 'Tasks'))->toArray(); $task = TasksServicesModel::first(array('task_id' => $syncTaskId, 'service_id' => $service['id']))->toArray(); $task['stats'] = json_decode($task['stats'], true); // lists $lists = BackupsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId, 'entity_type' => static::$kind['list']))->toArray(); if ($lists && ($task['stats']['lists'] || $destination['username']['id'] == $source['username']['id'])) { foreach ($lists as $list) { // $whitelist - shared or clean data if ($whitelist) { if (isset($whitelist['lists']) && in_array($list['entity_id'], $whitelist['lists'])) { \Rest::delete('https://www.googleapis.com/tasks/v1/users/@me/lists/' . $list['entity_id'], array(), $destination); } } else { if ($destination['username']['id'] == $source['username']['id']) { \Rest::delete('https://www.googleapis.com/tasks/v1/users/@me/lists/' . $list['entity_id'], array(), $destination); } else { if ($list['entity_new_id']) { \Rest::delete('https://www.googleapis.com/tasks/v1/users/@me/lists/' . $list['entity_new_id'], array(), $destination); } } } } } }
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'); }