Passed
Push — master ( 01a3c8...def57d )
by Rodrigo
02:34
created

ActionColumn::init()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 8
c 1
b 0
f 1
nc 4
nop 0
dl 0
loc 13
rs 8.8333
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 $pjax = null;
26
27
    public $messages = [];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function init()
33
    {
34
        parent::init();
35
        ActionColumnAsset::register(Yii::$app->view);
36
37
        if(!$this->controller && !$this->action){
38
            $this->action = "{action}";
39
        }
40
        else if ($this->controller && !$this->action){
41
            $this->action = "{controller}/{action}";
42
        }
43
        else if (!$this->controller && $this->action) {
44
            $this->controller = Yii::$app->controller->id;
45
        }
46
    }
47
48
    protected function initDefaultButtons()
49
    {
50
        $this->initDefaultButton('go', 'external-link-alt', ['target' => '_blank']);
51
        $this->initDefaultButton('view', 'eye');
52
        $this->initDefaultButton('update', 'edit');
53
        $this->initDefaultButton('delete', 'trash', [
54
            'data-success' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'success', 'Success!')),
55
            'data-delete' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'delete', 'Are you sure you want to delete this item?')),
56
            'data-yes' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'yes', 'Yes')),
57
            'data-no' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'no', 'No')),
58
            'data-pjax' => $this->pjax
59
        ]);
60
    }
61
    
62
    protected function initDefaultButton($name, $iconName, $additionalOptions = [])
63
    {
64
        if (!isset($this->buttons[$name]) && strpos($this->template, '{' . $name . '}') !== false) {
65
            $this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) {
66
                switch ($name) {
67
                    case 'go': 
68
                        $title = Yii::t('yii', 'Redirect');
69
                        
70
                        if ($this->redirect) {
71
                            $url = Url::to(["{$this->redirect}", 'id' => $model->id, 'slug' => $model->getAttribute('slug')], true);
72
                        } else {
73
                            $this->controller = Yii::$app->controller->id;
74
                            $url = Url::to(["{$this->controller}/view", 'id' => $model->id, 'slug' => $model->getAttribute('slug')], true);
75
                        }
76
77
                        $url = str_replace(Yii::$app->request->baseUrl, $this->baseUrl, $url);
78
                        break;
79
                    case 'view':
80
                        $title = Yii::t('yii', 'View');
81
                        break;
82
                    case 'update':
83
                        $title = Yii::t('yii', 'Update');
84
                        break;
85
                    case 'delete':
86
                        $title = Yii::t('yii', 'Delete');
87
                        break;
88
                    default:
89
                        $title = ucfirst($name);
90
                }
91
                $options = array_merge([
92
                    'title' => $title,
93
                    'aria-label' => $title,
94
                    'data-pjax' => '0',
95
                ], $additionalOptions, $this->buttonOptions);
0 ignored issues
show
Bug Best Practice introduced by
The property buttonOptions does not exist on dynamikaweb\grid\ActionColumn. Since you implemented __get, consider adding a @property annotation.
Loading history...
96
                $icon = Icon::show($iconName, ['framework' => Icon::FAS]);
97
                return Html::a($icon, $url, $options);
98
            };
99
        }
100
    }
101
102
    public function createUrl($action, $model, $key, $index)
103
    {
104
        if (is_callable($this->urlCreator)) {
105
            return call_user_func($this->urlCreator, $action, $model, $key, $index, $this);
106
        }
107
108
        $params = is_array($key) ? $key : ['id' => (string) $key];
109
        $params = array_merge($params, is_array($this->action)? $this->action: [0 => $this->action]);
110
        $params[0] = strtr($params[0], [
111
            '{controller}' => $this->controller,
112
            '{action}' => $action
113
        ]);
114
115
        return Url::toRoute($params);
116
    }
117
}
118