1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.writesdown.com/ |
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
5
|
|
|
* @license http://www.writesdown.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace common\models; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\db\ActiveRecord; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is the model class for table "{{%menu}}". |
15
|
|
|
* |
16
|
|
|
* @property integer $id |
17
|
|
|
* @property string $title |
18
|
|
|
* @property string $location |
19
|
|
|
* |
20
|
|
|
* @property MenuItem[] $menuItems |
21
|
|
|
* |
22
|
|
|
* @author Agiel K. Saputra <[email protected]> |
23
|
|
|
* @since 0.1.0 |
24
|
|
|
*/ |
25
|
|
|
class Menu extends ActiveRecord |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public static function tableName() |
31
|
|
|
{ |
32
|
|
|
return '{{%menu}}'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
public function rules() |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
[['title'], 'required'], |
42
|
|
|
[['title'], 'string', 'max' => 255], |
43
|
|
|
[['location'], 'string', 'max' => 50], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public function attributeLabels() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
'id' => Yii::t('writesdown', 'ID'), |
54
|
|
|
'title' => Yii::t('writesdown', 'Title'), |
55
|
|
|
'location' => Yii::t('writesdown', 'Location'), |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return \yii\db\ActiveQuery |
61
|
|
|
*/ |
62
|
|
|
public function getMenuItems() |
63
|
|
|
{ |
64
|
|
|
return $this->hasMany(MenuItem::className(), ['menu_id' => 'id']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get available menu items recursively for backend purpose. |
69
|
|
|
* |
70
|
|
|
* @param int $parent |
71
|
|
|
* @return array|null |
72
|
|
|
*/ |
73
|
|
|
public function getBackendItems($parent = 0) |
74
|
|
|
{ |
75
|
|
|
/* @var $model \common\models\MenuItem */ |
76
|
|
|
$models = $this->getMenuItems() |
77
|
|
|
->andWhere(['parent' => $parent]) |
78
|
|
|
->orderBy(['order' => SORT_ASC]) |
79
|
|
|
->indexBy('id') |
80
|
|
|
->all(); |
81
|
|
|
|
82
|
|
|
if (empty($models)) { |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
foreach ($models as $id => $model) { |
87
|
|
|
$models[$id]->items = $this->getBackendItems($model->id); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $models; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get menu by location. |
95
|
|
|
* Ready to render on frontend. |
96
|
|
|
* |
97
|
|
|
* @param $location |
98
|
|
|
* @return array|null |
99
|
|
|
*/ |
100
|
|
|
public static function get($location) |
101
|
|
|
{ |
102
|
|
|
$menu = static::getFrontendItems($location); |
103
|
|
|
|
104
|
|
|
if ($menu) { |
105
|
|
|
return $menu; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return []; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* List menu item by menu location; |
113
|
|
|
* |
114
|
|
|
* @param string $location |
115
|
|
|
* @param int $parent |
116
|
|
|
* @return array|null |
117
|
|
|
*/ |
118
|
|
|
protected static function getFrontendItems($location, $parent = 0) |
119
|
|
|
{ |
120
|
|
|
/* @var $menuItemModel \common\models\MenuItem[] */ |
121
|
|
|
$menuItem = []; |
122
|
|
|
|
123
|
|
|
$menuItemModel = MenuItem::find() |
124
|
|
|
->innerJoinWith(['menu']) |
125
|
|
|
->andWhere(['location' => $location]) |
126
|
|
|
->andWhere(['parent' => $parent]) |
127
|
|
|
->orderBy('order') |
128
|
|
|
->all(); |
129
|
|
|
|
130
|
|
|
if (empty($menuItemModel)) { |
131
|
|
|
return $menuItem = null; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
foreach ($menuItemModel as $model) { |
135
|
|
|
$menuItem[] = [ |
136
|
|
|
'id' => $model->id, |
137
|
|
|
'label' => $model->label, |
138
|
|
|
'url' => $model->url, |
139
|
|
|
'parent' => $model->parent, |
140
|
|
|
'items' => static::getFrontendItems($location, $model->id), |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $menuItem; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.