diff --git a/Resources/config/services/search.yml b/Resources/config/services/search.yml index b0da6c7f2..8b4db1373 100644 --- a/Resources/config/services/search.yml +++ b/Resources/config/services/search.yml @@ -17,6 +17,7 @@ services: - "@security.token_storage" - "@chill.main.security.authorization.helper" - "@chill_main.paginator_factory" + - '@chill.person.search_person' calls: - ['setContainer', ["@service_container"]] tags: diff --git a/Search/PersonSearch.php b/Search/PersonSearch.php index e94c28079..6d562ec95 100644 --- a/Search/PersonSearch.php +++ b/Search/PersonSearch.php @@ -201,7 +201,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, * @param array $terms * @return \Doctrine\ORM\QueryBuilder */ - protected function createQuery(array $terms) + public function createQuery(array $terms) { //get from cache $cacheKey = md5(serialize($terms)); diff --git a/Search/SimilarityPersonSearch.php b/Search/SimilarityPersonSearch.php index 9aac92a88..13c044592 100644 --- a/Search/SimilarityPersonSearch.php +++ b/Search/SimilarityPersonSearch.php @@ -50,6 +50,12 @@ class SimilarityPersonSearch extends AbstractSearch const NAME = "person_similarity"; + /** + * + * @var PersonSearch + */ + private $personSearch; + /** * SimilarityPersonSearch constructor. @@ -58,17 +64,20 @@ class SimilarityPersonSearch extends AbstractSearch * @param TokenStorage $tokenStorage * @param AuthorizationHelper $helper * @param PaginatorFactory $paginatorFactory + * @param PersonSearch $personSearch */ public function __construct( EntityManagerInterface $em, TokenStorage $tokenStorage, AuthorizationHelper $helper, - PaginatorFactory $paginatorFactory) + PaginatorFactory $paginatorFactory, + PersonSearch $personSearch) { $this->em = $em; $this->user = $tokenStorage->getToken()->getUser(); $this->helper = $helper; $this->paginatorFactory = $paginatorFactory; + $this->personSearch = $personSearch; // throw an error if user is not a valid user if (!$this->user instanceof \Chill\MainBundle\Entity\User) { @@ -218,28 +227,42 @@ class SimilarityPersonSearch extends AbstractSearch $qb = $this->em->createQueryBuilder(); - $qb->from('ChillPersonBundle:Person', 'p'); + $qb->from('ChillPersonBundle:Person', 'simi'); if ($terms['_default'] !== '') { $grams = explode(' ', $terms['_default']); foreach($grams as $key => $gram) { - $qb->andWhere( - 'SIMILARITY(p.fullnameCanonical, UNACCENT(LOWER(:default_'.$key.'))) >= 0.15') + $qb->andWhere('SIMILARITY(simi.fullnameCanonical, UNACCENT(LOWER(:default_'.$key.')) ) >= 0.15') ->setParameter('default_'.$key, '%'.$gram.'%'); } + + /// + //dump($this->personSearch->createQuery($terms)->addSelect('p.id')->getDQL()); + + $qb->andWhere($qb->expr() + ->notIn( + 'simi.id', + $this->personSearch->createQuery($terms) + ->select('p.id') + ->getDQL() + ) + ); + + /// } //restraint center for security $reachableCenters = $this->helper->getReachableCenters($this->user, new Role('CHILL_PERSON_SEE')); $qb->andWhere($qb->expr() - ->in('p.center', ':centers')) + ->in('simi.center', ':centers')) ->setParameter('centers', $reachableCenters) ; $this->_cacheQuery[$cacheKey] = $qb; + dump($qb->getDQL()); return clone $qb; }