|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Services; |
|
4
|
|
|
|
|
5
|
|
|
use DaveJamesMiller\Breadcrumbs\BreadcrumbsGenerator; |
|
6
|
|
|
use DaveJamesMiller\Breadcrumbs\BreadcrumbsManager; |
|
7
|
|
|
use Terranet\Administrator\Contracts\Module; |
|
8
|
|
|
use Terranet\Administrator\Services\Breadcrumbs\EloquentPresenter; |
|
9
|
|
|
|
|
10
|
|
|
class Breadcrumbs |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Current scaffold module. |
|
14
|
|
|
* |
|
15
|
|
|
* @var Module |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $module; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Breadcrumbs manager. |
|
21
|
|
|
* |
|
22
|
|
|
* @var BreadcrumbsManager |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $manager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param BreadcrumbsManager $manager |
|
28
|
|
|
* @param Module $module |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(BreadcrumbsManager $manager, Module $module) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->module = $module; |
|
33
|
|
|
$this->manager = $manager; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Render breadcrumbs. |
|
38
|
|
|
* |
|
39
|
|
|
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException |
|
40
|
|
|
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException |
|
41
|
|
|
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\ViewNotSetException |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function render() |
|
46
|
|
|
{ |
|
47
|
|
|
$action = $this->currentAction(); |
|
48
|
|
|
|
|
49
|
|
|
// assembly breadcrumbs |
|
50
|
|
|
$this->assembly($action); |
|
51
|
|
|
|
|
52
|
|
|
return $this->manager->view('administrator::partials.breadcrumbs', $action); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Detect current action. |
|
57
|
|
|
* |
|
58
|
|
|
* @return null|string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function currentAction() |
|
61
|
|
|
{ |
|
62
|
|
|
$action = substr($action = app('router')->currentRouteAction(), strpos($action, '@') + 1); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
if (!method_exists($this, $action)) { |
|
65
|
|
|
$action = 'index'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $action; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param $action |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function assembly($action) |
|
77
|
|
|
{ |
|
78
|
|
|
return \call_user_func_array([$this, $action], []); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Render `Index page` breadcrumbs. |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function index() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->manager->register('index', function (BreadcrumbsGenerator $breadcrumbs) { |
|
87
|
|
|
$breadcrumbs->push($this->module->title(), route('scaffold.index', [ |
|
|
|
|
|
|
88
|
|
|
'module' => $this->module->url(), |
|
89
|
|
|
])); |
|
90
|
|
|
}); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Render `Edit page` breadcrumbs. |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function edit() |
|
97
|
|
|
{ |
|
98
|
|
|
$this->index(); |
|
99
|
|
|
|
|
100
|
|
|
$this->manager->register('edit', function (BreadcrumbsGenerator $breadcrumbs) { |
|
101
|
|
|
$breadcrumbs->parent('index'); |
|
102
|
|
|
|
|
103
|
|
|
$breadcrumbs->push(trans('administrator::module.action.edit', [ |
|
|
|
|
|
|
104
|
|
|
'resource' => $this->module->singular(), |
|
|
|
|
|
|
105
|
|
|
'instance' => $this->presentEloquent(), |
|
106
|
|
|
]), null); |
|
107
|
|
|
}); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Render `Create page` breadcrumbs. |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function create() |
|
114
|
|
|
{ |
|
115
|
|
|
$this->index(); |
|
116
|
|
|
|
|
117
|
|
|
$this->manager->register('create', function (BreadcrumbsGenerator $breadcrumbs) { |
|
118
|
|
|
$breadcrumbs->parent('index'); |
|
119
|
|
|
$breadcrumbs->push(trans('administrator::module.action.create', [ |
|
|
|
|
|
|
120
|
|
|
'resource' => $this->module->singular(), |
|
121
|
|
|
]), null); |
|
122
|
|
|
}); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Render `View page` breadcrumbs. |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function view() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->index(); |
|
131
|
|
|
|
|
132
|
|
|
$this->manager->register('view', function (BreadcrumbsGenerator $breadcrumbs) { |
|
133
|
|
|
$breadcrumbs->parent('index'); |
|
134
|
|
|
$breadcrumbs->push(trans('administrator::module.action.view', [ |
|
|
|
|
|
|
135
|
|
|
'resource' => $this->module->singular(), |
|
136
|
|
|
'instance' => $this->presentEloquent(), |
|
137
|
|
|
]), route('scaffold.view', ['module' => $this->module->url(), 'id' => app('scaffold.model')->getKey()])); |
|
|
|
|
|
|
138
|
|
|
}); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return null|string |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function presentEloquent(): ?string |
|
145
|
|
|
{ |
|
146
|
|
|
if (!$model = app('scaffold.model')) { |
|
|
|
|
|
|
147
|
|
|
$model = app('scaffold.module')->model(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return (new EloquentPresenter($model))->present(); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|