1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\PageManager\app\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\UniquePages; |
6
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
7
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\SaveActions; |
8
|
|
|
use Backpack\PageManager\app\TraitReflections; |
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 can not 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
|
|
|
/** |
43
|
|
|
* As we want to edit pages by slug we need a new edit function |
44
|
|
|
* |
45
|
|
|
* @param string $slug the page slug |
46
|
|
|
* @return Response |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function uniqueEdit($slug) |
49
|
|
|
{ |
50
|
|
|
$model = $this->crud->model; |
51
|
|
|
$entry = $model::findBySlug($slug); |
52
|
|
|
|
53
|
|
|
if (! $entry) { |
54
|
|
|
$entry = $this->createMissingPage($slug); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->uniqueSetup($entry); |
58
|
|
|
|
59
|
|
|
return parent::edit($entry->id); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function update($slug, $id) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->setRoute($slug); |
65
|
|
|
|
66
|
|
|
return parent::updateCrud(); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setRoute($slug) |
70
|
|
|
{ |
71
|
|
|
$this->crud->setRoute(config('backpack.base.route_prefix').'/unique/'.$slug); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Populate the update form with basic fields, that all pages need. |
76
|
|
|
* |
77
|
|
|
* @param Model $page the page entity |
78
|
|
|
*/ |
79
|
|
|
public function addDefaultPageFields($page) |
80
|
|
|
{ |
81
|
|
|
$this->crud->addField([ |
82
|
|
|
'name' => 'template', |
83
|
|
|
'type' => 'hidden', |
84
|
|
|
]); |
85
|
|
|
$this->crud->addField([ |
86
|
|
|
'name' => 'name', |
87
|
|
|
'type' => 'hidden', |
88
|
|
|
]); |
89
|
|
|
$this->crud->addField([ |
90
|
|
|
'name' => 'title', |
91
|
|
|
'type' => 'hidden', |
92
|
|
|
]); |
93
|
|
|
$this->crud->addField([ |
94
|
|
|
'name' => 'slug', |
95
|
|
|
'type' => 'hidden', |
96
|
|
|
]); |
97
|
|
|
|
98
|
|
|
$this->crud->addField([ |
99
|
|
|
'name' => 'open', |
100
|
|
|
'type' => 'custom_html', |
101
|
|
|
'value' => $this->buttons($page) |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function buttons($page) |
106
|
|
|
{ |
107
|
|
|
$openButton = $page->getOpenButton(); |
108
|
|
|
$revisionsButton = view('crud::buttons.revisions', ['crud' => $this->crud, 'entry' => $page]); |
109
|
|
|
return $openButton .' '.$revisionsButton; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function createMissingPage($slug) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$pages = collect($this->getUniquePages()); |
115
|
|
|
|
116
|
|
|
$slugs = $pages->mapWithKeys(function($page) { |
117
|
|
|
return [str_slug($page->name) => $page->name]; |
118
|
|
|
}); |
119
|
|
|
|
120
|
|
|
if (! $page = $slugs->pull($slug)) { |
121
|
|
|
abort(404); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$model = $this->crud->model; |
125
|
|
|
|
126
|
|
|
return $model::create([ |
127
|
|
|
'template' => $page, |
128
|
|
|
'name' => camel_case($page), |
129
|
|
|
'title' => camel_case($page), |
130
|
|
|
'slug' => $slug, |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function uniqueRevisions($slug, $id) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$model = $this->crud->model; |
137
|
|
|
$entry = $model::findBySlugOrFail($slug); |
138
|
|
|
|
139
|
|
|
$this->uniqueSetup($entry); |
140
|
|
|
|
141
|
|
|
return parent::listRevisions($entry->id); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
View Code Duplication |
public function restoreUniqueRevision($slug, $id) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$model = $this->crud->model; |
147
|
|
|
$entry = $model::findBySlugOrFail($slug); |
148
|
|
|
|
149
|
|
|
$this->uniqueSetup($entry); |
150
|
|
|
|
151
|
|
|
return parent::restoreRevision($id); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function uniqueSetup($entry) |
155
|
|
|
{ |
156
|
|
|
$this->setRoute($entry->slug); |
157
|
|
|
|
158
|
|
|
$this->addDefaultPageFields($entry); |
159
|
|
|
$this->crud->setEntityNameStrings($this->crud->makeLabel($entry->template), ''); |
160
|
|
|
|
161
|
|
|
$this->{$entry->template}(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/* |
165
|
|
|
|-------------------------------------------------------------------------- |
166
|
|
|
| SaveActions overrides |
167
|
|
|
|-------------------------------------------------------------------------- |
168
|
|
|
*/ |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Overrides trait version to remove. |
172
|
|
|
*/ |
173
|
|
|
public function getSaveAction() |
174
|
|
|
{ |
175
|
|
|
$saveCurrent = [ |
176
|
|
|
'value' => $this->getSaveActionButtonName('save_and_back'), |
177
|
|
|
'label' => $this->getSaveActionButtonName('save_and_back'), |
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
return [ |
|
|
|
|
181
|
|
|
'active' => $saveCurrent, |
182
|
|
|
'options' => [], |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function setSaveAction($forceSaveAction = null) |
187
|
|
|
{ |
188
|
|
|
// do nothing to preserve session value for other crud |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
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.