EditController.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. class EditController extends Controller
  7. {
  8. public function updateOrderAction(Request $request, $playlist_id)
  9. {
  10. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_UPDATE_ORDER)) !== false)
  11. return $this->jsonResponseError($uncondition);
  12. $playlist_manager = $this->getPlaylistManager();
  13. if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser())) || !$request->get('elements'))
  14. return $this->jsonNotFoundResponse();
  15. $playlist_manager->updatePlaylistElementsOrder($playlist, $request->get('elements'));
  16. $this->flush();
  17. return $this->jsonSuccessResponse();
  18. }
  19. public function removeElementAction($playlist_id, $index)
  20. {
  21. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_REMOVE_ELEMENT)) !== false)
  22. return $this->jsonResponseError($uncondition);
  23. $playlist_manager = $this->getPlaylistManager();
  24. if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
  25. return $this->jsonNotFoundResponse();
  26. $playlist_manager->removePlaylistElementWithIndex($playlist, $index);
  27. $this->flush();
  28. return $this->jsonSuccessResponse();
  29. }
  30. public function addElementAction($playlist_id, $element_id)
  31. {
  32. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_ADD_ELEMENT)) !== false)
  33. return $this->jsonResponseError($uncondition);
  34. $playlist_manager = $this->getPlaylistManager();
  35. if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser()))
  36. || !($element = $this->getElementWithId($element_id)))
  37. return $this->jsonNotFoundResponse();
  38. $playlist_manager->addElementToPlaylist($element, $playlist);
  39. $this->flush();
  40. return $this->jsonSuccessResponse();
  41. }
  42. public function addElementAndCreateAction(Request $request, $element_id)
  43. {
  44. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_CREATE)) !== false)
  45. return $this->jsonResponseError($uncondition);
  46. if (!($element = $this->getElementWithId($element_id)))
  47. return $this->jsonNotFoundResponse();
  48. $form = $this->getPlaylistForm();
  49. $form->bind($request);
  50. if ($form->isValid())
  51. {
  52. $this->getPlaylistManager()->addElementToPlaylist($element, $form->getData());
  53. $this->flush();
  54. return $this->jsonSuccessResponse();
  55. }
  56. return $this->jsonResponseError('form_error',
  57. $this->render('MuzichPlaylistBundle:Show:form.html.twig', array(
  58. 'form' => $form->createView(),
  59. 'element_id' => $element_id
  60. ))->getContent()
  61. );
  62. }
  63. public function addElementAndCopyAction($playlist_id, $element_id)
  64. {
  65. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_COPY)) !== false)
  66. return $this->jsonResponseError($uncondition);
  67. if (!$this->tokenIsCorrect() || !($element = $this->getElementWithId($element_id)))
  68. return $this->jsonNotFoundResponse();
  69. if (!($playlist = $this->getPlaylistManager()->findOneAccessiblePlaylistWithId($playlist_id, $this->getUser())))
  70. return $this->jsonNotFoundResponse();
  71. $new_playlist = $this->getPlaylistManager()->copyPlaylist($this->getUser(), $playlist);
  72. $this->getPlaylistManager()->addElementToPlaylist($element, $new_playlist);
  73. $this->getPlaylistManager()->removePickedPlaylistToUser($this->getUser(), $playlist);
  74. $this->flush();
  75. return $this->jsonSuccessResponse();
  76. }
  77. public function deleteAction($playlist_id)
  78. {
  79. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_DELETE)) !== false)
  80. throw $this->createNotFoundException();
  81. if (!$this->tokenIsCorrect() || !($playlist = $this->getPlaylistManager()->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
  82. throw $this->createNotFoundException();
  83. $this->getPlaylistManager()->deletePlaylist($playlist);
  84. $this->flush();
  85. $this->setFlash('success', 'playlist.delete.success');
  86. return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $this->getUser()->getSlug())));
  87. }
  88. public function unpickAction($playlist_id, $redirect_owner = false)
  89. {
  90. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_UNPICK)) !== false)
  91. throw $this->createNotFoundException();
  92. $playlist_manager = $this->getPlaylistManager();
  93. if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findPlaylistWithId($playlist_id, $this->getUser())))
  94. throw $this->createNotFoundException();
  95. $playlist_manager->removePickedPlaylistToUser($this->getUser(), $playlist);
  96. $this->flush();
  97. $this->setFlash('success', 'playlist.delete.success');
  98. if ($redirect_owner)
  99. return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $playlist->getOwner()->getSlug())));
  100. return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $this->getUser()->getSlug())));
  101. }
  102. public function pickAction($playlist_id, $redirect_owner = false)
  103. {
  104. if (($uncondition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_PLAYLIST_PICK)) !== false)
  105. {
  106. if ($this->getRequest()->isXmlHttpRequest())
  107. return $this->jsonResponseError($uncondition);
  108. throw $this->createNotFoundException();
  109. }
  110. if (!$this->tokenIsCorrect() || !($playlist = $this->getPlaylistManager()->findOneAccessiblePlaylistWithId($playlist_id)))
  111. {
  112. if ($this->getRequest()->isXmlHttpRequest())
  113. return $this->jsonNotFoundResponse();
  114. throw $this->createNotFoundException();
  115. }
  116. $this->getPlaylistManager()->addPickedPlaylistToUser($this->getUser(), $playlist);
  117. $this->flush();
  118. if ($this->getRequest()->isXmlHttpRequest())
  119. return $this->jsonSuccessResponse();
  120. if ($redirect_owner)
  121. return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $playlist->getOwner()->getSlug())));
  122. return $this->redirect($this->generateUrl('playlists_user', array('user_slug' => $this->getUser()->getSlug())));
  123. }
  124. }