CoreController.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Muzich\CoreBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. //use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Muzich\CoreBundle\Entity\FollowUser;
  6. use Muzich\CoreBundle\Entity\FollowGroup;
  7. //use Doctrine\ORM\Query;
  8. use Muzich\CoreBundle\Form\Element\ElementAddForm;
  9. use Muzich\CoreBundle\ElementFactory\ElementManager;
  10. use Muzich\CoreBundle\Entity\Element;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. class CoreController extends Controller
  13. {
  14. public function changeLanguageAction($language, $redirect)
  15. {
  16. if($language != null)
  17. {
  18. $old = $this->get('session')->getLocale();
  19. $this->get('session')->setLocale($language);
  20. }
  21. $url_referer = $this->container->get('request')->headers->get('referer');
  22. // On effectue un contrôl un peu sépcial:
  23. // Si la page de demande était la page de connexion (hello)
  24. if (
  25. $this->generateUrl('index', array('_locale' => $old), true) == $url_referer
  26. || $this->generateUrl('index', array('_locale' => null), true) == $url_referer
  27. )
  28. {
  29. // On construit l'url
  30. $url = $this->generateUrl('index', array('_locale' => $language));
  31. }
  32. else
  33. {
  34. // Sinon on doit rediriger l'utilisateur vers son url d'origine
  35. if (preg_match('/user/', $url_referer))
  36. {
  37. $search = "/$old/user/";
  38. $replace = "/$language/user/";
  39. }
  40. elseif (preg_match('/group/', $url_referer))
  41. {
  42. $search = "/$old/group/";
  43. $replace = "/$language/group/";
  44. }
  45. else
  46. {
  47. $search = "/$old";
  48. $replace = "/$language";
  49. }
  50. $url = str_replace($search, $replace, $url_referer);
  51. }
  52. return new RedirectResponse($url);
  53. }
  54. /**
  55. *
  56. * @param string $type
  57. * @param int $id
  58. * @param string $salt
  59. */
  60. public function followAction($type, $id, $token)
  61. {
  62. $user = $this->getUser();
  63. // Vérifications préléminaires
  64. if ($user->getPersonalHash() != $token || !in_array($type, array('user', 'group')) || !is_numeric($id))
  65. {
  66. throw $this->createNotFoundException();
  67. }
  68. $em = $this->getDoctrine()->getEntityManager();
  69. $Follow = $em
  70. ->getRepository('MuzichCoreBundle:Follow' . ucfirst($type))
  71. ->findOneBy(
  72. array(
  73. 'follower' => $user->getId(),
  74. ($type == 'user') ? 'followed' : 'group' => $id
  75. )
  76. )
  77. ;
  78. if ($Follow)
  79. {
  80. // L'utilisateur suis déjà, on doit détruire l'entité
  81. $em->remove($Follow);
  82. $em->flush();
  83. }
  84. else
  85. {
  86. $followed = $em->getRepository('MuzichCoreBundle:'.ucfirst($type))->find($id);
  87. if (!$followed) {
  88. throw $this->createNotFoundException('No '.$type.' found for id '.$id);
  89. }
  90. if ($type == 'user') { $Follow = new FollowUser(); }
  91. else { $Follow = new FollowGroup(); }
  92. $Follow->setFollower($user);
  93. if ($type == 'user') { $Follow->setFollowed($followed); }
  94. else { $Follow->setGroup($followed); }
  95. $em->persist($Follow);
  96. $em->flush();
  97. }
  98. if ($this->getRequest()->isXmlHttpRequest())
  99. {
  100. }
  101. else
  102. {
  103. return $this->redirect($this->container->get('request')->headers->get('referer'));
  104. }
  105. }
  106. public function elementAddAction()
  107. {
  108. $user = $this->getUser();
  109. $em = $this->getDoctrine()->getEntityManager();
  110. $form = $this->createForm(
  111. new ElementAddForm(),
  112. array(),
  113. array('tags' => $this->getTagsArray())
  114. );
  115. if ($this->getRequest()->getMethod() == 'POST')
  116. {
  117. $form->bindRequest($this->getRequest());
  118. if ($form->isValid())
  119. {
  120. $data = $form->getData();
  121. $element = new Element();
  122. $factory = new ElementManager($element, $em, $this->container);
  123. $factory->proceedFill($data, $user);
  124. $em->persist($element);
  125. $em->flush();
  126. }
  127. }
  128. if ($this->getRequest()->isXmlHttpRequest())
  129. {
  130. }
  131. else
  132. {
  133. return $this->redirect($this->generateUrl('home'));
  134. }
  135. }
  136. // protected function proceedElement(Element $element)
  137. // {
  138. // $factory = new ElementFactory();
  139. // $factory->proceed($element, $form->getData());
  140. // }
  141. }