/**
  * @Route("/update-playlist/{playlistId}", name="_formUpdatePlaylist")
  * @Template()
  */
 public function updateAction($playlistId)
 {
     $data['title'] = 'Playlist updated';
     $em = $this->getDoctrine()->getManager();
     $playlistQuery = new PlaylistQuery($em);
     $updated = false;
     $dataContent = [];
     if ($this->getUser()) {
         $dataContent['connected'] = true;
         $dataContent['user'] = $this->getUser();
         $playlist = $playlistQuery->setId($playlistId)->getSingle('playlist_' . $playlistId);
         $form = $this->createForm(new PlaylistType(), $playlist);
         if ($this->getRequest()->isXmlHttpRequest()) {
             $form->handleRequest($this->getRequest());
             if ($playlist->getUser()->getId() == $this->getUser()->getId()) {
                 if ($form->isValid()) {
                     $playlist = $form->getData();
                     $playlistQuery->persist($playlist);
                     $updated = true;
                 }
             } else {
                 $updated = false;
             }
         }
         $dataContent['form'] = $form->createView();
         $dataContent['updated'] = $updated;
         $dataContent['playlist'] = $playlist;
     } else {
         $dataContent['connected'] = false;
     }
     $data['content'] = $this->renderView('VidlisCoreBundle:PlaylistActions:update.html.twig', $dataContent);
     $response = new Response(json_encode($data));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }
 /**
  * @Route("/import-playlist", name="_importPlaylist")
  * @Template()
  */
 public function importAction()
 {
     $this->initialize();
     $request = $this->getRequest();
     $data = array();
     if ($request->getSession()->get('token')) {
         if ($request->isXmlHttpRequest()) {
             $playlistIds = $request->request->get('playlistIds');
             $em = $this->getDoctrine()->getManager();
             $playlistQuery = new PlaylistQuery($em);
             $playlistItemQuery = new PlaylistItemQuery($em);
             foreach ($playlistIds as $id) {
                 $playlist = new Playlist();
                 $playlist->setName($this->get('youtubePlaylist')->setId($id)->getSingleResult()->snippet->title)->setPrivate(false)->setUser($this->getUser())->setCreationDate(new \DateTime());
                 $playlistQuery->persist($playlist);
                 $results = $this->get('youtubePlaylistItems')->setIdPlaylist($id)->getResults();
                 foreach ($results->items as $item) {
                     $playlistItem = new Playlistitem();
                     $playlistItem->setPlaylist($playlist)->setIdVideo($item->snippet->resourceId->videoId)->getVideoInformation($this->get('youtubeVideo'));
                     $playlistItemQuery->persist($playlistItem);
                 }
             }
             $data['playlistImported'] = true;
         } else {
             $this->client->setAccessToken($request->getSession()->get('token'));
             $youtube = new apiYouTubeService($this->client);
             $playlists = $youtube->playlists->listPlaylists('snippet', array('mine' => 'true', 'maxResults' => 20));
             $data['playlists'] = $playlists;
         }
     } else {
         $this->initialize();
         $state = mt_rand();
         $this->client->setState($state);
         $request->getSession()->set('stateYoutube', $state);
         $authUrl = $this->client->createAuthUrl();
         $data['authUrl'] = $authUrl;
     }
     return $data;
 }