SearchController.php 10KB

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