getSettings('period_type', static::PER_PERIOD); } public function getPeriod() { return $this->getSettings('period', 1); } public function getKey(): string { return 'fix'; } /** * Service can be in a pack * @return bool */ public function isPackable(): bool { return true; } public function formParams(array & $params = []) { if ($this->getPeriodType() == static::PER_DAY) { $days = $this->activationSettings['days'] ?? 1; if ($this->isPOST()) { if ($this->input->hasPost('fix_days')) { $days = $this->input->postget('fix_days', TYPE_UINT); } } else { if ($this->input->has('fix_days')) { $days = $this->input->postget('fix_days', TYPE_UINT); } } $this->activationSettings['days'] = $days; $params['fix_days'] = $this->activationSettings['days']; } } /** * Service cost * @return float */ public function getCost(): float { $cost = $this->getPrice(); if ($this->getPeriodType() == static::PER_DAY) { $this->formParams(); $days = 1; if (isset($this->activationSettings['days'])) { $days = $this->activationSettings['days']; } if ($days <= 0 || $days > 999) { $days = 1; } $cost = round($days * $cost, 2); } return $cost; } /** * Get service expiration date or 0 - unlimited * @return int */ public function getExpires(): int { if ($this->activationPack) { $period = $this->activationSettings['fix_days'] ?? 1; } else { if ($this->getPeriodType() == static::PER_DAY) { $period = 1; if (isset($this->activationSettings['days'])) { $period = $this->activationSettings['days']; } } else { $period = $this->getPeriod(); if (empty($period)) { return 0; } } } $exist = $this->getItemServiceData($this->getKey(), $this->itemID); if (! empty($exist['expires'])) { $expire = strtotime('+'.$period.' days', strtotime($exist['expires'])); } else { $expire = strtotime('+'.$period.' days'); } return $expire; } public function onActivateAfter() { do { if (! $this->itemID) { break; } $exist = $this->getItemServiceData($this->getKey(), $this->itemID); if (empty($exist)) { break; } $exist = $exist->toArray(); if (empty($exist['modified'])) { break; } $update = [ 'svc_fixed_order' => date('Y-m-d H:i:s', strtotime($exist['modified'])) ]; if (! empty($exist['expires'])) { $expires = strtotime($exist['expires']); $item = Listings::model()->itemData($this->itemID, ['publicated_to']); if (strtotime($item['publicated_to']) < $expires) { $update['publicated_to'] = date('Y-m-d H:i:s', $expires); } } Listings::model()->itemSave($this->itemID, $update); } while(false); } /** * Returns service bill description * @return string */ public function getBillDescription(): string { $item = $this->manager->getItemData($this->itemID, ['link', 'title']); if (! $item) { return ''; } $days = $this->activationSettings['days'] ?? 0; if ($this->getPeriodType() == static::PER_DAY && $days > 0) { return $this->plugin()->lang('Pin the listing to [days]
[title]', [ 'days' => tpl::declension($days, $this->plugin()->lang('day;days;days')), 'link' => $item['link'], 'title' => $item['title'], ]); } return $this->plugin()->lang('Pin the listing
[title]', [ 'link' => $item['link'], 'title' => $item['title'], ]); } /** * Admin settings form * @param Form $form * @return mixed|void */ public function onAdminSettings($form) { parent::onAdminSettings($form); $form->to('settings'); $form->radio('period_type', $this->plugin()->langAdmin('Service Cost'), static::PER_PERIOD) ->option(static::PER_PERIOD, $this->plugin()->langAdmin('For the Specified Period')) ->option(static::PER_DAY, $this->plugin()->langAdmin('For One Day')) ->before('icon_b'); $form->number('period', $this->plugin()->langAdmin('Service Validity Period'), 0) ->label($this->plugin()->langAdmin('days')) ->visibleIf('period_type', static::PER_PERIOD) ->onValidate(function ($p) { if (empty($p['value'])) { $p['value'] = 1; } return true; }) ->after('period_type'); $form->number('days_default', $this->plugin()->langAdmin('Days by default'), 0) ->label($this->plugin()->langAdmin('days')) ->visibleIf('period_type', static::PER_DAY) ->onValidate(function ($p) { if (empty($p['value'])) { $p['value'] = 1; } return true; }) ->after('period_type'); $form->endUnion(); $this->addAdminSettingsFormPrices($form); } /** * Admin pack settings form * @param Form $form * @return void */ public function onAdminSettingsPackServices($form) { $form ->number('fix_days', '', 0) ->stretch('mini') ->htmlAfter(' '.$this->plugin()->langAdmin('- amount of days')) ->together($this->getKey()) ; } public function getInstallSettings(): array { # todo translate return [ 'title' => [ 'en' => 'Fixed', 'ru' => 'Закрепление', ], 'description' => [ 'ru' => 'Закрепив объявление вы поднимаете его в начало списка объявлений схожей тематики на 7 дней. ' . PHP_EOL . '- в 10 раз больше просмотров ' . PHP_EOL . '- в 5 раз больше откликов ' . PHP_EOL . '- продажа совершается гораздо быстрее', ], 'description_view' => [ 'ru' => '

Закрепив объявление вы поднимаете его в начало списка объявлений схожей тематики на 7 дней. 

'. '

- В 10 раз больше просмотров 

'. '

- В 5 раз больше откликов 

'. '

- Продажа совершается гораздо быстрее

', ], 'price' => 1, 'settings' => [ 'period_type' => static::PER_PERIOD, 'period' => 7, 'add_form' => 1, ], ]; } public function onInstall(array $opts = []) { $opts['icons'] = [ 'icon_b' => $this->plugin()->path('static/top.svg'), 'icon_s' => $this->plugin()->path('static/top.svg'), ]; return parent::onInstall($opts); } /** * Prepare activation form data * @param array $data @ref * @param array $opts * @return void */ public function onActivationForm(array & $data, array $opts = []) { parent::onActivationForm($data); if ($this->getPeriodType() == static::PER_DAY) { $this->formParams(); $tplData = [ 'formData' => & $data, 'days' => $this->activationSettings['days'] ?? $this->getSettings('days_default', 1), 'price' => $this->getPrice(), ]; $data['form'] = $this->plugin()->template('tpl/form.fix', $tplData); } } }