Completed
Push — master ( 563fad...95699a )
by Klochok
10:47
created

MainColumn::renderNoteLink()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 6
nc 2
nop 3
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 array will be passed to the ```pluginOptions``` of [[XEditable]] plugin
32
     */
33
    public $noteOptions = [];
34
35
    /**
36
     * @var string|Closure badges string or callback to render badges
37
     */
38
    public $badges;
39
40
    /**
41
     * Builds url.
42
     * @param string $url
43
     * @return string
44
     */
45
    public function buildUrl($url)
46
    {
47
        if (strncmp($url, '/', 0) === 0) {
48
            return $url;
49
        }
50
        $baseUrl = isset($this->grid->controllerUrl) ? $this->grid->controllerUrl : '';
51
        return $baseUrl ? Url::to($baseUrl . '/' . $url) : Url::to($url);
52
    }
53
54
    public function init()
55
    {
56
        parent::init();
57
        $this->noteOptions = ArrayHelper::merge([
58
            'url' => $this->buildUrl('set-note'),
59
        ], $this->noteOptions);
60
    }
61
62
    /** {@inheritdoc} */
63
    protected function renderDataCellContent($model, $key, $index)
64
    {
65
        if ($this->value !== null) {
66
            if (is_string($this->value)) {
67
                $value = ArrayHelper::getValue($model, $this->value);
68
            } else {
69
                $value = call_user_func($this->value, $model, $key, $index, $this);
70
            }
71
        } else {
72
            $value = $this->renderViewLink($model, $key, $index);
73
        }
74
        $note = $this->renderNoteLink($model, $key, $index);
75
        $badges = $this->badges instanceof Closure ? call_user_func($this->badges, $model, $key, $index) : $this->badges;
76
77
        return $value . ($badges ? ' ' . $badges : '') . ($note ? '<br>' . $note : '');
78
    }
79
80
    public function renderViewLink($model, $key, $index)
81
    {
82
        $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...
83
        return Html::a($value, [$this->buildUrl('view'), 'id' => $model->id], ['class' => 'bold']);
84
    }
85
86
    /**
87
     * Renders link to edit note.
88
     * @param $model
89
     * @param $key
90
     * @param $index
91
     * @return string|null
92
     */
93
    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...
94
    {
95
        return $this->note ? Html::tag('span', Yii::t('hipanel', 'Note') . ': ', ['class' => 'bold']) . XEditable::widget([
96
                'model' => $model,
97
                'attribute' => $this->note === true ? 'note' : $this->note,
98
                'pluginOptions' => $this->noteOptions,
99
            ]) : null;
100
    }
101
}
102