|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yeelight\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Prettus\Repository\Contracts\Transformable; |
|
8
|
|
|
use Prettus\Repository\Traits\TransformableTrait; |
|
9
|
|
|
use Yeelight\Traits\ModelTree; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class AdminMenu |
|
13
|
|
|
* |
|
14
|
|
|
* @category Yeelight |
|
15
|
|
|
* |
|
16
|
|
|
* @package Yeelight\Models |
|
17
|
|
|
* |
|
18
|
|
|
* @author Sheldon Lee <[email protected]> |
|
19
|
|
|
* |
|
20
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
21
|
|
|
* |
|
22
|
|
|
* @link https://www.yeelight.com |
|
23
|
|
|
*/ |
|
24
|
|
|
class AdminMenu extends BaseModel implements Transformable |
|
25
|
|
|
{ |
|
26
|
|
|
use TransformableTrait, ModelTree { |
|
27
|
|
|
ModelTree::boot as treeBoot; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Indicates if the model should be auto set user_id. |
|
32
|
|
|
* |
|
33
|
|
|
* @var bool |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $autoUserId = false; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Indicates if the model should be recorded ips. |
|
39
|
|
|
* |
|
40
|
|
|
* @var bool |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $ips = false; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Indicates if the model should be recorded users. |
|
46
|
|
|
* |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $update_users = false; |
|
50
|
|
|
|
|
51
|
|
|
protected $primaryKey = 'id'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* The attributes that are mass assignable. |
|
55
|
|
|
* |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $fillable = ['parent_id', 'order', 'title', 'icon', 'uri']; |
|
59
|
|
|
|
|
60
|
|
|
// Fields to be converted to Carbon object automatically |
|
61
|
|
|
protected $dates = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Create a new Eloquent model instance. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $attributes attributes |
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct(array $attributes = []) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->setTable(config('yeelight.backend.database.admin_menus_table')); |
|
71
|
|
|
|
|
72
|
|
|
parent::__construct($attributes); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* A Menu belongs to many roles. |
|
77
|
|
|
* |
|
78
|
|
|
* @return BelongsToMany |
|
79
|
|
|
*/ |
|
80
|
|
|
public function roles() : BelongsToMany |
|
81
|
|
|
{ |
|
82
|
|
|
$pivotTable = config('yeelight.backend.database.admin_role_menus_table'); |
|
83
|
|
|
|
|
84
|
|
|
$relatedModel = config('yeelight.backend.database.admin_roles_model'); |
|
85
|
|
|
|
|
86
|
|
|
return $this->belongsToMany($relatedModel, $pivotTable, 'menu_id', 'role_id'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get allNodes |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function allNodes() : array |
|
95
|
|
|
{ |
|
96
|
|
|
$orderColumn = DB::getQueryGrammar()->wrap($this->orderColumn); |
|
97
|
|
|
$byOrder = $orderColumn.' = 0,'.$orderColumn; |
|
98
|
|
|
|
|
99
|
|
|
return static::with('roles')->orderByRaw($byOrder)->get()->toArray(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function onCreated() |
|
103
|
|
|
{ |
|
104
|
|
|
parent::onCreated(); |
|
105
|
|
|
$roles = array_filter(request()->get('roles', [])); |
|
106
|
|
|
if (!empty($roles)) { |
|
107
|
|
|
$this->roles()->sync($roles); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function onUpdated() |
|
112
|
|
|
{ |
|
113
|
|
|
parent::onUpdated(); |
|
114
|
|
|
$roles = array_filter(request()->get('roles', [])); |
|
115
|
|
|
if (!empty($roles)) { |
|
116
|
|
|
$this->roles()->sync($roles); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function onDeleting() |
|
121
|
|
|
{ |
|
122
|
|
|
parent::onDeleting(); |
|
123
|
|
|
$this->children()->delete(); |
|
124
|
|
|
$this->roles()->detach(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Detach models from the relationship. |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
|
protected static function boot() |
|
133
|
|
|
{ |
|
134
|
|
|
static::treeBoot(); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|