1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SoufieneSlimi\TemplateOperation; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Route; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use Template; |
8
|
|
|
|
9
|
|
|
trait TemplateOperation |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Define which routes are needed for this operation. |
13
|
|
|
* |
14
|
|
|
* @param string $segment Name of the current entity (singular). Used as first URL segment. |
15
|
|
|
* @param string $routeName prefix of the route name |
16
|
|
|
* @param string $controller name of the current CrudController |
17
|
|
|
*/ |
18
|
|
|
protected function setupTemplateRoutes($segment, $routeName, $controller) |
19
|
|
|
{ |
20
|
|
|
// list all templates for this model |
21
|
|
|
Route::get($segment.'/template', [ |
22
|
|
|
'as' => $routeName.'.listTemplate', |
23
|
|
|
'uses' => $controller.'@listTemplate', |
24
|
|
|
'operation' => 'template', |
25
|
|
|
]); |
26
|
|
|
|
27
|
|
|
// show the form to create a template |
28
|
|
|
Route::get($segment.'/template/create', [ |
29
|
|
|
'as' => $routeName.'.createTemplate', |
30
|
|
|
'uses' => $controller.'@createTemplate', |
31
|
|
|
'operation' => 'template', |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
// save the template to database |
35
|
|
|
Route::post($segment.'/template', [ |
36
|
|
|
'as' => $routeName.'.storeTemplate', |
37
|
|
|
'uses' => $controller.'@storeTemplate', |
38
|
|
|
'operation' => 'template', |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
// to apply a template (use this template button) |
42
|
|
|
Route::post($segment.'/create', [ |
43
|
|
|
'as' => $routeName.'.createFromTemplate', |
44
|
|
|
'uses' => $controller.'@create', |
45
|
|
|
'operation' => 'create', |
46
|
|
|
'middleware' => ApplyTemplate::class, |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
// delete a template |
50
|
|
|
Route::delete($segment.'/template/delete', [ |
51
|
|
|
'as' => $routeName.'.deleteTemplate', |
52
|
|
|
'uses' => $controller.'@deleteTemplate', |
53
|
|
|
'operation' => 'template', |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add the default settings, buttons, etc that this operation needs. |
59
|
|
|
*/ |
60
|
|
|
protected function setupTemplateDefaults() |
61
|
|
|
{ |
62
|
|
|
// allow access to the operation |
63
|
|
|
$this->crud->allowAccess('template'); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->crud->setRoute($this->crud->getRoute().'/template'); |
66
|
|
|
|
67
|
|
|
$this->crud->operation('template', function () { |
68
|
|
|
$this->crud->loadDefaultOperationSettingsFromConfig(); |
69
|
|
|
$this->crud->loadDefaultOperationSettingsFromConfig('backpack.crud.operations.create'); |
70
|
|
|
$this->crud->setupDefaultSaveActions(); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
$this->crud->operation('list', function () { |
74
|
|
|
$this->crud->addButton('top', 'template', 'view', 'template-operation::template_button'); |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Display the templates for this model. |
80
|
|
|
* |
81
|
|
|
* @return \Illuminate\View\View |
82
|
|
|
*/ |
83
|
|
|
public function listTemplate() |
84
|
|
|
{ |
85
|
|
|
$this->crud->hasAccessOrFail('template'); |
86
|
|
|
|
87
|
|
|
$this->data['crud'] = $this->crud; |
|
|
|
|
88
|
|
|
$this->data['title'] = $this->crud->getTitle() ?? mb_ucfirst($this->crud->entity_name).' '.trans('template-operation::template.templates'); |
89
|
|
|
$this->data['templates'] = Template::whereModelFqn(get_class($this->crud->model))->get(); |
90
|
|
|
|
91
|
|
|
return view($this->crud->get('template.listView') ?? 'template-operation::templates', $this->data); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Display the form to create a template for specified model. |
96
|
|
|
* |
97
|
|
|
* @return \Illuminate\View\View |
98
|
|
|
*/ |
99
|
|
|
public function createTemplate() |
100
|
|
|
{ |
101
|
|
|
// check permission |
102
|
|
|
$this->crud->hasAccessOrFail('template'); |
103
|
|
|
|
104
|
|
|
// add the template name field |
105
|
|
|
$this->crud->addField([ |
106
|
|
|
'name' => 'template_name', |
107
|
|
|
'type' => 'text', |
108
|
|
|
'label' => trans('template-operation::template.template_name'), |
109
|
|
|
'tab' => 'Template setting', |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
// update breadcrumbs |
113
|
|
|
$this->data['breadcrumbs'] = [ |
114
|
|
|
trans('backpack::crud.admin') => url(config('backpack.base.route_prefix'), 'dashboard'), |
115
|
|
|
$this->crud->entity_name_plural => url($this->crud->getRoute()), |
116
|
|
|
trans('template-operation::template.templates') => url($this->crud->getRoute().'/template'), |
117
|
|
|
trans('backpack::crud.add') => false, |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
// update some crud data for create operation |
121
|
|
|
$this->crud->removeSaveActions(['save_and_edit', 'save_and_preview']); |
122
|
|
|
$this->crud->entity_name_plural .= ' '.mb_strtolower(trans('template-operation::template.templates')); |
123
|
|
|
$this->crud->entity_name .= ' '.mb_strtolower(trans('template-operation::template.template')); |
124
|
|
|
|
125
|
|
|
// prepare the fields you need to show |
126
|
|
|
$this->crud->route .= '/template'; |
127
|
|
|
$this->data['crud'] = $this->crud; |
128
|
|
|
$this->data['saveAction'] = $this->crud->getSaveAction(); |
129
|
|
|
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.add').' '.$this->crud->entity_name; |
130
|
|
|
|
131
|
|
|
return view($this->crud->getCreateView(), $this->data); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Save template in database. |
136
|
|
|
* |
137
|
|
|
* @return \Illuminate\Http\RedirectResponse |
138
|
|
|
*/ |
139
|
|
|
public function storeTemplate() |
140
|
|
|
{ |
141
|
|
|
$this->crud->hasAccessOrFail('create'); |
142
|
|
|
|
143
|
|
|
// execute the FormRequest authorization and validation, if one is required |
144
|
|
|
$request = $this->crud->validateRequest(); |
|
|
|
|
145
|
|
|
// validate template name field |
146
|
|
|
$this->crud->getRequest()->validate([ |
147
|
|
|
'template_name' => 'required|min:3|max:255', |
148
|
|
|
], [ |
149
|
|
|
'template_name.required' => trans('template-operation::template.validation.template_name_required'), |
150
|
|
|
'template_name.min' => trans('template-operation::template.validation.template_name_min'), |
151
|
|
|
'template_name.max' => trans('template-operation::template.validation.template_name_max'), |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
$templateData = $this->crud->getStrippedSaveRequest(); |
155
|
|
|
|
156
|
|
|
// don't save the excluded inputs |
157
|
|
|
foreach ($this->crud->getOperationSetting('excludedInputs') ?? [] as $excluded) { |
158
|
|
|
unset($templateData[$excluded]); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// save the template |
162
|
|
|
Template::make( |
163
|
|
|
$this->crud->getRequest()->template_name, |
164
|
|
|
$templateData, |
165
|
|
|
get_class($this->crud->getModel()) |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$this->crud->setSaveAction(); |
169
|
|
|
|
170
|
|
|
return $this->crud->performSaveAction(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Delete a template via ajax. |
175
|
|
|
* |
176
|
|
|
* @return \Illuminate\Http\JsonResponse |
177
|
|
|
*/ |
178
|
|
|
public function deleteTemplate() |
179
|
|
|
{ |
180
|
|
|
$request =$this->crud->getRequest(); |
181
|
|
|
|
182
|
|
|
// see if the template exists in the database |
183
|
|
|
$validator = Validator::make($request->only('template_id'), [ |
184
|
|
|
'template_id' => 'required|exists:form_templates,id', |
185
|
|
|
]); |
186
|
|
|
|
187
|
|
|
if ($validator->fails()) { |
188
|
|
|
return response()->json($validator->errors(), 422); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
Template::find($request->template_id)->delete(); |
192
|
|
|
|
193
|
|
|
return response()->json([], 200); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: