SearchController.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. namespace Muzich\CoreBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Muzich\CoreBundle\Searcher\ElementSearcher;
  6. use Muzich\CoreBundle\Form\Search\ElementSearchForm;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Muzich\CoreBundle\Util\TagLike;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Muzich\CoreBundle\Searcher\GlobalSearcher;
  11. class SearchController extends Controller
  12. {
  13. /**
  14. * Procédure qui construit un réponse json contenant le html
  15. * par defalt de la liste d'élément.
  16. *
  17. * @param Collection $elements
  18. * @param sring $message
  19. * @param string $session_id
  20. * @return Response
  21. */
  22. protected function searchElementsMore($context, $autoplay_context, $elements, $message, $session_id)
  23. {
  24. $data = array();
  25. $end = (($count = count($elements)) < $this->container->getParameter('search_ajax_more'));
  26. $html = '';
  27. if ($count)
  28. {
  29. $html = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
  30. 'user' => $this->getUser(),
  31. 'elements' => $elements,
  32. 'display_autoplay' => $this->getDisplayAutoplayBooleanForContext($context),
  33. 'autoplay_context' => ($autoplay_context)?$autoplay_context:$context
  34. ))->getContent();
  35. $data['more_link_href'] = $this->generateUrl('search_elements_more', array(
  36. 'context' => $context,
  37. 'id_limit' => $elements[count($elements)-1]->getId(),
  38. 'session_id' => $session_id
  39. ));
  40. }
  41. return $this->jsonResponse(array(
  42. 'status' => 'success',
  43. 'count' => $count,
  44. 'message' => $message,
  45. 'html' => $html,
  46. 'end' => $end,
  47. 'data' => $data
  48. ));
  49. }
  50. /**
  51. * Procédure de recherche, qui met a jour l'objet de recherche (ainsi
  52. * que les paramétres en session).
  53. *
  54. */
  55. public function searchElementsAction($context, $id_limit = null, $session_id = null)
  56. {
  57. $request = $this->getRequest();
  58. $search_object = $this->getElementSearcher(null, false, $session_id);
  59. $search_form = $this->getSearchForm($search_object);
  60. $form_submited = false;
  61. if ($request->getMethod() == 'POST')
  62. {
  63. $form_submited = true;
  64. $search_form->bind($request);
  65. // Si le formulaire est valide
  66. if ($search_form->isValid())
  67. {
  68. // On met a jour l'objet avec les nouveaux paramétres saisie dans le form
  69. $data = $search_form->getData();
  70. // Le formulaire nous permet de récupérer uniquement les ids.
  71. // On va donc chercher les name en base pour le passer a l'objet
  72. // ElementSearch
  73. $data['tags'] = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  74. ->getTagsForElementSearch(json_decode($data['tags'], true));
  75. $search_object->update($data);
  76. // Et on met a jour la "mémoire" de la recherche
  77. $this->setElementSearcherParams($search_object->getParams());
  78. }
  79. }
  80. if ($this->getRequest()->isXmlHttpRequest())
  81. {
  82. if ($form_submited && !$id_limit)
  83. {
  84. $message = $this->trans(
  85. 'noelements.sentence_filter',
  86. array('%link_string%' => $this->trans(
  87. 'noelements.sentence_filter_link_string',
  88. array(),
  89. 'elements'
  90. )),
  91. 'elements'
  92. );
  93. }
  94. else
  95. {
  96. $message = $this->trans(
  97. 'elements.ajax.more.noelements',
  98. array(),
  99. 'elements'
  100. );
  101. }
  102. // template qui apelle doSearchElementsAction
  103. $search_object->update(array(
  104. 'count' => $this->container->getParameter('search_ajax_more'),
  105. 'id_limit' => $id_limit
  106. ));
  107. $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId(true));
  108. return $this->searchElementsMore($context, null, $elements, $message, $session_id);
  109. }
  110. else
  111. {
  112. return $this->redirect($this->generateUrl('home'));
  113. }
  114. }
  115. /**
  116. * Action (ajax) de récupération d'éléments en plus
  117. * [a check pour être sur] N'EST PLUS UTILISE
  118. *
  119. * @param string $type
  120. * @param string $object_id
  121. * @param int $id_limit
  122. * @return Response
  123. */
  124. public function searchElementsShowAction($context, $type, $object_id, $id_limit)
  125. {
  126. $autoplay_context = null;
  127. if ($this->getRequest()->isXmlHttpRequest())
  128. {
  129. $object = null;
  130. $param_id = '';
  131. if ($type == 'user')
  132. {
  133. $autoplay_context = 'show_user';
  134. $object = $this->getDoctrine()
  135. ->getRepository('MuzichCoreBundle:User')
  136. ->findOneBy(array('id' => $object_id))
  137. ;
  138. $param_id = 'user_id';
  139. }
  140. elseif ($type == 'group')
  141. {
  142. $autoplay_context = 'show_group';
  143. $object = $this->getDoctrine()
  144. ->getRepository('MuzichCoreBundle:Group')
  145. ->findOneById($object_id)
  146. ;
  147. $param_id = 'group_id';
  148. }
  149. if (!$object)
  150. {
  151. throw new \Exception('Object Unknow');
  152. }
  153. $search = $this->createSearchObject(array(
  154. $param_id => $object->getId(),
  155. 'count' => $this->container->getParameter('search_ajax_more'),
  156. 'id_limit' => $id_limit
  157. ));
  158. $elements = $search->getElements($this->getDoctrine(), $this->getUserId());
  159. return $this->searchElementsMore(
  160. $context,
  161. $autoplay_context,
  162. $elements,
  163. $this->trans(
  164. 'elements.ajax.more.noelements',
  165. array(),
  166. 'elements'
  167. )
  168. );
  169. }
  170. throw new \Exception('XmlHttpRequest only for this action');
  171. }
  172. /**
  173. * Action permettant d'afficher plus de résultats (éléments) dans
  174. * une global search.
  175. *
  176. * @param Request $request
  177. * @param int $last_id
  178. * @param string $string
  179. * @return Response
  180. */
  181. public function globalSearchMoreAction(Request $request, $context, $last_id, $string)
  182. {
  183. if (($response = $this->mustBeConnected(true)))
  184. {
  185. return $response;
  186. }
  187. $search = $this->createSearchObject(array(
  188. 'count' => $this->container->getParameter('search_ajax_more'),
  189. 'id_limit' => $last_id,
  190. 'string' => $string
  191. ));
  192. $elements = $search->getElements($this->getDoctrine(), $this->getUserId());
  193. return $this->searchElementsMore(
  194. $context,
  195. $autoplay_context = null,
  196. $elements,
  197. $this->trans(
  198. 'elements.ajax.more.noelements',
  199. array(),
  200. 'elements'
  201. ),
  202. null
  203. );
  204. }
  205. /**
  206. * Procédure (ajax) de recherche de tags. Essentielement utilisé dans
  207. * le tagPrompt.
  208. *
  209. * @param string $string_search
  210. * @param int $timestamp
  211. * @return Response
  212. */
  213. public function searchTagAction($timestamp)
  214. {
  215. $string_search = $this->getRequest()->request->get('string_search');
  216. if ($this->getRequest()->isXmlHttpRequest())
  217. {
  218. if (strlen(trim($string_search)) > 1)
  219. {
  220. // On utilise l'objet TagLike
  221. $TagLike = new TagLike($this->getDoctrine()->getEntityManager());
  222. // Pour trier nos tags d'une manière plus humaine
  223. $sort_response = $TagLike->getSimilarTags($string_search, $this->getUserId(true));
  224. $status = 'success';
  225. $error = '';
  226. $message = $this->trans(
  227. 'tags.search.message_found',
  228. array('%string%' => $string_search),
  229. 'userui'
  230. );
  231. }
  232. else
  233. {
  234. $status = 'error';
  235. $sort_response = array('tags' => array(), 'same_found' => false);
  236. $error = 'Vous devez saisir au moins deux caractères';
  237. $message = '';
  238. }
  239. $return_array = array(
  240. 'status' => $status,
  241. 'timestamp' => $timestamp,
  242. 'error' => $error,
  243. 'message' => $message,
  244. 'same_found' => $sort_response['same_found'],
  245. 'data' => $sort_response['tags']
  246. );
  247. $response = new Response(json_encode($return_array));
  248. $response->headers->set('Content-Type', 'application/json; charset=utf-8');
  249. return $response;
  250. }
  251. throw $this->createNotFoundException('Cette ressource n\'est pas accessible');
  252. }
  253. /**
  254. * Récupére l'id d'un tag (ajax)
  255. * [A check] mais ne doit et n'est plus utilisé.
  256. *
  257. * @param type $string_search
  258. * @return Response
  259. */
  260. public function searchTagIdAction($string_search)
  261. {
  262. if ($this->getRequest()->isXmlHttpRequest())
  263. {
  264. $tag_id = $this->getDoctrine()->getEntityManager()->createQuery("
  265. SELECT t.id FROM MuzichCoreBundle:Tag t
  266. WHERE t.name = :str
  267. ORDER BY t.name ASC"
  268. )->setParameter('str', $string_search)
  269. ->getSingleScalarResult()
  270. ;
  271. $response = new Response(json_encode($tag_id));
  272. $response->headers->set('Content-Type', 'application/json; charset=utf-8');
  273. return $response;
  274. }
  275. throw $this->createNotFoundException('Cette ressource n\'est pas accessible');
  276. }
  277. /**
  278. * Retourne une réponse contenant le dom du formulaire de recherche global
  279. *
  280. * @return \Symfony\Component\HttpFoundation\Response
  281. */
  282. public function renderGlobalSearchFormAction()
  283. {
  284. return $this->render(
  285. 'MuzichCoreBundle:GlobalSearch:form.html.twig',
  286. array('form' => $this->getGlobalSearchForm()->createView())
  287. );
  288. }
  289. /**
  290. * Page d'affichage des résultats pour une recherche globale.
  291. * * Users
  292. * * Groups
  293. * * Partages
  294. *
  295. * @return \Symfony\Component\HttpFoundation\Response
  296. * @Template("MuzichCoreBundle:GlobalSearch:results.html.twig")
  297. */
  298. public function globalAction(Request $request)
  299. {
  300. $form = $this->getGlobalSearchForm($searcher = new GlobalSearcher());
  301. $results = array(
  302. 'users' => null,
  303. 'groups' => null,
  304. 'elements' => null
  305. );
  306. if ($request->getMethod() == 'POST')
  307. {
  308. $form->bind($request);
  309. if ($form->isValid())
  310. {
  311. $results = $searcher->getResults(
  312. $this->getDoctrine(),
  313. $this->getUserId(),
  314. $this->container->getParameter('search_default_count'),
  315. $this->container->getParameter('search_global_elements_word_min_length')
  316. );
  317. }
  318. }
  319. return array(
  320. 'form' => $form->createView(),
  321. 'results' => $results,
  322. 'display_more_button' => (count($results['elements']))? (count($results['elements']) >= $this->container->getParameter('search_default_count'))? true : false : false
  323. );
  324. }
  325. }