Completed
Push — master ( e9269b...da7cba )
by Andrii
04:15
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 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 1
b 0
f 1
nc 2
nop 3
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
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 Closure callback to render cell
37
     */
38
    public $renderer;
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->renderer instanceof Closure) {
66
            return call_user_func($this->renderer, $this, $model, $key, $index);
67
        } else {
68
            $link = $this->renderViewLink($model, $key, $index);
69
            $note = $this->renderNoteLink($model, $key, $index);
70
71
            return $link . ($note ? '<br>' . $note : '');
72
        }
73
    }
74
75
    public function renderViewLink($model, $key, $index)
76
    {
77
        $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...
78
        return Html::a($value, [$this->buildUrl('view'), 'id' => $model->id], ['class' => 'bold']);
79
    }
80
81
    /**
82
     * Renders link to edit note.
83
     * @param $model
84
     * @param $key
85
     * @param $index
86
     * @return string|null
87
     */
88
    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...
89
    {
90
        return $this->note ? Html::tag('span', Yii::t('app', 'Note') . ': ', ['class' => 'bold']) . XEditable::widget([
91
            'model'         => $model,
92
            'attribute'     => $this->note === true ? 'note' : $this->note,
93
            'pluginOptions' => $this->noteOptions,
94
        ]) : null;
95
    }
96
}
97