-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMenu.php
205 lines (176 loc) · 4.9 KB
/
Menu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace yeesoft\models;
use omgdef\multilingual\MultilingualQuery;
use yeesoft\behaviors\MultilingualBehavior;
use yeesoft\helpers\FA;
use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\SluggableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
/**
* This is the model class for table "menu".
*
* @property string $id
* @property string $title
* @property integer $created_at
* @property integer $updated_at
* @property integer $created_by
* @property integer $updated_by
*
* @property MenuLink[] $menuLinks
*/
class Menu extends ActiveRecord implements OwnerAccess
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'menu';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
BlameableBehavior::className(),
TimestampBehavior::className(),
'sluggable' => [
'class' => SluggableBehavior::className(),
'slugAttribute' => 'id',
'attribute' => 'title',
],
'multilingual' => [
'class' => MultilingualBehavior::className(),
'langForeignKey' => 'menu_id',
'tableName' => "{{%menu_lang}}",
'attributes' => [
'title'
]
],
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['title'], 'required'],
['id', 'unique'],
[['created_by', 'updated_by', 'created_at', 'updated_at'], 'integer'],
[['id'], 'string', 'max' => 64],
[['title'], 'string', 'max' => 255],
[['id'], 'match', 'pattern' => '/^[a-z0-9_-]+$/', 'message' => Yii::t('yee', 'Menu ID can only contain lowercase alphanumeric characters, underscores and dashes.')],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('yee', 'ID'),
'title' => Yii::t('yee', 'Title'),
'created_by' => Yii::t('yee', 'Created By'),
'updated_by' => Yii::t('yee', 'Updated By'),
'created_at' => Yii::t('yee', 'Created'), '',
'updated_at' => Yii::t('yee', 'Updated'),
];
}
/**
* @return \omgdef\multilingual\MultilingualQuery
*/
public function getLinks()
{
return $this->hasMany(MenuLink::className(), ['menu_id' => 'id'])->joinWith('translations');
}
/**
* get list of menus
* @return array
*/
public static function getMenus()
{
return ArrayHelper::map(self::find()->joinWith('translations')->all(), 'id', 'title');
}
/**
* get list of menus
* @return array
*/
public static function getMenuItems($menu_id)
{
$links = self::findOne($menu_id)
->getLinks()
->orderBy(['parent_id' => 'ACS', 'order' => 'ACS'])
->all();
return self::generateNavigationItems($links);
}
private static function generateNavigationItems($links)
{
$items = [];
$linksByParent = [];
foreach ($links as $link) {
$linksByParent[$link->parent_id][] = $link;
}
foreach ($linksByParent[''] as $link) {
$items[] = self::generateItem($link, $linksByParent);
}
return $items;
}
private static function generateItem($link, $menuLinks)
{
$item = [];
$icon = (!empty($link->image)) ? FA::icon($link->image) . ' ' : '';
$subItems = self::generateSubItems($link->id, $menuLinks);
$item['label'] = $icon . $link->label;
if (isset($link->alwaysVisible) && $link->alwaysVisible) {
$item['visible'] = true;
}
if ($link->link) {
$url = parse_url($link->link);
$item['url'] = (isset($url['scheme'])) ? $link->link : [$link->link];
}
if (is_array($subItems)) {
$item['items'] = $subItems;
}
return $item;
}
private static function generateSubItems($parent_id, $menuLinks)
{
if (isset($menuLinks[$parent_id])) {
$items = [];
foreach ($menuLinks[$parent_id] as $link) {
$items[] = self::generateItem($link, $menuLinks);
}
return $items;
}
return NULL;
}
/**
* @inheritdoc
* @return MultilingualQuery the active query used by this AR class.
*/
public static function find()
{
return new MultilingualQuery(get_called_class());
}
/**
*
* @inheritdoc
*/
public static function getFullAccessPermission()
{
return 'fullMenuAccess';
}
/**
*
* @inheritdoc
*/
public static function getOwnerField()
{
return 'created_by';
}
}