ShowController.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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())
  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()),
  54. 'following' => $this->getUser()->isFollowingUserByQuery($this->getDoctrine(), $viewed_user->getId()),
  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()) ? '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() == $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()) ? $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())
  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 : false,
  110. 'elements' => $search_object->getElements($this->getDoctrine(), $this->getUserId()),
  111. 'following' => $this->getUser()->isFollowingGroupByQuery($this->getDoctrine(), $group->getId()),
  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 (($response = $this->mustBeConnected()))
  126. {
  127. return $response;
  128. }
  129. if ($type != 'user' && $type != 'group')
  130. {
  131. throw new \Exception("Wrong Type.");
  132. }
  133. $viewed_user = null;
  134. if ($type == 'user' && $object_id == $this->getUserId())
  135. {
  136. $object = $viewed_user = $this->getUser();
  137. }
  138. else if ($type == 'user')
  139. {
  140. $object = $viewed_user = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:User')
  141. ->findOneById($object_id, array())->getSingleResult();
  142. }
  143. else if ($type == 'group')
  144. {
  145. $object = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:Group')
  146. ->findOneById($object_id);
  147. }
  148. $search_object = new ElementSearcher();
  149. $tags = null;
  150. $tag_ids = json_decode($tags_ids_json);
  151. if (count($tag_ids))
  152. {
  153. $tags = array();
  154. foreach ($tag_ids as $id)
  155. {
  156. $tags[$id] = $id;
  157. }
  158. }
  159. $search_object->init(array(
  160. 'tags' => $tags,
  161. $type.'_id' => $object_id,
  162. 'count' => $this->container->getParameter('search_default_count'),
  163. 'id_limit' => $id_limit
  164. ));
  165. $message = $this->trans(
  166. 'elements.ajax.more.noelements',
  167. array(),
  168. 'elements'
  169. );
  170. $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId());
  171. $count = count($elements);
  172. $html = '';
  173. if ($count)
  174. {
  175. $html = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
  176. 'display_autoplay' => $this->getDisplayAutoplayBooleanForContext('show_'.$type),
  177. 'autoplay_context' => 'show_'.$type,
  178. 'user' => $this->getUser(),
  179. 'viewed_user' => $viewed_user,
  180. 'elements' => $elements,
  181. 'tag_ids_json' => $tags_ids_json,
  182. $type => $object
  183. ))->getContent();
  184. }
  185. return $this->jsonResponse(array(
  186. 'status' => 'success',
  187. 'count' => $count,
  188. 'message' => $message,
  189. 'html' => $html
  190. ));
  191. }
  192. }