Skip to content

Commit

Permalink
Универсальный трейт для гридов => немного рефакторинга для примера ра…
Browse files Browse the repository at this point in the history
…боты
  • Loading branch information
fuzegit committed Jun 29, 2022
1 parent e97b4b6 commit ee0d690
Show file tree
Hide file tree
Showing 35 changed files with 233 additions and 427 deletions.
2 changes: 1 addition & 1 deletion system/config/version.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
major = 2
minor = 15
build = 2
date = 20220109
date = 20220701
83 changes: 83 additions & 0 deletions system/controllers/admin/traits/listgrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
namespace icms\controllers\admin\traits;
/**
* @property \cmsModel $model
*/
trait listgrid {

protected $trait_params = [
'table_name' => '',
'grid_name' => '',
'grid_url' => '',
'title' => '',
'tool_buttons' => [],
'list_callback' => null
];

protected $ups_key, $grid;

protected function setProperty($key, $value) {
$this->trait_params[$key] = $value;
}

public function run(){

$this->setListGridParams();

if ($this->request->isAjax()) {

return $this->getListItems();
}

return $this->renderListItemsGrid();
}

public function setListGridParams() {

$this->ups_key = 'admin.grid_filter.' . $this->name .'_'. $this->trait_params['grid_name'];

$this->grid = $this->loadDataGrid($this->trait_params['grid_name'], false, $this->ups_key);

}

public function renderListItemsGrid(){

if($this->trait_params['tool_buttons']){
$this->cms_template->addToolButtons($this->trait_params['tool_buttons']);
}

return $this->cms_template->getRenderedAsset('ui/grid', [
'grid' => $this->grid,
'page_title' => $this->trait_params['title'],
'source_url' => $this->trait_params['grid_url']
]);
}

public function getListItems(){

$this->model->setPerPage(\admin::perpage);

$filter = [];
$filter_str = \cmsUser::getUPSActual($this->ups_key, $this->request->get('filter', ''));

if ($filter_str){
\parse_str($filter_str, $filter);
$this->model->applyGridFilter($this->grid, $filter);
}

if($this->trait_params['list_callback']){
$this->model = \call_user_func($this->trait_params['list_callback'], $this->model);
}

$total = $this->model->getCount($this->trait_params['table_name']);
$perpage = isset($filter['perpage']) ? $filter['perpage'] : \admin::perpage;
$pages = \ceil($total / $perpage);

$data = $this->model->get($this->trait_params['table_name']);

$this->cms_template->renderGridRowsJSON($this->grid, $data, $total, $pages);

return $this->halt();
}

}
36 changes: 27 additions & 9 deletions system/controllers/images/backend/actions/presets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@

