EditController.php 5.7KB

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