DefaultController.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /**
  12. *
  13. * @Template()
  14. */
  15. public function myListAction()
  16. {
  17. $user = $this->getUser(true, array('join' => array(
  18. 'groups_owned'
  19. )));
  20. $new_group = new Group();
  21. $form_new = $this->createForm(
  22. new GroupForm(),
  23. $new_group,
  24. array('tags' => $this->getTagsArray())
  25. );
  26. return array(
  27. 'groups' => $user->getGroupsOwned(),
  28. 'form_new' => $form_new->createView()
  29. );
  30. }
  31. /**
  32. * Procédure d'ajout d'un groupe
  33. */
  34. public function addAction(Request $request)
  35. {
  36. $user = $this->getUser();
  37. $em = $this->getDoctrine()->getEntityManager();
  38. $new_group = new Group();
  39. $new_group->setOwner($user);
  40. $form_new = $this->createForm(
  41. new GroupForm(),
  42. $new_group,
  43. array('tags' => $this->getTagsArray())
  44. );
  45. $form_new->bindRequest($request);
  46. if ($form_new->isValid())
  47. {
  48. $factory = new GroupManager($new_group, $em, $this->container);
  49. $factory->proceedTags($new_group->getTags());
  50. $em->persist($new_group);
  51. $em->flush();
  52. $this->setFlash('success', 'group.create.success');
  53. return $this->redirect($this->generateUrl('groups_own_list'));
  54. }
  55. else
  56. {
  57. $user = $this->getUser(true, array('join' => array(
  58. 'groups_owned'
  59. )));
  60. $this->setFlash('error', 'group.create.failure');
  61. return $this->render(
  62. 'GroupBundle:Default:myList.html.twig',
  63. array(
  64. 'groups' => $user->getGroupsOwned(),
  65. 'form_new' => $form_new->createView()
  66. )
  67. );
  68. }
  69. }
  70. }