SearchController.php 7.1KB

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