SearchController.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. class SearchController extends Controller
  10. {
  11. /**
  12. * Procédure qui construit un réponse json contenant le html
  13. * par defalt de la liste d'élément.
  14. *
  15. * @param Collection $elements
  16. * @param boolean $invertcolors
  17. * @param sring $message
  18. * @return Response
  19. */
  20. protected function searchElementsMore($elements, $invertcolors, $message)
  21. {
  22. $end = (($count = count($elements)) < $this->container->getParameter('search_ajax_more'));
  23. $html = '';
  24. if ($count)
  25. {
  26. $html = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
  27. 'user' => $this->getUser(),
  28. 'elements' => $elements,
  29. 'invertcolor' => $invertcolors
  30. ))->getContent();
  31. }
  32. return $this->jsonResponse(array(
  33. 'status' => 'success',
  34. 'count' => $count,
  35. 'message' => $message,
  36. 'html' => $html,
  37. 'end' => $end
  38. ));
  39. }
  40. /**
  41. * Procédure de recherche, qui met a jour l'objet de recherche (ainsi
  42. * que les paramétres en session).
  43. *
  44. */
  45. public function searchElementsAction($id_limit = null, $invertcolors = false)
  46. {
  47. if (($response = $this->mustBeConnected()))
  48. {
  49. return $response;
  50. }
  51. $request = $this->getRequest();
  52. $search_object = $this->getElementSearcher();
  53. $search_form = $this->getSearchForm($search_object);
  54. $form_submited = false;
  55. if ($request->getMethod() == 'POST')
  56. {
  57. $form_submited = true;
  58. $search_form->bindRequest($request);
  59. // Si le formulaire est valide
  60. if ($search_form->isValid())
  61. {
  62. // On met a jour l'objet avec les nouveaux paramétres saisie dans le form
  63. $data = $search_form->getData();
  64. // Le formulaire nous permet de récupérer uniquement les ids.
  65. // On va donc chercher les name en base pour le passer a l'objet
  66. // ElementSearch
  67. $data['tags'] = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
  68. ->getTagsForElementSearch(json_decode($data['tags'], true));
  69. $search_object->update($data);
  70. // Et on met a jour la "mémoire" de la recherche
  71. $this->setElementSearcherParams($search_object->getParams());
  72. }
  73. }
  74. if ($this->getRequest()->isXmlHttpRequest())
  75. {
  76. if ($form_submited)
  77. {
  78. $message = $this->trans(
  79. 'noelements.sentence_filter',
  80. array('%link_string%' => $this->trans(
  81. 'noelements.sentence_filter_link_string',
  82. array(),
  83. 'elements'
  84. )),
  85. 'elements'
  86. );
  87. }
  88. else
  89. {
  90. $message = $this->trans(
  91. 'elements.ajax.more.noelements',
  92. array(),
  93. 'elements'
  94. );
  95. }
  96. // template qui apelle doSearchElementsAction
  97. $search = $this->getElementSearcher();
  98. $search->update(array(
  99. 'count' => $this->container->getParameter('search_ajax_more'),
  100. 'id_limit' => $id_limit
  101. ));
  102. $elements = $search->getElements($this->getDoctrine(), $this->getUserId());
  103. return $this->searchElementsMore($elements, $invertcolors, $message);
  104. }
  105. else
  106. {
  107. return $this->redirect($this->generateUrl('home'));
  108. }
  109. }
  110. /**
  111. * Action (ajax) de récupération d'éléments en plus
  112. * [a check pour être sur] N'EST PLUS UTILISE
  113. *
  114. * @param string $type
  115. * @param string $object_id
  116. * @param int $id_limit
  117. * @param boolean $invertcolors
  118. * @return Response
  119. */
  120. public function searchElementsShowAction($type, $object_id, $id_limit, $invertcolors)
  121. {
  122. if ($this->getRequest()->isXmlHttpRequest())
  123. {
  124. $object = null;
  125. $param_id = '';
  126. if ($type == 'user')
  127. {
  128. $object = $this->getDoctrine()
  129. ->getRepository('MuzichCoreBundle:User')
  130. ->findOneBy(array('id' => $object_id))
  131. ;
  132. $param_id = 'user_id';
  133. }
  134. elseif ($type == 'group')
  135. {
  136. $object = $this->getDoctrine()
  137. ->getRepository('MuzichCoreBundle:Group')
  138. ->findOneById($object_id)
  139. ;
  140. $param_id = 'group_id';
  141. }
  142. if (!$object)
  143. {
  144. throw new \Exception('Object Unknow');
  145. }
  146. $search = $this->createSearchObject(array(
  147. $param_id => $object->getId(),
  148. 'count' => $this->container->getParameter('search_ajax_more'),
  149. 'id_limit' => $id_limit
  150. ));
  151. $elements = $search->getElements($this->getDoctrine(), $this->getUserId());
  152. return $this->searchElementsMore($elements, $invertcolors,
  153. $this->trans(
  154. 'elements.ajax.more.noelements',
  155. array(),
  156. 'elements'
  157. )
  158. );
  159. }
  160. throw new \Exception('XmlHttpRequest only for this action');
  161. }
  162. /**
  163. * Procédure (ajax) de recherche de tags. Essentielement utilisé dans
  164. * le tagPrompt.
  165. *
  166. * @param string $string_search
  167. * @param int $timestamp
  168. * @return Response
  169. */
  170. public function searchTagAction($string_search, $timestamp)
  171. {
  172. if (($response = $this->mustBeConnected()))
  173. {
  174. return $response;
  175. }
  176. if ($this->getRequest()->isXmlHttpRequest())
  177. {
  178. if (strlen(trim($string_search)) > 1)
  179. {
  180. // On utilise l'objet TagLike
  181. $TagLike = new TagLike($this->getDoctrine());
  182. // Pour trier nos tags d'une manière plus humaine
  183. $sort_response = $TagLike->getSimilarTags($string_search, $this->getUserId());
  184. $status = 'success';
  185. $error = '';
  186. $message = $this->trans(
  187. 'tags.search.message_found',
  188. array('%string%' => $string_search),
  189. 'userui'
  190. );
  191. }
  192. else
  193. {
  194. $status = 'error';
  195. $sort_response = array('tags' => array(), 'same_found' => false);
  196. $error = 'Vous devez saisir au moins deux caractères';
  197. $message = '';
  198. }
  199. $return_array = array(
  200. 'status' => $status,
  201. 'timestamp' => $timestamp,
  202. 'error' => $error,
  203. 'message' => $message,
  204. 'same_found' => $sort_response['same_found'],
  205. 'data' => $sort_response['tags']
  206. );
  207. $response = new Response(json_encode($return_array));
  208. $response->headers->set('Content-Type', 'application/json; charset=utf-8');
  209. return $response;
  210. }
  211. throw $this->createNotFoundException('Cette ressource n\'est pas accessible');
  212. }
  213. /**
  214. * Récupére l'id d'un tag (ajax)
  215. * [A check] mais ne doit et n'est plus utilisé.
  216. *
  217. * @param type $string_search
  218. * @return Response
  219. */
  220. public function searchTagIdAction($string_search)
  221. {
  222. if ($this->getRequest()->isXmlHttpRequest())
  223. {
  224. $tag_id = $this->getDoctrine()->getEntityManager()->createQuery("
  225. SELECT t.id FROM MuzichCoreBundle:Tag t
  226. WHERE t.name = :str
  227. ORDER BY t.name ASC"
  228. )->setParameter('str', $string_search)
  229. ->getSingleScalarResult()
  230. ;
  231. $response = new Response(json_encode($tag_id));
  232. $response->headers->set('Content-Type', 'application/json; charset=utf-8');
  233. return $response;
  234. }
  235. throw $this->createNotFoundException('Cette ressource n\'est pas accessible');
  236. }
  237. }