Beispiel #1
0
 public static function _clean($destination, $source, $syncTaskId = 0, $whitelist = array())
 {
     $service = ServicesModel::first(array('library' => 'Youtube'))->toArray();
     $task = TasksServicesModel::first(array('task_id' => $syncTaskId, 'service_id' => $service['id']))->toArray();
     $task['stats'] = json_decode($task['stats'], true);
     // Playlists
     $playlists = PlaylistsModel::all(array('user_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId))->toArray();
     // Videos
     $videos = array();
     $migratedVideos = MigratedDataModel::all(array('source_id' => $source['username']['id'], 'sync_task_id' => $syncTaskId, 'kind' => static::$kind['video']));
     if ($migratedVideos) {
         $migratedVideos = $migratedVideos->toArray();
         if ($migratedVideos) {
             $destinationPlaylists = PlaylistsModel::all(array('user_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId));
             if ($destinationPlaylists) {
                 $destinationPlaylists = $destinationPlaylists->toArray();
                 if ($destinationPlaylists) {
                     foreach ($destinationPlaylists as $playlist) {
                         foreach ($migratedVideos as $video) {
                             $payload = array('part' => static::$part['playlistItems']['minimal'], 'playlistId' => $playlist['youtube_playlist_id'], 'videoId' => $video['identifier']);
                             $vids = array();
                             $vids = \Rest::get(static::$endpoints['playlistItems'], $payload, $destination);
                             if (isset($vids['result']['error'])) {
                                 d($vids);
                             }
                             if ($vids && $vids['items']) {
                                 foreach ($vids['items'] as $vid) {
                                     $videos[] = $vid['id'];
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // Subscriptions
     $newSubscriptions = array();
     $oldSubscriptions = MigratedDataModel::all(array('destination_id' => $destination['username']['id'], 'sync_task_id' => $syncTaskId))->column('identifier');
     do {
         $payload = array('part' => static::$part['subscriptions']['minimal'], 'mine' => 'true', 'maxResults' => static::$limit);
         if (isset($subscriptions['nextPageToken'])) {
             $payload['pageToken'] = $subscriptions['nextPageToken'];
         }
         $subscriptions = \Rest::get(static::$endpoints['subscriptions'], $payload, $destination);
         if (isset($subscriptions['result']['error'])) {
             d($subscriptions);
         }
         if ($subscriptions['items']) {
             foreach ($subscriptions['items'] as $sub) {
                 $newSubscriptions[] = $sub;
             }
         }
     } while (isset($subscriptions['nextPageToken']) && $subscriptions['nextPageToken']);
     switch (true) {
         # Share / Clean
         ############################################################################
         case !empty($whitelist):
             // Playlist
             if ($playlists) {
                 foreach ($playlists as $playlist) {
                     if (isset($whitelist['playlists']) && in_array($playlist['youtube_playlist_id'], $whitelist['playlists'])) {
                         \Rest::delete(static::$endpoints['playlists'], array('id' => $playlist['youtube_playlist_id']), $destination);
                     }
                 }
             }
             // Subscriptions
             if ($newSubscriptions) {
                 foreach ($newSubscriptions as $sub) {
                     if (isset($whitelist['subscriptions']) && in_array($sub['snippet']['resourceId']['channelId'], $whitelist['subscriptions'])) {
                         \Rest::delete(static::$endpoints['subscriptions'], array('id' => $sub['id']), $destination);
                     }
                 }
             }
             break;
             # Move
             ############################################################################
         # Move
         ############################################################################
         case $destination['username']['id'] == $source['username']['id']:
             // Playlist
             if ($playlists) {
                 foreach ($playlists as $playlist) {
                     \Rest::delete(static::$endpoints['playlists'], array('id' => $playlist['youtube_playlist_id']), $destination);
                 }
             }
             // Subscriptions
             if ($newSubscriptions) {
                 foreach ($newSubscriptions as $sub) {
                     \Rest::delete(static::$endpoints['subscriptions'], array('id' => $sub['id']), $destination);
                 }
             }
             break;
             # Migrate / Sync
             ############################################################################
         # Migrate / Sync
         ############################################################################
         default:
             // Playlist
             if ($playlists && $task['stats']['playlists']) {
                 foreach ($playlists as $playlist) {
                     if ($playlist['new_youtube_id']) {
                         \Rest::delete(static::$endpoints['playlists'], array('id' => $playlist['new_youtube_id']), $destination);
                     }
                 }
             }
             // Videos
             if ($videos && $task['stats']['videos']) {
                 foreach ($videos as $videoId) {
                     \Rest::delete(static::$endpoints['playlistItems'], array('id' => $videoId), $destination);
                 }
             }
             // Subscriptions
             if ($newSubscriptions) {
                 foreach ($newSubscriptions as $sub) {
                     if (in_array($sub['snippet']['resourceId']['channelId'], $oldSubscriptions) && $task['stats']['subscriptions']) {
                         \Rest::delete(static::$endpoints['subscriptions'], array('id' => $sub['id']), $destination);
                     }
                 }
             }
             break;
     }
 }
Beispiel #2
0
 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');
 }