1
|
|
|
<?php |
2
|
|
|
namespace Afrittella\BackProject\Repositories; |
3
|
|
|
|
4
|
|
|
class Menus extends Base |
5
|
|
|
{ |
6
|
|
|
function model() |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
return 'Afrittella\BackProject\Models\Menu'; |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
public function all($columns = ['*']) |
12
|
|
|
{ |
13
|
|
|
return $this->model->withDepth()->defaultOrder()->whereIsRoot()->select($columns)->get(); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function children($id) |
17
|
|
|
{ |
18
|
|
|
return $this->model->withDepth()->defaultOrder()->descendantsOf($id)->where('parent_id', '=', $id); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function find($id, $columns = array('*')) |
22
|
|
|
{ |
23
|
|
|
$this->applyCriteria(); |
24
|
|
|
return $this->model->withDepth()->find($id, $columns); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $name |
29
|
|
|
*/ |
30
|
|
|
public function tree($name) |
31
|
|
|
{ |
32
|
|
|
$root = $this->findBy('name', $name); |
33
|
|
|
$tree = false; |
34
|
|
|
|
35
|
|
|
if (!empty($root)) { |
36
|
|
|
$tree = $this->model->withDepth()->defaultOrder()->descendantsOf($root->id)->toTree(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $tree; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function moveUp($id) |
43
|
|
|
{ |
44
|
|
|
$menu = $this->model->find($id); |
45
|
|
|
return $menu->up(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function moveDown($id) |
49
|
|
|
{ |
50
|
|
|
$menu = $this->model->find($id); |
51
|
|
|
return $menu->down(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function transform($data = [], $options = []) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if (empty($data)) { |
57
|
|
|
$data = $this->all(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Table header |
61
|
|
|
$head = [ |
62
|
|
|
'columns' => [ |
63
|
|
|
trans('back-project::menus.name'), |
64
|
|
|
trans('back-project::menus.title'), |
65
|
|
|
trans('back-project::menus.route'), |
66
|
|
|
trans('back-project::menus.icon'), |
67
|
|
|
trans('back-project::crud.actions'), |
68
|
|
|
] |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
$body = []; |
72
|
|
|
|
73
|
|
|
foreach ($data as $row): |
74
|
|
|
$actions = [ |
75
|
|
|
'edit' => ['url' => route('bp.menus.edit', [$row->id])] |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
if (!$row->is_protected) { |
79
|
|
|
$actions['delete'] = ['url' => route('bp.menus.delete', [$row->id])]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$body[] = [ |
83
|
|
|
'columns' => [ |
84
|
|
|
['content' => $row->name], |
85
|
|
|
['content' => $row->description], |
86
|
|
|
['content' => $row->route], |
87
|
|
|
['content' => $row->icon], |
88
|
|
|
['content' => false, 'actions' => $actions], |
89
|
|
|
] |
90
|
|
|
]; |
91
|
|
|
endforeach; |
92
|
|
|
|
93
|
|
|
return [ |
94
|
|
|
'head' => $head, |
95
|
|
|
'body' => $body |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.