UserController.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Muzich\UserBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  7. use FOS\UserBundle\Model\UserInterface;
  8. use Muzich\CoreBundle\Form\Tag\TagFavoritesForm;
  9. class UserController extends Controller
  10. {
  11. /**
  12. * @Template()
  13. */
  14. public function accountAction()
  15. {
  16. $user = $this->getUser();
  17. $form_password = $this->container->get('fos_user.change_password.form');
  18. $form_tags_favorites = $this->createForm(
  19. new TagFavoritesForm(),
  20. array('tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  21. ->getTagIdsFavorites($user->getId())
  22. ),
  23. array('tags' => $this->getTagsArray())
  24. );
  25. return array(
  26. 'user' => $user,
  27. 'form_password' => $form_password->createView(),
  28. 'form_tags_favorites' => $form_tags_favorites->createView()
  29. );
  30. }
  31. public function registerAction()
  32. {
  33. $form = $this->container->get('fos_user.registration.form');
  34. $formHandler = $this->container->get('fos_user.registration.form.handler');
  35. $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
  36. $process = $formHandler->process($confirmationEnabled);
  37. if ($process) {
  38. $user = $form->getData();
  39. if ($confirmationEnabled) {
  40. $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
  41. $route = 'fos_user_registration_check_email';
  42. } else {
  43. $this->authenticateUser($user);
  44. $route = 'start';
  45. }
  46. $this->setFlash('success', 'Votre compte a bien été créé');
  47. $url = $this->generateUrl($route);
  48. return new RedirectResponse($url);
  49. }
  50. return $this->container->get('templating')->renderResponse(
  51. 'MuzichIndexBundle:Index:index.html.twig',
  52. array(
  53. 'form' => $form->createView(),
  54. 'error' => null,
  55. 'last_username' => null
  56. )
  57. );
  58. }
  59. public function changePasswordAction()
  60. {
  61. $user = $this->getUser();
  62. if (!is_object($user) || !$user instanceof UserInterface) {
  63. throw new AccessDeniedException('This user does not have access to this section.');
  64. }
  65. $form = $this->container->get('fos_user.change_password.form');
  66. $formHandler = $this->container->get('fos_user.change_password.form.handler');
  67. $process = $formHandler->process($user);
  68. if ($process)
  69. {
  70. $this->container->get('session')->setFlash('fos_user_success', 'change_password.flash.success');
  71. return new RedirectResponse($this->generateUrl('my_account'));
  72. }
  73. else
  74. {
  75. return $this->container->get('templating')->renderResponse(
  76. 'MuzichUserBundle:User:account.html.twig',
  77. array(
  78. 'form_password' => $form->createView(),
  79. 'user' => $user
  80. )
  81. );
  82. }
  83. }
  84. /**
  85. *
  86. * @Template()
  87. */
  88. public function startAction()
  89. {
  90. $user = $this->getUser();
  91. $form = $this->createForm(
  92. new TagFavoritesForm(),
  93. array('tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  94. ->getTagIdsFavorites($user->getId())
  95. ),
  96. array('tags' => $this->getTagsArray())
  97. );
  98. return array(
  99. 'form' => $form->createView()
  100. );
  101. }
  102. /**
  103. *
  104. * @param string $redirect
  105. */
  106. public function updateTagFavoritesAction($redirect)
  107. {
  108. $request = $this->getRequest();
  109. $user = $this->getUser(true, array('join' => array('favorites_tags')));
  110. $form = $this->createForm(
  111. new TagFavoritesForm(),
  112. array('tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  113. ->getTagIdsFavorites($user->getId())
  114. ),
  115. array('tags' => $this->getTagsArray())
  116. );
  117. if ($request->getMethod() == 'POST')
  118. {
  119. $form->bindRequest($request);
  120. if ($form->isValid())
  121. {
  122. $data = $form->getData();
  123. $user->updateTagsFavoritesById($this->getDoctrine()->getEntityManager(), $data['tags']);
  124. $this->container->get('session')->setFlash('success', 'Vos tags péférés ont correctements été mis a jour.');
  125. }
  126. else
  127. {
  128. return $this->container->get('templating')->renderResponse(
  129. 'MuzichUserBundle:User:start.html.twig',
  130. array(
  131. 'form' => $form->createView()
  132. )
  133. );
  134. }
  135. }
  136. // (Il y aura aussi une redirection vers "mon compte / tags")
  137. if ($redirect == 'home')
  138. {
  139. return $this->redirect($this->generateUrl('home'));
  140. }
  141. else
  142. {
  143. return $this->redirect($this->generateUrl('my_account'));
  144. }
  145. }
  146. }