/**
  * Compile SearchParameterBag
  * 
  * @param SearchParameterBag $parameterBag
  * @return \HealthCareAbroad\SearchBundle\Services\CompiledSearchParameter
  */
 public function compile(SearchParameterBag $parameterBag)
 {
     $urlGenerator = new SearchUrlGenerator();
     $variables = $this->_extractVariables($parameterBag);
     $searchState = 0;
     // search form parameter to search url route key mapping
     $searchUrlParameterKeyMapping = SearchUrlGenerator::getSearchParameterKeyToSearchUrlKeyMapping();
     // parameter key to search state value mapping
     $searchStateMapping = SearchStates::getSearchParameterKeyMappingToSearchStateValues();
     // get the current state of the search based on current variables, prepare also the url parameters for url generator
     foreach ($variables as $key => $variableObj) {
         // add the state value for this key
         $searchState += $searchStateMapping[$key];
         // map to the route key
         $searchUrlKey = \array_key_exists($key, $searchUrlParameterKeyMapping) ? $searchUrlParameterKeyMapping[$key] : $key;
         // TODO: this will fail if slug will not be used as a URL parameter value
         if (\method_exists($variableObj, 'getSlug')) {
             $urlGenerator->addParameter($searchUrlKey, $variableObj->getSlug());
         }
     }
     // route to state mapping
     $routeToStateMapping = SearchStates::getStateToRouteMapping();
     $routeName = $routeToStateMapping[SearchStates::getSearchStateFromValue($searchState)];
     $url = $urlGenerator->generateByRouteName($routeName, false);
     // generate the url
     $compiledSearch = new CompiledSearchParameter($variables, $url, $searchState);
     return $compiledSearch;
 }
 /**
  * Create a PageMetaConfiguration based on the current search objects
  * 
  * @param array $searchObjects
  * @return \HealthCareAbroad\HelperBundle\Entity\PageMetaConfiguration
  */
 public function buildFromSearchObjects(array $searchObjects = array())
 {
     $searchState = 0;
     $searchStateMapping = SearchStates::getSearchUrlParameterKeyToSearchStateValueMapping();
     $pageMetaVariables = array();
     foreach ($searchObjects as $key => $_searchObject) {
         $searchState += $searchStateMapping[$key];
         $pageMetaVariables[$key] = \is_object($_searchObject) ? $_searchObject->getName() : $_searchObject;
     }
     $searchStateValue = SearchStates::getSearchStateFromValue($searchState);
     $title = '';
     $description = '';
     $keywords = '';
     switch ($searchStateValue) {
         // singe searches - destination
         case SearchStates::COUNTRY_SEARCH:
         case SearchStates::CITY_SEARCH:
             $destinationArr = $this->_extractDestinationValues($pageMetaVariables);
             $destination = \implode(', ', $destinationArr);
             $title = 'Healthcare and Dental Clinics in ' . $destination . ' - ' . $this->siteName;
             $description = 'Compare {' . PageMetaConfigurationService::CLINIC_RESULTS_COUNT_VARIABLE . '} clinics that offer medical specialties in ' . $destination . '. ' . $this->siteName . ' is an unbiased international directory of Healthcare and Dental Clinics';
             $keywords = $destination . ', Medical tourism, compare, international, abroad, doctor, dr, dentist';
             break;
             // single searches - specialization
         // single searches - specialization
         case SearchStates::SPECIALIZATION_SEARCH:
             $title = $pageMetaVariables[SearchUrlGenerator::SEARCH_URL_PARAMETER_SPECIALIZATION] . ' - ' . $this->siteName;
             $description = 'Compare {' . PageMetaConfigurationService::CLINIC_RESULTS_COUNT_VARIABLE . '} clinics that offer ' . $pageMetaVariables[SearchUrlGenerator::SEARCH_URL_PARAMETER_SPECIALIZATION] . ' in different countries. ' . $this->siteName . ' is an unbiased international directory of Healthcare and Dental Clinics';
             $keywords = $pageMetaVariables[SearchUrlGenerator::SEARCH_URL_PARAMETER_SPECIALIZATION] . ', Medical tourism, compare, international, abroad, doctor, dr, dentist';
             break;
             // single searches - Sub-specialization
         // single searches - Sub-specialization
         case SearchStates::SUB_SPECIALIZATION_SEARCH:
             $treatmentsArr = $this->_extractTreatmentValues($pageMetaVariables);
             $title = \implode(' - ', $treatmentsArr) . ' - ' . $this->siteName;
             $description = 'Compare {' . PageMetaConfigurationService::CLINIC_RESULTS_COUNT_VARIABLE . '} clinics that offer ' . $pageMetaVariables[SearchUrlGenerator::SEARCH_URL_PARAMETER_SUB_SPECIALIZATION] . ' in different countries. ' . $this->siteName . ' is an unbiased international directory of Healthcare and Dental Clinics';
             $keywords = \implode(', ', $treatmentsArr) . ', Medical tourism, compare, international, abroad, doctor, dr, dentist';
             break;
             // single searches - treatment
         // single searches - treatment
         case SearchStates::TREATMENT_SEARCH:
             $treatmentsArr = $this->_extractTreatmentValues($pageMetaVariables);
             $title = \implode(' - ', $treatmentsArr) . ' - ' . $this->siteName;
             $description = 'Compare {' . PageMetaConfigurationService::CLINIC_RESULTS_COUNT_VARIABLE . '} ' . $pageMetaVariables[SearchUrlGenerator::SEARCH_URL_PARAMETER_TREATMENT] . ' clinics in different countries. ' . $this->siteName . ' is an unbiased international directory of Healthcare and Dental Clinics';
             $keywords = \implode(', ', $treatmentsArr) . ', Medical tourism, compare, international, abroad, doctor, dr, dentist';
             break;
             // combination search - destination + specialization
         // combination search - destination + specialization
         case SearchStates::COUNTRY_SPECIALIZATION_SEARCH:
         case SearchStates::CITY_SPECIALIZATION_SEARCH:
             $destinationArr = $this->_extractDestinationValues($pageMetaVariables);
             $destination = \implode(', ', $destinationArr);
             $title = $pageMetaVariables[SearchUrlGenerator::SEARCH_URL_PARAMETER_SPECIALIZATION] . ' in ' . $destination . ' - ' . $this->siteName;
             $description = 'Compare {' . PageMetaConfigurationService::CLINIC_RESULTS_COUNT_VARIABLE . '} clinics that offer ' . $pageMetaVariables[SearchUrlGenerator::SEARCH_URL_PARAMETER_SPECIALIZATION] . ' in ' . $destination . '. ' . $this->siteName . ' is an unbiased international directory of Healthcare and Dental Clinics';
             $keywords = $destination . ', ' . $pageMetaVariables[SearchUrlGenerator::SEARCH_URL_PARAMETER_SPECIALIZATION] . ', Medical tourism, compare, international, abroad, doctor, dr, dentist';
             break;
         case SearchStates::COUNTRY_SUB_SPECIALIZATION_SEARCH:
         case SearchStates::CITY_SUB_SPECIALIZATION_SEARCH:
             $destinationArr = $this->_extractDestinationValues($pageMetaVariables);
             $treatmentsArr = $this->_extractTreatmentValues($pageMetaVariables);
             $destination = \implode(', ', $destinationArr);
             $title = \implode(' - ', $treatmentsArr) . ' in ' . $destination . ' - ' . $this->siteName;
             $description = 'Compare {' . PageMetaConfigurationService::CLINIC_RESULTS_COUNT_VARIABLE . '} clinics that offer ' . $pageMetaVariables[SearchUrlGenerator::SEARCH_URL_PARAMETER_SUB_SPECIALIZATION] . ' in ' . $destination . '. ' . $this->siteName . ' is an unbiased international directory of Healthcare and Dental Clinics';
             $keywords = $destination . ', ' . \implode(', ', $treatmentsArr) . ', Medical tourism, compare, international, abroad, doctor, dr, dentist';
             break;
         case SearchStates::COUNTRY_TREATMENT_SEARCH:
         case SearchStates::CITY_TREATMENT_SEARCH:
             $destinationArr = $this->_extractDestinationValues($pageMetaVariables);
             $treatmentsArr = $this->_extractTreatmentValues($pageMetaVariables);
             $destination = \implode(', ', $destinationArr);
             $title = \implode(' - ', $treatmentsArr) . ' in ' . $destination . ' - ' . $this->siteName;
             $description = 'Compare {' . PageMetaConfigurationService::CLINIC_RESULTS_COUNT_VARIABLE . '} ' . $pageMetaVariables[SearchUrlGenerator::SEARCH_URL_PARAMETER_TREATMENT] . ' clinics in ' . $destination . '. ' . $this->siteName . ' is an unbiased international directory of Healthcare and Dental Clinics';
             $keywords = $destination . ', ' . \implode(', ', $treatmentsArr) . ', Medical tourism, compare, international, abroad, doctor, dr, dentist';
             break;
     }
     $metaConfig = new PageMetaConfiguration();
     $metaConfig->setTitle($title);
     $metaConfig->setDescription($description);
     $metaConfig->setKeywords($keywords);
     $metaConfig->setPageType(PageMetaConfiguration::PAGE_TYPE_SEARCH_RESULTS);
     return $metaConfig;
 }
 /**
  * Mimic frontend search process and get results for ranking management
  * 
  * @param Request $request
  * @return \Symfony\Component\HttpFoundation\Response
  * @author acgvelarde
  */
 public function processSearchAction(Request $request)
 {
     $searchParameterService = $this->get('services.search.parameters');
     $compiledSearch = $searchParameterService->compileRequest($request);
     $searchStateLabel = SearchStates::getSearchStateFromValue($compiledSearch->getSearchState());
     $searchVariables = $compiledSearch->getVariables();
     $searchService = $this->get('services.search');
     // flag on what ranking point the search service is using
     $isUsingInstitutionRankingPoints = false;
     switch ($searchStateLabel) {
         // treatments only search
         case SearchStates::SPECIALIZATION_SEARCH:
             $specialization = $searchVariables[SearchParameterService::PARAMETER_KEY_SPECIALIZATION_ID];
             $results = $searchService->searchBySpecialization($specialization);
             $isUsingInstitutionRankingPoints = false;
             // uses clinic ranking points
             break;
         case SearchStates::SUB_SPECIALIZATION_SEARCH:
             $subSpecialization = $searchVariables[SearchParameterService::PARAMETER_KEY_SUB_SPECIALIZATION_ID];
             $results = $searchService->searchBySubSpecialization($subSpecialization);
             $isUsingInstitutionRankingPoints = false;
             // uses clinic ranking points
             break;
         case SearchStates::TREATMENT_SEARCH:
             $isUsingInstitutionRankingPoints = false;
             // uses clinic ranking points
             $treatment = $searchVariables[SearchParameterService::PARAMETER_KEY_TREATMENT_ID];
             $results = $searchService->searchByTreatment($treatment);
             break;
             // destinations only search
         // destinations only search
         case SearchStates::COUNTRY_SEARCH:
             $isUsingInstitutionRankingPoints = true;
             // uses hospital ranking points
             $country = $searchVariables[SearchParameterService::PARAMETER_KEY_COUNTRY_ID];
             $results = $searchService->searchByCountry($country);
             break;
         case SearchStates::CITY_SEARCH:
             $city = $searchVariables[SearchParameterService::PARAMETER_KEY_CITY_ID];
             $results = $searchService->searchByCity($city);
             $isUsingInstitutionRankingPoints = true;
             // uses hospital ranking points
             break;
             // combination search
         // combination search
         case SearchStates::COUNTRY_SPECIALIZATION_SEARCH:
             $isUsingInstitutionRankingPoints = false;
             $specialization = $searchVariables[SearchParameterService::PARAMETER_KEY_SPECIALIZATION_ID];
             $country = $searchVariables[SearchParameterService::PARAMETER_KEY_COUNTRY_ID];
             $results = $this->getDoctrine()->getManager()->getRepository('TermBundle:SearchTerm')->findByFilters(array($specialization, $country));
             break;
         case SearchStates::COUNTRY_SUB_SPECIALIZATION_SEARCH:
             $isUsingInstitutionRankingPoints = false;
             $subSpecialization = $searchVariables[SearchParameterService::PARAMETER_KEY_SUB_SPECIALIZATION_ID];
             $country = $searchVariables[SearchParameterService::PARAMETER_KEY_COUNTRY_ID];
             $results = $this->getDoctrine()->getManager()->getRepository('TermBundle:SearchTerm')->findByFilters(array($subSpecialization, $country));
             break;
         case SearchStates::COUNTRY_TREATMENT_SEARCH:
             $isUsingInstitutionRankingPoints = false;
             $treatment = $searchVariables[SearchParameterService::PARAMETER_KEY_TREATMENT_ID];
             $country = $searchVariables[SearchParameterService::PARAMETER_KEY_COUNTRY_ID];
             $results = $this->getDoctrine()->getManager()->getRepository('TermBundle:SearchTerm')->findByFilters(array($treatment, $country));
             break;
         case SearchStates::CITY_SPECIALIZATION_SEARCH:
             $isUsingInstitutionRankingPoints = false;
             $city = $searchVariables[SearchParameterService::PARAMETER_KEY_CITY_ID];
             $specialization = $searchVariables[SearchParameterService::PARAMETER_KEY_SPECIALIZATION_ID];
             $results = $this->getDoctrine()->getManager()->getRepository('TermBundle:SearchTerm')->findByFilters(array($specialization, $city));
             break;
         case SearchStates::CITY_SUB_SPECIALIZATION_SEARCH:
             $isUsingInstitutionRankingPoints = false;
             $subSpecialization = $searchVariables[SearchParameterService::PARAMETER_KEY_SUB_SPECIALIZATION_ID];
             $city = $searchVariables[SearchParameterService::PARAMETER_KEY_CITY_ID];
             $results = $this->getDoctrine()->getManager()->getRepository('TermBundle:SearchTerm')->findByFilters(array($subSpecialization, $city));
             break;
         case SearchStates::CITY_TREATMENT_SEARCH:
             $isUsingInstitutionRankingPoints = false;
             $city = $searchVariables[SearchParameterService::PARAMETER_KEY_CITY_ID];
             $treatment = $searchVariables[SearchParameterService::PARAMETER_KEY_TREATMENT_ID];
             $results = $this->getDoctrine()->getManager()->getRepository('TermBundle:SearchTerm')->findByFilters(array($treatment, $city));
             break;
     }
     //TODO: for now, we only need the first page for ranking purposes
     $pagerAdapter = new ArrayAdapter($results);
     $pager = new Pager($pagerAdapter, array('page' => 1, 'limit' => 20));
     // we compose the response data
     $responseData = array('results' => array(), 'totalNumberOfResults' => \count($results), 'searchState' => $searchStateLabel);
     foreach ($pager->getResults() as $searchTerm) {
         if ($searchTerm instanceof SearchTerm) {
             $institution = $searchTerm->getInstitution();
             $institutionMedicalCenter = $searchTerm->getInstitutionMedicalCenter();
             $arr = array('institution' => array('id' => $institution->getId(), 'name' => $institution->getName(), 'totalClinicRankingPoints' => $institution->getTotalClinicRankingPoints() ? $institution->getTotalClinicRankingPoints() : 0), 'institutionMedicalCenter' => array('id' => $institutionMedicalCenter->getId(), 'name' => $institutionMedicalCenter->getName(), 'rankingPoints' => $institutionMedicalCenter->getRankingPoints() ? $institutionMedicalCenter->getRankingPoints() : 0));
             $arr['isUsingInstitutionRankingPoints'] = $isUsingInstitutionRankingPoints;
             $arr['rankingPoints'] = $isUsingInstitutionRankingPoints ? $arr['institution']['totalClinicRankingPoints'] : $arr['institutionMedicalCenter']['rankingPoints'];
             $responseData['results'][] = $arr;
         }
     }
     return new Response(\json_encode($responseData), 200, array('content-type' => 'application/json'));
 }