public static function _transfer($source, $destination, $syncTaskId = 0, $ignoreUpdate = false, $whitelist = array()) { // Stats $stats = array('contacts' => 0); // Get source data $contacts = BackupsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId, 'entity_type' => static::$kind['contact']))->toArray(); $destinationContacts = BackupsModel::all(array('user_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId, 'entity_type' => static::$kind['contact']))->column('entity_title'); $syncedContacts = MigratedDataModel::all(array('source_id' => $source['username']['id'], 'destination_id' => $destination['username']['id'], 'status' => MigratedDataModel::STATUS_ACTIVE, 'kind' => static::$kind['contact']))->column('identifier'); if ($contacts) { foreach ($contacts as $contact) { $contactData = json_decode($contact['entity'], true); // Whitelisting used for share feature $whiteListed = true; if ($whitelist) { if (isset($whitelist['contacts']) && $whitelist['contacts']) { if (!in_array($contactData['id'], $whitelist['contacts'])) { $whiteListed = false; } } } if (!in_array(md5($contactData['name']), $syncedContacts) && $whiteListed) { $body = <<<EOD <?xml version='1.0' encoding='UTF-8'?> <entry \txmlns='http://www.w3.org/2005/Atom' \txmlns:gd='http://schemas.google.com/g/2005'> EOD; if (in_array($contactData['name'], $destinationContacts) && !$ignoreUpdate) { $contactData['name'] = $contactData['name'] . ' (2)'; } else { $contactData['name'] = $contactData['name']; } $body .= '<title type="text">' . $contactData['name'] . '</title>'; // Emails if (isset($contactData['emails']) && $contactData['emails']) { foreach ($contactData['emails'] as $email) { $body .= '<gd:email rel="' . (isset($email['rel']) ? $email['rel'] : 'http://schemas.google.com/g/2005#other') . '"'; if (isset($email['primary'])) { $body .= ' primary="true" '; } $body .= ' address="' . $email['address'] . '" />'; } } // IM if (isset($contactData['im']) && $contactData['im']) { foreach ($contactData['im'] as $im) { $body .= '<gd:im address="' . $im['address'] . '" protocol="' . $im['protocol'] . '" rel="' . $im['rel'] . '"/>'; } } // Numbers if (isset($contactData['phoneNumbers']) && $contactData['phoneNumbers']) { foreach ($contactData['phoneNumbers'] as $number) { $body .= '<gd:phoneNumber rel="' . $number['rel'] . '">' . $number['number'] . '</gd:phoneNumber>'; } } // Postal if (isset($contactData['postalAddress']) && $contactData['postalAddress']) { foreach ($contactData['postalAddress'] as $address) { $body .= '<gd:postalAddress rel="' . $address['rel'] . '">' . $address['address'] . '</gd:postalAddress>'; } } $body .= '</entry>'; $newContact = \Rest::postXML(static::$endpoints['contacts'], $body, $destination); // Update playlist with new id if (!$ignoreUpdate && $newContact) { $newContactId = @basename($newContact->id); if ($newContactId) { $oldPlaylist = BackupsModel::first($contact['id']); $oldPlaylist->entity_new_id = $newContactId; $oldPlaylist->save(); } } $stats['contacts']++; $syncedContact = MigratedDataModel::create(); $syncedContact->source_id = $source['username']['id']; $syncedContact->destination_id = $destination['username']['id']; $syncedContact->task_id = 0; $syncedContact->sync_task_id = $syncTaskId; $syncedContact->table = BackupsModel::$schema['table']; $syncedContact->table_id = $contact['id']; $syncedContact->kind = static::$kind['contact']; $syncedContact->identifier = md5($contactData['name']); $syncedContact->status = MigratedDataModel::STATUS_ACTIVE; $syncedContact->created = date(DATE_TIME); $syncedContact->save(); } } } return $stats; }
public static function _transfer($source, $destination, $syncTaskId = 0, $ignoreUpdate = false, $whitelist = array()) { // Stats $stats = array('lists' => 0, 'tasks' => 0); // Get source data $tasks = array(); $lists = BackupsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId, 'entity_type' => static::$kind['list']))->toArray(); $tasksDB = BackupsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId, 'entity_type' => static::$kind['task']))->toArray(); if ($tasksDB) { foreach ($tasksDB as $task) { $tasks[$task['parent_id']][] = $task; } } $syncedLists = MigratedDataModel::all(array('source_id' => $source['username']['id'], 'destination_id' => $destination['username']['id'], 'kind' => static::$kind['list']))->column('identifier'); $syncedTasks = MigratedDataModel::all(array('source_id' => $source['username']['id'], 'destination_id' => $destination['username']['id'], 'kind' => static::$kind['task']))->column('identifier'); $destinationLists = BackupsModel::all(array('user_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId, 'entity_type' => static::$kind['list']))->column('entity_title'); if ($lists) { foreach ($lists as $list) { // Whitelisting used for share feature $whiteListed = true; if ($whitelist) { if (isset($whitelist['lists']) && $whitelist['lists']) { if (!in_array($list['entity_id'], $whitelist['lists'])) { $whiteListed = false; } } } if (!in_array($list['entity_id'], $syncedLists) && $whiteListed) { $list['entity'] = json_decode($list['entity'], true); $newList = array_diff_key($list['entity'], array_flip(static::$skip['list'])); if (in_array($newList['title'], $destinationLists) && !$ignoreUpdate) { $newList['title'] = $newList['title'] . ' (2)'; } else { $newList['title'] = $newList['title']; } $newList = \Rest::postJSON(static::$endpoints['lists'], $newList, $destination); if (isset($newList['result']['error'])) { d($newList); } $stats['lists']++; // Update playlist with new id if (!$ignoreUpdate && $newList) { $oldPlaylist = BackupsModel::first($list['id']); $oldPlaylist->entity_new_id = $newList['id']; $oldPlaylist->save(); } $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 = BackupsModel::$schema['table']; $syncedCalendar->table_id = $list['id']; $syncedCalendar->kind = static::$kind['list']; $syncedCalendar->identifier = $list['entity_id']; $syncedCalendar->status = MigratedDataModel::STATUS_ACTIVE; $syncedCalendar->created = date(DATE_TIME); $syncedCalendar->save(); // Add tasks if (isset($tasks[$list['id']])) { foreach ($tasks[$list['id']] as $task) { if (!in_array($task['entity_id'], $syncedTasks)) { $task['entity'] = json_decode($task['entity'], true); $newTask = array_diff_key($task['entity'], array_flip(static::$skip['task'])); $newTask = \Rest::postJSON(sprintf(static::$endpoints['tasks'], $newList['id']), $newTask, $destination); if (isset($newTask['result']['error'])) { d($newTask); } $stats['tasks']++; $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 = BackupsModel::$schema['table']; $syncedEvent->table_id = $task['id']; $syncedEvent->kind = static::$kind['task']; $syncedEvent->identifier = $task['entity_id']; $syncedEvent->status = MigratedDataModel::STATUS_ACTIVE; $syncedEvent->created = date(DATE_TIME); $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'); }