SideMenu   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 44
c 3
b 1
f 0
dl 0
loc 77
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A renderItem() 0 26 2
A run() 0 19 3
A isItemActive() 0 16 4
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 01.02.2018
6
 */
7
8
namespace app\modules\admin\widgets\favorites;
9
10
11
use app\components\ArrayHelper;
12
use app\models\Favorite;
13
use app\modules\admin\widgets\AjaxButton;
14
use dmstr\widgets\Menu;
15
use Yii;
16
use yii\helpers\Url;
17
18
class SideMenu extends Menu
19
{
20
    public $encodeLabels = false;
21
    public $linkTemplate = '<a href="{url}">{icon} {label} {btn-delete}</a>';
22
23
    public function init()
24
    {
25
        $this->options = ['class' => 'sidebar-menu tree favorites', 'data-widget' => 'tree'];
26
        $this->defaultIconHtml = '<i class="fa fa-star"></i> ';
27
        parent::init();
28
    }
29
30
    public function run()
31
    {
32
        $favorites = Favorite::find()
33
            ->andWhere([
34
                'user_id' => Yii::$app->user->id,
35
            ])
36
            ->orderBy('label ASC')
37
            ->all();
38
        foreach ($favorites as $favorite) {
39
            $this->items[] = [
40
                'label' => $favorite->label,
0 ignored issues
show
Bug introduced by
Accessing label on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
41
                'url' => $favorite->url,
0 ignored issues
show
Bug introduced by
Accessing url on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
42
                'id' => $favorite->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface yii\db\ActiveRecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
43
            ];
44
        }
45
        if ($this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
46
            array_unshift($this->items, ['label' => 'Favorites', 'options' => ['class' => 'header']]);
47
        }
48
        parent::run();
49
    }
50
51
    protected function isItemActive($item)
52
    {
53
        if (!isset($item['url'])) {
54
            return false;
55
        }
56
57
        if ($item['url'] == Url::current()) {
58
            return true;
59
        }
60
        if (isset($item['url'])) {
61
            $url = Url::to(["/admin/{$this->view->context->id}/dashboard", 'id' => Yii::$app->request->get('id')]);
0 ignored issues
show
Bug introduced by
Accessing id on the interface yii\base\ViewContextInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
62
63
            return $item['url'] == $url;
64
        }
65
66
        return false;
67
    }
68
69
    protected function renderItem($item)
70
    {
71
        $tmp = $this->linkTemplate;
72
        $id = ArrayHelper::getValue($item, 'id');
73
        $btnDelete = '';
74
        if ($id) {
75
            $btnDelete = AjaxButton::widget([
76
                'confirm' => true,
77
                'tag' => 'span',
78
                'text' => '<i class="fa fa-trash-o pull-right delete"></i>',
79
                'url' => ['favorite/delete', 'id' => $id],
80
                'options' => [
81
                    'class' => 'pull-right-container',
82
                    'data' => [
83
                        'style' => 'slide-right',
84
                    ],
85
                ],
86
            ]);
87
        }
88
89
        $this->linkTemplate = str_replace('{btn-delete}', $btnDelete, $this->linkTemplate);
90
91
        $html = parent::renderItem($item);
92
        $this->linkTemplate = $tmp;
93
94
        return $html;
95
    }
96
}