/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/grid/ActionColumn.php
244 строки
8 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\grid; use common\components\crud\GridCrud; use common\helpers\UrlHelper; use common\models\base\BaseModel; use Yii; use yii\bootstrap\Html; use yii\helpers\ArrayHelper; use yii\helpers\Url; use yii\widgets\Menu; /** * Колонка меню для * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2020 */ class ActionColumn extends \yii\grid\Column { /** * @inheritdoc */ public $headerOptions = ['class' => 'action-column']; /** * @var string the ID of the controller that should handle the actions specified here. * If not set, it will use the currently active controller. This property is mainly used by * [[urlCreator]] to create URLs for different actions. The value of this property will be prefixed * to each action name to form the route of the action. */ public $controller; /** * @var callable a callback that creates a button URL using the specified model information. * The signature of the callback should be the same as that of [[createUrl()]] * Since 2.0.10 it can accept additional parameter, which refers to the column instance itself: * * ```php * function (string $action, mixed $model, mixed $key, integer $index, ActionColumn $this) { * //return string; * } * ``` * * If this property is not set, button URLs will be created using [[createUrl()]]. */ public $urlCreator; /** * @var GridCrud */ public $crud; /** * Массив кнопок меню * @var array */ public $buttons = []; /** * Базовый объект с которым работаем * @var null|BaseModel */ public $parent; /** * Initializes the default button rendering callbacks. */ protected function initDefaultButtons($model) { $this->initDefaultButton('view', Yii::t('app', 'View'), 'eye'); $this->initDefaultButton('update', Yii::t('app', 'Edit'), 'pencil'); if (is_array($model)) { $label = ArrayHelper::getValue($model, 'label'); if (!$label) { $label = ArrayHelper::getValue($model, 'title'); } } else { $label = $model->__toString(); } if ($label) { $confirm = Yii::t('app', 'Please confirm to delete «{0}»?', [ '0' => $label ]); } else { $confirm = Yii::t('app', 'Please confirm to delete ?'); } $this->initDefaultButton('delete', Yii::t('app', 'Delete'), 'cross2', [ 'class' => 'text-danger', 'data' => [ 'confirm' => $confirm, ] ]); } /** * Creates a URL for the given action and model. * This method is called for each button and each row. * @param string $action the button name (or action ID) * @param \yii\db\ActiveRecordInterface $model the data model * @param mixed $key the key associated with the data model * @param int $index the current row index * @return string the created URL */ public function createUrl($action, $model, $key, $index) { if (is_callable($this->urlCreator)) { return call_user_func($this->urlCreator, $action, $model, $key, $index, $this); } else { $params = is_array($key) ? $key : ['id' => (string)$key]; array_walk($params, function (&$item) { $item = (string)($item); }); $url = $this->controller ? $this->controller . '/' . $action : $action; return UrlHelper::to_sig($url, $params); } } /** * @param $title * @param $icon * @param array $options * @return array */ protected function initDefaultButton($name, $title, $icon = null, $options = []) { $rule = $this->crud->controller ? ['rule' => $this->crud->controller->getPermission($name)] : []; $this->buttons[$name] = ArrayHelper::merge([ 'label' => $title, 'icon' => $icon, 'name' => $name, 'options' => array_merge([ 'title' => $title, 'aria-label' => $title, 'data' => ['pjax' => '0'], ], $options) ], $rule); } /** * @inheritdoc */ protected function renderDataCellContent($model, $key, $index) { $items = []; if ($this->crud) { $this->initDefaultButtons($model); $this->buttons = $this->crud->initButtons($this->buttons, $model); } $key_array = []; if ($this->parent) { $key_array['parent_id'] = $this->parent->primaryKey; } $key_array['id'] = $key; $user = Yii::$app->user; // сортировка кнопок $buttons = []; $the_index = 0; foreach ($this->buttons as $button) { if ($label = ArrayHelper::getValue($button, 'label')) { $ind = ArrayHelper::getValue($button, 'options.index', $the_index); $button['options'] = ArrayHelper::merge(ArrayHelper::getValue($button, 'options'), [ 'title' => $label, 'aria-label' => $label, 'data' => ['pjax' => '0'], ]); $buttons[$ind] = $button; $the_index += 10; } } ksort($buttons); foreach ($buttons as $the_index => $button) { $rule = ArrayHelper::getValue($button, 'rule'); if (!$rule or $user->can($rule)) { if ($button['label'] == '-') { $items[] = [ 'options' => [ 'class' => 'divider' ] ]; } else { if (!($url = ArrayHelper::getValue($button, 'url'))) { $url = $this->createUrl($button['name'], $model, $key_array, $index); } $items[] = [ 'label' => Html::a( Html::icon($button['icon'], ['tag' => 'i', 'prefix' => 'icon-']) . ' ' . $button['label'], $url, ArrayHelper::getValue($button, 'options') ) ]; } } } if (count($items) > 0) { $items = Menu::widget([ 'encodeLabels' => false, 'options' => [ 'class' => 'dropdown-menu dropdown-menu-right', 'style' => 'width: 250px' ], 'items' => $items ]); return Menu::widget([ 'encodeLabels' => false, 'options' => [ 'class' => 'icons-list', 'data' => 'menu', ], 'items' => [ [ 'label' => Html::a( Html::icon('menu9', ['prefix' => 'icon-']), '#', [ 'class' => 'dropdown-toggle', 'data' => [ 'toggle' => 'dropdown', ], 'aria-expanded' => false ] ) . $items ] ] ]); } else { return null; } } }