Completed
Push — master ( edb2b9...da334f )
by Dmitry
13:12
created

MainColumn::buildUrl()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.9666
c 0
b 0
f 0
cc 4
nc 5
nop 1
crap 20
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\grid;
12
13
use Closure;
14
use hipanel\helpers\ArrayHelper;
15
use hipanel\widgets\NoteBlock;
16
use hipanel\widgets\XEditable;
17
use Yii;
18
use yii\helpers\Html;
19
use yii\helpers\Url;
20
21
class MainColumn extends DataColumn
22
{
23
    /**
24
     * @var true|string|array
25
     * true - note editing is enabled in this column, target attribute name is `note`
26
     * string - target atttribute name
27
     * array - array of notes for an attribute
28
     */
29
    public $note;
30
31
    /**
32
     * @var string name of attribute with extra data showed under main data
33
     */
34
    public $extraAttribute;
35
36
    /**
37
     * @var array will be passed to the ```pluginOptions``` of [[XEditable]] plugin
38
     */
39
    public $noteOptions = [];
40
41
    /**
42
     * @var string|Closure badges string or callback to render badges
43
     */
44
    public $badges;
45
46
    /**
47
     * Builds url.
48
     * @param string $url
49
     * @return string
50
     */
51
    protected function buildUrl($url)
52
    {
53
        if (strncmp($url, '/', 1) === 0) {
54
            return $url;
55
        }
56
        $baseUrl = isset($this->grid->controllerUrl) ? $this->grid->controllerUrl : '';
57
58
        return $baseUrl ? Url::to($baseUrl . '/' . $url) : Url::to($url);
59
    }
60
61
    public function init()
62
    {
63
        parent::init();
64
        $this->noteOptions = ArrayHelper::merge([
65
            'url' => $this->buildUrl('set-note'),
66
        ], $this->noteOptions);
67
    }
68
69
    /** {@inheritdoc} */
70
    protected function renderDataCellContent($model, $key, $index)
71
    {
72
        $value = $this->renderValue($model, $key, $index);
73
        $note = $this->renderNoteLink($model, $key, $index);
74
        $extra = $this->renderExtra($model);
75
        $badges = $this->renderBadges($model, $key, $index);
76
77
        return $value . $extra . $badges . $note;
78
    }
79
80
    protected function renderValue($model, $key, $index)
81
    {
82
        if ($this->value !== null) {
83
            if (is_string($this->value)) {
84
                $value = ArrayHelper::getValue($model, $this->value);
85
            } else {
86
                $value = call_user_func($this->value, $model, $key, $index, $this);
87
            }
88
        } else {
89
            $value = $this->renderViewLink($model, $key, $index);
90
        }
91
        return $value;
92
    }
93
94
    protected function renderBadges($model, $key, $index)
95
    {
96
        $badges = $this->badges instanceof Closure
97
                    ? call_user_func($this->badges, $model, $key, $index)
98
                    : $this->badges;
99
100
        return $badges ? (' ' . $badges) : '';
101
    }
102
103
    protected function renderExtra($model)
104
    {
105
        $value = $this->extraAttribute ? $model->{$this->extraAttribute} : null;
106
107
        return $value ? "<br>$value" : '';
108
    }
109
110
    protected function renderViewLink($model, $key, $index)
111
    {
112
        $value = parent::renderDataCellContent($model, $key, $index);
113
114
        return Html::a($value, [$this->buildUrl('view'), 'id' => $model->id], ['class' => 'bold']);
115
    }
116
117
    /**
118
     * Renders link to edit note.
119
     * @param $model
120
     * @param $key
121
     * @param $index
122
     * @return string
123
     */
124
    protected function renderNoteLink($model, $key, $index)
125
    {
126
        if (empty($this->note)) {
127
            return '';
128
        }
129
        if (is_array($this->note)) {
130
            return array_reduce(
131
                $this->note,
132
                fn (string $res, string $note): string => $res . NoteBlock::widget([
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE, expecting ',' or ')'
Loading history...
133
                    'model' => $model,
134
                    'note' => $note,
135
                    'noteOptions' => $this->noteOptions[$note],
136
                ]),
137
                '',
138
            );
139
        }
140
        if (is_string($this->note)) {
141
            return NoteBlock::widget([
142
                'model' => $model,
143
                'note' => $this->note,
144
                'noteOptions' => $this->noteOptions,
145
            ]);
146
        }
147
        return '';
148
    }
149
}
150