getItemActiveServices($itemID); return Listings::model()->itemSave($itemID, ['svc' => join(',', $active)]); } /** * Get item data * @param int|string $itemID * @param array $columns * @return array|null */ public function getItemData($itemID, array $columns = []): ?array { return Listings::model()->itemData($itemID, $columns); } /** * Update item data * @param int|string $itemID * @param array $data * @return bool */ public function updateItemData($itemID, array $data): bool { return Listings::model()->itemSave($itemID, $data); } /** * Get services list * @param array $filter * @return array */ public function list(array $filter = []): array { $filter = $this->defaults($filter, [ 'type' => [Service::TYPE_SERVICE, Service::TYPE_SERVICEPACK], 'enabled' => true, ]); if (! is_array($filter['type'])) { $filter['type'] = [$filter['type']]; } $icons = ['icon_b', 'icon_s']; $result = []; foreach ($this->services as $k => $v) { if (empty($v['service_type']) || ! in_array($v['service_type'], $filter['type'])) { continue; } if ($filter['enabled'] && ! $v['enabled']) { continue; } if (! empty($v['class_name'])) { $s = $this->getService($v['class_name']); if (! $s || ! $s->isAllowed() || ! $s->isEditable()) { continue; } } foreach ($icons as $i) { if (isset($v['settings'][$i])) { $v[$i] = FormData::getImageURL($v['settings'][$i], 'o'); } } $result[$k] = $v; } # Sort by type: services first uasort($result, function ($a, $b) { if ($a['service_type'] == $b['service_type']) { return ($a['num'] > $b['num'] ? 1 : -1); } return ($a['service_type'] > $b['service_type'] ? 1 : -1); }); return $result; } /** * Get item promote services list * @param int $itemID * @param int $userID * @param array $itemData * @param array $opts * @return array */ public function getItemPromoteServices($itemID, $userID, array $itemData = [], array $opts = []) { $services = $this->list(); foreach ($services as $k => & $v) { $service = $this->getService($v['class_name']); if (! $service) { unset($services[$k]); continue; } $service->setSettings($v); $service->setActivationSettings($itemID, $userID, $opts['activationSettings'] ?? null); $v['price'] = $service->getPrice(); $service->onActivationForm($v); } unset($v); return $services; } /** * Add active services info to cabinet items list * @param array $data items data * @return void */ public function addCabinetItemsListActiveServices(array &$data) { $svcDD = ['mark', 'fix', 'premium', 'quick']; $items = []; foreach ($data as $v) { $items[] = $v['id']; } $services = []; $cacheService = function ($key) use (&$services) { if (isset($services[$key])) { return $services[$key]; } return $services[$key] = $this->getService($key); }; if (! empty($items)) { $statuses = ItemServiceStatus::whereIn('item_id', $items) ->whereIn('service_key', $svcDD) ->where('service_status', Service::STATUS_ACTIVE) ->get() ->groupBy('item_id') ->map(function ($pb) { return $pb->keyBy('service_key'); }) ->toArray(); foreach ($data as & $v) { $v['svc_list'] = []; if (isset($statuses[ $v['id'] ])) { $i = $statuses[ $v['id'] ]; foreach ($svcDD as $vv) { if (! isset($i[$vv])) { continue; } $service = $cacheService($vv); if (! $service) { continue; } $v['svc_list'][$vv] = [ 'title' => $service->getTitle(), 'date' => $i[$vv]['expires'], ]; } } } unset($v); $statuses = ItemServiceStatus::whereIn('item_id', array_unique($items)) ->where('service_key', 'up') ->where('service_status', Service::STATUS_ACTIVE) ->get() ->keyBy('item_id') ->toArray(); foreach ($data as & $v) { if (isset($statuses[ $v['id'] ])) { if (! empty($statuses[ $v['id'] ]['settings']['up_on'])) { $v['svc_upauto_on'] = true; } } if (strtotime($v['svc_up_free']) < strtotime($v['created'])) { $v['svc_up_free'] = $v['created']; } } unset($v); } } /** * Retrieve services form params * @param array $params * @return void */ public function formParams(&$params) { $svc = $this->input->postget('svc', TYPE_STR); if ($svc) { $params['svc'] = $svc; $service = $this->getService($svc); if ($service) { $service->formParams($params); } } } /** * Get notifications services macro * @param bool $preview * @param array $filter [type, enabled] * @return array */ public function getNotificationsMacro(bool $preview = false, array $filter = []): array { $macro = []; foreach ($this->list($filter) as $key => $service) { $title = $service['title'] ?? ''; if ($service['service_type'] == Service::TYPE_SERVICE) { $keyPrefix = 'svc_'; if ($preview) { $title = _t('listings', 'Service link "[name]"', ['name' => $title]); } } elseif ($service['service_type'] == Service::TYPE_SERVICEPACK) { $keyPrefix = 'pack_'; if ($preview) { $title = _t('listings', '"[title]" Package Link', ['title' => $title]); } } else { continue; } $macro[$keyPrefix . $key] = ($preview ? $title : $key); } return $macro; } }