app->plugin('services\listings'); } /** * @return \Plugin_Services_Listings_p0e50fd | null */ protected function addon() { return null; } /** * Admin settings form * @param Form $form * @return mixed|void */ public function onAdminSettings($form) { $form->to('settings'); $form ->images('icon_b', $this->plugin()->langAdmin('Icon (big)'), 1) ->param('extensionsAllowed', ['jpg', 'jpeg', 'gif', 'png', 'svg']) ->param('folderByID', false) ->images('icon_s', $this->plugin()->langAdmin('Icon (small)'), 1) ->param('extensionsAllowed', ['jpg', 'jpeg', 'gif', 'png', 'svg']) ->param('folderByID', false) ->checkbox('add_form', $this->plugin()->langAdmin('In the Add Form'), false) ; $form->endUnion(); } protected function addAdminSettingsFormPrices(Form $form) { $form->group( 'prices', $this->plugin()->langAdmin('Add'), $this->plugin()->langAdmin('Regional Cost'), ['plus' => ['type' => 'href']] ) ->text('price', $this->plugin()->langAdmin('Cost'), 0, false) ->label(Currency::default()) ->required($this->plugin()->langAdmin('Specify regional cost')) ->onValidate(function ($params){ $params['value'] = floatval($params['value']); return true; }) ->tags( 'cats', $this->plugin()->langAdmin('Categories'), function ($q){ $parents = Listings::model()->catsOptions('adm-svc-prices-ex', 0, false, ['numlevel' => 1], ['array' => true]); $parents = func::array_transparent($parents, 'id', true); $data = Listings::model()->catsOptions('adm-svc-prices-ex', 0, false, ['q' => $q], ['array' => true]); $result = []; foreach ($data as $v) { $title = $v['title']; if (isset($parents[ $v['pid'] ])) { $title = $parents[ $v['pid'] ]['title'] . ' / ' . $title; } $result[] = [$v['id'], $title]; } return $result; }, function ($ids){ if (empty($ids)) return []; $data = Listings::model()->catsOptions('adm-svc-prices-ex', 0, false, [], ['array' => true]); $data = func::array_transparent($data, 'id', true); $result = []; foreach ($ids as $v) { if (isset($data[$v])) { $title = $data[$v]['title']; $pid = $data[$v]['pid']; if (isset($data[$pid])) { $title = $data[$pid]['title'] . ' / ' . $title; } $result[] = [ 'value' => $v, 'title' => $title, ]; } } return $result; }, false, $this->plugin()->langAdmin('Enter category name') ) ->param('suggest', function (){ $data = Listings::model()->catsOptions('adm-svc-prices-ex', 0, false, ['numlevel' => 1], ['array' => true]); $result = []; foreach ($data as $v) { $result[] = [$v['id'], $v['title']]; } return $result; }) ->required($this->plugin()->langAdmin('Select a category for regional cost')) ->tags( 'regions', $this->plugin()->langAdmin('Regions'), function ($q){ $data = Geo::regionSuggest([ 'q' => $q, 'country' => 1, 'reg' => 1, 'asArray' => true ]); $data = $data['list'] ?? []; $result = []; foreach ($data as $v) { $title = ''; if (($v['numlevel'] ?? 0) > 1) { if (isset($v['parents'][1]['title'])) { $title = $v['parents'][1]['title'] . ' / '; } } $title .= $v['title']; $result[] = [$v['id'], $title]; } return $result; }, function ($ids){ if (empty($ids)) return []; $data = Geo::model()->regionsListing(['id' => $ids], ['keyBy' => 'id', 'fields' => ['parents']]); $result = []; foreach ($ids as $v) { if ( ! isset($data[$v])) continue; $title = ''; $c = $data[$v]; if (($c['numlevel'] ?? 0) > 1) { if (isset($c['parents'][1]['title'])) { $title = $c['parents'][1]['title'] . ' / '; } } $title .= $c['title']; $result[] = [ 'value' => $v, 'title' => $title, ]; } return $result; }, false, $this->plugin()->langAdmin('Enter name of region') ) ->endGroup() ->after('price') ; } public function onInstall(array $opts = []) { if (! empty($opts['id']) && ! empty($opts['icons'])) { $this->manager->installServiceImages( $this->addon() ?? $this->plugin(), $opts['id'], $opts['icons'] ); } return parent::onInstall($opts); } /** * Set activation settings * @param mixed $itemID * @param mixed $userID * @param array|null $settings * @return mixed|void */ public function setActivationSettings($itemID, $userID, ?array $settings = null) { parent::setActivationSettings($itemID, $userID, $settings); if ($this->itemID) { $required = ['company_id']; if (! isset($this->activationSettings['cat_id1'])) { $required[] = 'cat_id1'; $required[] = 'cat_id2'; } if (! isset($this->activationSettings['geo_city'])) { $required[] = 'geo_city'; $deep = Geo::maxDeep(); for ($i = 1; $i <= $deep; $i++) { $required[] = 'geo_region' . $i; } } if (! $this->userID) { $required[] = 'user_id'; } if (! empty($required)) { $data = $this->manager->getItemData($this->itemID, $required); if (! empty($data)) { foreach ($data as $k => $v) { if (! in_array($k, $required)) { unset($data[$k]); } } if (isset($data['user_id'])) { $this->userID = $data['user_id']; unset($data['user_id']); } $this->activationSettings = array_merge($this->activationSettings ?? [], $data); } } } else { if ($this->isPOST()) { $data = $this->input->postm([ 'cat' => TYPE_UINT, 'city' => TYPE_UINT, 'shop' => TYPE_UINT, ]); if ($data['cat']) { $catParents = Listings::model()->catParentsID($data['cat']); $this->activationSettings['cat_id1'] = $catParents[1] ?? 0; $this->activationSettings['cat_id2'] = $catParents[2] ?? 0; } if ($data['city']) { $cityData = Geo::regionData($data['city']); $this->activationSettings = array_merge($this->activationSettings, Geo::regionParents($cityData)['db'] ?? []); } if ($data['shop']) { $this->activationSettings['company_id'] = User::companyID(); } } } } /** * Service price * @return float */ public function calculatePrice(): float { $price = parent::calculatePrice(); if (! isset($this->settings['prices'])) { $data = $this->manager->getServiceModel()->one([ 'group_key' => $this->getGroupKey(), 'service_key' => $this->getKey(), ], ['*'], ['prices']); if (isset($data['prices'])) { $this->settings['prices'] = $data['prices']; } } if (! empty($this->settings['prices'])) { $prices = $this->settings['prices']; $cat1 = $this->activationSettings['cat_id1'] ?? 0; $cat2 = $this->activationSettings['cat_id2'] ?? 0; $cats = [$cat1]; if ($cat2) { $cats[] = $cat2; } $geo = []; $deep = Geo::maxDeep(); for ($i = $deep; $i >= 1; $i--) { $f = 'geo_region' . $i; if (! empty($this->activationSettings[$f])) { $geo[$f] = $this->activationSettings[$f]; } } do { foreach ($cats as $cat) { # категория + регион(город, регионы+, страна) foreach ($prices as $v) { if ($v['category_id'] == $cat) { foreach ($geo as $vv) { if ($v['region_id'] == $vv) { $price = $v['price']; break 4; } } } } } # категория foreach ($cats as $cat) { foreach ($prices as $v) { if ($v['category_id'] == $cat && $v['region_id'] == 0) { $price = $v['price']; break 3; } } } } while(false); } return $price; } public function itemsIDs($order = '') { return ItemServiceStatus::where('group_key', $this->getGroupKey()) ->where('service_key', $this->getKey()) ->where('service_status', static::STATUS_ACTIVE) ->when($order === 'rand', function ($query) { /**@var ItemServiceStatus $query */ $query->orderByRaw('RAND()'); }, function ($query) { /**@var ItemServiceStatus $query */ $query->orderBy('modified', 'desc'); }) ->pluck('item_id') ->toArray(); } }