1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\PageManager\app\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\PageTemplates; |
6
|
|
|
// VALIDATION: change the requests to match your own file names if you need form validation |
7
|
|
|
use Backpack\PageManager\app\TraitReflections; |
8
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
9
|
|
|
use Backpack\PageManager\app\Http\Requests\PageRequest as StoreRequest; |
10
|
|
|
use Backpack\PageManager\app\Http\Requests\PageRequest as UpdateRequest; |
11
|
|
|
|
12
|
|
|
class PageCrudController extends CrudController |
13
|
|
|
{ |
14
|
|
|
use PageTemplates; |
15
|
|
|
use TraitReflections; |
16
|
|
|
|
17
|
|
|
public function setup($template_name = false) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
parent::__construct(); |
|
|
|
|
20
|
|
|
|
21
|
|
|
$modelClass = config('backpack.pagemanager.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
|
|
|
$this->crud->setRoute(config('backpack.base.route_prefix').'/page'); |
32
|
|
|
$this->crud->setEntityNameStrings(trans('backpack::pagemanager.page'), trans('backpack::pagemanager.pages')); |
33
|
|
|
|
34
|
|
|
$template_names = $this->getTemplateNames(); |
35
|
|
|
$this->crud->addClause('whereIn', 'template', $template_names); |
36
|
|
|
|
37
|
|
|
/* |
38
|
|
|
|-------------------------------------------------------------------------- |
39
|
|
|
| COLUMNS |
40
|
|
|
|-------------------------------------------------------------------------- |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
$this->crud->addColumn([ |
44
|
|
|
'name' => 'name', |
45
|
|
|
'label' => trans('backpack::pagemanager.name'), |
46
|
|
|
]); |
47
|
|
|
$this->crud->addColumn([ |
48
|
|
|
'name' => 'template', |
49
|
|
|
'label' => trans('backpack::pagemanager.template'), |
50
|
|
|
'type' => 'model_function', |
51
|
|
|
'function_name' => 'getTemplateName', |
52
|
|
|
]); |
53
|
|
|
$this->crud->addColumn([ |
54
|
|
|
'name' => 'slug', |
55
|
|
|
'label' => trans('backpack::pagemanager.slug'), |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
|-------------------------------------------------------------------------- |
60
|
|
|
| FIELDS |
61
|
|
|
|-------------------------------------------------------------------------- |
62
|
|
|
*/ |
63
|
|
|
|
64
|
|
|
// In PageManager, |
65
|
|
|
// - default fields, that all templates are using, are set using $this->addDefaultPageFields(); |
66
|
|
|
// - template-specific fields are set per-template, in the PageTemplates trait; |
67
|
|
|
|
68
|
|
|
/* |
69
|
|
|
|-------------------------------------------------------------------------- |
70
|
|
|
| BUTTONS |
71
|
|
|
|-------------------------------------------------------------------------- |
72
|
|
|
*/ |
73
|
|
|
$this->crud->addButtonFromModelFunction('line', 'open', 'getOpenButton', 'beginning'); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// ----------------------------------------------- |
77
|
|
|
// Overwrites of CrudController |
78
|
|
|
// ----------------------------------------------- |
79
|
|
|
|
80
|
|
|
// Overwrites the CrudController create() method to add template usage. |
81
|
|
|
public function create($template = false) |
82
|
|
|
{ |
83
|
|
|
$this->addDefaultPageFields($template); |
|
|
|
|
84
|
|
|
$this->useTemplate($template); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return parent::create(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Overwrites the CrudController store() method to add template usage. |
90
|
|
|
public function store(StoreRequest $request) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$this->addDefaultPageFields(\Request::input('template')); |
|
|
|
|
93
|
|
|
$this->useTemplate(\Request::input('template')); |
|
|
|
|
94
|
|
|
|
95
|
|
|
return parent::storeCrud(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Overwrites the CrudController edit() method to add template usage. |
99
|
|
|
public function edit($id, $template = false) |
100
|
|
|
{ |
101
|
|
|
// if the template in the GET parameter is missing, figure it out from the db |
102
|
|
|
if ($template == false) { |
|
|
|
|
103
|
|
|
$model = $this->crud->model; |
104
|
|
|
$this->data['entry'] = $model::findOrFail($id); |
105
|
|
|
$template = $this->data['entry']->template; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->addDefaultPageFields($template); |
109
|
|
|
$this->useTemplate($template); |
110
|
|
|
|
111
|
|
|
return parent::edit($id); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Overwrites the CrudController update() method to add template usage. |
115
|
|
|
public function update(UpdateRequest $request) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$this->addDefaultPageFields(\Request::input('template')); |
|
|
|
|
118
|
|
|
$this->useTemplate(\Request::input('template')); |
|
|
|
|
119
|
|
|
|
120
|
|
|
return parent::updateCrud(); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// ----------------------------------------------- |
124
|
|
|
// Methods that are particular to the PageManager. |
125
|
|
|
// ----------------------------------------------- |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Populate the create/update forms with basic fields, that all pages need. |
129
|
|
|
* |
130
|
|
|
* @param string $template The name of the template that should be used in the current form. |
|
|
|
|
131
|
|
|
*/ |
132
|
|
|
public function addDefaultPageFields($template = false) |
133
|
|
|
{ |
134
|
|
|
$this->crud->addField([ |
135
|
|
|
'name' => 'template', |
136
|
|
|
'label' => trans('backpack::pagemanager.template'), |
137
|
|
|
'type' => 'select_page_template', |
138
|
|
|
'options' => $this->getTemplatesArray(), |
139
|
|
|
'value' => $template, |
140
|
|
|
'allows_null' => false, |
141
|
|
|
'wrapperAttributes' => [ |
142
|
|
|
'class' => 'form-group col-md-6', |
143
|
|
|
], |
144
|
|
|
]); |
145
|
|
|
$this->crud->addField([ |
146
|
|
|
'name' => 'name', |
147
|
|
|
'label' => trans('backpack::pagemanager.page_name'), |
148
|
|
|
'type' => 'text', |
149
|
|
|
'wrapperAttributes' => [ |
150
|
|
|
'class' => 'form-group col-md-6', |
151
|
|
|
], |
152
|
|
|
// 'disabled' => 'disabled' |
|
|
|
|
153
|
|
|
]); |
154
|
|
|
$this->crud->addField([ |
155
|
|
|
'name' => 'title', |
156
|
|
|
'label' => trans('backpack::pagemanager.page_title'), |
157
|
|
|
'type' => 'text', |
158
|
|
|
// 'disabled' => 'disabled' |
|
|
|
|
159
|
|
|
]); |
160
|
|
|
$this->crud->addField([ |
161
|
|
|
'name' => 'slug', |
162
|
|
|
'label' => trans('backpack::pagemanager.page_slug'), |
163
|
|
|
'type' => 'text', |
164
|
|
|
'hint' => trans('backpack::pagemanager.page_slug_hint'), |
165
|
|
|
// 'disabled' => 'disabled' |
|
|
|
|
166
|
|
|
]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Add the fields defined for a specific template. |
171
|
|
|
* |
172
|
|
|
* @param string $template_name The name of the template that should be used in the current form. |
|
|
|
|
173
|
|
|
*/ |
174
|
|
|
public function useTemplate($template_name = false) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$templates = $this->getTemplates(); |
177
|
|
|
|
178
|
|
|
// set the default template |
179
|
|
|
if ($template_name == false) { |
|
|
|
|
180
|
|
|
$template_name = $templates[0]->name; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// actually use the template |
184
|
|
|
if ($template_name) { |
185
|
|
|
$this->{$template_name}(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get all defined template as an array. |
191
|
|
|
* |
192
|
|
|
* Used to populate the template dropdown in the create/update forms. |
193
|
|
|
*/ |
194
|
|
|
public function getTemplatesArray() |
195
|
|
|
{ |
196
|
|
|
$templates = $this->getTemplates(); |
197
|
|
|
|
198
|
|
|
foreach ($templates as $template) { |
199
|
|
|
$templates_array[$template->name] = $this->crud->makeLabel($template->name); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $templates_array; |
|
|
|
|
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.