scope('listing', _t('@banners', 'Banners List View / Positions / Statistics')) ->methods(['listing', 'statistic']) ->scope('edit', _t('@banners', 'Banners / Positions Management')) ->method('positions'); # 'listing' => list:onDelete,onToggle } # Баннеры public function listing() { if (! $this->haveAccessTo('listing')) { return $this->showAccessDenied(); } $positions = $this->model->positionsList()->toArray(); $form = AdminHelpers::form($this, 'admin/form', [ 'id' => 'banners.banner', 'templateDir' => $this->module_dir_tpl_core, 'data' => ['positions' => $positions], ]); $list = AdminHelpers::list($this, 'admin/listing', [ 'id' => 'banners.banner', 'templateDir' => $this->module_dir_tpl_core, 'data' => ['positions' => $positions, 'localeHTML' => ''], ]); $onLoad = function ($id, $data) { return $this->model->banner($id, ['*'], ['regions'])->toArray(); }; $form ->onSubmitAfter(function ($id, $params) use ($form, $onLoad) { if (! $id) { return; } if (! isset($params['saved'])) { return; } $data = $params['saved']; if (empty($data['type'])) { return; } if (empty($data['pos'])) { return; } $pos = $data['pos']; $update = []; switch ($data['type']) { case static::TYPE_IMAGE: # загружаем изображение $type_data = []; if (empty($params['inserted'])) { $prev = $onLoad($id, ['type_data']); $type_data = $prev['type_data'] ?? []; } if (! is_array($type_data)) { $type_data = []; } $devices = $this->app->devices(); foreach ($devices as $v) { if (! empty($v['alias'])) { continue; } $name = 'img_' . $v['id']; $fileName = $this->imgUpload($id, $pos, $name); if ($fileName !== false) { if (! empty($type_data[$name]['name'])) { $this->imgDelete($id, $type_data[$name]['name']); } $type_data[$name] = ['name' => $fileName]; $update['type_data'] = $type_data; $params['response']['reload'] = 1; } } break; } if (! empty($update)) { $res = $this->model->banner($id)->update($update); $this->imageWidthHeight($id, ['update' => true]); } }) ->onLoad($onLoad) ->onSave(function ($id, $data) { $res = $this->model->bannerSave($id, $data); $this->model->cacheReset(); return $res; }, false) ; $list->formAdd($form); $list ->onDelete(function ($id) { if (! $this->haveAccessTo('edit')) { return $this->showAccessDenied(); } if (! $id) { return $this->showImpossible(); } $this->deleteBanner($id); return; }) ->onToggle(function ($id) { if (! $this->haveAccessTo('edit')) { return $this->showAccessDenied(); } if (! $id) { return $this->showImpossible(); } $banner = $this->model->banner($id); if (! $banner) { return $this->showImpossible(); } # Проверка возможно ли включить баннер (не используется ли на неротируемой позиции другой баннер) if (! $banner['enabled'] && $this->checkPositionRotation($banner['pos'])) { $banner->toggle('enabled'); $this->model->cacheReset(); } elseif ($banner['enabled']) { $banner->toggle('enabled'); $this->model->cacheReset(); } else { $this->errors->set(_t('@banners', 'Banners rotation is denied for this position')); } return ['id' => $id]; }) ->onRotate(function () { $pos = $this->input->postget('pos', TYPE_UINT); if (! $pos) { return; } $this->model->bannersRotate($pos); }) ->onFordev(_t('@banners', 'Clear Cache'), function () { if (!$this->isAdminFordev()) { return $this->showAccessDenied(); } $this->model->cacheReset(); return $this->adminRedirect(Errors::SUCCESS); }, false) ; $list->trigger('extend'); $filter = []; switch ($list->tab()) { case 1: # включенные $filter['enabled'] = 0; break; case 2: # выключенные $filter['enabled'] = 1; break; } if ($f = $list->filter('q')) { $filter['query'] = $f; } if ($f = $list->filter('pos')) { $filter['pos'] = $f; } if ($f = $list->filter('locale')) { $filter['locale'] = $f; } if ($f = $list->filter('region')) { $filter['region'] = $f; } $showStart = $list->filter('show_start'); $showStart = (!empty($showStart) ? strtotime($showStart) : 0); $showFinish = $list->filter('show_finish'); $showFinish = (!empty($showFinish) ? strtotime($showFinish) : 0); if ($showStart && $showFinish) { if ($showStart > $showFinish) { $filter[] = ['show_start', '>=', date('Y-m-d', $showStart)]; $f['show_finish'] = ''; } else { $filter[] = ['show_start', '>=', date('Y-m-d', $showStart)]; $filter[] = ['show_finish', '<=', date('Y-m-d', $showFinish)]; } } else { if ($showStart) { $filter[] = ['show_start', '>=' ,date('Y-m-d', $showStart)]; } elseif ($showFinish) { $filter[] = ['show_finish', '<=' ,date('Y-m-d', $showFinish)]; } } $list->trigger('filter', ['filter' => &$filter]); $rows = $this->model->bannersListing($filter); $order = $list->order('id-desc', false); if ($list->filter('pos')) { $order = 'num-asc'; } if (! empty($rows)) { foreach ($rows as &$v) { if (isset($positions[$v['pos']])) { $v['pos'] = $positions[$v['pos']]; } else { $v['pos'] = ['id' => $v['pos'], 'title' => '']; } $v['ctr'] = round(($v['clicks'] / ($v['shows'] ? $v['shows'] : 1)) * 100, 2); } unset($v); list($orderBy, $orderDir) = explode('-', $order); usort($rows, function ($a, $b) use ($orderBy, $orderDir) { if ($orderDir == 'asc') { return strnatcasecmp($a[$orderBy], $b[$orderBy]); } else { return strnatcasecmp($b[$orderBy], $a[$orderBy]); } }); } $list->rows($rows); return $list->view(); } # Статистика public function statistic() { if (! $this->haveAccessTo('listing')) { return $this->showAccessDenied(); } $id = $this->input->postget('banner_id', TYPE_UINT); $banner = $this->model->banner($id)->toArray(); if (empty($banner)) { return $this->showImpossible(); } $list = AdminHelpers::list($this, 'admin/statistic', [ 'id' => 'banners.statistic', 'templateDir' => $this->module_dir_tpl_core, 'data' => ['banner' => $banner], ]); $list->ajaxParams(['banner_id' => $id]); $list->trigger('extend'); $filter = ['banner_id' => $id]; $start = $list->filter('start'); $start = (!empty($start) ? strtotime($start) : 0); if ($start) { $filter[] = ['period', '>=', date('Y-m-d', $start)]; } $finish = $list->filter('finish'); $finish = (!empty($finish) ? strtotime($finish) : 0); if ($finish) { $filter[] = ['period', '<=', date('Y-m-d', $finish)]; } $list->rowsIdKey('banner_id'); $list->perPageRange([ 20 => '20', 40 => '40', 100 => '100', -1 => _t('@', 'All')]); $list->trigger('filter', ['filter' => &$filter]); $list->total($this->model->bannerStatisticListing($filter, true)); $rows = $this->model->bannerStatisticListing($filter, false, $list->order('period-desc', true), $list->getLimit(), $list->getOffset())->toArray(); $list->rows($rows); return $list->view(); } # Позиции баннеров public function positions() { if (! $this->haveAccessTo('edit')) { return $this->showAccessDenied(); } $fordev = $this->isAdminFordev(); $list = AdminHelpers::list($this, 'admin/positions.listing', [ 'id' => 'banners.position', 'templateDir' => $this->module_dir_tpl_core, ]); $form = AdminHelpers::form($this, 'admin/positions.form', [ 'id' => 'banners.position', 'templateDir' => $this->module_dir_tpl_core, 'data' => ['list' => $list], ]); $form ->onLoad(function ($id, $data) { return $this->model->positionData($id); }) ->onSave(function ($id, $data) { $this->model->positionSave($id, $data); $this->model->cacheReset(); return true; }, false) ; $list->formAdd($form, ['add' => $fordev]); if ($fordev) { $form = AdminHelpers::form($this, 'admin/position.delete.form', [ 'id' => 'banners.position', 'templateDir' => $this->module_dir_tpl_core, ]) ->onSubmit(function ($id, $params) { if (empty($id) || empty($params['data'])) { return false; } $data = $params['data']; if (empty($data['mode'])) { return false; } switch ($data['mode']) { case 'replace': if (empty($data['next'])) { $this->errors->set(_t('@banners', 'Choose a new position to move the banners')); return false; } $nextID = $data['next']; $nextData = $this->model->positionData($nextID); if (empty($nextData) || $data['next'] == $id) { $this->errors->set(_t('@banners', 'Choose a new position to move the banners')); return false; } else { # перемещаем баннеры $bannersID = $this->model->bannersByPosition($id); $this->model->bannersToPosition($bannersID, $nextID); $this->model->cacheReset(); # удаляем позицию $this->model->positionDelete($id); } break; case 'all': # удаляем баннеры связанные с позицией $bannersID = $this->model->bannersByPosition($id); if (!empty($bannersID)) { $this->deleteBanner($bannersID); } # удаляем позицию $this->model->positionDelete($id); break; } return true; }) ->onLoad(function ($id, $data) { $position = $this->model->positionData($id); $position['positions'] = $this->model->positionsList([['id', '!=', $id]])->toArray(); return $position; }) ->onSave(function ($id, $data) { return true; }, false) ; $list->formCustom($form, 'position_delete', ['idKey' => 'position_id']); } $list->onToggle(function ($id) { if (! $id) { $this->errors->unknownRecord(); return; } $this->model->position($id)->toggle('enabled'); $this->model->cacheReset(); }); $list->trigger('extend'); if ($list->subAction() == 'rotation') { $response = []; do { $id = $this->input->post('id', TYPE_UINT); if (!$id) { $this->errors->unknownRecord(); break; } $this->model->position($id)->toggle('rotation'); $this->model->cacheReset(); } while (false); return $this->ajaxResponseForm($response); } $rows = $this->model->positionsList()->toArray(); $list->rows($rows); return $list->view(); } }