Geo * @copyright Tamaranga */ class Geo extends FormBlock { public function init() { parent::init(); $this->setTemplate('form/blocks/geo', 'listings'); $this->setKey('geo'); $this->setTitle(_t('item-form', 'Location')); } public function data() { $data = parent::data(); $data['visible'] = $this->visible(); return $data; } public function blocks() { $this->addBlock('geo_city', CitySelect::class, function (CitySelect $block) { $block->cityId = function () { return $this->form->item['geo_city'] ?? 0; }; $block->onReloadBlock = function ($event) use ($block) { if (! in_array($event, ['categoryChange'])) { return false; } if (! empty($this->form->category['settings']['geo']['enabled'])) { return $this->render(); } return ''; }; $block->prerenderable(true); }); $this->addBlock('regions_delivery', RegionsDelivery::class); $this->addBlock('district', District::class); $this->addBlock('metro_id', Metro::class); $this->addBlock('addr_addr', MapAddr::class); } public function visible() { return ! empty($this->form->category['settings']['geo']['enabled']); } public function reloadBlock($event) { if (! in_array($event, ['categoryChange', 'publisherChange'])) { return false; } if ($this->visible()) { return $this->render(); } return ''; } public function onLoad($data = []) { if (! $this->form->edit()) { if (empty($this->form->item['geo_city'])) { if (bff::moduleExists('business') && User::companyID() && ! empty($this->form->item['company_id'])) { $company = $this->form->loadCompanyData(); if (! empty($company['geo_city'])) { $this->form->item->fill(['geo_city' => $company['geo_city']]); } } else { if (! empty($this->form->user['geo_city'])) { $this->form->item->fill(['geo_city' => $this->form->user['geo_city']]); } } } } parent::onLoad($data); } public function validate($data = []) { do { $geo = $this->form->category['settings']['geo'] ?? []; if (empty($geo['enabled'])) { unset($data['geo_city'], $data['district_id'], $data['metro_id'], $data['addr_addr'], $data['addr_lat'], $data['addr_lon']); break; } if (isset($data['geo_city'])) { $this->form->item['geo_city'] = $data['geo_city']; } $data = parent::validate($data); if (! empty($data['geo_city'])) { $regions = GeoBase::regionParentsCache($data['geo_city']); } else { $this->errors->set(_t('listings', 'Incorrect city'), 'city'); break; } if (!GeoBase::coveringType(GeoBase::COVERING_COUNTRY)) { $cityData = GeoBase::regionData($data['geo_city']); if (!$cityData || !GeoBase::coveringRegionCorrect($cityData)) { $this->errors->set(_t('listings', 'Incorrect city'), 'city'); } } } while (false); $data['geo_path'] = Listings::itemGeoPath($geo['enabled'] ?? false, $data['regions_delivery'] ?? 0, $regions['db'] ?? []); if (! empty($data['geo_city']) && ! empty($regions['db'])) { foreach ($regions['db'] as $k => $v) { $data[$k] = $v; } } return $data; } }