Completed
Push — master ( 28d4b7...655eb8 )
by Andrii
06:02
created

MainColumn::renderExtra()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 3
eloc 3
nc 4
nop 1
crap 12
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\grid;
13
14
use hipanel\helpers\ArrayHelper;
15
use hipanel\widgets\XEditable;
16
use Closure;
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
25
     * true - note editing is enabled in this column, target attribute name is `note`
26
     * string - target atttribute name
27
     */
28
    public $note;
29
30
    /**
31
     * @var string name of attribute with extra data showed under main data
32
     */
33
    public $extraAttribute;
34
35
    /**
36
     * @var array will be passed to the ```pluginOptions``` of [[XEditable]] plugin
37
     */
38
    public $noteOptions = [];
39
40
    /**
41
     * @var string|Closure badges string or callback to render badges
42
     */
43
    public $badges;
44
45
    /**
46
     * Builds url.
47
     * @param string $url
48
     * @return string
49
     */
50
    public function buildUrl($url)
51
    {
52
        if (strncmp($url, '/', 0) === 0) {
53
            return $url;
54
        }
55
        $baseUrl = isset($this->grid->controllerUrl) ? $this->grid->controllerUrl : '';
56
        return $baseUrl ? Url::to($baseUrl . '/' . $url) : Url::to($url);
57
    }
58
59
    public function init()
60
    {
61
        parent::init();
62
        $this->noteOptions = ArrayHelper::merge([
63
            'url' => $this->buildUrl('set-note'),
64
        ], $this->noteOptions);
65
    }
66
67
    /** {@inheritdoc} */
68
    protected function renderDataCellContent($model, $key, $index)
69
    {
70
        if ($this->value !== null) {
71
            if (is_string($this->value)) {
72
                $value = ArrayHelper::getValue($model, $this->value);
73
            } else {
74
                $value = call_user_func($this->value, $model, $key, $index, $this);
75
            }
76
        } else {
77
            $value = $this->renderViewLink($model, $key, $index);
78
        }
79
        $note = $this->renderNoteLink($model, $key, $index);
80
        $extra = $this->renderExtra($model);
81
        $badges = $this->badges instanceof Closure ? call_user_func($this->badges, $model, $key, $index) : $this->badges;
82
83
        return $value . $extra . ($badges ? ' ' . $badges : '') . ($note ? '<br>' . $note : '');
84
    }
85
86
    public function renderExtra($model)
87
    {
88
        $value = $this->extraAttribute ? $model->{$this->extraAttribute} : null;
89
90
        return $value ? "<br>$value" : '';
91
    }
92
93
    public function renderViewLink($model, $key, $index)
94
    {
95
        $value = parent::renderDataCellContent($model, $key, $index);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (renderDataCellContent() instead of renderViewLink()). Are you sure this is correct? If so, you might want to change this to $this->renderDataCellContent().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
96
        return Html::a($value, [$this->buildUrl('view'), 'id' => $model->id], ['class' => 'bold']);
97
    }
98
99
    /**
100
     * Renders link to edit note.
101
     * @param $model
102
     * @param $key
103
     * @param $index
104
     * @return string|null
105
     */
106
    public function renderNoteLink($model, $key, $index)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $index is not used and could be removed.

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

Loading history...
107
    {
108
        return $this->note ? Html::tag('span', Yii::t('hipanel', 'Note') . ': ', ['class' => 'bold']) . XEditable::widget([
109
                'model' => $model,
110
                'attribute' => $this->note === true ? 'note' : $this->note,
111
                'pluginOptions' => $this->noteOptions,
112
            ]) : null;
113
    }
114
}
115