Completed
Push — master ( b97c15...2a91fa )
by Loban
03:36
created

ActionColumn   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 66
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C initDefaultButtons() 0 35 7
1
<?php
2
/**
3
 * @link https://github.com/LAV45/yii2-translated-behavior
4
 * @copyright Copyright (c) 2015 LAV45!
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\translate\grid;
10
11
use yii\helpers\Url;
12
use yii\helpers\Html;
13
14
/**
15
 * Class ActionColumn
16
 * @package lav45\translate\grid
17
 */
18
class ActionColumn extends \yii\grid\ActionColumn
19
{
20
    /**
21
     * @var string
22
     */
23
    public $header = 'Translate';
24
    /**
25
     * @var string
26
     */
27
    public $template = '';
28
    /**
29
     * @var array
30
     */
31
    public $buttons = [];
32
    /**
33
     * @var array
34
     */
35
    public $languages = [];
36
    /**
37
     * @var string
38
     */
39
    public $languageAttribute = 'lang_id';
40
    /**
41
     * @var bool
42
     */
43
    public $ajax = false;
44
45
    /**
46
     * @inheritdoc
47
     */
48
    protected function initDefaultButtons()
49
    {
50
        foreach ($this->languages as $lang_id => $lang) {
51
            $name = "update-$lang_id";
52
            $this->template .= ' {' . $name . '}';
53
            if (!isset($this->buttons[$name])) {
54
                $this->buttons[$name] = function() use ($lang, $lang_id) {
55
                    /** @var TranslatedTrait $model */
56
                    $model = func_get_arg(1);
57
                    $key = func_get_arg(2);
58
59
                    $params = is_array($key) ? $key : ['id' => (string) $key];
60
                    $params[$this->languageAttribute] = $lang_id;
61
                    $params[0] = $this->controller ? $this->controller . '/update' : 'update';
62
63
                    $url = Url::toRoute($params);
64
65
                    $color = $model->hasTranslate($lang_id) ? 'info' : 'default';
66
67
                    $options = [
68
                        'class' => "btn btn-xs btn-$color",
69
                        'title' => "Edit $lang version",
70
                        'data-pjax' => '0',
71
                    ];
72
73
                    if ($this->ajax) {
74
                        $options['data-href'] = $url;
75
                        return Html::button('<span class="glyphicon glyphicon-pencil"></span> ' . $lang, $options);
76
                    } else {
77
                        return Html::a('<span class="glyphicon glyphicon-pencil"></span> ' . $lang, $url, $options);
78
                    }
79
                };
80
            }
81
        }
82
    }
83
}