Menu.php
3.88 KB
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
<?php
namespace mdm\admin\models;
use Yii;
use mdm\admin\components\Configs;
use yii\db\Query;
/**
* This is the model class for table "menu".
*
* @property integer $id Menu id(autoincrement)
* @property string $name Menu name
* @property integer $parent Menu parent
* @property string $route Route for this menu
* @property integer $order Menu order
* @property string $data Extra information for this menu
*
* @property Menu $menuParent Menu parent
* @property Menu[] $menus Menu children
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class Menu extends \yii\db\ActiveRecord
{
public $parent_name;
/**
* @inheritdoc
*/
public static function tableName()
{
return Configs::instance()->menuTable;
}
/**
* @inheritdoc
*/
public static function getDb()
{
if (Configs::instance()->db !== null) {
return Configs::instance()->db;
} else {
return parent::getDb();
}
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['parent_name'], 'in',
'range' => static::find()->select(['name'])->column(),
'message' => 'Menu "{value}" not found.'],
[['parent', 'route', 'data', 'order'], 'default'],
[['parent'], 'filterParent', 'when' => function() {
return !$this->isNewRecord;
}],
[['order'], 'integer'],
[['route'], 'in',
'range' => static::getSavedRoutes(),
'message' => 'Route "{value}" not found.']
];
}
/**
* Use to loop detected.
*/
public function filterParent()
{
$parent = $this->parent;
$db = static::getDb();
$query = (new Query)->select(['parent'])
->from(static::tableName())
->where('[[id]]=:id');
while ($parent) {
if ($this->id == $parent) {
$this->addError('parent_name', 'Loop detected.');
return;
}
$parent = $query->params([':id' => $parent])->scalar($db);
}
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('rbac-admin', 'ID'),
'name' => Yii::t('rbac-admin', 'Name'),
'parent' => Yii::t('rbac-admin', 'Parent'),
'parent_name' => Yii::t('rbac-admin', 'Parent Name'),
'route' => Yii::t('rbac-admin', 'Route'),
'order' => Yii::t('rbac-admin', 'Order'),
'data' => Yii::t('rbac-admin', 'Data'),
];
}
/**
* Get menu parent
* @return \yii\db\ActiveQuery
*/
public function getMenuParent()
{
return $this->hasOne(Menu::className(), ['id' => 'parent']);
}
/**
* Get menu children
* @return \yii\db\ActiveQuery
*/
public function getMenus()
{
return $this->hasMany(Menu::className(), ['parent' => 'id']);
}
private static $_routes;
/**
* Get saved routes.
* @return array
*/
public static function getSavedRoutes()
{
if (self::$_routes === null) {
self::$_routes = [];
foreach (Configs::authManager()->getPermissions() as $name => $value) {
if ($name[0] === '/' && substr($name, -1) != '*') {
self::$_routes[] = $name;
}
}
}
return self::$_routes;
}
public static function getMenuSource()
{
$tableName = static::tableName();
return (new \yii\db\Query())
->select(['m.id', 'm.name', 'm.route', 'parent_name' => 'p.name'])
->from(['m' => $tableName])
->leftJoin(['p' => $tableName], '[[m.parent]]=[[p.id]]')
->all(static::getDb());
}
}