1 | <?php |
||
2 | |||
3 | namespace dynamikaweb\grid; |
||
4 | |||
5 | use kartik\icons\Icon; |
||
6 | use yii\helpers\Html; |
||
7 | use yii\helpers\Url; |
||
8 | use yii\helpers\ArrayHelper; |
||
9 | |||
10 | |||
11 | use Yii; |
||
12 | |||
13 | class ActionColumn extends \yii\grid\ActionColumn |
||
14 | { |
||
15 | public $headerOptions = ['class' => 'action-column', 'width' => '110px']; |
||
16 | |||
17 | public $redirect = null; |
||
18 | |||
19 | public $baseUrl = ""; |
||
20 | |||
21 | public $controller = null; |
||
22 | |||
23 | public $action = null; |
||
24 | |||
25 | public $modelClass = null; |
||
26 | |||
27 | public $pjax = null; |
||
28 | |||
29 | public $messages = []; |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | public function init() |
||
35 | { |
||
36 | parent::init(); |
||
37 | ActionColumnAsset::register(Yii::$app->view); |
||
38 | |||
39 | if(!$this->controller && !$this->action){ |
||
40 | $this->action = "{action}"; |
||
41 | } |
||
42 | else if ($this->controller && !$this->action){ |
||
43 | $this->action = "{controller}/{action}"; |
||
44 | } |
||
45 | else if (!$this->controller && $this->action) { |
||
46 | $this->controller = Yii::$app->controller->id; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | protected function initDefaultButtons() |
||
51 | { |
||
52 | $this->initDefaultButton('go', 'share', ['target' => '_blank']); |
||
53 | $this->initDefaultButton('view', 'eye'); |
||
54 | $this->initDefaultButton('update', 'pen'); |
||
55 | $this->initDefaultButton('delete', 'trash', [ |
||
56 | 'data-success' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'success', 'Success!')), |
||
57 | 'data-delete' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'delete', 'Are you sure you want to delete this item?')), |
||
58 | 'data-yes' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'yes', 'Yes')), |
||
59 | 'data-no' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'no', 'No')), |
||
60 | 'data-pjax' => $this->pjax |
||
61 | ]); |
||
62 | } |
||
63 | |||
64 | protected function initDefaultButton($name, $iconName, $additionalOptions = []) |
||
65 | { |
||
66 | if (!isset($this->buttons[$name]) && strpos($this->template, '{' . $name . '}') !== false) { |
||
67 | $this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) { |
||
68 | switch ($name) { |
||
69 | case 'go': |
||
70 | $title = Yii::t('yii', 'Redirect'); |
||
71 | |||
72 | if ($this->redirect) { |
||
73 | $url = Url::to(["{$this->redirect}", 'id' => $model->id, 'slug' => $model->getAttribute('slug')], true); |
||
74 | } else { |
||
75 | $this->controller = Yii::$app->controller->id; |
||
76 | $url = Url::to(["{$this->controller}/view", 'id' => $model->id, 'slug' => $model->getAttribute('slug')], true); |
||
77 | } |
||
78 | |||
79 | $url = preg_replace(['#'.Yii::$app->request->baseUrl.'#'], $this->baseUrl, $url, 1); |
||
80 | break; |
||
81 | case 'view': |
||
82 | $title = Yii::t('yii', 'View'); |
||
83 | break; |
||
84 | case 'update': |
||
85 | $title = Yii::t('yii', 'Update'); |
||
86 | break; |
||
87 | case 'delete': |
||
88 | $title = Yii::t('yii', 'Delete'); |
||
89 | break; |
||
90 | default: |
||
91 | $title = ucfirst($name); |
||
92 | } |
||
93 | $options = array_merge([ |
||
94 | 'title' => $title, |
||
95 | 'aria-label' => $title, |
||
96 | 'data-pjax' => '0', |
||
97 | ], $additionalOptions, $this->buttonOptions); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
98 | $icon = Icon::show($iconName, ['framework' => Icon::FAS]); |
||
99 | return Html::a($icon, $url, $options); |
||
100 | }; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | public function createUrl($action, $model, $key, $index) |
||
105 | { |
||
106 | if (is_callable($this->urlCreator)) { |
||
107 | return call_user_func($this->urlCreator, $action, $model, $key, $index, $this); |
||
108 | } |
||
109 | |||
110 | $params = is_array($key) ? $key : ['id' => (string) $key]; |
||
111 | $params = isset($this->modelClass)?array_merge($params, ['modelClass' => $this->modelClass]):$params; |
||
112 | $params = array_merge($params, is_array($this->action)? $this->action: [0 => $this->action]); |
||
113 | $params[0] = strtr($params[0], [ |
||
114 | '{controller}' => $this->controller, |
||
115 | '{action}' => $action |
||
116 | ]); |
||
117 | |||
118 | return Url::toRoute($params); |
||
119 | } |
||
120 | } |
||
121 |