|
- <?php
-
-
- namespace yii\grid;
-
- use Yii;
- use yii\helpers\Html;
- use yii\helpers\Url;
-
-
- class ActionColumn extends Column
- {
-
-
- public $headerOptions = ['class' => 'action-column'];
-
-
- public $controller;
-
-
- public $template = '{view} {update} {delete}';
-
-
- public $buttons = [];
-
-
- public $visibleButtons = [];
-
-
- public $urlCreator;
-
-
- public $buttonOptions = [];
-
-
-
-
- public function init()
- {
- parent::init();
- $this->initDefaultButtons();
- }
-
-
-
- protected function initDefaultButtons()
- {
- if (!isset($this->buttons['view'])) {
- $this->buttons['view'] = function ($url, $model, $key) {
- $options = array_merge([
- 'title' => Yii::t('yii', 'View'),
- 'aria-label' => Yii::t('yii', 'View'),
- 'data-pjax' => '0',
- ], $this->buttonOptions);
- return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, $options);
- };
- }
- if (!isset($this->buttons['update'])) {
- $this->buttons['update'] = function ($url, $model, $key) {
- $options = array_merge([
- 'title' => Yii::t('yii', 'Update'),
- 'aria-label' => Yii::t('yii', 'Update'),
- 'data-pjax' => '0',
- ], $this->buttonOptions);
- return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
- };
- }
- if (!isset($this->buttons['delete'])) {
- $this->buttons['delete'] = function ($url, $model, $key) {
- $options = array_merge([
- 'title' => Yii::t('yii', 'Delete'),
- 'aria-label' => Yii::t('yii', 'Delete'),
- 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
- 'data-method' => 'post',
- 'data-pjax' => '0',
- ], $this->buttonOptions);
- return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
- };
- }
- }
-
-
-
- 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];
- $params[0] = $this->controller ? $this->controller . '/' . $action : $action;
-
- return Url::toRoute($params);
- }
- }
-
-
-
- protected function renderDataCellContent($model, $key, $index)
- {
- return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
- $name = $matches[1];
-
- if (isset($this->visibleButtons[$name])) {
- $isVisible = $this->visibleButtons[$name] instanceof \Closure
- ? call_user_func($this->visibleButtons[$name], $model, $key, $index)
- : $this->visibleButtons[$name];
- } else {
- $isVisible = true;
- }
-
- if ($isVisible && isset($this->buttons[$name])) {
- $url = $this->createUrl($name, $model, $key, $index);
- return call_user_func($this->buttons[$name], $url, $model, $key);
- } else {
- return '';
- }
- }, $this->template);
- }
- }
|