示例#1
0
 /**
  * Fills coordinates if not already set and saving
  * @param DataContainer
  * @return bool
  */
 public function fillCoordinates(DataContainer $dc)
 {
     // Return if both are numeric
     if (is_numeric($dc->activeRecord->latitude) && is_numeric($dc->activeRecord->longitude)) {
         return;
     }
     // find coordinates
     $arrCoords = AnyStores::getLonLat($dc->activeRecord->street . ' ' . $dc->activeRecord->postal . ' ' . $dc->activeRecord->city, $dc->activeRecord->country);
     if (!empty($arrCoords)) {
         $objStore = AnyStoresModel::findByPk($dc->id);
         $objStore->latitude = $arrCoords['latitude'];
         $objStore->longitude = $arrCoords['longitude'];
         $objStore->save();
     }
 }
示例#2
0
 /**
  * Fills coordinates if not already set and saving
  * @param DataContainer
  * @return bool
  */
 public function fillCoordinates(DataContainer $dc)
 {
     // Return if both are numeric
     if (is_numeric($dc->activeRecord->latitude) && is_numeric($dc->activeRecord->longitude)) {
         return;
     }
     // Get country name
     $arrCountries = \System::getCountries();
     // find coordinates
     $arrCoords = AnyStores::getLonLat(sprintf('%s, %s %s, %s', $dc->activeRecord->street, $dc->activeRecord->postal, $dc->activeRecord->city, $arrCountries[$dc->activeRecord->country]));
     if (!empty($arrCoords)) {
         $objStore = AnyStoresModel::findByPk($dc->id);
         $objStore->latitude = $arrCoords['latitude'];
         $objStore->longitude = $arrCoords['longitude'];
         $objStore->save();
     }
 }
示例#3
0
 public static function findPublishedByAdressAndCountryAndCategory($strSearch, $strCountry = null, array $arrCategories, $intLimit = null, $intMaxDistance = null)
 {
     $t = static::$strTable;
     $arrCoordinates = AnyStores::getLonLat($strSearch, $strCountry);
     if (!$arrCoordinates) {
         \System::log("Can't find coordinates for '{$strSearch}'", __METHOD__, TL_ERROR);
         return;
     }
     $arrOptions = array('fields' => array("{$t}.id", "( 6371 * acos( cos( radians(?) ) * cos( radians( {$t}.latitude ) ) * cos( radians( {$t}.longitude ) - radians(?) ) + sin( radians(?) ) * sin( radians( {$t}.latitude ) ) ) ) AS distance"), 'table' => $t, 'column' => array("{$t}.pid IN(" . implode(',', array_map('intval', $arrCategories)) . ")", "({$t}.start='' OR {$t}.start<UNIX_TIMESTAMP()) AND ({$t}.stop='' OR {$t}.stop>UNIX_TIMESTAMP()) AND {$t}.published=1"), 'order' => "distance");
     // Country
     if ($strCountry) {
         $arrOptions['column'][] = "{$t}.country=?";
     }
     // Maximun distance
     if (is_numeric($intMaxDistance)) {
         $arrOptions['having'] = "distance < " . $intMaxDistance;
     }
     // Get query
     $strQuery = \Model\QueryBuilder::find($arrOptions);
     // replace * with additional fields
     $strQuery = preg_replace('/\\*/', implode(',', $arrOptions['fields']), $strQuery, 1);
     $objResult = \Database::getInstance()->prepare($strQuery)->limit($intLimit)->execute($arrCoordinates['latitude'], $arrCoordinates['longitude'], $arrCoordinates['latitude'], $strCountry ?: null);
     if (!$objResult->numRows) {
         \System::log("No results for '{$objResult->query}'", __METHOD__, TL_ERROR);
         return;
     }
     // Create store models from database result
     while ($objResult->next()) {
         $objModel = AnyStoresModel::findByPk($objResult->id);
         $objModel->distance = $objResult->distance;
         $objModel->preventSaving();
         $arrModels[] = $objModel;
     }
     // Return model collection
     return new \Model\Collection($arrModels, 'tl_anystores');
 }