setTemplate('city_select', 'geo'); $this->setKey('city_select'); } public function data() { $data = parent::data(); $data['coveringType'] = Geo::coveringType(); if (is_null($this->countrySelect)) { $data['countrySelect'] = $this->countrySelect = Geo::coveringType(Geo::COVERING_COUNTRIES); } if ($this->countrySelect) { $data['countryOptions'] = Geo::countriesList(); } if (is_null($this->countryEmpty)) { $data['countryEmpty'] = $this->countryEmpty = _t('geo', 'Select Country'); } $data['citySelect'] = ! Geo::coveringType(Geo::COVERING_CITY); $cityId = $this->cityId(); if (! $data['citySelect'] && ! $cityId) { $cityId = Geo::coveringRegion(); } $data['cityId'] = $cityId; $data['city'] = Geo::regionData($cityId); $region1 = $this->region1(); if ($this->countrySelect && ! $region1) { $data['region1'] = $region1 = $data['city']['parents'][1]['id'] ?? 0; } $data['presuggest'] = []; if ($this->countrySelect) { foreach ($data['countryOptions'] as $v) { $data['presuggest'][$v['id']] = Geo::regionPreSuggest($v['id'], ['json' => false]); } } else { $data['presuggest'] = [0 => Geo::regionPreSuggest(0, ['json' => false])]; } $data['ex'] = []; if ($cityId) { $ex = Geo::regionSuggest(['id' => $cityId, 'asArray' => true])['response'] ?? []; if (! empty($ex)) { $data['ex'] = reset($ex); } } return $data; } public function cityId() { if (is_callable($this->cityId)) { return call_user_func($this->cityId); } return $this->cityId; } public function region1() { if (is_callable($this->region1)) { return call_user_func($this->region1); } return $this->region1; } public function submit() { } public function rules(): array { return [ $this->fieldName => [TYPE_UINT], ]; } public function validateUsingRules(array $data = [], ?array $rules = null) { do { if (empty($this->fieldName)) { break; } $data = parent::validateUsingRules($data, $rules); $cityId = $data[$this->fieldName] ?? 0; if (Geo::coveringType(Geo::COVERING_CITY)) { $cityId = Geo::coveringRegion(); $data[$this->fieldName] = $cityId; } $city = Geo::regionData($cityId); if (empty($city['id'])) { unset($data[$this->fieldName]); break; } if (! empty($country = $this->countryFieldName)) { $data[$country] = $city['parents'][1]['id'] ?? 0; } } while (false); return $data; } }