Passed
Push — master ( 7d9ffe...01a3c8 )
by Rodrigo
01:49
created

ActionColumn   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 47
dl 0
loc 71
rs 10
c 2
b 1
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
B initDefaultButton() 0 36 8
A initDefaultButtons() 0 11 1
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 $pjax = null;
22
23
    public $messages = [];
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function init()
29
    {
30
        parent::init();
31
        ActionColumnAsset::register(Yii::$app->view);
32
    }
33
34
    protected function initDefaultButtons()
35
    {
36
        $this->initDefaultButton('go', 'external-link-alt', ['target' => '_blank']);
37
        $this->initDefaultButton('view', 'eye');
38
        $this->initDefaultButton('update', 'edit');
39
        $this->initDefaultButton('delete', 'trash', [
40
            'data-success' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'success', 'Success!')),
41
            'data-delete' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'delete', 'Are you sure you want to delete this item?')),
42
            'data-yes' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'yes', 'Yes')),
43
            'data-no' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'no', 'No')),
44
            'data-pjax' => $this->pjax
45
        ]);
46
    }
47
    
48
    protected function initDefaultButton($name, $iconName, $additionalOptions = [])
49
    {
50
        if (!isset($this->buttons[$name]) && strpos($this->template, '{' . $name . '}') !== false) {
51
            $this->buttons[$name] = function ($url, $model, $key) use ($name, $iconName, $additionalOptions) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
            $this->buttons[$name] = function ($url, $model, /** @scrutinizer ignore-unused */ $key) use ($name, $iconName, $additionalOptions) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
                switch ($name) {
53
                    case 'go': 
54
                        $title = Yii::t('yii', 'Redirect');
55
                        
56
                        if ($this->redirect) {
57
                            $url = Url::to(["{$this->redirect}", 'id' => $model->id, 'slug' => $model->getAttribute('slug')], true);
58
                        } else {
59
                            $this->controller = Yii::$app->controller->id;
60
                            $url = Url::to(["{$this->controller}/view", 'id' => $model->id, 'slug' => $model->getAttribute('slug')], true);
61
                        }
62
63
                        $url = str_replace(Yii::$app->request->baseUrl, $this->baseUrl, $url);
64
                        break;
65
                    case 'view':
66
                        $title = Yii::t('yii', 'View');
67
                        break;
68
                    case 'update':
69
                        $title = Yii::t('yii', 'Update');
70
                        break;
71
                    case 'delete':
72
                        $title = Yii::t('yii', 'Delete');
73
                        break;
74
                    default:
75
                        $title = ucfirst($name);
76
                }
77
                $options = array_merge([
78
                    'title' => $title,
79
                    'aria-label' => $title,
80
                    'data-pjax' => '0',
81
                ], $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...
82
                $icon = Icon::show($iconName, ['framework' => Icon::FAS]);
83
                return Html::a($icon, $url, $options);
84
            };
85
        }
86
    }
87
88
}
89