123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?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()
- {
- $this->initDefaultButton('view', 'eye-open');
- $this->initDefaultButton('update', 'pencil');
- $this->initDefaultButton('delete', 'trash', [
- 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
- 'data-method' => 'post',
- ]);
- }
-
- protected function initDefaultButton($name, $iconName, $additionalOptions = [])
- {
- if (!isset($this->buttons[$name]) && strpos($this->template, '{' . $name . '}') !== false) {
- $this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) {
- switch ($name) {
- case 'view':
- $title = Yii::t('yii', 'View');
- break;
- case 'update':
- $title = Yii::t('yii', 'Update');
- break;
- case 'delete':
- $title = Yii::t('yii', 'Delete');
- break;
- default:
- $title = ucfirst($name);
- }
- $options = array_merge([
- 'title' => $title,
- 'aria-label' => $title,
- 'data-pjax' => '0',
- ], $additionalOptions, $this->buttonOptions);
- $icon = Html::tag('span', '', ['class' => "glyphicon glyphicon-$iconName"]);
- return Html::a($icon, $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);
- }
- $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);
- }
- return '';
- }, $this->template);
- }
- }
|