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