EditController.php 12KB

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