|
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) { |
|
|
|
|
|
|
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')]); |
|
|
|
|
|
|
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
|
|
|
} |
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.