/** * Generate module */ protected function compile() { $this->Template = new \FrontendTemplate($this->storelocator_list_tpl); $sSearchVal = $this->Input->get('search') ? $this->Input->get('search') : NULL; $sSearchCountry = $this->Input->get('country') ? $this->Input->get('country') : NULL; $aEntries = array(); // check if an empty search is allowed if (!$this->storelocator_allow_empty_search && !$sSearchVal && $sSearchCountry) { $this->Template->error = true; } else { $term = NULL; // add country code for correct search results if (!empty($sSearchCountry)) { if (!empty($sSearchVal)) { $term = $sSearchVal . ', ' . $sSearchCountry; } else { $term = $aCountryNames[$entry['country']]; } } $aCategories = array(); $aCategories = deserialize($this->storelocator_list_categories); $aCountryNames = $this->getCountries(); if (!empty($term)) { // get coordinates of searched destination $sl = new StoreLocator(); $aCoordinates = array(); $aCoordinates = $sl->getCoordinatesByString($term); if (!empty($aCoordinates)) { $objStores = NULL; // search all countries if (!empty($sSearchVal)) { $objStores = $this->Database->prepare("\n SELECT\n *\n , 3956 * 1.6 * 2 * ASIN(SQRT( POWER(SIN((? -abs(latitude)) * pi()/180 / 2),2) + COS(? * pi()/180 ) * COS( abs(latitude) * pi()/180) * POWER(SIN((?-longitude) * pi()/180 / 2), 2) )) AS `distance`\n FROM `tl_storelocator_stores`\n WHERE\n pid IN(" . implode(',', $aCategories) . ")\n AND latitude != '' \n AND longitude != ''\n " . ($this->storelocator_limit_distance ? "HAVING distance < {$this->storelocator_max_distance} " : '') . "\n ORDER BY `highlight` DESC ,`distance` ASC\n ")->limit($this->storelocator_list_limit)->execute($aCoordinates['latitude'], $aCoordinates['latitude'], $aCoordinates['longitude']); // search selected country only } else { $objStores = $this->Database->prepare("\n SELECT\n *\n , 3956 * 1.6 * 2 * ASIN(SQRT( POWER(SIN((? -abs(latitude)) * pi()/180 / 2),2) + COS(? * pi()/180 ) * COS( abs(latitude) * pi()/180) * POWER(SIN((?-longitude) * pi()/180 / 2), 2) )) AS `distance`\n FROM `tl_storelocator_stores`\n WHERE\n pid IN(" . implode(',', $aCategories) . ")\n AND latitude != '' \n AND longitude != '' \n " . ($this->storelocator_limit_distance ? "HAVING distance < {$this->storelocator_max_distance} " : '') . "\n AND country = ?\n ORDER BY `highlight` DESC ,`distance` ASC\n ")->limit($this->storelocator_list_limit)->execute($aCoordinates['latitude'], $aCoordinates['latitude'], $aCoordinates['longitude'], strtoupper($sSearchCountry)); } $entries = array(); $entries = $objStores->fetchAllAssoc(); if (!empty($entries)) { foreach ($entries as $entry) { if (empty($sSearchVal)) { $entry['distance'] = NULL; } $entry['class'] = $entry['highlight'] ? 'starred' : ''; $entry['country_code'] = $entry['country']; $entry['country_name'] = $aCountryNames[$entry['country']]; // generate link $link = null; if ($this->jumpTo) { $objLink = $this->Database->prepare("SELECT * FROM tl_page WHERE id = ?;")->execute($this->jumpTo); $entry['link'] = $this->generateFrontendUrl($objLink->fetchAssoc(), (!$GLOBALS['TL_CONFIG']['useAutoItem'] ? '/store/' : '/') . $entry['id'] . '-' . standardize($entry['name'] . ' ' . $entry['city'])); } // get opening times $entry['opening_times'] = unserialize($entry['opening_times']); $entry['opening_times'] = !empty($entry['opening_times'][0]['from']) ? $entry['opening_times'] : NULL; $aEntries[] = $entry; } } } } } $this->Template->entries = $aEntries; }