/**
  * @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;
 }
 /**
  * @Route("/add-to-playlist/{idPlaylist}/{vidId}", requirements={"idPlaylist" = "\d+"}, name="_formAddToPlaylist")
  * @Template()
  */
 public function addtoAction($idPlaylist, $vidId = null)
 {
     $data['title'] = 'Video added';
     $em = $this->getDoctrine()->getManager();
     $playlistQuery = new PlaylistQuery($em);
     $playlistItemQuery = new PlaylistItemQuery($em);
     $playlist = $playlistQuery->setId($idPlaylist)->getSingle('playlist_' . $idPlaylist);
     if ($playlist->getUser()->getId() == $this->getUser()->getId()) {
         $playlistItem = new Playlistitem();
         $playlistItem->setPlaylist($playlist)->setIdVideo($vidId)->getVideoInformation($this->get('youtubeVideo'));
         $playlistItemQuery->persist($playlistItem);
         $result = true;
     } else {
         $result = false;
     }
     $data['content'] = $this->renderView('VidlisCoreBundle:PlaylistActions:addto.html.twig', array('playlistItem' => $playlistItem, 'result' => $result));
     $response = new Response(json_encode($data));
     $response->headers->set('Content-Type', 'application/json');
     return $response;
 }