SearchController.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. class SearchController extends Controller
  9. {
  10. protected function searchElementsMore($elements, $invertcolors, $message)
  11. {
  12. $end = (($count = count($elements)) < $this->container->getParameter('search_ajax_more'));
  13. $html = '';
  14. if ($count)
  15. {
  16. $html = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
  17. 'user' => $this->getUser(),
  18. 'elements' => $elements,
  19. 'invertcolor' => $invertcolors
  20. ))->getContent();
  21. }
  22. return $this->jsonResponse(array(
  23. 'count' => $count,
  24. 'message' => $message,
  25. 'html' => $html,
  26. 'end' => $end
  27. ));
  28. }
  29. /**
  30. * Procédure de recherche, qui met a jour l'objet de recherche (ainsi
  31. * que les paramétres en session).
  32. *
  33. */
  34. public function searchElementsAction($id_limit = null, $invertcolors = false)
  35. {
  36. if ($this->getUser() == 'anon.')
  37. {
  38. if ($this->getRequest()->isXmlHttpRequest())
  39. {
  40. return $this->jsonResponse(array(
  41. 'status' => 'mustbeconnected'
  42. ));
  43. }
  44. else
  45. {
  46. return $this->redirect($this->generateUrl('index'));
  47. }
  48. }
  49. $request = $this->getRequest();
  50. $search_object = $this->getElementSearcher();
  51. $search_form = $this->getSearchForm($search_object);
  52. $form_submited = false;
  53. if ($request->getMethod() == 'POST')
  54. {
  55. $form_submited = true;
  56. $search_form->bindRequest($request);
  57. // Si le formulaire est valide
  58. if ($search_form->isValid())
  59. {
  60. // On met a jour l'objet avec les nouveaux paramétres saisie dans le form
  61. $data = $search_form->getData();
  62. // Le formulaire nous permet de récupérer uniquement les ids.
  63. // On va donc chercher les name en base pour le passer a l'objet
  64. // ElementSearch
  65. $data['tags'] = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  66. ->getTagsForElementSearch(json_decode($data['tags'], true));
  67. $search_object->update($data);
  68. // Et on met a jour la "mémoire" de la recherche
  69. $this->setElementSearcherParams($search_object->getParams());
  70. }
  71. }
  72. if ($this->getRequest()->isXmlHttpRequest())
  73. {
  74. if ($form_submited)
  75. {
  76. $message = $this->trans(
  77. 'noelements.sentence_filter',
  78. array('%link_string%' => $this->trans(
  79. 'noelements.sentence_filter_link_string',
  80. array(),
  81. 'elements'
  82. )),
  83. 'elements'
  84. );
  85. }
  86. else
  87. {
  88. $message = $this->trans(
  89. 'elements.ajax.more.noelements',
  90. array(),
  91. 'elements'
  92. );
  93. }
  94. // template qui apelle doSearchElementsAction
  95. $search = $this->getElementSearcher();
  96. $search->update(array(
  97. 'count' => $this->container->getParameter('search_ajax_more'),
  98. 'id_limit' => $id_limit
  99. ));
  100. $elements = $search->getElements($this->getDoctrine(), $this->getUserId());
  101. return $this->searchElementsMore($elements, $invertcolors, $message);
  102. }
  103. else
  104. {
  105. return $this->redirect($this->generateUrl('home'));
  106. }
  107. }
  108. public function searchElementsShowAction($type, $object_id, $id_limit, $invertcolors)
  109. {
  110. if ($this->getRequest()->isXmlHttpRequest())
  111. {
  112. $object = null;
  113. $param_id = '';
  114. if ($type == 'user')
  115. {
  116. $object = $this->getDoctrine()
  117. ->getRepository('MuzichCoreBundle:User')
  118. ->findOneBy(array('id' => $object_id))
  119. ;
  120. $param_id = 'user_id';
  121. }
  122. elseif ($type == 'group')
  123. {
  124. $object = $this->getDoctrine()
  125. ->getRepository('MuzichCoreBundle:Group')
  126. ->findOneById($object_id)
  127. ;
  128. $param_id = 'group_id';
  129. }
  130. if (!$object)
  131. {
  132. throw new \Exception('Object Unknow');
  133. }
  134. $search = $this->createSearchObject(array(
  135. $param_id => $object->getId(),
  136. 'count' => $this->container->getParameter('search_ajax_more'),
  137. 'id_limit' => $id_limit
  138. ));
  139. $elements = $search->getElements($this->getDoctrine(), $this->getUserId());
  140. return $this->searchElementsMore($elements, $invertcolors,
  141. $this->trans(
  142. 'elements.ajax.more.noelements',
  143. array(),
  144. 'elements'
  145. )
  146. );
  147. }
  148. throw new \Exception('XmlHttpRequest only for this action');
  149. }
  150. protected function sort_search_tags($tags, $search)
  151. {
  152. $tag_sorted = $tags;
  153. foreach ($tags as $i => $tag)
  154. {
  155. // Pas plus de trois caractères en plus de la recherche
  156. foreach (explode(' ', $search) as $word)
  157. {
  158. if (strlen(str_replace(strtoupper($word), '', strtoupper($tag['name']))) < 4)
  159. {
  160. unset($tag_sorted[$i]);
  161. $tag_sorted = array_merge(array($tag), $tag_sorted);
  162. }
  163. }
  164. }
  165. $tags = $tag_sorted;
  166. foreach ($tags as $i => $tag)
  167. {
  168. // Chaine de caractère identique
  169. foreach (explode(' ', $search) as $word)
  170. {
  171. if (strtoupper($word) == strtoupper($tag['name']))
  172. {
  173. unset($tag_sorted[$i]);
  174. $tag_sorted = array_merge(array($tag), $tag_sorted);
  175. }
  176. }
  177. }
  178. return $tag_sorted;
  179. }
  180. /**
  181. *
  182. * @param string $string_search
  183. */
  184. public function searchTagAction($string_search, $timestamp)
  185. {
  186. if ($this->getUser() == 'anon.')
  187. {
  188. if ($this->getRequest()->isXmlHttpRequest())
  189. {
  190. return $this->jsonResponse(array(
  191. 'status' => 'mustbeconnected'
  192. ));
  193. }
  194. else
  195. {
  196. return $this->redirect($this->generateUrl('index'));
  197. }
  198. }
  199. if ($this->getRequest()->isXmlHttpRequest())
  200. {
  201. if (strlen($string_search) > 1)
  202. {
  203. $words = explode(' ', $string_search);
  204. $where = '';
  205. $params = array();
  206. foreach ($words as $i => $word)
  207. {
  208. if ($where == '')
  209. {
  210. $where .= 'WHERE UPPER(t.name) LIKE :str'.$i;
  211. }
  212. else
  213. {
  214. $where .= ' OR UPPER(t.name) LIKE :str'.$i;
  215. }
  216. $params['str'.$i] = '%'.strtoupper($word).'%';
  217. }
  218. $tags = $this->getDoctrine()->getEntityManager()->createQuery("
  219. SELECT t.name, t.id FROM MuzichCoreBundle:Tag t
  220. $where
  221. ORDER BY t.name ASC"
  222. )->setParameters($params)
  223. ->getScalarResult()
  224. ;
  225. $tags_response = array();
  226. foreach ($tags as $tag)
  227. {
  228. $tags_response[] = array('name' => $tag['name'], 'id' => $tag['id']);
  229. }
  230. $tags_response = $this->sort_search_tags($tags_response, $string_search);
  231. $status = 'success';
  232. $error = '';
  233. }
  234. else
  235. {
  236. $status = 'error';
  237. $tags_response = array();
  238. $error = 'Vous devez saisir au moins deux caractères';
  239. }
  240. $return_array = array(
  241. 'status' => $status,
  242. 'timestamp' => $timestamp,
  243. 'error' => $error,
  244. 'data' => $tags_response
  245. );
  246. $response = new Response(json_encode($return_array));
  247. $response->headers->set('Content-Type', 'application/json; charset=utf-8');
  248. return $response;
  249. }
  250. throw $this->createNotFoundException('Cette ressource n\'est pas accessible');
  251. }
  252. /**
  253. *
  254. * @param type $string_search
  255. * @return Response
  256. */
  257. public function searchTagIdAction($string_search)
  258. {
  259. if ($this->getRequest()->isXmlHttpRequest())
  260. {
  261. $tag_id = $this->getDoctrine()->getEntityManager()->createQuery("
  262. SELECT t.id FROM MuzichCoreBundle:Tag t
  263. WHERE t.name = :str
  264. ORDER BY t.name ASC"
  265. )->setParameter('str', $string_search)
  266. ->getSingleScalarResult()
  267. ;
  268. $response = new Response(json_encode($tag_id));
  269. $response->headers->set('Content-Type', 'application/json; charset=utf-8');
  270. return $response;
  271. }
  272. throw $this->createNotFoundException('Cette ressource n\'est pas accessible');
  273. }
  274. }