FavoriteController.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Muzich\FavoriteBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Muzich\CoreBundle\Entity\UsersElementsFavorites;
  6. use Muzich\CoreBundle\Searcher\ElementSearcher;
  7. //use Muzich\CoreBundle\Entity\Group;
  8. //use Muzich\CoreBundle\Form\Group\GroupForm;
  9. //use Symfony\Component\HttpFoundation\Request;
  10. //use Muzich\CoreBundle\Managers\GroupManager;
  11. class FavoriteController extends Controller
  12. {
  13. /**
  14. * Ajoute comme favoris l'element en id
  15. *
  16. * @param int $id
  17. * @param string $token
  18. */
  19. public function addAction($id, $token)
  20. {
  21. $user = $this->getUser();
  22. $em = $this->getDoctrine()->getEntityManager();
  23. if ($user->getPersonalHash() != $token || !is_numeric($id)
  24. || !($element = $em->getRepository('MuzichCoreBundle:Element')->findOneById($id))
  25. )
  26. {
  27. throw $this->createNotFoundException();
  28. }
  29. // Si l'élément n'est pas déjà en favoris
  30. if (!$em->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  31. ->findOneBy(array(
  32. 'user' => $user->getId(),
  33. 'element' => $id
  34. )))
  35. {
  36. // On créer un objet
  37. $favorite = new UsersElementsFavorites();
  38. $favorite->setUser($user);
  39. $favorite->setElement($element);
  40. $em->persist($favorite);
  41. $em->flush();
  42. }
  43. if ($this->getRequest()->isXmlHttpRequest())
  44. {
  45. }
  46. else
  47. {
  48. return $this->redirect($this->container->get('request')->headers->get('referer'));
  49. }
  50. }
  51. /**
  52. * Page affichant les elements favoris de l'utilisateur
  53. *
  54. * @Template()
  55. */
  56. public function myListAction()
  57. {
  58. $search_object = $this->createSearchObject(array(
  59. 'user_id' => $this->getUserId(),
  60. 'favorite' => true
  61. ));
  62. return array(
  63. 'user' => $this->getUser(),
  64. 'elements' => $search_object->getElements($this->getDoctrine(), $this->getUserId())
  65. );
  66. }
  67. /**
  68. * Affichage des elements favoris d'un utilisateur particulier.
  69. *
  70. * @param type $slug
  71. * @Template()
  72. */
  73. public function userListAction($slug)
  74. {
  75. $viewed_user = $this->findUserWithSlug($slug);
  76. $search_object = $this->createSearchObject(array(
  77. 'user_id' => $viewed_user->getId(),
  78. 'favorite' => true
  79. ));
  80. return array(
  81. 'user' => $this->getUser(),
  82. 'viewed_user' => $viewed_user,
  83. 'elements' => $search_object->getElements($this->getDoctrine(), $this->getUserId())
  84. );
  85. }
  86. }