DefaultController.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Muzich\GroupBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Muzich\CoreBundle\Entity\Group;
  6. use Muzich\CoreBundle\Form\Group\GroupForm;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Muzich\CoreBundle\Managers\GroupManager;
  9. class DefaultController extends Controller
  10. {
  11. protected function getGroupForm($group)
  12. {
  13. return $this->createForm(
  14. new GroupForm(),
  15. $group
  16. );
  17. }
  18. /**
  19. * Page listant les groupes possédés par l'utilisateur. Comporte egallement
  20. * un formulaire pour ajouter un groupe.
  21. *
  22. * @Template()
  23. */
  24. public function myListAction()
  25. {
  26. $user = $this->getUser(true, array('join' => array(
  27. 'groups_owned'
  28. )));
  29. $new_group = new Group();
  30. $form_new = $this->getGroupForm($new_group);
  31. return array(
  32. 'groups' => $user->getGroupsOwned(),
  33. 'form_new' => $form_new->createView(),
  34. 'form_new_name' => $form_new->getName()
  35. );
  36. }
  37. /**
  38. * Procédure d'ajout d'un groupe
  39. *
  40. * @param Request $request
  41. * @return redirect|template
  42. */
  43. public function addAction(Request $request)
  44. {
  45. $user = $this->getUser();
  46. /**
  47. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  48. * Docrine le voit si on faire une requete directe.
  49. */
  50. if ($this->container->getParameter('env') == 'test')
  51. {
  52. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  53. $this->container->get('security.context')->getToken()->getUser()->getId(),
  54. array()
  55. )->getSingleResult();
  56. }
  57. $em = $this->getDoctrine()->getEntityManager();
  58. $new_group = new Group();
  59. $new_group->setOwner($user);
  60. $form_new = $this->getGroupForm($new_group);
  61. $form_new->bindRequest($request);
  62. if ($form_new->isValid())
  63. {
  64. $factory = new GroupManager($new_group, $em, $this->container);
  65. $factory->proceedTags(json_decode($new_group->getTags()));
  66. $em->persist($new_group);
  67. $em->flush();
  68. $this->setFlash('success', 'group.create.success');
  69. return $this->redirect($this->generateUrl('groups_own_list'));
  70. }
  71. else
  72. {
  73. $user = $this->getUser(true, array('join' => array(
  74. 'groups_owned'
  75. )));
  76. //$this->setFlash('error', 'group.create.failure');
  77. return $this->render(
  78. 'MuzichGroupBundle:Default:myList.html.twig',
  79. array(
  80. 'groups' => $user->getGroupsOwned(),
  81. 'form_new' => $form_new->createView(),
  82. 'form_new_name' => $form_new->getName()
  83. )
  84. );
  85. }
  86. }
  87. /**
  88. * Modification d'un groupe
  89. *
  90. * @Template()
  91. * @param Request $request
  92. * @param string $slug
  93. * @return array
  94. */
  95. public function editAction(Request $request, $slug)
  96. {
  97. $user = $this->getUser();
  98. try {
  99. $group = $this->getDoctrine()
  100. ->getRepository('MuzichCoreBundle:Group')
  101. ->findOneBySlug($slug)
  102. ->getSingleResult()
  103. ;
  104. } catch (\Doctrine\ORM\NoResultException $e) {
  105. throw $this->createNotFoundException('Groupe introuvable.');
  106. }
  107. if ($group->getOwner()->getId() != $user->getId())
  108. {
  109. throw $this->createNotFoundException('Vous n\'ête pas le créateur de ce groupe.');
  110. }
  111. $group->setTagsToIds();
  112. $form = $this->getGroupForm($group);
  113. return array(
  114. 'group' => $group,
  115. 'form' => $form->createView() ,
  116. 'form_name' => $form->getName()
  117. );
  118. }
  119. public function updateAction(Request $request, $slug)
  120. {
  121. $em = $this->getDoctrine()->getEntityManager();
  122. $group = $this->findGroupWithSlug($slug);
  123. if ($group->getOwner()->getId() != $this->getUserId())
  124. {
  125. throw $this->createNotFoundException('Vous n\'ête pas le créateur de ce groupe.');
  126. }
  127. // Pour être compatible avec le formulaire, la collection de tags dois être
  128. // une collection d'id
  129. $group->setTagsToIds();
  130. $form = $this->getGroupForm($group);
  131. $form->bindRequest($request);
  132. if ($form->isValid())
  133. {
  134. $factory = new GroupManager($group, $em, $this->container);
  135. $factory->proceedTags(json_decode($group->getTags()));
  136. $em->persist($group);
  137. $em->flush();
  138. $this->setFlash('success', 'group.update.success');
  139. return $this->redirect($this->generateUrl('show_group', array('slug' => $group->getSlug())));
  140. }
  141. else
  142. {
  143. $this->setFlash('error', 'group.update.failure');
  144. return $this->render(
  145. 'GroupBundle:Default:edit.html.twig',
  146. array(
  147. 'form_new' => $form->createView(),
  148. 'form_new_name' => $form_new->getName()
  149. )
  150. );
  151. }
  152. }
  153. }