ShowController.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Muzich\HomeBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Muzich\CoreBundle\Form\Element\ElementAddForm;
  6. use Muzich\CoreBundle\Entity\Element;
  7. use Muzich\CoreBundle\Searcher\ElementSearcher;
  8. use Muzich\CoreBundle\lib\Tag as TagLib;
  9. use Muzich\CoreBundle\Entity\User;
  10. class ShowController extends Controller
  11. {
  12. /**
  13. * Page public de l'utilisateur demandé.
  14. *
  15. * @Template()
  16. * @param string $slug
  17. */
  18. public function showUserAction($slug, $count = null)
  19. {
  20. $viewed_user = $this->findUserWithSlug($slug);
  21. $search_object = $this->createSearchObject(array(
  22. 'user_id' => $viewed_user->getId(),
  23. 'count' => ($count)?$count:$this->container->getParameter('search_default_count')
  24. ));
  25. $tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  26. ->getElementsTags($viewed_user->getId(), $this->getUserId(true))
  27. ;
  28. // Organisation des tags en fonction de leurs utilisation
  29. $tag_lib = new TagLib();
  30. $tags = $tag_lib->sortTagWithOrderedReference($tags,
  31. $viewed_user->getData(User::DATA_TAGS_ORDER_DIFF, array()));
  32. $tags_id = array();
  33. foreach ($tags as $tag)
  34. {
  35. $tags_id[] = $tag->getId();
  36. }
  37. $element_ids_owned = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  38. ->getElementIdsOwned($viewed_user->getId())
  39. ;
  40. $count_favorited = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  41. ->countFavoritedForUserElements($viewed_user->getId(), $element_ids_owned)
  42. ;
  43. $count_favorited_users = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  44. ->countFavoritedUsersForUserElements($viewed_user->getId(), $element_ids_owned)
  45. ;
  46. $count_followers = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  47. ->countFollowers($viewed_user->getId())
  48. ;
  49. return array(
  50. 'tags' => $tags,
  51. 'tags_id_json' => json_encode($tags_id),
  52. 'viewed_user' => $viewed_user,
  53. 'elements' => $search_object->getElements($this->getDoctrine(), $this->getUserId(true)),
  54. 'following' => (!$this->isVisitor())?$this->getUser()->isFollowingUserByQuery($this->getDoctrine(), $viewed_user->getId()):false,
  55. 'user' => $this->getUser(),
  56. 'more_count' => ($count)?$count+$this->container->getParameter('search_default_count'):$this->container->getParameter('search_default_count')*2,
  57. 'more_route' => 'show_user_more',
  58. 'topmenu_active' => ($viewed_user->getId() == $this->getUserId(true)) ? 'myfeeds' : 'public',
  59. 'count_owned' => count($element_ids_owned),
  60. 'count_favorited' => $count_favorited,
  61. 'count_favorited_users' => $count_favorited_users,
  62. 'count_followers' => $count_followers,
  63. 'add_form' => ($this->getUserId(true) == $viewed_user->getId())?$this->getAddForm()->createView():null,
  64. 'add_form_name' => 'add'
  65. );
  66. }
  67. /**
  68. * Page publique du groupe demandé.
  69. *
  70. * @Template()
  71. * @param string $slug
  72. */
  73. public function showGroupAction($slug, $count = null)
  74. {
  75. $group = $this->findGroupWithSlug($slug);
  76. $search_object = $this->createSearchObject(array(
  77. 'group_id' => $group->getId(),
  78. 'count' => ($count)?$count:$this->container->getParameter('search_default_count')
  79. ));
  80. ($group->getOwner()->getId() == $this->getUserId(true)) ? $his = true : $his = false;
  81. if ($his || $group->getOpen())
  82. {
  83. $add_form = $this->getAddForm();
  84. }
  85. $tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  86. ->getElementsTags($group->getId(), $this->getUserId(true))
  87. ;
  88. $tags_id = array();
  89. foreach ($tags as $tag)
  90. {
  91. $tags_id[] = $tag->getId();
  92. }
  93. $element_ids_owned = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  94. ->getElementIdsOwned($group->getId())
  95. ;
  96. $count_favorited = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  97. ->countFavoritedForUserElements(null, $element_ids_owned)
  98. ;
  99. $count_favorited_users = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  100. ->countFavoritedUsersForUserElements(null, $element_ids_owned)
  101. ;
  102. $count_followers = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  103. ->countFollowers($group->getId())
  104. ;
  105. return array(
  106. 'tags' => $tags,
  107. 'tags_id_json' => json_encode($tags_id),
  108. 'group' => $group,
  109. 'his_group' => ($group->getOwner()->getId() == $this->getUserId(true)) ? true : false,
  110. 'elements' => $search_object->getElements($this->getDoctrine(), $this->getUserId(true)),
  111. 'following' => (!$this->isVisitor())?$this->getUser()->isFollowingGroupByQuery($this->getDoctrine(), $group->getId()):false,
  112. 'user' => $this->getUser(),
  113. 'add_form' => (isset($add_form)) ? $add_form->createView() : null,
  114. 'add_form_name' => (isset($add_form)) ? 'add' : null,
  115. 'more_count' => ($count)?$count+$this->container->getParameter('search_default_count'):$this->container->getParameter('search_default_count')*2,
  116. 'more_route' => 'show_group_more',
  117. 'count_owned' => count($element_ids_owned),
  118. 'count_favorited' => $count_favorited,
  119. 'count_favorited_users' => $count_favorited_users,
  120. 'count_followers' => $count_followers
  121. );
  122. }
  123. public function getElementsAction($type, $object_id, $tags_ids_json, $id_limit = null)
  124. {
  125. if ($type != 'user' && $type != 'group')
  126. {
  127. throw new \Exception("Wrong Type.");
  128. }
  129. $viewed_user = null;
  130. if ($type == 'user' && $object_id == $this->getUserId(true))
  131. {
  132. $object = $viewed_user = $this->getUser();
  133. }
  134. else if ($type == 'user')
  135. {
  136. $object = $viewed_user = $this->getDoctrine()->getManager()->getRepository('MuzichCoreBundle:User')
  137. ->findOneById($object_id, array())->getSingleResult();
  138. }
  139. else if ($type == 'group')
  140. {
  141. $object = $this->getDoctrine()->getManager()->getRepository('MuzichCoreBundle:Group')
  142. ->findOneById($object_id);
  143. }
  144. $search_object = new ElementSearcher();
  145. $tags = null;
  146. $tag_ids = json_decode($tags_ids_json);
  147. if (count($tag_ids))
  148. {
  149. $tags = array();
  150. foreach ($tag_ids as $id)
  151. {
  152. $tags[$id] = $id;
  153. }
  154. }
  155. $search_object->init(array(
  156. 'tags' => $tags,
  157. $type.'_id' => $object_id,
  158. 'count' => $this->container->getParameter('search_default_count'),
  159. 'id_limit' => $id_limit
  160. ));
  161. $message = $this->trans(
  162. 'elements.ajax.more.noelements',
  163. array(),
  164. 'elements'
  165. );
  166. $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId(true));
  167. $count = count($elements);
  168. $html = '';
  169. if ($count)
  170. {
  171. $html = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
  172. 'display_autoplay' => $this->getDisplayAutoplayBooleanForContext('show_'.$type),
  173. 'autoplay_context' => 'show_'.$type,
  174. 'user' => $this->getUser(),
  175. 'viewed_user' => $viewed_user,
  176. 'elements' => $elements,
  177. 'tag_ids_json' => $tags_ids_json,
  178. $type => $object
  179. ))->getContent();
  180. }
  181. return $this->jsonResponse(array(
  182. 'status' => 'success',
  183. 'count' => $count,
  184. 'message' => $message,
  185. 'html' => $html
  186. ));
  187. }
  188. }