|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the HRis Software package. |
|
5
|
|
|
* |
|
6
|
|
|
* HRis - Human Resource and Payroll System |
|
7
|
|
|
* |
|
8
|
|
|
* @link http://github.com/HB-Co/HRis |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace HRis\Api\Controllers\PIM\Configuration; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use HRis\Api\Controllers\BaseController; |
|
14
|
|
|
use HRis\Api\Eloquent\CustomField; |
|
15
|
|
|
use HRis\Api\Eloquent\CustomFieldOption; |
|
16
|
|
|
use HRis\Api\Eloquent\CustomFieldSection; |
|
17
|
|
|
use HRis\Api\Eloquent\CustomFieldType; |
|
18
|
|
|
use HRis\Api\Eloquent\Navlink; |
|
19
|
|
|
use HRis\Api\Requests\PIM\CustomFieldRequest; |
|
20
|
|
|
use HRis\Api\Requests\PIM\CustomFieldSectionsRequest; |
|
21
|
|
|
use Illuminate\Http\Request; |
|
22
|
|
|
use Illuminate\Support\Facades\DB; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class CustomFieldsController. |
|
26
|
|
|
*/ |
|
27
|
|
|
class CustomFieldsController extends BaseController |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var CustomFieldSection |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $custom_field_section; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var CustomField |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $custom_field; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var CustomFieldType |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $custom_field_type; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var CustomFieldOption |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $custom_field_option; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param CustomFieldSection $custom_field_section |
|
51
|
|
|
* @param CustomField $custom_field |
|
52
|
|
|
* @param CustomFieldType $custom_field_type |
|
53
|
|
|
* @param CustomFieldOption $custom_field_option |
|
54
|
|
|
* |
|
55
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
56
|
|
|
*/ |
|
57
|
36 |
|
public function __construct(CustomFieldSection $custom_field_section, CustomField $custom_field, CustomFieldType $custom_field_type, CustomFieldOption $custom_field_option) |
|
58
|
|
|
{ |
|
59
|
36 |
|
$this->custom_field = $custom_field; |
|
60
|
36 |
|
$this->custom_field_section = $custom_field_section; |
|
61
|
36 |
|
$this->custom_field_type = $custom_field_type; |
|
62
|
36 |
|
$this->custom_field_option = $custom_field_option; |
|
63
|
36 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Delete the PIM - Custom Field Section. |
|
67
|
|
|
* |
|
68
|
|
|
* @param CustomFieldSectionsRequest $request |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Dingo\Api\Http\Response |
|
71
|
|
|
* |
|
72
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function destroy(CustomFieldSectionsRequest $request) |
|
75
|
|
|
{ |
|
76
|
4 |
|
return $this->destroyModel($request, $this->custom_field_section); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
public function destroyCustomField(CustomFieldRequest $request) |
|
80
|
|
|
{ |
|
81
|
4 |
|
return $this->destroyModel($request, $this->custom_field); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Show the PIM - Custom Fields. |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Dingo\Api\Http\Response |
|
88
|
|
|
* |
|
89
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function index() |
|
92
|
|
|
{ |
|
93
|
2 |
|
$custom_field_sections = $this->custom_field_section->with('screen')->paginate(ROWS_PER_PAGE); |
|
94
|
|
|
|
|
95
|
2 |
|
return $this->responseAPI(200, SUCCESS_RETRIEVE_MESSAGE, ['data' => $custom_field_sections, 'table' => $this->setupDataTable($custom_field_sections)]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Setup table for custom field section. |
|
100
|
|
|
* |
|
101
|
|
|
* @param $custom_field_sections |
|
102
|
|
|
* |
|
103
|
|
|
* @return array |
|
104
|
|
|
* |
|
105
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function setupDataTable($custom_field_sections) |
|
108
|
|
|
{ |
|
109
|
2 |
|
$table = []; |
|
110
|
|
|
|
|
111
|
2 |
|
$table['title'] = 'Custom Field Sections'; |
|
112
|
2 |
|
$table['permission'] = 'pim.configuration.custom-field-sections'; |
|
113
|
2 |
|
$table['headers'] = ['Id', 'Name', 'Screen']; |
|
114
|
2 |
|
$table['model'] = [ |
|
115
|
2 |
|
'singular' => 'custom_field_section', |
|
116
|
2 |
|
'plural' => 'custom_field_sections', |
|
117
|
2 |
|
'dashed' => 'custom-field-sections', |
|
118
|
|
|
]; |
|
119
|
2 |
|
$table['items'] = $custom_field_sections; |
|
120
|
|
|
|
|
121
|
2 |
|
return $table; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Show a PIM - Custom Field Section. |
|
126
|
|
|
* |
|
127
|
|
|
* @param Request $request |
|
128
|
|
|
* |
|
129
|
|
|
* @return \Dingo\Api\Http\Response |
|
130
|
|
|
* |
|
131
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
132
|
|
|
*/ |
|
133
|
4 |
|
public function show(Request $request) |
|
134
|
|
|
{ |
|
135
|
4 |
|
$custom_field_section_id = $request->get('custom_field_section_id'); |
|
136
|
4 |
|
$custom_field_section = $this->custom_field_section->whereId($custom_field_section_id)->first(); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
4 |
|
if (!$custom_field_section) { |
|
139
|
2 |
|
return $this->responseAPI(404, UNABLE_RETRIEVE_MESSAGE); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
$custom_fields = $this->custom_field->with('type', 'options')->whereCustomFieldSectionId($custom_field_section_id)->paginate(ROWS_PER_PAGE); |
|
143
|
|
|
|
|
144
|
2 |
|
return $this->responseAPI(200, SUCCESS_RETRIEVE_MESSAGE, ['data' => $custom_fields, 'table' => $this->setupDataTableCustomField($custom_fields)]); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Setup table for custom field. |
|
149
|
|
|
* |
|
150
|
|
|
* @param $custom_fields |
|
151
|
|
|
* |
|
152
|
|
|
* @return array |
|
153
|
|
|
* |
|
154
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
155
|
|
|
*/ |
|
156
|
2 |
|
public function setupDataTableCustomField($custom_fields) |
|
157
|
|
|
{ |
|
158
|
2 |
|
$table = []; |
|
159
|
|
|
|
|
160
|
2 |
|
$table['title'] = 'Custom Fields'; |
|
161
|
2 |
|
$table['permission'] = 'pim.configuration.custom-fields'; |
|
162
|
2 |
|
$table['headers'] = ['Id', 'Name', 'Type', 'Mask', 'Has Options', 'Required']; |
|
163
|
2 |
|
$table['model'] = [ |
|
164
|
2 |
|
'singular' => 'custom_field', |
|
165
|
2 |
|
'plural' => 'custom_fields', |
|
166
|
2 |
|
'dashed' => 'custom-fields', |
|
167
|
|
|
]; |
|
168
|
2 |
|
$table['items'] = $custom_fields; |
|
169
|
|
|
|
|
170
|
2 |
|
return $table; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Save the PIM - Custom Field Section. |
|
175
|
|
|
* |
|
176
|
|
|
* @param CustomFieldSectionsRequest $request |
|
177
|
|
|
* |
|
178
|
|
|
* @return \Dingo\Api\Http\Response |
|
179
|
|
|
* |
|
180
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
181
|
|
|
*/ |
|
182
|
20 |
|
public function store(CustomFieldSectionsRequest $request) |
|
183
|
|
|
{ |
|
184
|
20 |
|
return $this->storeModel($request, $this->custom_field_section, 'custom_field_section'); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Save the PIM - Custom Field. |
|
189
|
|
|
* |
|
190
|
|
|
* @param CustomFieldRequest $request |
|
191
|
|
|
* |
|
192
|
|
|
* @return \Dingo\Api\Http\Response |
|
193
|
|
|
* |
|
194
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
195
|
|
|
*/ |
|
196
|
14 |
|
public function storeCustomField(CustomFieldRequest $request) |
|
197
|
|
|
{ |
|
198
|
14 |
|
$custom_field_section_id = $request->get('custom_field_section_id'); |
|
199
|
|
|
try { |
|
200
|
14 |
|
DB::beginTransaction(); |
|
201
|
|
|
|
|
202
|
14 |
|
$custom_field_section = $this->custom_field_section->findOrFail($custom_field_section_id); |
|
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
$data = [ |
|
205
|
12 |
|
'custom_field_type_id' => $request->get('type_id'), |
|
206
|
12 |
|
'name' => $request->get('name'), |
|
207
|
12 |
|
'required' => $request->get('required'), |
|
208
|
12 |
|
'mask' => $request->has('mask') ? $request->get('mask') : null, |
|
209
|
12 |
|
]; |
|
210
|
|
|
|
|
211
|
|
|
// Create CustomField and attach it to the CustomFieldSection |
|
212
|
12 |
|
$custom_field = $custom_field_section->customFields()->create($data); |
|
213
|
|
|
|
|
214
|
12 |
|
$custom_field_type = $this->custom_field_type->whereId($data['custom_field_type_id'])->first(); |
|
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
// Checks if the CustomFieldType has options |
|
217
|
12 |
|
if ($custom_field_type->has_options) { |
|
218
|
12 |
|
$options = explode(',', $request->get('custom_field_options')); |
|
219
|
|
|
|
|
220
|
12 |
|
foreach ($options as $option) { |
|
221
|
12 |
|
$custom_field->options()->create(['name' => $option]); |
|
222
|
12 |
|
} |
|
223
|
12 |
|
} |
|
224
|
14 |
|
} catch (Exception $e) { |
|
225
|
2 |
|
DB::rollback(); |
|
226
|
|
|
|
|
227
|
2 |
|
return $this->responseAPI(422, UNABLE_ADD_MESSAGE); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
12 |
|
DB::commit(); |
|
231
|
|
|
|
|
232
|
12 |
|
$custom_field = $this->custom_field->with('type', 'options')->whereId($custom_field->id)->first(); |
|
233
|
|
|
|
|
234
|
12 |
|
return $this->responseAPI(201, SUCCESS_ADD_MESSAGE, compact('custom_field')); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Update the PIM - Custom Field Section. |
|
239
|
|
|
* |
|
240
|
|
|
* @param CustomFieldSectionsRequest $request |
|
241
|
|
|
* |
|
242
|
|
|
* @return \Dingo\Api\Http\Response |
|
243
|
|
|
* |
|
244
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
245
|
|
|
*/ |
|
246
|
6 |
View Code Duplication |
public function update(CustomFieldSectionsRequest $request) |
|
|
|
|
|
|
247
|
|
|
{ |
|
248
|
|
|
try { |
|
249
|
6 |
|
$custom_field_section = $this->custom_field_section->whereId($request->get('custom_field_section_id'))->first(); |
|
|
|
|
|
|
250
|
|
|
|
|
251
|
4 |
|
if (!$custom_field_section) { |
|
252
|
2 |
|
return $this->responseAPI(404, UNABLE_RETRIEVE_MESSAGE); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
2 |
|
$custom_field_section->update($request->only(['name', 'screen_id'])); |
|
256
|
4 |
|
} catch (Exception $e) { |
|
257
|
|
|
|
|
258
|
2 |
|
return $this->responseAPI(422, UNABLE_UPDATE_MESSAGE); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
2 |
|
return $this->responseAPI(200, SUCCESS_UPDATE_MESSAGE); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Update the PIM - Custom Field. |
|
266
|
|
|
* |
|
267
|
|
|
* @param CustomFieldRequest $request |
|
268
|
|
|
* |
|
269
|
|
|
* @return \Dingo\Api\Http\Response |
|
270
|
|
|
* |
|
271
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
272
|
|
|
*/ |
|
273
|
6 |
|
public function updateCustomField(CustomFieldRequest $request) |
|
274
|
|
|
{ |
|
275
|
|
|
try { |
|
276
|
6 |
|
$custom_field = $this->custom_field->whereId($request->get('custom_field_id'))->first(); |
|
|
|
|
|
|
277
|
|
|
|
|
278
|
4 |
|
if (!$custom_field) { |
|
279
|
2 |
|
return $this->responseAPI(404, UNABLE_RETRIEVE_MESSAGE); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
2 |
|
$data = $this->getRequestData($request); |
|
283
|
|
|
|
|
284
|
2 |
|
$custom_field->update($data); |
|
285
|
|
|
|
|
286
|
2 |
|
$custom_field_type = $this->custom_field_type->whereId($data['custom_field_type_id'])->first(); |
|
|
|
|
|
|
287
|
|
|
|
|
288
|
|
|
// Checks if the CustomFieldType has options. |
|
289
|
2 |
|
if ($custom_field_type->has_options) { |
|
290
|
2 |
|
$old_options = $this->custom_field_option->whereCustomFieldId($custom_field->id)->get(); |
|
|
|
|
|
|
291
|
2 |
|
$options = explode(',', $request->get('custom_field_options')); |
|
292
|
|
|
|
|
293
|
|
|
// Delete database entry that aren't in the new option list. |
|
294
|
2 |
|
$this->deleteOldOptions($options, $old_options); |
|
295
|
|
|
|
|
296
|
|
|
// Assign / Create database entry basing on the new option list. |
|
297
|
2 |
|
$this->createNewOptions($options, $custom_field); |
|
298
|
2 |
|
} |
|
299
|
4 |
|
} catch (Exception $e) { |
|
300
|
2 |
|
return $this->responseAPI(422, UNABLE_UPDATE_MESSAGE); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
2 |
|
return $this->responseAPI(200, SUCCESS_UPDATE_MESSAGE); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Get Custom Field Sections by Screen Id. |
|
308
|
|
|
* |
|
309
|
|
|
* @param Request $request |
|
310
|
|
|
* |
|
311
|
|
|
* @return \Dingo\Api\Http\Response |
|
312
|
|
|
* |
|
313
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
314
|
|
|
*/ |
|
315
|
2 |
|
public function getCustomFieldSectionsByScreenId(Request $request) |
|
316
|
|
|
{ |
|
317
|
2 |
|
$screen_id = Navlink::whereName($request->get('screen_name'))->value('id'); |
|
318
|
|
|
|
|
319
|
2 |
|
$custom_field_sections = $this->custom_field_section->with('customFields.options')->whereScreenId($screen_id)->get(); |
|
320
|
|
|
|
|
321
|
2 |
|
$custom_field_sections->each(function ($custom_field_section) { |
|
322
|
2 |
|
$custom_fields = $custom_field_section->customFields; |
|
323
|
|
|
|
|
324
|
2 |
|
$custom_field_section->fields = array_chunk($custom_fields->toArray(), 2); |
|
325
|
2 |
|
}); |
|
326
|
|
|
|
|
327
|
2 |
|
return $this->responseAPI(200, SUCCESS_RETRIEVE_MESSAGE, compact('custom_field_sections')); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Delete database entry that aren't in the new option list. |
|
332
|
|
|
* |
|
333
|
|
|
* @param $options |
|
334
|
|
|
* @param $old_options |
|
335
|
|
|
* |
|
336
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
337
|
|
|
*/ |
|
338
|
2 |
|
private function deleteOldOptions($options, $old_options) |
|
339
|
|
|
{ |
|
340
|
2 |
|
foreach ($old_options as $option) { |
|
341
|
2 |
|
if (!in_array($option->name, $options)) { |
|
342
|
2 |
|
$option->delete(); |
|
343
|
2 |
|
} |
|
344
|
2 |
|
} |
|
345
|
2 |
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Create database entry basing on the new option list. |
|
349
|
|
|
* |
|
350
|
|
|
* @param $options |
|
351
|
|
|
* @param $custom_field |
|
352
|
|
|
* |
|
353
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
354
|
|
|
*/ |
|
355
|
2 |
|
private function createNewOptions($options, $custom_field) |
|
356
|
|
|
{ |
|
357
|
2 |
|
foreach ($options as $option) { |
|
358
|
2 |
|
$custom_field_option = $this->custom_field_option->whereCustomFieldId($custom_field->id)->whereName($option)->count(); |
|
|
|
|
|
|
359
|
|
|
|
|
360
|
|
|
// Only add to database those options that aren't there yet. |
|
361
|
2 |
|
if (!$custom_field_option) { |
|
362
|
2 |
|
$custom_field->options()->create(['name' => $option]); |
|
363
|
2 |
|
} |
|
364
|
2 |
|
} |
|
365
|
2 |
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Set the data before processing it. |
|
369
|
|
|
* |
|
370
|
|
|
* @param CustomFieldRequest $request |
|
371
|
|
|
* @return array |
|
372
|
|
|
* |
|
373
|
|
|
* @author Bertrand Kintanar <[email protected]> |
|
374
|
|
|
*/ |
|
375
|
2 |
|
private function getRequestData(CustomFieldRequest $request) |
|
376
|
|
|
{ |
|
377
|
|
|
return [ |
|
378
|
2 |
|
'custom_field_type_id' => $request->get('type_id'), |
|
379
|
2 |
|
'name' => $request->get('name'), |
|
380
|
2 |
|
'required' => $request->get('required'), |
|
381
|
2 |
|
'mask' => $request->has('mask') ? $request->get('mask') : null, |
|
382
|
2 |
|
]; |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: