Example #1
0
 /**
  * @param array $params
  *
  * @return array
  */
 public function getResults(array $params)
 {
     $locale = $this->context->get('language');
     $brand = $this->context->get('brand');
     $brands = array_keys($this->context->getValues('brand'));
     $results = array();
     $params = array_merge(array('term' => '', 'method' => 'lieu'), $params);
     if ('lieu' == ($method = $params['method'])) {
         $params['term'] = '';
     }
     $term = $params['term'];
     $localizedNameField = sprintf('names.%s', $locale);
     if (!in_array($method, array('lieu', 'location'))) {
         $queryBrands = array($brand);
         $brandTerms = array();
         if ('seh' == $brand) {
             $queryBrands = array_diff($brands, $queryBrands);
         }
         foreach ($queryBrands as $queryBrand) {
             $brandTerms[] = array('term' => array('brandSlug' => $queryBrand));
         }
         $queryArray = array('query' => array('filtered' => array('query' => array('match' => array('name' => array('query' => $term, 'operator' => 'and'))), 'filter' => array('bool' => array('should' => $brandTerms)))), 'sort' => array('name' => 'asc'));
         $query = new Query();
         $query->setParams($queryArray);
         $hotels = $this->hotelType->search($query);
         /** @var Result $hotel */
         foreach ($hotels as $hotel) {
             $hotel = $hotel->getSource();
             if ($hotel['active']) {
                 $result = array('brand' => $hotel['brandName'], 'brandId' => strtolower($hotel['brandSlug']), 'label' => (isset($params['hotel_name_brand']) and $params['hotel_name_brand']) ? sprintf('%s %s', $hotel['brandName'], $hotel['name']) : $hotel['name'], 'typeResult' => 'hotel', 'id' => $hotel['id'], 'activeLocationId' => $hotel['cityId'], 'nbResults' => '', 'category' => $this->translator->trans('form.search.autocomplete.hotels'), 'cssClass' => 'lvl1', 'latLng' => $hotel['latLng']);
                 if ($method == 'lieu') {
                     $result['type'] = 'lieu';
                 }
                 $results[] = $result;
             }
         }
     }
     if (!in_array($method, array('lieu', 'hotel'))) {
         $queryBrands = array($brand);
         $brandTerms = array();
         if ('seh' == $brand) {
             $queryBrands = array_diff($brands, $queryBrands);
         }
         foreach ($queryBrands as $queryBrand) {
             $brandTerms[] = array('range' => array($queryBrand => array('gt' => 0)));
         }
         $queryArray = array('query' => array('filtered' => array('query' => array('match' => array($localizedNameField => array('query' => $term, 'operator' => 'and'))), 'filter' => array('bool' => array('should' => $brandTerms)))), 'sort' => array($localizedNameField => 'asc'));
         $query = new Query();
         $query->setParams($queryArray);
         $cities = $this->cityType->search($query);
         /** @var Result $city */
         foreach ($cities as $city) {
             $city = $city->getSource();
             $results[] = array('label' => $city['names'][$locale], 'typeResult' => 'city', 'id' => $city['id'], 'activeLocationId' => sprintf('city%s', $city['id']), 'nbResults' => '', 'category' => $this->translator->trans('form.search.autocomplete.cities'), 'cssClass' => 'lvl1');
         }
     }
     if (!in_array($method, array('hotel', 'location'))) {
         $queryBrands = array($brand);
         $brandTerms = array();
         if ('seh' == $brand) {
             $queryBrands = array_diff($brands, $queryBrands);
         }
         foreach ($queryBrands as $queryBrand) {
             $brandTerms[] = array('range' => array($queryBrand => array('gt' => 0)));
         }
         $queryArray = array('query' => array('filtered' => array('query' => array('match' => array($localizedNameField => array('query' => $term, 'operator' => 'and'))), 'filter' => array('bool' => array('should' => $brandTerms)))), 'sort' => array($localizedNameField => 'asc'));
         $query = new Query();
         $query->setParams($queryArray);
         $departments = $this->departmentType->search($query);
         /** @var Result $department */
         foreach ($departments as $department) {
             $department = $department->getSource();
             $cityIds = $department['citiesIds'];
             $cityIds = array_map(function ($cityId) {
                 return sprintf('city%s', $cityId);
             }, $cityIds);
             $result = array('label' => $department['names'][$locale], 'typeResult' => 'department', 'id' => $department['id'], 'activeLocationId' => implode(',', $cityIds), 'nbResults' => '', 'category' => $this->translator->trans('form.search.autocomplete.departments'), 'cssClass' => 'lvl1');
             if ($method == 'lieu') {
                 $result['type'] = 'lieu';
                 $result['cssClass'] = 'lvl3';
             }
             $results[] = $result;
         }
     }
     if (!in_array($method, array('hotel'))) {
         $queryBrands = array($brand);
         $brandTerms = array();
         if ('seh' == $brand) {
             $queryBrands = array_diff($brands, $queryBrands);
         }
         foreach ($queryBrands as $queryBrand) {
             $brandTerms[] = array('range' => array($queryBrand => array('gt' => 0)));
         }
         $queryArray = array('query' => array('filtered' => array('query' => array('match' => array($localizedNameField => array('query' => $term, 'operator' => 'and'))), 'filter' => array('bool' => array('should' => $brandTerms)))), 'sort' => array($localizedNameField => 'asc'));
         $query = new Query();
         $query->setParams($queryArray);
         $regions = $this->regionType->search($query);
         /** @var Result $region */
         foreach ($regions as $region) {
             $region = $region->getSource();
             $result = array('label' => $region['names'][$locale], 'typeResult' => 'region', 'id' => $region['id'], 'activeLocationId' => sprintf('region%s', $region['id']), 'nbResults' => '', 'category' => $this->translator->trans('form.search.autocomplete.regions'), 'cssClass' => 'lvl1');
             if ($method == 'lieu') {
                 $result['type'] = 'lieu';
                 $result['cssClass'] = 'lvl2';
             }
             $results[] = $result;
         }
     }
     if (!in_array($method, array('hotel'))) {
         $queryBrands = array($brand);
         $brandTerms = array();
         if ('seh' == $brand) {
             $queryBrands = array_diff($brands, $queryBrands);
         }
         foreach ($queryBrands as $queryBrand) {
             $brandTerms[] = array('range' => array($queryBrand => array('gt' => 0)));
         }
         $queryArray = array('query' => array('filtered' => array('query' => array('match' => array($localizedNameField => array('query' => $term, 'operator' => 'and'))), 'filter' => array('bool' => array('should' => $brandTerms)))), 'sort' => array($localizedNameField => 'asc'));
         $query = new Query();
         $query->setParams($queryArray);
         $countries = $this->countryType->search($query);
         /** @var Result $country */
         foreach ($countries as $country) {
             $country = $country->getSource();
             $result = array('label' => $country['names'][$locale], 'typeResult' => 'country', 'id' => $country['id'], 'activeLocationId' => sprintf('country%s', $country['id']), 'nbResults' => '', 'category' => $this->translator->trans('form.search.autocomplete.countries'), 'cssClass' => 'lvl1');
             if ($method == 'lieu') {
                 $result['type'] = 'lieu';
             }
             $results[] = $result;
         }
     }
     return $results;
 }