ShowController.php 7.6KB

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