EditController.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace Muzich\PlaylistBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Muzich\CoreBundle\Security\Context as SecurityContext;
  6. use Muzich\CoreBundle\Propagator\EventElement;
  7. use Muzich\CoreBundle\Form\Playlist\PrivateLinksForm;
  8. use Muzich\CoreBundle\Entity\Playlist;
  9. use Muzich\CoreBundle\Entity\User;
  10. class EditController extends Controller
  11. {
  12. public function updateOrderAction(Request $request, $playlist_id)
  13. {
  14. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_UPDATE_ORDER)) !== false)
  15. return $this->jsonResponseError($uncondition);
  16. $playlist_manager = $this->getPlaylistManager();
  17. if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser())) || !$request->get('elements'))
  18. return $this->jsonNotFoundResponse();
  19. $playlist_manager->updatePlaylistElementsOrder($playlist, $request->get('elements'));
  20. $this->flush();
  21. return $this->jsonSuccessResponse();
  22. }
  23. public function removeElementAction($playlist_id, $index)
  24. {
  25. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_REMOVE_ELEMENT)) !== false)
  26. return $this->jsonResponseError($uncondition);
  27. $playlist_manager = $this->getPlaylistManager();
  28. if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
  29. return $this->jsonNotFoundResponse();
  30. $element = $playlist_manager->getElementWithIndex($playlist, $index);
  31. if (!$element)
  32. return $this->jsonNotFoundResponse();
  33. $playlist_manager->removePlaylistElementWithIndex($playlist, $index);
  34. $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
  35. $event = new EventElement($this->container);
  36. $event->removedFromPlaylist($element, $this->getUser(), $playlist);
  37. $this->persist($element);
  38. $this->flush();
  39. return $this->jsonSuccessResponse(array(
  40. 'element_remove_links' => $this->getRemoveLinksForPlaylist($playlist)
  41. ));
  42. }
  43. protected function getRemoveLinksForPlaylist(Playlist $playlist)
  44. {
  45. $element_remove_urls = array();
  46. foreach ($playlist->getElements() as $index => $element_data)
  47. {
  48. $element_remove_urls[] = $this->generateUrl('playlist_remove_element', array(
  49. 'playlist_id' => $playlist->getId(),
  50. 'index' => $index,
  51. 'token' => $this->getToken()
  52. ));
  53. }
  54. return $element_remove_urls;
  55. }
  56. public function addElementAction($playlist_id, $element_id)
  57. {
  58. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_ADD_ELEMENT)) !== false)
  59. return $this->jsonResponseError($uncondition);
  60. $playlist_manager = $this->getPlaylistManager();
  61. if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser()))
  62. || !($element = $this->getElementWithId($element_id)))
  63. return $this->jsonNotFoundResponse();
  64. $playlist_manager->addElementToPlaylist($element, $playlist);
  65. $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
  66. $event = new EventElement($this->container);
  67. $event->addedToPlaylist($element, $this->getUser(), $playlist);
  68. $this->persist($element);
  69. $this->flush();
  70. return $this->jsonSuccessResponse();
  71. }
  72. public function addElementAndCreateAction(Request $request, $element_id)
  73. {
  74. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_CREATE)) !== false)
  75. return $this->jsonResponseError($uncondition);
  76. if (!($element = $this->getElementWithId($element_id)))
  77. return $this->jsonNotFoundResponse();
  78. $form = $this->getPlaylistForm();
  79. $form->bind($request);
  80. if ($form->isValid())
  81. {
  82. $this->getPlaylistManager()->addElementToPlaylist($element, $form->getData());
  83. $event = new EventElement($this->container);
  84. $event->addedToPlaylist($element, $this->getUser(), $form->getData());
  85. $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
  86. $this->persist($element);
  87. $this->flush();
  88. return $this->jsonSuccessResponse();
  89. }
  90. return $this->jsonResponseError('form_error',
  91. $this->render('MuzichPlaylistBundle:Show:form.html.twig', array(
  92. 'form' => $form->createView(),
  93. 'element_id' => $element_id
  94. ))->getContent()
  95. );
  96. }
  97. public function addElementAndCopyAction($playlist_id, $element_id)
  98. {
  99. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_COPY)) !== false)
  100. return $this->jsonResponseError($uncondition);
  101. if (!$this->tokenIsCorrect() || !($element = $this->getElementWithId($element_id)))
  102. return $this->jsonNotFoundResponse();
  103. if (!($playlist = $this->getPlaylistManager()->findOneAccessiblePlaylistWithId($playlist_id, $this->getUser())))
  104. return $this->jsonNotFoundResponse();
  105. $new_playlist = $this->getPlaylistManager()->copyPlaylist($this->getUser(), $playlist);
  106. $this->getPlaylistManager()->addElementToPlaylist($element, $new_playlist);
  107. $this->getPlaylistManager()->removePickedPlaylistToUser($this->getUser(), $playlist);
  108. $event = new EventElement($this->container);
  109. $event->addedToPlaylist($element, $this->getUser(), $new_playlist);
  110. $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
  111. $this->persist($element);
  112. $this->flush();
  113. return $this->jsonSuccessResponse();
  114. }
  115. public function deleteAction($playlist_id)
  116. {
  117. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_DELETE)) !== false)
  118. throw $this->createNotFoundException();
  119. if (!$this->tokenIsCorrect() || !($playlist = $this->getPlaylistManager()->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
  120. throw $this->createNotFoundException();
  121. $this->getPlaylistManager()->deletePlaylist($playlist);
  122. $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
  123. $this->flush();
  124. $this->setFlash('success', 'playlist.delete.success');
  125. return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $this->getUser()->getSlug())));
  126. }
  127. public function unpickAction($playlist_id, $redirect_owner = false)
  128. {
  129. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_UNPICK)) !== false)
  130. throw $this->createNotFoundException();
  131. $playlist_manager = $this->getPlaylistManager();
  132. if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findPlaylistWithId($playlist_id, $this->getUser())))
  133. throw $this->createNotFoundException();
  134. $playlist_manager->removePickedPlaylistToUser($this->getUser(), $playlist);
  135. $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
  136. $this->flush();
  137. $this->setFlash('success', 'playlist.delete.success');
  138. if ($redirect_owner)
  139. return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $playlist->getOwner()->getSlug())));
  140. return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $this->getUser()->getSlug())));
  141. }
  142. public function pickAction($playlist_id, $redirect_owner = false)
  143. {
  144. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_PICK)) !== false)
  145. {
  146. if ($this->getRequest()->isXmlHttpRequest())
  147. return $this->jsonResponseError($uncondition);
  148. throw $this->createNotFoundException();
  149. }
  150. if (!$this->tokenIsCorrect() || !($playlist = $this->getPlaylistManager()->findOneAccessiblePlaylistWithId($playlist_id)))
  151. {
  152. if ($this->getRequest()->isXmlHttpRequest())
  153. return $this->jsonNotFoundResponse();
  154. throw $this->createNotFoundException();
  155. }
  156. $this->getPlaylistManager()->addPickedPlaylistToUser($this->getUser(), $playlist);
  157. $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
  158. $this->flush();
  159. if ($this->getRequest()->isXmlHttpRequest())
  160. return $this->jsonSuccessResponse();
  161. if ($redirect_owner)
  162. return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $playlist->getOwner()->getSlug())));
  163. return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $this->getUser()->getSlug())));
  164. }
  165. public function createAction(Request $request)
  166. {
  167. $playlist_form = $this->getPlaylistForm($this->getPlaylistManager()
  168. ->getNewPlaylist($this->getUser()));
  169. if ($request->getMethod() == 'POST')
  170. {
  171. $playlist_form->bind($request);
  172. if ($playlist_form->isValid())
  173. {
  174. $this->persist($playlist_form->getData());
  175. $this->getUser()->setData(User::DATA_PLAY_UPDATED, true);
  176. $this->flush();
  177. $this->setFlash('success', $this->trans('playlist.create.success', array(), 'flash'));
  178. return $this->redirect($this->generateUrl('playlist', array(
  179. 'user_slug' => $this->getUser()->getSlug(),
  180. 'playlist_id' => $playlist_form->getData()->getId()
  181. )));
  182. }
  183. }
  184. return $this->render('MuzichPlaylistBundle:Edit:create.html.twig', array(
  185. 'form' => $playlist_form->createView()
  186. ));
  187. }
  188. public function editAction($playlist_id)
  189. {
  190. if (!($playlist = $this->getPlaylistManager()->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
  191. throw $this->createNotFoundException();
  192. return $this->render('MuzichPlaylistBundle:Edit:edit.html.twig', array(
  193. 'form' => $this->getPlaylistForm($playlist)->createView(),
  194. 'playlist' => $playlist,
  195. 'playlist_name' => $playlist->getName()
  196. ));
  197. }
  198. public function updateAction(Request $request, $playlist_id)
  199. {
  200. if (!($playlist = $this->getPlaylistManager()->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
  201. throw $this->createNotFoundException();
  202. $playlist_name = $playlist->getName();
  203. $playlist_public = $playlist->isPublic();
  204. $form = $this->getPlaylistForm($playlist);
  205. $form->bind($request);
  206. if ($form->isValid())
  207. {
  208. if ($playlist_public && !$playlist->isPublic())
  209. {
  210. $this->getPlaylistManager()->privatizePlaylist($playlist);
  211. }
  212. $this->persist($form->getData());
  213. $this->flush();
  214. $this->setFlash('success', 'playlist.update.success');
  215. return $this->redirect($this->generateUrl('playlist', array(
  216. 'user_slug' => $playlist->getOwner()->getSlug(),
  217. 'playlist_id' => $playlist->getId()
  218. )));
  219. }
  220. return $this->render('MuzichPlaylistBundle:Edit:edit.html.twig', array(
  221. 'form' => $form->createView(),
  222. 'playlist' => $playlist,
  223. 'playlist_name' => $playlist_name
  224. ));
  225. }
  226. public function addPrivateLinksAction(Request $request, $playlist_id)
  227. {
  228. if (!($playlist = $this->getPlaylistManager()->findOneAccessiblePlaylistWithId($playlist_id, $this->getUser())))
  229. throw $this->createNotFoundException();
  230. $form = $this->createForm(new PrivateLinksForm());
  231. $form->bind($request);
  232. $data = $form->getData();
  233. if (!$data['links'])
  234. {
  235. $this->setFlash('warning', $this->trans('playlist.no_links_added', array(), 'elements'));
  236. return $this->redirect($this->generateUrl('playlist', array(
  237. 'user_slug' => $this->getUser()->getSlug(),
  238. 'playlist_id' => $playlist_id
  239. )));
  240. }
  241. $count_added = $this->getPlaylistManager()->addPrivateLinks($playlist, $this->getUser(), explode("\n", $data['links']), $this->container);
  242. if ($count_added == count(explode("\n", $data['links'])))
  243. {
  244. $this->setFlash('success', $this->trans('playlist.links_added', array(), 'elements'));
  245. }
  246. else
  247. {
  248. $this->setFlash('warning', $this->trans('playlist.links_added_witherr', array(), 'elements'));
  249. }
  250. return $this->redirect($this->generateUrl('playlist', array(
  251. 'user_slug' => $this->getUser()->getSlug(),
  252. 'playlist_id' => $playlist_id
  253. )));
  254. }
  255. }