ActionColumn   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 63
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B initDefaultButtons() 0 36 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\Html;
12
use yii\helpers\Url;
13
14
/**
15
 * Class ActionColumn
16
 * @package lav45\translate\grid
17
 */
18
class ActionColumn extends \yii\grid\ActionColumn
19
{
20
    /** @var string */
21
    public $header = 'Translate';
22
    /** @var string */
23
    public $template = '';
24
    /** @var array */
25
    public $languages = [];
26
    /** @var string */
27
    public $languageAttribute = 'lang_id';
28
    /** @var bool */
29
    public $ajax = false;
30
    /** @var string */
31
    public $buttonTextTemplate = '<span class="glyphicon glyphicon-pencil"></span> {lang}';
32
    /** @var string */
33
    public $successTranslateButtonClass = 'btn-info';
34
    /** @var string */
35
    public $notTranslateButtonClass = 'btn-default';
36
    /** @var array */
37
    public $buttonOptions = [
38
        'class' => 'btn btn-xs',
39
    ];
40
41
    /**
42
     * @inheritdoc
43
     */
44
    protected function initDefaultButtons()
45
    {
46
        foreach ($this->languages as $lang_id => $lang) {
47
            $name = "update-{$lang_id}";
48
            $this->template .= ' {' . $name . '}';
49
            if (!isset($this->buttons[$name])) {
50
                $this->buttons[$name] = function($_url, $model, $key) use ($lang, $lang_id) {
51
                    /** @var \lav45\translate\TranslatedTrait $model */
52
                    $params = is_array($key) ? $key : ['id' => (string) $key];
53
                    $params[$this->languageAttribute] = $lang_id;
54
                    $params[0] = $this->controller ? $this->controller . '/update' : 'update';
55
56
                    $url = Url::toRoute($params);
57
58
                    $title = "Edit {$lang} version";
59
                    $options = array_merge([
60
                        'title' => $title,
61
                        'aria-label' => $title,
62
                        'data-pjax' => '0',
63
                    ], $this->buttonOptions);
64
65
                    $color = $model->hasTranslate($lang_id) ?
66
                        $this->successTranslateButtonClass :
67
                        $this->notTranslateButtonClass;
68
                    Html::addCssClass($options, $color);
69
70
                    $text = str_replace('{lang}', $lang, $this->buttonTextTemplate);
71
                    if ($this->ajax) {
72
                        $options['data-href'] = $url;
73
                        return Html::button($text, $options);
74
                    }
75
                    return Html::a($text, $url, $options);
76
                };
77
            }
78
        }
79
    }
80
}