GlobalSearcher.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Muzich\CoreBundle\Searcher;
  3. use Symfony\Component\Validator\Constraints as Assert;
  4. use Doctrine\Bundle\DoctrineBundle\Registry;
  5. use Muzich\CoreBundle\Searcher\UserAndGroupSearcher;
  6. use Muzich\CoreBundle\Searcher\ElementSearcher;
  7. /**
  8. *
  9. */
  10. class GlobalSearcher extends Searcher implements SearcherInterface
  11. {
  12. /**
  13. * Chaine de caractère représentant la recherche.
  14. *
  15. * @var string
  16. * @Assert\NotBlank()
  17. * @Assert\Type("string")
  18. * @Assert\Length(min = 3)
  19. */
  20. protected $string;
  21. public function setString($string)
  22. {
  23. $this->string = $string;
  24. }
  25. public function getString()
  26. {
  27. return $this->string;
  28. }
  29. /**
  30. * @see SearcherInterface
  31. *
  32. * @return array
  33. */
  34. public function getParams()
  35. {
  36. return array(
  37. 'string' => $this->string
  38. );
  39. }
  40. /**
  41. * Retourne les user et groupes correspondant a la recherche
  42. *
  43. * @param Registry $doctrine
  44. * @return array
  45. */
  46. public function getResults(Registry $doctrine, $user_id, $search_elements_count, $min_word_length = null)
  47. {
  48. // On remplace le caratcère '%' au cas ou un malin l'insére.
  49. $string = str_replace('%', '#', $this->string);
  50. // instancier objet SearchUser and groups;
  51. $ugs = new UserAndGroupSearcher();
  52. $ugs->setString($this->string);
  53. // puis on fait recherche sur elements
  54. $es = new ElementSearcher();
  55. $es->init(array(
  56. 'string' => $string,
  57. 'count' => $search_elements_count
  58. ));
  59. $results = $ugs->getResults($doctrine);
  60. $results['elements'] = $es->getElements(
  61. $doctrine,
  62. $user_id,
  63. 'execute',
  64. array('word_min_length' => $min_word_length)
  65. );
  66. return $results;
  67. }
  68. }