UserController.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * Page de configuration de son compte
  13. *
  14. * @Template()
  15. */
  16. public function accountAction()
  17. {
  18. $user = $this->getUser();
  19. $form_password = $this->container->get('fos_user.change_password.form');
  20. $form_tags_favorites = $this->createForm(
  21. new TagFavoritesForm(),
  22. array('tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  23. ->getTagIdsFavorites($user->getId())
  24. ),
  25. array('tags' => $this->getTagsArray())
  26. );
  27. return array(
  28. 'user' => $user,
  29. 'form_password' => $form_password->createView(),
  30. 'form_tags_favorites' => $form_tags_favorites->createView()
  31. );
  32. }
  33. public function registerAction()
  34. {
  35. $form = $this->container->get('fos_user.registration.form');
  36. $formHandler = $this->container->get('fos_user.registration.form.handler');
  37. $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
  38. $process = $formHandler->process($confirmationEnabled);
  39. if ($process) {
  40. $user = $form->getData();
  41. if ($confirmationEnabled) {
  42. $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
  43. $route = 'fos_user_registration_check_email';
  44. } else {
  45. $this->authenticateUser($user);
  46. $route = 'start';
  47. }
  48. $this->setFlash('fos_user_success', 'registration.flash.user_created');
  49. $url = $this->generateUrl($route);
  50. return new RedirectResponse($url);
  51. }
  52. return $this->container->get('templating')->renderResponse(
  53. 'MuzichIndexBundle:Index:index.html.twig',
  54. array(
  55. 'form' => $form->createView(),
  56. 'error' => null,
  57. 'last_username' => null
  58. )
  59. );
  60. }
  61. public function changePasswordAction()
  62. {
  63. $user = $this->getUser();
  64. if (!is_object($user) || !$user instanceof UserInterface) {
  65. throw new AccessDeniedException('This user does not have access to this section.');
  66. }
  67. $form = $this->container->get('fos_user.change_password.form');
  68. $formHandler = $this->container->get('fos_user.change_password.form.handler');
  69. $process = $formHandler->process($user);
  70. if ($process)
  71. {
  72. $this->container->get('session')->setFlash('fos_user_success', 'change_password.flash.success');
  73. return new RedirectResponse($this->generateUrl('my_account'));
  74. }
  75. else
  76. {
  77. return $this->container->get('templating')->renderResponse(
  78. 'MuzichUserBundle:User:account.html.twig',
  79. array(
  80. 'form_password' => $form->createView(),
  81. 'user' => $user
  82. )
  83. );
  84. }
  85. }
  86. /**
  87. * Page ouverte après l'inscription sur laquelle on propose de saisir ses
  88. * tags favoris.
  89. *
  90. * @Template()
  91. */
  92. public function startAction()
  93. {
  94. $user = $this->getUser();
  95. $form = $this->createForm(
  96. new TagFavoritesForm(),
  97. array('tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  98. ->getTagIdsFavorites($user->getId())
  99. ),
  100. array('tags' => $this->getTagsArray())
  101. );
  102. return array(
  103. 'form' => $form->createView()
  104. );
  105. }
  106. /**
  107. *
  108. * @param string $redirect
  109. */
  110. public function updateTagFavoritesAction($redirect)
  111. {
  112. $request = $this->getRequest();
  113. $user = $this->getUser(true, array('join' => array('favorites_tags')));
  114. /**
  115. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  116. * Docrine le voit si on faire une requete directe.
  117. */
  118. if ($this->container->getParameter('env') == 'test')
  119. {
  120. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  121. $this->container->get('security.context')->getToken()->getUser()->getId(),
  122. array()
  123. )->getSingleResult();
  124. }
  125. $form = $this->createForm(
  126. new TagFavoritesForm(),
  127. array('tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  128. ->getTagIdsFavorites($user->getId())
  129. ),
  130. array('tags' => $this->getTagsArray())
  131. );
  132. if ($request->getMethod() == 'POST')
  133. {
  134. $form->bindRequest($request);
  135. if ($form->isValid())
  136. {
  137. $data = $form->getData();
  138. $user->updateTagsFavoritesById($this->getDoctrine()->getEntityManager(), $data['tags']);
  139. $this->container->get('session')->setFlash('success', 'Vos tags péférés ont correctements été mis a jour.');
  140. }
  141. else
  142. {
  143. return $this->container->get('templating')->renderResponse(
  144. 'MuzichUserBundle:User:start.html.twig',
  145. array(
  146. 'form' => $form->createView()
  147. )
  148. );
  149. }
  150. }
  151. // (Il y aura aussi une redirection vers "mon compte / tags")
  152. if ($redirect == 'home')
  153. {
  154. return $this->redirect($this->generateUrl('home'));
  155. }
  156. else
  157. {
  158. return $this->redirect($this->generateUrl('my_account'));
  159. }
  160. }
  161. }