DefaultController.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. use Muzich\CoreBundle\Security\Context as SecurityContext;
  10. class DefaultController extends Controller
  11. {
  12. protected function getGroupForm($group)
  13. {
  14. return $this->createForm(
  15. new GroupForm(),
  16. $group
  17. );
  18. }
  19. /**
  20. * Page listant les groupes possédés par l'utilisateur. Comporte egallement
  21. * un formulaire pour ajouter un groupe.
  22. *
  23. * @Template()
  24. */
  25. public function myListAction()
  26. {
  27. $user = $this->getUser(true, array('join' => array(
  28. 'groups_owned'
  29. )));
  30. $new_group = new Group();
  31. $form_new = $this->getGroupForm($new_group);
  32. return array(
  33. 'groups' => $user->getGroupsOwned(),
  34. 'form_new' => $form_new->createView(),
  35. 'form_new_name' => $form_new->getName(),
  36. 'open_add_group' => false
  37. );
  38. }
  39. /**
  40. * Procédure d'ajout d'un groupe
  41. *
  42. * @param Request $request
  43. * @return redirect|template
  44. */
  45. public function addAction(Request $request)
  46. {
  47. $user = $this->getUser();
  48. if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_GROUP_ADD)) !== false)
  49. {
  50. throw $this->createNotFoundException();
  51. }
  52. /**
  53. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  54. * Docrine le voit si on faire une requete directe.
  55. */
  56. if ($this->container->getParameter('env') == 'test')
  57. {
  58. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  59. $this->container->get('security.context')->getToken()->getUser()->getId(),
  60. array()
  61. )->getSingleResult();
  62. }
  63. $em = $this->getDoctrine()->getEntityManager();
  64. $new_group = new Group();
  65. $new_group->setOwner($user);
  66. $form_new = $this->getGroupForm($new_group);
  67. $form_new->bind($request);
  68. if ($form_new->isValid())
  69. {
  70. $factory = new GroupManager($new_group, $em, $this->container);
  71. $factory->proceedTags(json_decode($new_group->getTags()));
  72. $em->persist($new_group);
  73. $em->flush();
  74. $this->setFlash('success', 'group.create.success');
  75. return $this->redirect($this->generateUrl('groups_own_list'));
  76. }
  77. else
  78. {
  79. $user = $this->getUser(true, array('join' => array(
  80. 'groups_owned'
  81. )));
  82. //$this->setFlash('error', 'group.create.failure');
  83. return $this->render(
  84. 'MuzichGroupBundle:Default:myList.html.twig',
  85. array(
  86. 'groups' => $user->getGroupsOwned(),
  87. 'form_new' => $form_new->createView(),
  88. 'form_new_name' => $form_new->getName(),
  89. 'open_add_group' => true
  90. )
  91. );
  92. }
  93. }
  94. /**
  95. * Modification d'un groupe
  96. *
  97. * @Template()
  98. * @param Request $request
  99. * @param string $slug
  100. * @return array
  101. */
  102. public function editAction(Request $request, $slug)
  103. {
  104. $user = $this->getUser();
  105. try {
  106. $group = $this->getDoctrine()
  107. ->getEntityManager()->createQuery('SELECT g, t FROM MuzichCoreBundle:Group g
  108. LEFT JOIN g.tags t WHERE g.slug = :gslug')
  109. ->setParameter('gslug', $slug)
  110. ->getSingleResult()
  111. ;
  112. } catch (\Doctrine\ORM\NoResultException $e) {
  113. return $this->createNotFoundException();
  114. }
  115. if ($group->getOwner()->getId() != $user->getId())
  116. {
  117. return $this->createNotFoundException();
  118. }
  119. $prompt_tags = array();
  120. foreach ($group->getTags() as $tag)
  121. {
  122. $prompt_tags[$tag->getTag()->getId()] = $tag->getTag()->getName();
  123. }
  124. $group->setTags($group->getTagsIdsJson());
  125. $form = $this->getGroupForm($group);
  126. return array(
  127. 'group' => $group,
  128. 'form' => $form->createView(),
  129. 'form_name' => 'group',
  130. 'search_tags' => $prompt_tags
  131. );
  132. }
  133. public function updateAction(Request $request, $slug)
  134. {
  135. $em = $this->getDoctrine()->getEntityManager();
  136. $group = $this->findGroupWithSlug($slug);
  137. if ($group->getOwner()->getId() != $this->getUserId())
  138. {
  139. throw $this->createNotFoundException('Vous n\'ête pas le créateur de ce groupe.');
  140. }
  141. $prompt_tags = array();
  142. foreach ($group->getTags() as $tag)
  143. {
  144. $prompt_tags[$tag->getTag()->getId()] = $tag->getTag()->getName();
  145. }
  146. // Pour être compatible avec le formulaire
  147. $group->setTags($group->getTagsIdsJson());
  148. $form = $this->getGroupForm($group);
  149. $form->bind($request);
  150. if ($form->isValid())
  151. {
  152. $factory = new GroupManager($group, $em, $this->container);
  153. $factory->proceedTags(json_decode($group->getTags()));
  154. $em->persist($group);
  155. $em->flush();
  156. $this->setFlash('success', 'group.update.success');
  157. return $this->redirect($this->generateUrl('show_group', array('slug' => $group->getSlug())));
  158. }
  159. else
  160. {
  161. return $this->render(
  162. 'MuzichGroupBundle:Default:edit.html.twig',
  163. array(
  164. 'group' => $group,
  165. 'form' => $form->createView(),
  166. 'form_name' => 'group',
  167. 'search_tags' => $prompt_tags
  168. )
  169. );
  170. }
  171. }
  172. public function deleteAction($group_id, $token)
  173. {
  174. $user = $this->getUser();
  175. if ($user->getPersonalHash($group_id) != $token)
  176. {
  177. throw $this->createNotFoundException('Accès non autorisé.');
  178. }
  179. $group = $this->findGroupWithId($group_id);
  180. if ($user->getId() != $group->getOwner()->getId())
  181. {
  182. throw $this->createNotFoundException('Accès non autorisé.');
  183. }
  184. $em = $this->getDoctrine()->getEntityManager();
  185. // Il faudra le faire avec doctrine:
  186. $elements = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  187. ->findBy(array('group' => $group->getId()))
  188. ;
  189. foreach ($elements as $element)
  190. {
  191. $element->setGroup(null);
  192. $em->persist($element);
  193. }
  194. $em->remove($group);
  195. $em->flush();
  196. return $this->redirect($this->container->get('request')->headers->get('referer'));
  197. }
  198. }