1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\PageManager\app\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\UniquePages; |
6
|
|
|
use Backpack\PageManager\app\TraitReflections; |
7
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
8
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\SaveActions; |
9
|
|
|
|
10
|
|
|
class UniquePageCrudController extends CrudController |
11
|
|
|
{ |
12
|
|
|
use SaveActions; |
13
|
|
|
use UniquePages; |
14
|
|
|
use TraitReflections; |
15
|
|
|
|
16
|
|
|
public function setup() |
17
|
|
|
{ |
18
|
|
|
parent::__construct(); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$modelClass = config('backpack.pagemanager.unique_page_model_class', 'Backpack\PageManager\app\Models\Page'); |
21
|
|
|
|
22
|
|
|
$this->checkForTemplatesAndUniquePagesNotDistinct(); |
23
|
|
|
|
24
|
|
|
/* |
25
|
|
|
|-------------------------------------------------------------------------- |
26
|
|
|
| BASIC CRUD INFORMATION |
27
|
|
|
|-------------------------------------------------------------------------- |
28
|
|
|
*/ |
29
|
|
|
$this->crud->setModel($modelClass); |
30
|
|
|
// Don't set route or entity names here. These depend on the page you are editing |
31
|
|
|
|
32
|
|
|
// unique pages cannot be created nor deleted |
33
|
|
|
$this->crud->denyAccess('create'); |
34
|
|
|
$this->crud->denyAccess('delete'); |
35
|
|
|
|
36
|
|
|
if (config('backpack.pagemanager.unique_page_revisions')) { |
37
|
|
|
$this->crud->allowAccess('revisions'); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Edit the unique page retrieved by slug. |
43
|
|
|
* |
44
|
|
|
* @param string $slug |
45
|
|
|
* @return Response |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
public function uniqueEdit($slug) |
48
|
|
|
{ |
49
|
|
|
$model = $this->crud->model; |
50
|
|
|
$entry = $model::findBySlug($slug); |
51
|
|
|
|
52
|
|
|
if (! $entry) { |
53
|
|
|
$entry = $this->createMissingPage($slug); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->uniqueSetup($entry); |
57
|
|
|
|
58
|
|
|
return parent::edit($entry->id); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Update the unique page. |
63
|
|
|
* |
64
|
|
|
* @param string $slug |
65
|
|
|
* @param int $id |
66
|
|
|
* @return \Illuminate\Http\RedirectResponse |
67
|
|
|
*/ |
68
|
|
|
public function update($slug, $id) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$this->setRoute($slug); |
71
|
|
|
|
72
|
|
|
return parent::updateCrud(); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the crud route. |
77
|
|
|
* |
78
|
|
|
* @param $slug |
79
|
|
|
*/ |
80
|
|
|
public function setRoute($slug) |
81
|
|
|
{ |
82
|
|
|
$this->crud->setRoute(config('backpack.base.route_prefix').'/unique/'.$slug); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Populate the update form with basic fields that all pages need. |
87
|
|
|
* |
88
|
|
|
* @param Model $page |
89
|
|
|
*/ |
90
|
|
|
public function addDefaultPageFields($page) |
91
|
|
|
{ |
92
|
|
|
$fields = [ |
93
|
|
|
[ |
94
|
|
|
'name' => 'buttons', |
95
|
|
|
'type' => 'custom_html', |
96
|
|
|
'value' => $this->buttons($page), |
97
|
|
|
], |
98
|
|
|
[ |
99
|
|
|
'name' => 'template', |
100
|
|
|
'type' => 'hidden', |
101
|
|
|
], |
102
|
|
|
[ |
103
|
|
|
'name' => 'name', |
104
|
|
|
], |
105
|
|
|
[ |
106
|
|
|
'name' => 'title', |
107
|
|
|
], |
108
|
|
|
[ |
109
|
|
|
'name' => 'slug', |
110
|
|
|
'type' => 'hidden', |
111
|
|
|
], |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
$this->crud->addFields($fields); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Build the buttons html for the edit form. |
119
|
|
|
* |
120
|
|
|
* @param Model $page |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function buttons($page) |
124
|
|
|
{ |
125
|
|
|
$openButton = $page->getOpenButton(); |
126
|
|
|
$revisionsButton = view('crud::buttons.revisions', ['crud' => $this->crud, 'entry' => $page]); |
127
|
|
|
|
128
|
|
|
return $openButton.' '.$revisionsButton; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Create the page by slug. |
133
|
|
|
* |
134
|
|
|
* @param $slug |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
|
|
public function createMissingPage($slug) |
138
|
|
|
{ |
139
|
|
|
$slugs = $this->getUniqueSlugs(); |
140
|
|
|
|
141
|
|
|
if (! $slugs->has($slug)) { |
142
|
|
|
abort(404); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$page = $slugs->pull($slug); |
146
|
|
|
$model = $this->crud->model; |
147
|
|
|
|
148
|
|
|
return $model::create([ |
149
|
|
|
'template' => $page, |
150
|
|
|
'name' => camel_case($page), |
151
|
|
|
'title' => camel_case($page), |
152
|
|
|
'slug' => $slug, |
153
|
|
|
]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Display the revisions for specified resource. |
158
|
|
|
* |
159
|
|
|
* @param $slug |
160
|
|
|
* @param $id |
161
|
|
|
* @return Response |
|
|
|
|
162
|
|
|
*/ |
163
|
|
View Code Duplication |
public function uniqueRevisions($slug, $id) |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$model = $this->crud->model; |
166
|
|
|
$entry = $model::findBySlugOrFail($slug); |
167
|
|
|
|
168
|
|
|
$this->uniqueSetup($entry); |
169
|
|
|
|
170
|
|
|
return parent::listRevisions($entry->id); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Restore a specific revision for the specified resource. |
175
|
|
|
* |
176
|
|
|
* Used via AJAX in the revisions view |
177
|
|
|
* |
178
|
|
|
* @param string $slug |
179
|
|
|
* @param int $id |
180
|
|
|
* |
181
|
|
|
* @return JSON Response containing the new revision that was created from the update |
182
|
|
|
* @return HTTP 500 if the request did not contain the revision ID |
|
|
|
|
183
|
|
|
*/ |
184
|
|
View Code Duplication |
public function restoreUniqueRevision($slug, $id) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$model = $this->crud->model; |
187
|
|
|
$entry = $model::findBySlugOrFail($slug); |
188
|
|
|
|
189
|
|
|
$this->uniqueSetup($entry); |
190
|
|
|
|
191
|
|
|
return parent::restoreRevision($id); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Setup the controller for the entry. |
196
|
|
|
* |
197
|
|
|
* @param $entry |
198
|
|
|
*/ |
199
|
|
|
protected function uniqueSetup($entry) |
200
|
|
|
{ |
201
|
|
|
$this->setRoute($entry->slug); |
202
|
|
|
|
203
|
|
|
$this->addDefaultPageFields($entry); |
204
|
|
|
$this->crud->setEntityNameStrings($this->crud->makeLabel($entry->template), ''); |
205
|
|
|
|
206
|
|
|
$this->{$entry->template}(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/* |
210
|
|
|
|-------------------------------------------------------------------------- |
211
|
|
|
| SaveActions overrides |
212
|
|
|
|-------------------------------------------------------------------------- |
213
|
|
|
*/ |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Overrides trait version to remove 'save_and_back' and 'save_and_new'. |
217
|
|
|
* |
218
|
|
|
* @return [type] [description] |
|
|
|
|
219
|
|
|
*/ |
220
|
|
|
public function getSaveAction() |
221
|
|
|
{ |
222
|
|
|
$saveCurrent = [ |
223
|
|
|
'value' => $this->getSaveActionButtonName('save_and_back'), |
224
|
|
|
'label' => $this->getSaveActionButtonName('save_and_back'), |
225
|
|
|
]; |
226
|
|
|
|
227
|
|
|
return [ |
|
|
|
|
228
|
|
|
'active' => $saveCurrent, |
229
|
|
|
'options' => [], |
230
|
|
|
]; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Override trait version to not update the session variable. |
235
|
|
|
* |
236
|
|
|
* @param [type] $forceSaveAction [description] |
|
|
|
|
237
|
|
|
*/ |
238
|
|
|
public function setSaveAction($forceSaveAction = null) |
239
|
|
|
{ |
240
|
|
|
// do nothing to preserve session value for other crud |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.