|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\MenuCRUD\app\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Models\Traits\CrudTrait; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
|
|
8
|
|
|
class Menu extends Model |
|
9
|
|
|
{ |
|
10
|
|
|
use CrudTrait; |
|
11
|
|
|
|
|
12
|
|
|
/* |
|
13
|
|
|
|-------------------------------------------------------------------------- |
|
14
|
|
|
| GLOBAL VARIABLES |
|
15
|
|
|
|-------------------------------------------------------------------------- |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
protected $table = 'menus'; |
|
19
|
|
|
// protected $primaryKey = 'id'; |
|
20
|
|
|
// public $timestamps = false; |
|
21
|
|
|
protected $guarded = ['id']; |
|
22
|
|
|
// protected $fillable = []; |
|
23
|
|
|
// protected $hidden = []; |
|
24
|
|
|
// protected $dates = []; |
|
25
|
|
|
|
|
26
|
|
|
/* |
|
27
|
|
|
|-------------------------------------------------------------------------- |
|
28
|
|
|
| FUNCTIONS |
|
29
|
|
|
|-------------------------------------------------------------------------- |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
public static function boot() |
|
33
|
|
|
{ |
|
34
|
|
|
parent::boot(); |
|
35
|
|
|
|
|
36
|
|
|
static::updated(function ($menu) { |
|
37
|
|
|
if ($menu->wasChanged('placement')) { |
|
38
|
|
|
\Backpack\MenuCRUD\app\Models\Menu::where('placement', $menu->placement) |
|
39
|
|
|
->where('id', '!=', $menu->id) |
|
40
|
|
|
->update(['placement' => null]); |
|
41
|
|
|
} |
|
42
|
|
|
}); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get all menu items, in a hierarchical collection. |
|
47
|
|
|
* Supports infinite levels of indentation. |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function getTree($placement) |
|
50
|
|
|
{ |
|
51
|
|
|
$menu = self::where('placement', $placement)->first(); |
|
52
|
|
|
if ($menu == null) { |
|
53
|
|
|
return []; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$menus = \Backpack\MenuCRUD\app\Models\MenuItem::where('menu_id', $menu->id)->orderBy('lft', 'asc')->get()->keyBy('id'); |
|
57
|
|
|
|
|
58
|
|
|
// Build the tree using reference method |
|
59
|
|
|
$flat = []; |
|
60
|
|
|
$tree = []; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($menus as $primaryKey => $menu) { |
|
63
|
|
|
if (! isset($flat[$primaryKey])) { |
|
64
|
|
|
$flat[$primaryKey]['menu_data'] = $menu; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($menu->parent_id != null) { |
|
68
|
|
|
$flat[$menu->parent_id]['submenus'][$primaryKey] = &$flat[$primaryKey]; |
|
69
|
|
|
} else { |
|
70
|
|
|
$tree[$primaryKey] = &$flat[$primaryKey]; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $tree; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function customActions($crud = false) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
return ' |
|
80
|
|
|
<a href="'.backpack_url('menu-item', $this->id).'" class="btn btn-sm btn-link"><i class="la la-list"></i> Menu Items</a>'; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/* |
|
84
|
|
|
|-------------------------------------------------------------------------- |
|
85
|
|
|
| RELATIONS |
|
86
|
|
|
|-------------------------------------------------------------------------- |
|
87
|
|
|
*/ |
|
88
|
|
|
|
|
89
|
|
|
/* |
|
90
|
|
|
|-------------------------------------------------------------------------- |
|
91
|
|
|
| SCOPES |
|
92
|
|
|
|-------------------------------------------------------------------------- |
|
93
|
|
|
*/ |
|
94
|
|
|
|
|
95
|
|
|
/* |
|
96
|
|
|
|-------------------------------------------------------------------------- |
|
97
|
|
|
| ACCESSORS |
|
98
|
|
|
|-------------------------------------------------------------------------- |
|
99
|
|
|
*/ |
|
100
|
|
|
|
|
101
|
|
|
/* |
|
102
|
|
|
|-------------------------------------------------------------------------- |
|
103
|
|
|
| MUTATORS |
|
104
|
|
|
|-------------------------------------------------------------------------- |
|
105
|
|
|
*/ |
|
106
|
|
|
} |
|
107
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.