|
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\CRUD\app\Http\Controllers\CrudController; |
|
8
|
|
|
use Backpack\PageManager\app\Http\Requests\PageRequest; |
|
9
|
|
|
|
|
10
|
|
|
class PageCrudController extends CrudController |
|
11
|
|
|
{ |
|
12
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation; |
|
13
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { create as traitCreate; } |
|
14
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { edit as traitEdit; } |
|
15
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\CloneOperation; |
|
16
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation; |
|
17
|
|
|
use PageTemplates; |
|
18
|
|
|
|
|
19
|
|
|
public function setup() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->crud->setModel(config('backpack.pagemanager.page_model_class', 'Backpack\PageManager\app\Models\Page')); |
|
22
|
|
|
$this->crud->setRoute(config('backpack.base.route_prefix').'/page'); |
|
23
|
|
|
$this->crud->setEntityNameStrings(trans('backpack::pagemanager.page'), trans('backpack::pagemanager.pages')); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function setupListOperation() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->crud->addColumn([ |
|
29
|
|
|
'name' => 'name', |
|
30
|
|
|
'label' => trans('backpack::pagemanager.name'), |
|
31
|
|
|
]); |
|
32
|
|
|
$this->crud->addColumn([ |
|
33
|
|
|
'name' => 'template', |
|
34
|
|
|
'label' => trans('backpack::pagemanager.template'), |
|
35
|
|
|
'type' => 'model_function', |
|
36
|
|
|
'function_name' => 'getTemplateName', |
|
37
|
|
|
]); |
|
38
|
|
|
$this->crud->addColumn([ |
|
39
|
|
|
'name' => 'slug', |
|
40
|
|
|
'label' => trans('backpack::pagemanager.slug'), |
|
41
|
|
|
]); |
|
42
|
|
|
$this->crud->addButtonFromModelFunction('line', 'open', 'getOpenButton', 'beginning'); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// ----------------------------------------------- |
|
46
|
|
|
// Overwrites of CrudController |
|
47
|
|
|
// ----------------------------------------------- |
|
48
|
|
|
|
|
49
|
|
|
protected function setupCreateOperation() |
|
50
|
|
|
{ |
|
51
|
|
|
// Note: |
|
52
|
|
|
// - default fields, that all templates are using, are set using $this->addDefaultPageFields(); |
|
53
|
|
|
// - template-specific fields are set per-template, in the PageTemplates trait; |
|
54
|
|
|
|
|
55
|
|
|
$this->addDefaultPageFields(\Request::input('template')); |
|
|
|
|
|
|
56
|
|
|
$this->useTemplate(\Request::input('template')); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
$this->crud->setValidation(PageRequest::class); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function setupUpdateOperation() |
|
62
|
|
|
{ |
|
63
|
|
|
// if the template in the GET parameter is missing, figure it out from the db |
|
64
|
|
|
$template = \Request::input('template') ?? $this->crud->getCurrentEntry()->template; |
|
65
|
|
|
|
|
66
|
|
|
$this->addDefaultPageFields($template); |
|
67
|
|
|
$this->useTemplate($template); |
|
68
|
|
|
|
|
69
|
|
|
$this->crud->setValidation(PageRequest::class); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// ----------------------------------------------- |
|
73
|
|
|
// Methods that are particular to the PageManager. |
|
74
|
|
|
// ----------------------------------------------- |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Populate the create/update forms with basic fields, that all pages need. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $template The name of the template that should be used in the current form. |
|
|
|
|
|
|
80
|
|
|
*/ |
|
81
|
|
|
public function addDefaultPageFields($template = false) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->crud->addField([ |
|
84
|
|
|
'name' => 'template', |
|
85
|
|
|
'label' => trans('backpack::pagemanager.template'), |
|
86
|
|
|
'type' => 'select_page_template', |
|
87
|
|
|
'view_namespace' => 'pagemanager::fields', |
|
88
|
|
|
'options' => $this->getTemplatesArray(), |
|
89
|
|
|
'value' => $template, |
|
90
|
|
|
'allows_null' => false, |
|
91
|
|
|
'wrapperAttributes' => [ |
|
92
|
|
|
'class' => 'form-group col-md-6', |
|
93
|
|
|
], |
|
94
|
|
|
]); |
|
95
|
|
|
$this->crud->addField([ |
|
96
|
|
|
'name' => 'name', |
|
97
|
|
|
'label' => trans('backpack::pagemanager.page_name'), |
|
98
|
|
|
'type' => 'text', |
|
99
|
|
|
'wrapperAttributes' => [ |
|
100
|
|
|
'class' => 'form-group col-md-6', |
|
101
|
|
|
], |
|
102
|
|
|
// 'disabled' => 'disabled' |
|
103
|
|
|
]); |
|
104
|
|
|
$this->crud->addField([ |
|
105
|
|
|
'name' => 'title', |
|
106
|
|
|
'label' => trans('backpack::pagemanager.page_title'), |
|
107
|
|
|
'type' => 'text', |
|
108
|
|
|
// 'disabled' => 'disabled' |
|
109
|
|
|
]); |
|
110
|
|
|
$this->crud->addField([ |
|
111
|
|
|
'name' => 'slug', |
|
112
|
|
|
'label' => trans('backpack::pagemanager.page_slug'), |
|
113
|
|
|
'type' => 'text', |
|
114
|
|
|
'hint' => trans('backpack::pagemanager.page_slug_hint'), |
|
115
|
|
|
// 'disabled' => 'disabled' |
|
116
|
|
|
]); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Add the fields defined for a specific template. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $template_name The name of the template that should be used in the current form. |
|
|
|
|
|
|
123
|
|
|
*/ |
|
124
|
|
|
public function useTemplate($template_name = false) |
|
125
|
|
|
{ |
|
126
|
|
|
$templates = $this->getTemplates(); |
|
127
|
|
|
|
|
128
|
|
|
// set the default template |
|
129
|
|
|
if ($template_name == false) { |
|
|
|
|
|
|
130
|
|
|
$template_name = $templates[0]->name; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// actually use the template |
|
134
|
|
|
if ($template_name) { |
|
135
|
|
|
$this->{$template_name}(); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get all defined templates. |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getTemplates($template_name = false) |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
$templates_array = []; |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
$templates_trait = new \ReflectionClass('App\PageTemplates'); |
|
147
|
|
|
$templates = $templates_trait->getMethods(\ReflectionMethod::IS_PRIVATE); |
|
148
|
|
|
|
|
149
|
|
|
if (! count($templates)) { |
|
150
|
|
|
abort(503, trans('backpack::pagemanager.template_not_found')); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $templates; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get all defined template as an array. |
|
158
|
|
|
* |
|
159
|
|
|
* Used to populate the template dropdown in the create/update forms. |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getTemplatesArray() |
|
162
|
|
|
{ |
|
163
|
|
|
$templates = $this->getTemplates(); |
|
164
|
|
|
|
|
165
|
|
|
foreach ($templates as $template) { |
|
166
|
|
|
$templates_array[$template->name] = str_replace('_', ' ', title_case($template->name)); |
|
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $templates_array; |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: