IndexController.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Muzich\IndexBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Symfony\Component\Security\Core\SecurityContext;
  6. use Symfony\Component\Validator\Constraints\Email;
  7. use Symfony\Component\Validator\Constraints\Collection;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Muzich\UserBundle\Form\Type\RegistrationFormType;
  10. use Muzich\CoreBundle\Entity\User;
  11. class IndexController extends Controller
  12. {
  13. /**
  14. *
  15. */
  16. public function indexAction()
  17. {
  18. // On rajoute le test sur l'environnement car dans les tests, d'un test a l'autre
  19. // l'utilisateur reste connecté et pas moyen de le déco ...
  20. if ($this->getUser() != 'anon.' && $this->container->getParameter('env') != 'test')
  21. {
  22. return $this->redirect($this->generateUrl('home'));
  23. }
  24. $vars = $this->proceedLogin();
  25. $form = $this->getRegistrationForm();
  26. return $this->render('MuzichIndexBundle:Index:index.html.twig', array_merge($vars, array(
  27. 'form' => $form->createView(),
  28. 'presubscription_form' => $this->getPreSubscriptionForm()->createView()
  29. )));
  30. }
  31. protected function getRegistrationForm()
  32. {
  33. return $this->createForm(new RegistrationFormType(), new User());
  34. }
  35. /**
  36. * Gestion du formulaire d'identification sur la page d'index.
  37. *
  38. * @return type array
  39. */
  40. protected function proceedLogin()
  41. {
  42. $request = $this->container->get('request');
  43. /* @var $request \Symfony\Component\HttpFoundation\Request */
  44. $session = $request->getSession();
  45. /* @var $session Symfony\Component\HttpFoundation\Session\Session */
  46. // get the error if any (works with forward and redirect -- see below)
  47. if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
  48. $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
  49. } elseif (null !== $session && $session->has(SecurityContext::AUTHENTICATION_ERROR)) {
  50. $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
  51. $session->remove(SecurityContext::AUTHENTICATION_ERROR);
  52. } else {
  53. $error = '';
  54. }
  55. if ($error) {
  56. $error = $this->trans('login.fail', array(), 'users');
  57. }
  58. // last username entered by the user
  59. $lastUsername = (null === $session) ? '' : $session->get(SecurityContext::LAST_USERNAME);
  60. return array(
  61. 'last_username' => $lastUsername,
  62. 'error' => $error,
  63. 'registration_errors_pers' => array()
  64. );
  65. }
  66. // public function presubscriptionAction(Request $request)
  67. // {
  68. // $form = $this->getPreSubscriptionForm();
  69. // $form->bind($request);
  70. // if ($form->isValid())
  71. // {
  72. // $message = \Swift_Message::newInstance()
  73. // ->setSubject($this->trans('mail.presubscription.subject', array(), 'text'))
  74. // ->setFrom(array(
  75. // $this->container->getParameter('emails_from') => $this->container->getParameter('emails_from_name')
  76. // ))
  77. // ->setTo($form->getData()->getEmail())
  78. // ->setBody(
  79. // $this->renderView(
  80. // 'MuzichIndexBundle:Presubscription:confirm.txt.twig',
  81. // array(
  82. // 'url' => $this->generateUrl('presubscription_register_confirm', array(
  83. // 'token' => $form->getData()->getToken()
  84. // ), true)
  85. // )
  86. // )
  87. // )
  88. // ;
  89. // $message->getHeaders()->addTextHeader('List-Unsubscribe', 'unsubscribe@muzi.ch');
  90. //
  91. // $this->get('mailer')->send($message);
  92. //
  93. //
  94. // $this->persist($form->getData());
  95. // $this->flush();
  96. // $this->setFlash('info', 'presubscription.success');
  97. // return $this->redirect($this->generateUrl('index'));
  98. // }
  99. //
  100. // $this->setFlash('error', 'presubscription.error');
  101. // return $this->render('MuzichIndexBundle:Index:index.html.twig', array(
  102. // 'form' => $this->getRegistrationForm()->createView(),
  103. // 'presubscription_form' => $form->createView(),
  104. // 'last_username' => '',
  105. // 'error' => '',
  106. // 'registration_errors_pers' => array()
  107. // ));
  108. // }
  109. //
  110. // public function presubscriptionConfirmAction($token)
  111. // {
  112. // $presubscription = $this->getDoctrine()->getRepository('MuzichCoreBundle:Presubscription')->findOneBy(array(
  113. // 'token' => $token,
  114. // 'confirmed' => false
  115. // ));
  116. //
  117. // if (!$presubscription)
  118. // {
  119. // throw $this->createNotFoundException();
  120. // }
  121. //
  122. // $presubscription->setConfirmed(true);
  123. // $this->persist($presubscription);
  124. // $this->flush();
  125. //
  126. // $this->setFlash('success', 'presubscription.confirmed');
  127. // return $this->redirect($this->generateUrl('index'));
  128. // }
  129. }