SearchController.php 10.0KB

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