Passed
Push — master ( 5d7723...f7e125 )
by Paweł
03:17
created

SideMenu::isItemActive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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\AjaxButton;
12
use app\components\ArrayHelper;
13
use app\models\Favorite;
14
use dmstr\widgets\Menu;
15
use yii\helpers\Url;
16
use yii\web\JsExpression;
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
//        $this->view->registerJs('
28
//        jQuery(\'.favorites .delete\').click(function(e){
29
//            e.preventDefault();
30
//            var $el = jQuery(this);
31
//            var id = $el.attr(\'data-id\');
32
//            jQuery.ajax({
33
//                url: \'' . Url::to(['favorite/delete']) . '\',
34
//                data: {id: id},
35
//                success: function(){
36
//                    jQuery(\'.favorites\').find(\'li[data-id=\'+id+\']\').remove();
37
//                }
38
//            })
39
//        });
40
//        ');
41
        parent::init();
42
    }
43
44
    public function run()
45
    {
46
        $favorites = Favorite::find()
47
            ->orderBy('label ASC')
48
            ->all();
49
        foreach ($favorites as $favorite) {
50
            $this->items[] = [
51
                'label' => $favorite->label,
52
                'url' => $favorite->url,
53
                'options' => [
54
                    'data' => [
55
                        'id' => $favorite->id,
56
                    ],
57
                ],
58
            ];
59
        }
60
        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...
61
            array_unshift($this->items, ['label' => 'Favorites', 'options' => ['class' => 'header']]);
62
        }
63
        parent::run();
64
    }
65
66
    protected function isItemActive($item)
67
    {
68
        if (isset($item['url'])) {
69
            $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...
70
71
            return $item['url'] == $url;
72
        }
73
74
        return false;
75
    }
76
77
    protected function renderItem($item)
78
    {
79
        $tmp = $this->linkTemplate;
80
81
        $id = ArrayHelper::getValue($item, 'options.data.id');
82
        if ($id) {
83
            $btnDelete = AjaxButton::widget([
84
                'tag' => 'span',
85
                'text' => '<i class="fa fa-trash-o pull-right delete"></i>',
86
                'confirm' => 'Are you sure?',
87
                'url' => ['favorite/delete', 'id' => $id],
88
                'data' => [
89
                    'id' => $id,
90
                ],
91
                'options' => [
92
                    'class' => 'pull-right-container',
93
                ],
94
                'successCallback' => new JsExpression(sprintf("function(){jQuery('.favorites').find('li[data-id=%s]').remove();}", $id)),
95
            ]);
96
97
            $this->linkTemplate = str_replace('{btn-delete}', $btnDelete, $this->linkTemplate);
98
        }
99
100
        $html = parent::renderItem($item);
101
        $this->linkTemplate = $tmp;
102
103
        return $html;
104
    }
105
}