ShowController.php 7.8KB

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