class actionImagesPresets extends cmsAction {

public function run(){

$grid = $this->loadDataGrid('presets');

return cmsTemplate::getInstance()->render('backend/presets', array(
'grid' => $grid
));
use icms\controllers\admin\traits\listgrid;

public function __construct($controller, $params = []) {

parent::__construct($controller, $params);

$this->setProperty('table_name', 'images_presets');
$this->setProperty('grid_name', 'presets');
$this->setProperty('grid_url', $this->cms_template->href_to('presets'));
$this->setProperty('title', LANG_IMAGES_PRESETS);
$this->setProperty('tool_buttons', [
[
'class' => 'add',
'title' => LANG_ADD,
'href' => $this->cms_template->href_to('presets_add')
]
]);
$this->setProperty('list_callback', function ($model) {

$model->orderByList([
['by' => 'is_internal', 'to' => 'asc'],
['by' => 'width', 'to' => 'asc'],
['by' => 'quality', 'to' => 'desc']
]);

return $model;
});

}

}


21 changes: 0 additions & 21 deletions system/controllers/images/backend/actions/presets_ajax.php

This file was deleted.

4 changes: 0 additions & 4 deletions system/controllers/images/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

class modelImages extends cmsModel{

public function getPresetsCount(){
return $this->getCount('images_presets');
}

public function getPresets(){
return $this->get('images_presets', function($item, $model){
$item['wm_image'] = cmsModel::yamlToArray($item['wm_image']);
Expand Down
34 changes: 6 additions & 28 deletions system/controllers/rss/backend/actions/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,15 @@

class actionRssIndex extends cmsAction {

public function run(){
use icms\controllers\admin\traits\listgrid;

$grid = $this->loadDataGrid('feeds');
public function __construct($controller, $params = []) {

if ($this->request->isAjax()) {
parent::__construct($controller, $params);

$this->model->setPerPage(admin::perpage);

$filter = array();
$filter_str = $this->request->get('filter', '');

if ($filter_str){
parse_str($filter_str, $filter);
$this->model->applyGridFilter($grid, $filter);
}

$total = $this->model->getFeedsCount();
$perpage = isset($filter['perpage']) ? $filter['perpage'] : admin::perpage;
$pages = ceil($total / $perpage);

$feeds = $this->model->getFeeds();

$this->cms_template->renderGridRowsJSON($grid, $feeds, $total, $pages);

$this->halt();

}

return $this->cms_template->render('backend/index', array(
'grid' => $grid
));
$this->setProperty('table_name', 'rss_feeds');
$this->setProperty('grid_name', 'feeds');
$this->setProperty('grid_url', $this->cms_template->href_to(''));

}

Expand Down
8 changes: 0 additions & 8 deletions system/controllers/rss/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

class modelRss extends cmsModel {

public function getFeedsCount() {
return $this->getCount('rss_feeds');
}

public function getFeeds() {
return $this->get('rss_feeds');
}

private function feed() {
return $this->getItem('rss_feeds', function ($item, $model) {
$item['mapping'] = cmsModel::yamlToArray($item['mapping']);
Expand Down
33 changes: 0 additions & 33 deletions system/controllers/tags/backend/actions/ajax.php

This file was deleted.

19 changes: 14 additions & 5 deletions system/controllers/tags/backend/actions/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

class actionTagsIndex extends cmsAction {

public function run(){
use icms\controllers\admin\traits\listgrid;

$grid = $this->loadDataGrid('tags');
public function __construct($controller, $params = []) {

return cmsTemplate::getInstance()->render('backend/tags', array(
'grid' => $grid
));
parent::__construct($controller, $params);

$this->setProperty('table_name', 'tags');
$this->setProperty('grid_name', 'tags');
$this->setProperty('grid_url', $this->cms_template->href_to('index'));
$this->setProperty('tool_buttons', [
[
'class' => 'refresh',
'title' => LANG_TAGS_RECOUNT,
'href' => $this->cms_template->href_to('recount')
]
]);

}

Expand Down
26 changes: 21 additions & 5 deletions system/controllers/users/backend/actions/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@

class actionUsersFields extends cmsAction {

public function run(){
use icms\controllers\admin\traits\listgrid;

$grid = $this->loadDataGrid('fields');
public function __construct($controller, $params = []) {

return cmsTemplate::getInstance()->render('backend/fields', array(
'grid' => $grid
));
parent::__construct($controller, $params);

$this->setProperty('table_name', '{users}_fields');
$this->setProperty('grid_name', 'fields');
$this->setProperty('grid_url', $this->cms_template->href_to('fields'));
$this->setProperty('title', LANG_USERS_CFG_FIELDS);
$this->setProperty('tool_buttons', [
[
'class' => 'add',
'title' => LANG_CP_FIELD_ADD,
'href' => $this->cms_template->href_to('fields_add')
]
]);
$this->setProperty('list_callback', function ($model) {

$model->orderBy('ordering');

return $model;
});

}

Expand Down
23 changes: 0 additions & 23 deletions system/controllers/users/backend/actions/fields_ajax.php

This file was deleted.

20 changes: 15 additions & 5 deletions system/controllers/users/backend/actions/migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

class actionUsersMigrations extends cmsAction {

public function run(){
use icms\controllers\admin\traits\listgrid;

$grid = $this->loadDataGrid('migrations');
public function __construct($controller, $params = []) {

return cmsTemplate::getInstance()->render('backend/migrations', array(
'grid' => $grid
));
parent::__construct($controller, $params);

$this->setProperty('table_name', '{users}_groups_migration');
$this->setProperty('grid_name', 'migrations');
$this->setProperty('grid_url', $this->cms_template->href_to('migrations'));
$this->setProperty('title', LANG_USERS_CFG_MIGRATION);
$this->setProperty('tool_buttons', [
[
'class' => 'add',
'title' => LANG_USERS_MIG_ADD,
'href' => $this->cms_template->href_to('migrations_add')
]
]);

}

Expand Down
Loading

0 comments on commit ee0d690

Please sign in to comment.