FavoriteController.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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\Propagator\EventElement;
  8. use Muzich\CoreBundle\Entity\User;
  9. use Muzich\CoreBundle\lib\Tag as TagLib;
  10. use Muzich\CoreBundle\Security\Context as SecurityContext;
  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. if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES)) !== false)
  22. {
  23. return $this->jsonResponseError($non_condition);
  24. }
  25. if (($response = $this->mustBeConnected()))
  26. {
  27. return $response;
  28. }
  29. $user = $this->getUser();
  30. $em = $this->getEntityManager();
  31. if ($user->getPersonalHash($id) != $token || !is_numeric($id)
  32. || !($element = $em->getRepository('MuzichCoreBundle:Element')->findOneById($id))
  33. )
  34. {
  35. throw $this->createNotFoundException();
  36. }
  37. // Si l'élément n'est pas déjà en favoris
  38. if (!$em->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  39. ->findOneBy(array(
  40. 'user' => $user->getId(),
  41. 'element' => $id
  42. )))
  43. {
  44. // On créer un objet
  45. $favorite = new UsersElementsFavorites();
  46. $favorite->setUser($user);
  47. $favorite->setElement($element);
  48. if ($user->getId() != $element->getOwner()->getId())
  49. {
  50. // On déclenche les événements liés a cette action
  51. $event = new EventElement($this->container);
  52. $event->addedToFavorites($element, $user);
  53. $em->persist($user);
  54. }
  55. // On signale que cet user a modifié sa liste de favoris
  56. $user->setData(User::DATA_FAV_UPDATED, true);
  57. $em->persist($favorite);
  58. $em->persist($user);
  59. $em->flush();
  60. }
  61. if ($this->getRequest()->isXmlHttpRequest())
  62. {
  63. return $this->jsonResponse(array(
  64. 'favorite' => true,
  65. 'link_new_url' => $this->generateUrl('favorite_remove', array(
  66. 'id' => $id,
  67. 'token' => $user->getPersonalHash($id)
  68. )),
  69. 'img_new_src' => $this->getAssetUrl('img/icon_star_2_red.png'),
  70. 'img_new_title' => $this->trans('element.favorite.remove', array(), 'elements')
  71. ));
  72. }
  73. else
  74. {
  75. return $this->redirect($this->container->get('request')->headers->get('referer'));
  76. }
  77. }
  78. /**
  79. * Retire comme favoris l'element en id
  80. *
  81. * @param int $id
  82. * @param string $token
  83. */
  84. public function removeAction($id, $token)
  85. {
  86. if (($response = $this->mustBeConnected()))
  87. {
  88. return $response;
  89. }
  90. $user = $this->getUser();
  91. $em = $this->getDoctrine()->getEntityManager();
  92. if ($user->getPersonalHash($id) != $token || !is_numeric($id)
  93. || !($element = $em->getRepository('MuzichCoreBundle:Element')->findOneById($id))
  94. )
  95. {
  96. throw $this->createNotFoundException();
  97. }
  98. // Si l'élément est déjà en favoris, ce qui est cencé être le cas
  99. if (($fav = $em->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  100. ->findOneBy(array(
  101. 'user' => $user->getId(),
  102. 'element' => $id
  103. ))))
  104. {
  105. if ($user->getId() != $element->getOwner()->getId())
  106. {
  107. // On déclenche les événements liés a cette action
  108. $event = new EventElement($this->container);
  109. $event->removedFromFavorites($element, $user);
  110. }
  111. // On signale que cet user a modifié sa liste de favoris
  112. $user->setData(User::DATA_FAV_UPDATED, true);
  113. $em->persist($element->getOwner());
  114. $em->remove($fav);
  115. $em->flush();
  116. }
  117. if ($this->getRequest()->isXmlHttpRequest())
  118. {
  119. return $this->jsonResponse(array(
  120. 'favorite' => true,
  121. 'link_new_url' => $this->generateUrl('favorite_add', array(
  122. 'id' => $id,
  123. 'token' => $user->getPersonalHash($id)
  124. )),
  125. 'img_new_src' => $this->getAssetUrl('img/icon_star_2.png'),
  126. 'img_new_title' => $this->trans('element.favorite.add', array(), 'elements')
  127. ));
  128. }
  129. else
  130. {
  131. return $this->redirect($this->container->get('request')->headers->get('referer'));
  132. }
  133. }
  134. /**
  135. * Page affichant les elements favoris de l'utilisateur
  136. *
  137. */
  138. public function myListAction()
  139. {
  140. $user = $this->getUser();
  141. $search_object = $this->createSearchObject(array(
  142. 'user_id' => $user->getId(),
  143. 'favorite' => true,
  144. 'count' => $this->container->getParameter('search_default_count')
  145. ));
  146. // Récupération des tags
  147. $tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  148. ->getTags($this->getUserId(), $this->getUserId())
  149. ;
  150. // Organisation des tags en fonction de leurs utilisation
  151. $tag_lib = new TagLib();
  152. $tags = $tag_lib->sortTagWithOrderedReference($tags,
  153. $user->getData(User::DATA_TAGS_ORDER_PAGE_FAV, array()));
  154. $tags_id = array();
  155. foreach ($tags as $tag)
  156. {
  157. $tags_id[] = $tag->getId();
  158. }
  159. return $this->render('MuzichFavoriteBundle:Favorite:myList.html.twig', array(
  160. 'tags' => $tags,
  161. 'tags_id_json' => json_encode($tags_id),
  162. 'user' => $this->getUser(),
  163. 'elements' => $search_object->getElements($this->getDoctrine(), $this->getUserId())
  164. ));
  165. }
  166. /**
  167. * Affichage des elements favoris d'un utilisateur particulier.
  168. *
  169. * @param type $slug
  170. * @Template()
  171. */
  172. public function userListAction($slug)
  173. {
  174. $viewed_user = $this->findUserWithSlug($slug);
  175. $tags = array();
  176. $tags_id = array();
  177. $elements = array();
  178. if ($viewed_user->isFavoritesPublics() || $viewed_user->getId() == $this->getUserId(true))
  179. {
  180. $search_object = $this->createSearchObject(array(
  181. 'user_id' => $viewed_user->getId(),
  182. 'favorite' => true,
  183. 'count' => $this->container->getParameter('search_default_count')
  184. ));
  185. $tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  186. ->getTags($viewed_user->getId(), $this->getUserId(true))
  187. ;
  188. $tags_id = array();
  189. foreach ($tags as $tag)
  190. {
  191. $tags_id[] = $tag->getId();
  192. }
  193. $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId(true));
  194. }
  195. return array(
  196. 'tags' => $tags,
  197. 'tags_id_json' => json_encode($tags_id),
  198. 'user' => $this->getUser(),
  199. 'viewed_user' => $viewed_user,
  200. 'elements' => $elements
  201. );
  202. }
  203. public function getElementsAction($user_id, $tags_ids_json, $id_limit = null)
  204. {
  205. $autoplay_context = 'favorite_user';
  206. if ($user_id == $this->getUserId(true))
  207. {
  208. $autoplay_context = 'favorite_my';
  209. }
  210. $tag_ids = json_decode($tags_ids_json);
  211. $search_object = new ElementSearcher();
  212. $tags = null;
  213. //die(var_dump($tag_ids));
  214. if (count($tag_ids))
  215. {
  216. $tags = array();
  217. foreach ($tag_ids as $id)
  218. {
  219. $tags[$id] = $id;
  220. }
  221. }
  222. $search_object->init(array(
  223. 'tags' => $tags,
  224. 'user_id' => $user_id,
  225. 'favorite' => true,
  226. 'count' => $this->container->getParameter('search_default_count'),
  227. 'id_limit' => $id_limit
  228. ));
  229. $message = $this->trans(
  230. 'elements.ajax.more.noelements',
  231. array(),
  232. 'elements'
  233. );
  234. $viewed_user = $this->getUser();
  235. if ($user_id != $this->getUserId(true))
  236. {
  237. $viewed_user = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:User')
  238. ->findOneById($user_id, array())->getSingleResult();
  239. }
  240. $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId(true));
  241. $count = count($elements);
  242. $html = '';
  243. if ($count)
  244. {
  245. $html = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
  246. 'display_autoplay' => $this->getDisplayAutoplayBooleanForContext($autoplay_context),
  247. 'autoplay_context' => $autoplay_context,
  248. 'user' => $this->getUser(),
  249. 'elements' => $elements,
  250. 'tag_ids_json' => $tags_ids_json,
  251. 'viewed_user' => $viewed_user
  252. ))->getContent();
  253. }
  254. return $this->jsonResponse(array(
  255. 'count' => $count,
  256. 'message' => $message,
  257. 'html' => $html
  258. ));
  259. }
  260. }