|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Auth\Database; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
class Menu extends Model |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
protected static $branchOrder = []; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The attributes that are mass assignable. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $fillable = ['parent_id', 'order', 'title', 'icon', 'uri']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create a new Eloquent model instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $attributes |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(array $attributes = []) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->table = config('admin.database.menu_table'); |
|
29
|
|
|
|
|
30
|
|
|
parent::__construct($attributes); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* A Menu belongs to many roles. |
|
35
|
|
|
* |
|
36
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
37
|
|
|
*/ |
|
38
|
|
|
public function roles() |
|
39
|
|
|
{ |
|
40
|
|
|
$pivotTable = config('admin.database.role_menu_table'); |
|
41
|
|
|
|
|
42
|
|
|
return $this->belongsToMany(Role::class, $pivotTable, 'menu_id', 'role_id'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Format data to tree like array. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $elements |
|
49
|
|
|
* @param int $parentId |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function toTree(array $elements = [], $parentId = 0) |
|
54
|
|
|
{ |
|
55
|
|
|
$branch = []; |
|
56
|
|
|
|
|
57
|
|
|
if (empty($elements)) { |
|
58
|
|
|
$elements = static::with('roles')->orderByRaw('`order` = 0,`order`')->get()->toArray(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
foreach ($elements as $element) { |
|
62
|
|
|
if ($element['parent_id'] == $parentId) { |
|
63
|
|
|
$children = static::toTree($elements, $element['id']); |
|
64
|
|
|
|
|
65
|
|
|
if ($children) { |
|
66
|
|
|
$element['children'] = $children; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$branch[] = $element; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $branch; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Set the order of branches in the tree. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $order |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
protected static function setBranchOrder(array $order) |
|
84
|
|
|
{ |
|
85
|
|
|
static::$branchOrder = array_flip(array_flatten($order)); |
|
86
|
|
|
|
|
87
|
|
|
static::$branchOrder = array_map(function ($item) { |
|
88
|
|
|
return ++$item; |
|
89
|
|
|
}, static::$branchOrder); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Save a tree from a tree like array. |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $tree |
|
96
|
|
|
* @param int $parentId |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function saveTree($tree = [], $parentId = 0) |
|
99
|
|
|
{ |
|
100
|
|
|
if (empty(static::$branchOrder)) { |
|
101
|
|
|
static::setBranchOrder($tree); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
foreach ($tree as $branch) { |
|
105
|
|
|
$node = static::find($branch['id']); |
|
106
|
|
|
|
|
107
|
|
|
$node->parent_id = $parentId; |
|
108
|
|
|
$node->order = static::$branchOrder[$branch['id']]; |
|
109
|
|
|
$node->save(); |
|
110
|
|
|
|
|
111
|
|
|
if (isset($branch['children'])) { |
|
112
|
|
|
static::saveTree($branch['children'], $branch['id']); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Build options of select field in form. |
|
119
|
|
|
* |
|
120
|
|
|
* @param array $elements |
|
121
|
|
|
* @param int $parentId |
|
122
|
|
|
* @param string $prefix |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function buildSelectOptions(array $elements = [], $parentId = 0, $prefix = '') |
|
127
|
|
|
{ |
|
128
|
|
|
$prefix = $prefix ?: str_repeat(' ', 6); |
|
129
|
|
|
|
|
130
|
|
|
$options = []; |
|
131
|
|
|
|
|
132
|
|
|
if (empty($elements)) { |
|
133
|
|
|
$elements = static::orderByRaw('`order` = 0,`order`')->get(['id', 'parent_id', 'title'])->toArray(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
foreach ($elements as $element) { |
|
137
|
|
|
$element['title'] = $prefix.' '.$element['title']; |
|
138
|
|
|
if ($element['parent_id'] == $parentId) { |
|
139
|
|
|
$children = static::buildSelectOptions($elements, $element['id'], $prefix.$prefix); |
|
140
|
|
|
|
|
141
|
|
|
$options[$element['id']] = $element['title']; |
|
142
|
|
|
|
|
143
|
|
|
if ($children) { |
|
144
|
|
|
$options += $children; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $options; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Delete current item and its children. |
|
154
|
|
|
* |
|
155
|
|
|
* @throws \Exception |
|
156
|
|
|
* |
|
157
|
|
|
* @return bool|null |
|
158
|
|
|
*/ |
|
159
|
|
|
public function delete() |
|
160
|
|
|
{ |
|
161
|
|
|
$this->where('parent_id', $this->id)->delete(); |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
return parent::delete(); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.