Views Statistic * @copyright Tamaranga */ class ViewsStatisticBlock extends Block { /** @var int item id */ public $itemId; /** @var bool item owner is current user */ public $itemOwner = false; /** @var int total item views counter */ public $views_total = 0; /** @var int today item views counter */ public $views_today = 0; /** @var int visible for */ public $visibleFor = 0; public function init() { parent::init(); $this->setTemplate('item/views.statistic', 'listings'); $this->setKey('view.views.statistics'); $this->setTitle(_t('listings', 'Views Statistics')); } public function data() { $data = parent::data(); # Hide block if (! $this->isVisible()) { return ''; } return $data; } public function isVisible() { # No views if (! $this->views_total) { return false; } # Hide from current user switch ($this->visibleFor) { case 0: # to all return true; case 1: # logged in return $this->request->user(); case 2: # lising owner return $this->itemOwner; case 3: return false; } return false; } public function onViewsStatAction() { do { if (! $this->itemId || ! $this->security->validateReferer()) { $this->errors->reloadPage(); break; } if (! $this->isVisible()) { $this->errors->set(_t('listings', 'There are no statistics for this listing.')); break; } # получаем данные $statistic = Listings::model()->itemViewsData($this->itemId); if (empty($statistic['data'])) { $this->errors->set(_t('listings', 'There are no statistics for this listing.')); break; } $statistic['promoteBlock'] = $this->getParentPage()->getBlock('promoteBlock') ?? ''; $this->respond('popup', Listings::template('item/views.statistic.popup', $statistic)); $this->respond('stat', $statistic['data']); $this->respond('lang', [ 'y_title' => _t('view', 'Number of Views'), 'total' => _t('view', 'Total'), 'item_views' => _t('view', 'Listings views'), 'contacts_views' => _t('view', 'Contact views'), 'months' => explode(',', _t('view', 'January,February,March,April,May,June,July,August,September,October,November,December')), 'shortMonths' => explode(',', _t('view', 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec')), //'weekdays' => explode(',', _t('view', 'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday')), ]); } while (false); return $this->getActionResponse(); } public function settingsForm($form) { $form->select('visibleFor', _t('@listings', 'Listing statistics visibility'), $this->visibleFor, function () { return [ ['id' => 0, 'title' => _t('@listings', 'To All')], ['id' => 1, 'title' => _t('@listings', 'Logged In')], ['id' => 2, 'title' => _t('@listings', 'Listing Owner')], ['id' => 3, 'title' => _t('@listings', 'Nobody')], ]; }); } }