FavoriteController.php 8.6KB

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