ShowController.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. class ShowController extends Controller
  9. {
  10. /**
  11. * Page public de l'utilisateur demandé.
  12. *
  13. * @Template()
  14. * @param string $slug
  15. */
  16. public function showUserAction($slug, $count = null)
  17. {
  18. $viewed_user = $this->findUserWithSlug($slug);
  19. $search_object = $this->createSearchObject(array(
  20. 'user_id' => $viewed_user->getId(),
  21. 'count' => ($count)?$count:$this->container->getParameter('search_default_count')
  22. ));
  23. $tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  24. ->getElementsTags($viewed_user->getId(), $this->getUserId())
  25. ;
  26. $tags_id = array();
  27. foreach ($tags as $tag)
  28. {
  29. $tags_id[] = $tag->getId();
  30. }
  31. $element_ids_owned = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  32. ->getElementIdsOwned($viewed_user->getId())
  33. ;
  34. $count_favorited = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  35. ->countFavoritedForUserElements($viewed_user->getId(), $element_ids_owned)
  36. ;
  37. $count_favorited_users = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  38. ->countFavoritedUsersForUserElements($viewed_user->getId(), $element_ids_owned)
  39. ;
  40. $count_followers = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
  41. ->countFollowers($viewed_user->getId())
  42. ;
  43. return array(
  44. 'tags' => $tags,
  45. 'tags_id_json' => json_encode($tags_id),
  46. 'viewed_user' => $viewed_user,
  47. 'elements' => $search_object->getElements($this->getDoctrine(), $this->getUserId()),
  48. 'following' => $this->getUser()->isFollowingUserByQuery($this->getDoctrine(), $viewed_user->getId()),
  49. 'user' => $this->getUser(),
  50. 'more_count' => ($count)?$count+$this->container->getParameter('search_default_count'):$this->container->getParameter('search_default_count')*2,
  51. 'more_route' => 'show_user_more',
  52. 'topmenu_active' => ($viewed_user->getId() == $this->getUserId()) ? 'myfeeds' : 'public',
  53. 'count_owned' => count($element_ids_owned),
  54. 'count_favorited' => $count_favorited,
  55. 'count_favorited_users' => $count_favorited_users,
  56. 'count_followers' => $count_followers,
  57. 'add_form' => ($this->getUserId() == $viewed_user->getId())?$this->getAddForm()->createView():null,
  58. 'add_form_name' => 'add'
  59. );
  60. }
  61. /**
  62. * Page publique du groupe demandé.
  63. *
  64. * @Template()
  65. * @param string $slug
  66. */
  67. public function showGroupAction($slug, $count = null)
  68. {
  69. $group = $this->findGroupWithSlug($slug);
  70. $search_object = $this->createSearchObject(array(
  71. 'group_id' => $group->getId(),
  72. 'count' => ($count)?$count:$this->container->getParameter('search_default_count')
  73. ));
  74. ($group->getOwner()->getId() == $this->getUserId()) ? $his = true : $his = false;
  75. if ($his || $group->getOpen())
  76. {
  77. $add_form = $this->getAddForm();
  78. }
  79. $tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  80. ->getElementsTags($group->getId(), $this->getUserId())
  81. ;
  82. $tags_id = array();
  83. foreach ($tags as $tag)
  84. {
  85. $tags_id[] = $tag->getId();
  86. }
  87. $element_ids_owned = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  88. ->getElementIdsOwned($group->getId())
  89. ;
  90. $count_favorited = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  91. ->countFavoritedForUserElements(null, $element_ids_owned)
  92. ;
  93. $count_favorited_users = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
  94. ->countFavoritedUsersForUserElements(null, $element_ids_owned)
  95. ;
  96. $count_followers = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
  97. ->countFollowers($group->getId())
  98. ;
  99. return array(
  100. 'tags' => $tags,
  101. 'tags_id_json' => json_encode($tags_id),
  102. 'group' => $group,
  103. 'his_group' => ($group->getOwner()->getId() == $this->getUserId()) ? true : false,
  104. 'elements' => $search_object->getElements($this->getDoctrine(), $this->getUserId()),
  105. 'following' => $this->getUser()->isFollowingGroupByQuery($this->getDoctrine(), $group->getId()),
  106. 'user' => $this->getUser(),
  107. 'add_form' => (isset($add_form)) ? $add_form->createView() : null,
  108. 'add_form_name' => (isset($add_form)) ? 'add' : null,
  109. 'more_count' => ($count)?$count+$this->container->getParameter('search_default_count'):$this->container->getParameter('search_default_count')*2,
  110. 'more_route' => 'show_group_more',
  111. 'count_owned' => count($element_ids_owned),
  112. 'count_favorited' => $count_favorited,
  113. 'count_favorited_users' => $count_favorited_users,
  114. 'count_followers' => $count_followers
  115. );
  116. }
  117. public function getElementsAction($type, $object_id, $tags_ids_json, $id_limit = null, $invert = false)
  118. {
  119. if (($response = $this->mustBeConnected()))
  120. {
  121. return $response;
  122. }
  123. if ($type != 'user' && $type != 'group')
  124. {
  125. throw new \Exception("Wrong Type.");
  126. }
  127. $tag_ids = json_decode($tags_ids_json);
  128. $search_object = new ElementSearcher();
  129. $tags = array();
  130. foreach ($tag_ids as $id)
  131. {
  132. $tags[$id] = $id;
  133. }
  134. $search_object->init(array(
  135. 'tags' => $tags,
  136. $type.'_id' => $object_id,
  137. 'count' => $this->container->getParameter('search_default_count'),
  138. 'id_limit' => $id_limit
  139. ));
  140. $message = $this->trans(
  141. 'elements.ajax.more.noelements',
  142. array(),
  143. 'elements'
  144. );
  145. $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId());
  146. $count = count($elements);
  147. $html = '';
  148. if ($count)
  149. {
  150. $html = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
  151. 'user' => $this->getUser(),
  152. 'elements' => $elements,
  153. 'invertcolor' => $invert
  154. ))->getContent();
  155. }
  156. return $this->jsonResponse(array(
  157. 'status' => 'success',
  158. 'count' => $count,
  159. 'message' => $message,
  160. 'html' => $html
  161. ));
  162. }
  163. }