We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
@@ -14,579 +14,579 @@ |
||
14 | 14 | |
15 | 15 | class CrudController extends BaseController { |
16 | 16 | |
17 | - use DispatchesJobs, ValidatesRequests; |
|
18 | - |
|
19 | - public $data = array(); |
|
20 | - public $crud = array( |
|
21 | - "model" => "\App\Models\Entity", |
|
22 | - "entity_name" => "entry", |
|
23 | - "entity_name_plural" => "entries", |
|
24 | - "view_table_permission" => true, |
|
25 | - "add_permission" => true, |
|
26 | - "edit_permission" => true, |
|
27 | - "delete_permission" => true, |
|
28 | - "reorder_permission" => true, |
|
29 | - "reorder_max_level" => 3, |
|
30 | - "details_row" => false, |
|
31 | - ); |
|
32 | - |
|
33 | - public function __construct() |
|
34 | - { |
|
35 | - $this->data['crud'] = $this->crud; |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Display all rows in the database for this entity. |
|
40 | - * |
|
41 | - * @return Response |
|
42 | - */ |
|
43 | - public function index() |
|
44 | - { |
|
45 | - // SECURITY: |
|
46 | - // if view_table_permission is false, abort |
|
47 | - if (isset($this->crud['view_table_permission']) && !$this->crud['view_table_permission']) { |
|
48 | - abort(403, 'Not allowed.'); |
|
49 | - } |
|
50 | - |
|
51 | - // get all results for that entity |
|
52 | - $model = $this->crud['model']; |
|
53 | - if (property_exists($model, 'translatable')) |
|
54 | - { |
|
55 | - $this->data['entries'] = $model::where('translation_lang', \Lang::locale())->get(); |
|
56 | - } else |
|
57 | - { |
|
58 | - $this->data['entries'] = $model::all(); |
|
59 | - } |
|
60 | - |
|
61 | - // add the fake fields for each entry |
|
62 | - foreach ($this->data['entries'] as $key => $entry) { |
|
63 | - $entry->addFakes($this->getFakeColumnsAsArray()); |
|
64 | - } |
|
65 | - |
|
66 | - $this->prepareColumns(); |
|
67 | - $this->data['crud'] = $this->crud; |
|
68 | - $this->data['title'] = ucfirst($this->crud['entity_name_plural']); |
|
69 | - |
|
70 | - // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
71 | - return view('crud::list', $this->data); |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * Show the form for creating inserting a new row. |
|
77 | - * |
|
78 | - * @return Response |
|
79 | - */ |
|
80 | - public function create() |
|
81 | - { |
|
82 | - // SECURITY: |
|
83 | - // if add_permission is false, abort |
|
84 | - if (isset($this->crud['add_permission']) && !$this->crud['add_permission']) { |
|
85 | - abort(403, 'Not allowed.'); |
|
86 | - } |
|
87 | - |
|
88 | - // get the fields you need to show |
|
89 | - if (isset($this->data['crud']['create_fields'])) |
|
90 | - { |
|
91 | - $this->crud['fields'] = $this->data['crud']['create_fields']; |
|
92 | - } |
|
93 | - |
|
94 | - // prepare the fields you need to show |
|
95 | - $this->prepareFields(); |
|
96 | - $this->data['crud'] = $this->crud; |
|
97 | - $this->data['title'] = trans('backpack::crud.add').' '.$this->crud['entity_name']; |
|
98 | - |
|
99 | - // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
100 | - return view('crud::create', $this->data); |
|
101 | - } |
|
102 | - |
|
103 | - |
|
104 | - /** |
|
105 | - * Store a newly created resource in the database. |
|
106 | - * |
|
107 | - * @param StoreRequest $request - type injection used for validation using Requests |
|
108 | - * @return \Illuminate\Http\RedirectResponse |
|
109 | - */ |
|
110 | - public function storeCrud(StoreRequest $request = null) |
|
111 | - { |
|
112 | - // SECURITY: |
|
113 | - // if add_permission is false, abort |
|
114 | - if (isset($this->crud['add_permission']) && !$this->crud['add_permission']) { |
|
115 | - abort(403, 'Not allowed.'); |
|
116 | - } |
|
117 | - |
|
118 | - // compress the fake fields into one field |
|
119 | - $model = $this->crud['model']; |
|
120 | - $values_to_store = $this->compactFakeFields(\Request::all()); |
|
121 | - $item = $model::create($values_to_store); |
|
122 | - |
|
123 | - // if it's a relationship with a pivot table, also sync that |
|
124 | - $this->prepareFields(); |
|
125 | - foreach ($this->crud['fields'] as $k => $field) { |
|
126 | - if (isset($field['pivot']) && $field['pivot'] == true && \Request::has($field['name'])) |
|
127 | - { |
|
128 | - $model::find($item->id)->$field['name']()->attach(\Request::input($field['name'])); |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - // show a success message |
|
133 | - \Alert::success(trans('backpack::crud.insert_success'))->flash(); |
|
134 | - |
|
135 | - // redirect the user where he chose to be redirected |
|
136 | - switch (\Request::input('redirect_after_save')) { |
|
137 | - case 'current_item_edit': |
|
138 | - return \Redirect::to($this->crud['route'].'/'.$item->id.'/edit'); |
|
139 | - |
|
140 | - default: |
|
141 | - return \Redirect::to(\Request::input('redirect_after_save')); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - |
|
146 | - /** |
|
147 | - * Show the form for editing the specified resource. |
|
148 | - * |
|
149 | - * @param int $id |
|
150 | - * @return Response |
|
151 | - */ |
|
152 | - public function edit($id) |
|
153 | - { |
|
154 | - // SECURITY: |
|
155 | - // if edit_permission is false, abort |
|
156 | - if (isset($this->crud['edit_permission']) && !$this->crud['edit_permission']) { |
|
157 | - abort(403, 'Not allowed.'); |
|
158 | - } |
|
159 | - |
|
160 | - // get the info for that entry |
|
161 | - $model = $this->crud['model']; |
|
162 | - $this->data['entry'] = $model::find($id); |
|
163 | - $this->data['entry']->addFakes($this->getFakeColumnsAsArray()); |
|
164 | - |
|
165 | - if (isset($this->data['crud']['update_fields'])) |
|
166 | - { |
|
167 | - $this->crud['fields'] = $this->data['crud']['update_fields']; |
|
168 | - } |
|
169 | - |
|
170 | - // prepare the fields you need to show and prepopulate the values |
|
171 | - $this->prepareFields($this->data['entry']); |
|
172 | - $this->data['crud'] = $this->crud; |
|
173 | - $this->data['title'] = trans('backpack::crud.edit').' '.$this->crud['entity_name']; |
|
174 | - |
|
175 | - // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
176 | - return view('crud::edit', $this->data); |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * Update the specified resource in the database. |
|
182 | - * |
|
183 | - * @param UpdateRequest $request - type injection used for validation using Requests |
|
184 | - * @return \Illuminate\Http\RedirectResponse |
|
185 | - */ |
|
186 | - public function updateCrud(UpdateRequest $request = null) |
|
187 | - { |
|
188 | - // if edit_permission is false, abort |
|
189 | - if (isset($this->crud['edit_permission']) && !$this->crud['edit_permission']) { |
|
190 | - abort(403, 'Not allowed.'); |
|
191 | - } |
|
192 | - |
|
193 | - $model = $this->crud['model']; |
|
194 | - $this->prepareFields($model::find(\Request::input('id'))); |
|
195 | - |
|
196 | - $model::find(\Request::input('id'))->update($this->compactFakeFields(\Request::all())); |
|
197 | - |
|
198 | - // if it's a relationship with a pivot table, also sync that |
|
199 | - foreach ($this->crud['fields'] as $k => $field) { |
|
200 | - if (isset($field['pivot']) && $field['pivot'] == true && \Request::has($field['name'])) |
|
201 | - { |
|
202 | - $model::find(\Request::input('id'))->$field['name']()->sync(\Request::input($field['name'])); |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - // show a success message |
|
207 | - \Alert::success(trans('backpack::crud.update_success'))->flash(); |
|
208 | - |
|
209 | - return \Redirect::to($this->crud['route']); |
|
210 | - } |
|
211 | - |
|
212 | - |
|
213 | - /** |
|
214 | - * Display the specified resource. |
|
215 | - * |
|
216 | - * @param int $id |
|
217 | - * @return Response |
|
218 | - */ |
|
219 | - public function show($id) |
|
220 | - { |
|
221 | - // get the info for that entry |
|
222 | - $model = $this->crud['model']; |
|
223 | - $this->data['entry'] = $model::find($id); |
|
224 | - $this->data['entry']->addFakes($this->getFakeColumnsAsArray()); |
|
225 | - $this->data['crud'] = $this->crud; |
|
226 | - $this->data['title'] = trans('backpack::crud.preview').' '.$this->crud['entity_name']; |
|
227 | - |
|
228 | - // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
229 | - return view('crud::show', $this->data); |
|
230 | - } |
|
231 | - |
|
232 | - |
|
233 | - /** |
|
234 | - * Remove the specified resource from storage. |
|
235 | - * |
|
236 | - * @param int $id |
|
237 | - * @return string |
|
238 | - */ |
|
239 | - public function destroy($id) |
|
240 | - { |
|
241 | - // SECURITY: |
|
242 | - // if delete_permission is false, abort |
|
243 | - if (isset($this->crud['delete_permission']) && !$this->crud['delete_permission']) { |
|
244 | - abort(403, trans('backpack::crud.unauthorized_access')); |
|
245 | - } |
|
246 | - |
|
247 | - $model = $this->crud['model']; |
|
248 | - $item = $model::find($id); |
|
249 | - $item->delete(); |
|
250 | - |
|
251 | - return 'true'; |
|
252 | - } |
|
253 | - |
|
254 | - |
|
255 | - /** |
|
256 | - * Reorder the items in the database using the Nested Set pattern. |
|
257 | - * |
|
258 | - * Database columns needed: id, parent_id, lft, rgt, depth, name/title |
|
259 | - * |
|
260 | - * @return Response |
|
261 | - */ |
|
262 | - public function reorder($lang = false) |
|
263 | - { |
|
264 | - // if reorder_table_permission is false, abort |
|
265 | - if (isset($this->crud['reorder_permission']) && !$this->crud['reorder_permission']) { |
|
266 | - abort(403, 'Not allowed.'); |
|
267 | - } |
|
268 | - |
|
269 | - if ($lang == false) |
|
270 | - { |
|
271 | - $lang = \Lang::locale(); |
|
272 | - } |
|
273 | - |
|
274 | - // get all results for that entity |
|
275 | - $model = $this->crud['model']; |
|
276 | - if (property_exists($model, 'translatable')) |
|
277 | - { |
|
278 | - $this->data['entries'] = $model::where('translation_lang', $lang)->get(); |
|
279 | - $this->data['languages'] = \Backpack\LangFileManager\app\Models\Language::all(); |
|
280 | - $this->data['active_language'] = $lang; |
|
281 | - } else |
|
282 | - { |
|
283 | - $this->data['entries'] = $model::all(); |
|
284 | - } |
|
285 | - $this->data['crud'] = $this->crud; |
|
286 | - $this->data['title'] = trans('backpack::crud.reorder').' '.$this->crud['entity_name']; |
|
287 | - |
|
288 | - // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
289 | - return view('crud::reorder', $this->data); |
|
290 | - } |
|
291 | - |
|
292 | - |
|
293 | - /** |
|
294 | - * Save the new order, using the Nested Set pattern. |
|
295 | - * |
|
296 | - * Database columns needed: id, parent_id, lft, rgt, depth, name/title |
|
297 | - * |
|
298 | - * @return |
|
299 | - */ |
|
300 | - public function saveReorder() |
|
301 | - { |
|
302 | - // if reorder_table_permission is false, abort |
|
303 | - if (isset($this->crud['reorder_permission']) && !$this->crud['reorder_permission']) { |
|
304 | - abort(403, 'Not allowed.'); |
|
305 | - } |
|
306 | - |
|
307 | - $model = $this->crud['model']; |
|
308 | - $count = 0; |
|
309 | - $all_entries = \Request::input('tree'); |
|
310 | - |
|
311 | - if (count($all_entries)) { |
|
312 | - foreach ($all_entries as $key => $entry) { |
|
313 | - if ($entry['item_id'] != "" && $entry['item_id'] != null) { |
|
314 | - $item = $model::find($entry['item_id']); |
|
315 | - $item->parent_id = $entry['parent_id']; |
|
316 | - $item->depth = $entry['depth']; |
|
317 | - $item->lft = $entry['left']; |
|
318 | - $item->rgt = $entry['right']; |
|
319 | - $item->save(); |
|
320 | - |
|
321 | - $count++; |
|
322 | - } |
|
323 | - } |
|
324 | - } else |
|
325 | - { |
|
326 | - return false; |
|
327 | - } |
|
328 | - |
|
329 | - return 'success for '.$count." items"; |
|
330 | - } |
|
331 | - |
|
332 | - |
|
333 | - /** |
|
334 | - * Used with AJAX in the list view (datatables) to show extra information about that row that didn't fit in the table. |
|
335 | - * It defaults to showing all connected translations and their CRUD buttons. |
|
336 | - * |
|
337 | - * It's enabled by: |
|
338 | - * - setting the $crud['details_row'] variable to true; |
|
339 | - * - adding the details route for the entity; ex: Route::get('page/{id}/details', 'PageCrudController@showDetailsRow'); |
|
340 | - */ |
|
341 | - public function showDetailsRow($id) |
|
342 | - { |
|
343 | - // get the info for that entry |
|
344 | - $model = $this->crud['model']; |
|
345 | - $this->data['entry'] = $model::find($id); |
|
346 | - $this->data['entry']->addFakes($this->getFakeColumnsAsArray()); |
|
347 | - $this->data['original_entry'] = $this->data['entry']; |
|
348 | - $this->data['crud'] = $this->crud; |
|
349 | - |
|
350 | - if (property_exists($model, 'translatable')) |
|
351 | - { |
|
352 | - $this->data['translations'] = $this->data['entry']->translations(); |
|
353 | - |
|
354 | - // create a list of languages the item is not translated in |
|
355 | - $this->data['languages'] = \Backpack\LangFileManager\app\Models\Language::all(); |
|
356 | - $this->data['languages_already_translated_in'] = $this->data['entry']->translationLanguages(); |
|
357 | - $this->data['languages_to_translate_in'] = $this->data['languages']->diff($this->data['languages_already_translated_in']); |
|
358 | - $this->data['languages_to_translate_in'] = $this->data['languages_to_translate_in']->reject(function($item) { |
|
359 | - return $item->abbr == \Lang::locale(); |
|
360 | - }); |
|
361 | - } |
|
362 | - |
|
363 | - // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
364 | - return view('crud::details_row', $this->data); |
|
365 | - } |
|
366 | - |
|
367 | - |
|
368 | - /** |
|
369 | - * Duplicate an existing item into another language and open it for editing. |
|
370 | - */ |
|
371 | - public function translateItem($id, $lang) |
|
372 | - { |
|
373 | - $model = $this->crud['model']; |
|
374 | - $this->data['entry'] = $model::find($id); |
|
375 | - // check if there isn't a translation already |
|
376 | - $existing_translation = $this->data['entry']->translation($lang); |
|
377 | - |
|
378 | - if ($existing_translation) |
|
379 | - { |
|
380 | - $new_entry = $existing_translation; |
|
381 | - } else |
|
382 | - { |
|
383 | - // get the info for that entry |
|
384 | - $new_entry_attributes = $this->data['entry']->getAttributes(); |
|
385 | - $new_entry_attributes['translation_lang'] = $lang; |
|
386 | - $new_entry_attributes['translation_of'] = $id; |
|
387 | - $new_entry_attributes = array_except($new_entry_attributes, 'id'); |
|
388 | - |
|
389 | - $new_entry = $model::create($new_entry_attributes); |
|
390 | - } |
|
391 | - |
|
392 | - // redirect to the edit form for that translation |
|
393 | - return redirect(str_replace($id, $new_entry->id, str_replace('translate/'.$lang, 'edit', \Request::url()))); |
|
394 | - } |
|
395 | - |
|
396 | - |
|
397 | - |
|
398 | - /** |
|
399 | - * COMMODITY FUNCTIONS |
|
400 | - */ |
|
401 | - |
|
402 | - |
|
403 | - /** |
|
404 | - * Refactor the request array to something that can be passed to the model's create or update function. |
|
405 | - * The resulting array will only include the fields that are stored in the database and their values, |
|
406 | - * plus the '_token' and 'redirect_after_save' variables. |
|
407 | - * |
|
408 | - * @param Request $request - everything that was sent from the form, usually \Request::all() |
|
409 | - * @return array |
|
410 | - */ |
|
411 | - protected function compactFakeFields($request) { |
|
412 | - |
|
413 | - $this->prepareFields(); |
|
414 | - |
|
415 | - $fake_field_columns_to_encode = []; |
|
416 | - |
|
417 | - // go through each defined field |
|
418 | - foreach ($this->crud['fields'] as $k => $field) { |
|
419 | - // if it's a fake field |
|
420 | - if (isset($this->crud['fields'][$k]['fake']) && $this->crud['fields'][$k]['fake'] == true) { |
|
421 | - // add it to the request in its appropriate variable - the one defined, if defined |
|
422 | - if (isset($this->crud['fields'][$k]['store_in'])) { |
|
423 | - $request[$this->crud['fields'][$k]['store_in']][$this->crud['fields'][$k]['name']] = $request[$this->crud['fields'][$k]['name']]; |
|
424 | - |
|
425 | - $remove_fake_field = array_pull($request, $this->crud['fields'][$k]['name']); |
|
426 | - if (!in_array($this->crud['fields'][$k]['store_in'], $fake_field_columns_to_encode, true)) { |
|
427 | - array_push($fake_field_columns_to_encode, $this->crud['fields'][$k]['store_in']); |
|
428 | - } |
|
429 | - } else //otherwise in the one defined in the $crud variable |
|
430 | - { |
|
431 | - $request['extras'][$this->crud['fields'][$k]['name']] = $request[$this->crud['fields'][$k]['name']]; |
|
432 | - |
|
433 | - $remove_fake_field = array_pull($request, $this->crud['fields'][$k]['name']); |
|
434 | - if (!in_array('extras', $fake_field_columns_to_encode, true)) { |
|
435 | - array_push($fake_field_columns_to_encode, 'extras'); |
|
436 | - } |
|
437 | - } |
|
438 | - } |
|
439 | - } |
|
440 | - |
|
441 | - // json_encode all fake_value columns in the database, so they can be properly stored and interpreted |
|
442 | - if (count($fake_field_columns_to_encode)) { |
|
443 | - foreach ($fake_field_columns_to_encode as $key => $value) { |
|
444 | - $request[$value] = json_encode($request[$value]); |
|
445 | - } |
|
446 | - } |
|
447 | - |
|
448 | - // if there are no fake fields defined, this will just return the original Request in full |
|
449 | - // since no modifications or additions have been made to $request |
|
450 | - return $request; |
|
451 | - } |
|
452 | - |
|
453 | - |
|
454 | - /** |
|
455 | - * Returns an array of database columns names, that are used to store fake values. |
|
456 | - * Returns ['extras'] if no columns have been found. |
|
457 | - * |
|
458 | - */ |
|
459 | - protected function getFakeColumnsAsArray() { |
|
460 | - |
|
461 | - $this->prepareFields(); |
|
462 | - |
|
463 | - $fake_field_columns_to_encode = []; |
|
464 | - |
|
465 | - foreach ($this->crud['fields'] as $k => $field) { |
|
466 | - // if it's a fake field |
|
467 | - if (isset($this->crud['fields'][$k]['fake']) && $this->crud['fields'][$k]['fake'] == true) { |
|
468 | - // add it to the request in its appropriate variable - the one defined, if defined |
|
469 | - if (isset($this->crud['fields'][$k]['store_in'])) { |
|
470 | - if (!in_array($this->crud['fields'][$k]['store_in'], $fake_field_columns_to_encode, true)) { |
|
471 | - array_push($fake_field_columns_to_encode, $this->crud['fields'][$k]['store_in']); |
|
472 | - } |
|
473 | - } else //otherwise in the one defined in the $crud variable |
|
474 | - { |
|
475 | - if (!in_array('extras', $fake_field_columns_to_encode, true)) { |
|
476 | - array_push($fake_field_columns_to_encode, 'extras'); |
|
477 | - } |
|
478 | - } |
|
479 | - } |
|
480 | - } |
|
481 | - |
|
482 | - if (!count($fake_field_columns_to_encode)) { |
|
483 | - return ['extras']; |
|
484 | - } |
|
485 | - |
|
486 | - return $fake_field_columns_to_encode; |
|
487 | - } |
|
488 | - |
|
489 | - |
|
490 | - // If it's not an array of array and it's a simple array, create a proper array of arrays for it |
|
491 | - protected function prepareColumns() |
|
492 | - { |
|
493 | - // if the columns aren't set, we can't show this page |
|
494 | - // TODO: instead of dying, show the columns defined as visible on the model |
|
495 | - if (!isset($this->crud['columns'])) |
|
496 | - { |
|
497 | - abort(500, "CRUD columns are not defined."); |
|
498 | - } |
|
499 | - |
|
500 | - // if the columns are defined as a string, transform it to a proper array |
|
501 | - if (!is_array($this->crud['columns'])) |
|
502 | - { |
|
503 | - $current_columns_array = explode(",", $this->crud['columns']); |
|
504 | - $proper_columns_array = array(); |
|
505 | - |
|
506 | - foreach ($current_columns_array as $key => $col) { |
|
507 | - $proper_columns_array[] = [ |
|
508 | - 'name' => $col, |
|
509 | - 'label' => ucfirst($col) //TODO: also replace _ with space |
|
510 | - ]; |
|
511 | - } |
|
512 | - |
|
513 | - $this->crud['columns'] = $proper_columns_array; |
|
514 | - } |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * Prepare the fields to be shown, stored, updated or created. |
|
519 | - * |
|
520 | - * Makes sure $this->crud['fields'] is in the proper format (array of arrays); |
|
521 | - * Makes sure $this->crud['fields'] also contains the id of the current item; |
|
522 | - * Makes sure $this->crud['fields'] also contains the values for each field; |
|
523 | - * |
|
524 | - */ |
|
525 | - protected function prepareFields($entry = false) |
|
526 | - { |
|
527 | - // if the fields have been defined separately for create and update, use that |
|
528 | - if (!isset($this->crud['fields'])) |
|
529 | - { |
|
530 | - if (isset($this->crud['create_fields'])) |
|
531 | - { |
|
532 | - $this->crud['fields'] = $this->crud['create_fields']; |
|
533 | - } elseif (isset($this->crud['update_fields'])) |
|
534 | - { |
|
535 | - $this->crud['fields'] = $this->crud['update_fields']; |
|
536 | - } |
|
537 | - } |
|
538 | - |
|
539 | - // PREREQUISITES CHECK: |
|
540 | - // if the fields aren't set, trigger error |
|
541 | - if (!isset($this->crud['fields'])) |
|
542 | - { |
|
543 | - abort(500, "The CRUD fields are not defined."); |
|
544 | - } |
|
545 | - |
|
546 | - // if the fields are defined as a string, transform it to a proper array |
|
547 | - if (!is_array($this->crud['fields'])) |
|
548 | - { |
|
549 | - $current_fields_array = explode(",", $this->crud['fields']); |
|
550 | - $proper_fields_array = array(); |
|
551 | - |
|
552 | - foreach ($current_fields_array as $key => $field) { |
|
553 | - $proper_fields_array[] = [ |
|
554 | - 'name' => $field, |
|
555 | - 'label' => ucfirst($field), // TODO: also replace _ with space |
|
556 | - 'type' => 'text' // TODO: choose different types of fields depending on the MySQL column type |
|
557 | - ]; |
|
558 | - } |
|
559 | - |
|
560 | - $this->crud['fields'] = $proper_fields_array; |
|
561 | - } |
|
562 | - |
|
563 | - // if no field type is defined, assume the "text" field type |
|
564 | - foreach ($this->crud['fields'] as $k => $field) { |
|
565 | - if (!isset($this->crud['fields'][$k]['type'])) { |
|
566 | - $this->crud['fields'][$k]['type'] = 'text'; |
|
567 | - } |
|
568 | - } |
|
569 | - |
|
570 | - // if an entry was passed, we're preparing for the update form, not create |
|
571 | - if ($entry) { |
|
572 | - // put the values in the same 'fields' variable |
|
573 | - $fields = $this->crud['fields']; |
|
574 | - |
|
575 | - foreach ($fields as $k => $field) { |
|
576 | - // set the value |
|
577 | - if (!isset($this->crud['fields'][$k]['value'])) |
|
578 | - { |
|
579 | - $this->crud['fields'][$k]['value'] = $entry->$field['name']; |
|
580 | - } |
|
581 | - } |
|
582 | - |
|
583 | - // always have a hidden input for the entry id |
|
584 | - $this->crud['fields'][] = array( |
|
585 | - 'name' => 'id', |
|
586 | - 'value' => $entry->id, |
|
587 | - 'type' => 'hidden' |
|
588 | - ); |
|
589 | - } |
|
590 | - } |
|
17 | + use DispatchesJobs, ValidatesRequests; |
|
18 | + |
|
19 | + public $data = array(); |
|
20 | + public $crud = array( |
|
21 | + "model" => "\App\Models\Entity", |
|
22 | + "entity_name" => "entry", |
|
23 | + "entity_name_plural" => "entries", |
|
24 | + "view_table_permission" => true, |
|
25 | + "add_permission" => true, |
|
26 | + "edit_permission" => true, |
|
27 | + "delete_permission" => true, |
|
28 | + "reorder_permission" => true, |
|
29 | + "reorder_max_level" => 3, |
|
30 | + "details_row" => false, |
|
31 | + ); |
|
32 | + |
|
33 | + public function __construct() |
|
34 | + { |
|
35 | + $this->data['crud'] = $this->crud; |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Display all rows in the database for this entity. |
|
40 | + * |
|
41 | + * @return Response |
|
42 | + */ |
|
43 | + public function index() |
|
44 | + { |
|
45 | + // SECURITY: |
|
46 | + // if view_table_permission is false, abort |
|
47 | + if (isset($this->crud['view_table_permission']) && !$this->crud['view_table_permission']) { |
|
48 | + abort(403, 'Not allowed.'); |
|
49 | + } |
|
50 | + |
|
51 | + // get all results for that entity |
|
52 | + $model = $this->crud['model']; |
|
53 | + if (property_exists($model, 'translatable')) |
|
54 | + { |
|
55 | + $this->data['entries'] = $model::where('translation_lang', \Lang::locale())->get(); |
|
56 | + } else |
|
57 | + { |
|
58 | + $this->data['entries'] = $model::all(); |
|
59 | + } |
|
60 | + |
|
61 | + // add the fake fields for each entry |
|
62 | + foreach ($this->data['entries'] as $key => $entry) { |
|
63 | + $entry->addFakes($this->getFakeColumnsAsArray()); |
|
64 | + } |
|
65 | + |
|
66 | + $this->prepareColumns(); |
|
67 | + $this->data['crud'] = $this->crud; |
|
68 | + $this->data['title'] = ucfirst($this->crud['entity_name_plural']); |
|
69 | + |
|
70 | + // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
71 | + return view('crud::list', $this->data); |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * Show the form for creating inserting a new row. |
|
77 | + * |
|
78 | + * @return Response |
|
79 | + */ |
|
80 | + public function create() |
|
81 | + { |
|
82 | + // SECURITY: |
|
83 | + // if add_permission is false, abort |
|
84 | + if (isset($this->crud['add_permission']) && !$this->crud['add_permission']) { |
|
85 | + abort(403, 'Not allowed.'); |
|
86 | + } |
|
87 | + |
|
88 | + // get the fields you need to show |
|
89 | + if (isset($this->data['crud']['create_fields'])) |
|
90 | + { |
|
91 | + $this->crud['fields'] = $this->data['crud']['create_fields']; |
|
92 | + } |
|
93 | + |
|
94 | + // prepare the fields you need to show |
|
95 | + $this->prepareFields(); |
|
96 | + $this->data['crud'] = $this->crud; |
|
97 | + $this->data['title'] = trans('backpack::crud.add').' '.$this->crud['entity_name']; |
|
98 | + |
|
99 | + // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
100 | + return view('crud::create', $this->data); |
|
101 | + } |
|
102 | + |
|
103 | + |
|
104 | + /** |
|
105 | + * Store a newly created resource in the database. |
|
106 | + * |
|
107 | + * @param StoreRequest $request - type injection used for validation using Requests |
|
108 | + * @return \Illuminate\Http\RedirectResponse |
|
109 | + */ |
|
110 | + public function storeCrud(StoreRequest $request = null) |
|
111 | + { |
|
112 | + // SECURITY: |
|
113 | + // if add_permission is false, abort |
|
114 | + if (isset($this->crud['add_permission']) && !$this->crud['add_permission']) { |
|
115 | + abort(403, 'Not allowed.'); |
|
116 | + } |
|
117 | + |
|
118 | + // compress the fake fields into one field |
|
119 | + $model = $this->crud['model']; |
|
120 | + $values_to_store = $this->compactFakeFields(\Request::all()); |
|
121 | + $item = $model::create($values_to_store); |
|
122 | + |
|
123 | + // if it's a relationship with a pivot table, also sync that |
|
124 | + $this->prepareFields(); |
|
125 | + foreach ($this->crud['fields'] as $k => $field) { |
|
126 | + if (isset($field['pivot']) && $field['pivot'] == true && \Request::has($field['name'])) |
|
127 | + { |
|
128 | + $model::find($item->id)->$field['name']()->attach(\Request::input($field['name'])); |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + // show a success message |
|
133 | + \Alert::success(trans('backpack::crud.insert_success'))->flash(); |
|
134 | + |
|
135 | + // redirect the user where he chose to be redirected |
|
136 | + switch (\Request::input('redirect_after_save')) { |
|
137 | + case 'current_item_edit': |
|
138 | + return \Redirect::to($this->crud['route'].'/'.$item->id.'/edit'); |
|
139 | + |
|
140 | + default: |
|
141 | + return \Redirect::to(\Request::input('redirect_after_save')); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + |
|
146 | + /** |
|
147 | + * Show the form for editing the specified resource. |
|
148 | + * |
|
149 | + * @param int $id |
|
150 | + * @return Response |
|
151 | + */ |
|
152 | + public function edit($id) |
|
153 | + { |
|
154 | + // SECURITY: |
|
155 | + // if edit_permission is false, abort |
|
156 | + if (isset($this->crud['edit_permission']) && !$this->crud['edit_permission']) { |
|
157 | + abort(403, 'Not allowed.'); |
|
158 | + } |
|
159 | + |
|
160 | + // get the info for that entry |
|
161 | + $model = $this->crud['model']; |
|
162 | + $this->data['entry'] = $model::find($id); |
|
163 | + $this->data['entry']->addFakes($this->getFakeColumnsAsArray()); |
|
164 | + |
|
165 | + if (isset($this->data['crud']['update_fields'])) |
|
166 | + { |
|
167 | + $this->crud['fields'] = $this->data['crud']['update_fields']; |
|
168 | + } |
|
169 | + |
|
170 | + // prepare the fields you need to show and prepopulate the values |
|
171 | + $this->prepareFields($this->data['entry']); |
|
172 | + $this->data['crud'] = $this->crud; |
|
173 | + $this->data['title'] = trans('backpack::crud.edit').' '.$this->crud['entity_name']; |
|
174 | + |
|
175 | + // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
176 | + return view('crud::edit', $this->data); |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * Update the specified resource in the database. |
|
182 | + * |
|
183 | + * @param UpdateRequest $request - type injection used for validation using Requests |
|
184 | + * @return \Illuminate\Http\RedirectResponse |
|
185 | + */ |
|
186 | + public function updateCrud(UpdateRequest $request = null) |
|
187 | + { |
|
188 | + // if edit_permission is false, abort |
|
189 | + if (isset($this->crud['edit_permission']) && !$this->crud['edit_permission']) { |
|
190 | + abort(403, 'Not allowed.'); |
|
191 | + } |
|
192 | + |
|
193 | + $model = $this->crud['model']; |
|
194 | + $this->prepareFields($model::find(\Request::input('id'))); |
|
195 | + |
|
196 | + $model::find(\Request::input('id'))->update($this->compactFakeFields(\Request::all())); |
|
197 | + |
|
198 | + // if it's a relationship with a pivot table, also sync that |
|
199 | + foreach ($this->crud['fields'] as $k => $field) { |
|
200 | + if (isset($field['pivot']) && $field['pivot'] == true && \Request::has($field['name'])) |
|
201 | + { |
|
202 | + $model::find(\Request::input('id'))->$field['name']()->sync(\Request::input($field['name'])); |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + // show a success message |
|
207 | + \Alert::success(trans('backpack::crud.update_success'))->flash(); |
|
208 | + |
|
209 | + return \Redirect::to($this->crud['route']); |
|
210 | + } |
|
211 | + |
|
212 | + |
|
213 | + /** |
|
214 | + * Display the specified resource. |
|
215 | + * |
|
216 | + * @param int $id |
|
217 | + * @return Response |
|
218 | + */ |
|
219 | + public function show($id) |
|
220 | + { |
|
221 | + // get the info for that entry |
|
222 | + $model = $this->crud['model']; |
|
223 | + $this->data['entry'] = $model::find($id); |
|
224 | + $this->data['entry']->addFakes($this->getFakeColumnsAsArray()); |
|
225 | + $this->data['crud'] = $this->crud; |
|
226 | + $this->data['title'] = trans('backpack::crud.preview').' '.$this->crud['entity_name']; |
|
227 | + |
|
228 | + // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
229 | + return view('crud::show', $this->data); |
|
230 | + } |
|
231 | + |
|
232 | + |
|
233 | + /** |
|
234 | + * Remove the specified resource from storage. |
|
235 | + * |
|
236 | + * @param int $id |
|
237 | + * @return string |
|
238 | + */ |
|
239 | + public function destroy($id) |
|
240 | + { |
|
241 | + // SECURITY: |
|
242 | + // if delete_permission is false, abort |
|
243 | + if (isset($this->crud['delete_permission']) && !$this->crud['delete_permission']) { |
|
244 | + abort(403, trans('backpack::crud.unauthorized_access')); |
|
245 | + } |
|
246 | + |
|
247 | + $model = $this->crud['model']; |
|
248 | + $item = $model::find($id); |
|
249 | + $item->delete(); |
|
250 | + |
|
251 | + return 'true'; |
|
252 | + } |
|
253 | + |
|
254 | + |
|
255 | + /** |
|
256 | + * Reorder the items in the database using the Nested Set pattern. |
|
257 | + * |
|
258 | + * Database columns needed: id, parent_id, lft, rgt, depth, name/title |
|
259 | + * |
|
260 | + * @return Response |
|
261 | + */ |
|
262 | + public function reorder($lang = false) |
|
263 | + { |
|
264 | + // if reorder_table_permission is false, abort |
|
265 | + if (isset($this->crud['reorder_permission']) && !$this->crud['reorder_permission']) { |
|
266 | + abort(403, 'Not allowed.'); |
|
267 | + } |
|
268 | + |
|
269 | + if ($lang == false) |
|
270 | + { |
|
271 | + $lang = \Lang::locale(); |
|
272 | + } |
|
273 | + |
|
274 | + // get all results for that entity |
|
275 | + $model = $this->crud['model']; |
|
276 | + if (property_exists($model, 'translatable')) |
|
277 | + { |
|
278 | + $this->data['entries'] = $model::where('translation_lang', $lang)->get(); |
|
279 | + $this->data['languages'] = \Backpack\LangFileManager\app\Models\Language::all(); |
|
280 | + $this->data['active_language'] = $lang; |
|
281 | + } else |
|
282 | + { |
|
283 | + $this->data['entries'] = $model::all(); |
|
284 | + } |
|
285 | + $this->data['crud'] = $this->crud; |
|
286 | + $this->data['title'] = trans('backpack::crud.reorder').' '.$this->crud['entity_name']; |
|
287 | + |
|
288 | + // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
289 | + return view('crud::reorder', $this->data); |
|
290 | + } |
|
291 | + |
|
292 | + |
|
293 | + /** |
|
294 | + * Save the new order, using the Nested Set pattern. |
|
295 | + * |
|
296 | + * Database columns needed: id, parent_id, lft, rgt, depth, name/title |
|
297 | + * |
|
298 | + * @return |
|
299 | + */ |
|
300 | + public function saveReorder() |
|
301 | + { |
|
302 | + // if reorder_table_permission is false, abort |
|
303 | + if (isset($this->crud['reorder_permission']) && !$this->crud['reorder_permission']) { |
|
304 | + abort(403, 'Not allowed.'); |
|
305 | + } |
|
306 | + |
|
307 | + $model = $this->crud['model']; |
|
308 | + $count = 0; |
|
309 | + $all_entries = \Request::input('tree'); |
|
310 | + |
|
311 | + if (count($all_entries)) { |
|
312 | + foreach ($all_entries as $key => $entry) { |
|
313 | + if ($entry['item_id'] != "" && $entry['item_id'] != null) { |
|
314 | + $item = $model::find($entry['item_id']); |
|
315 | + $item->parent_id = $entry['parent_id']; |
|
316 | + $item->depth = $entry['depth']; |
|
317 | + $item->lft = $entry['left']; |
|
318 | + $item->rgt = $entry['right']; |
|
319 | + $item->save(); |
|
320 | + |
|
321 | + $count++; |
|
322 | + } |
|
323 | + } |
|
324 | + } else |
|
325 | + { |
|
326 | + return false; |
|
327 | + } |
|
328 | + |
|
329 | + return 'success for '.$count." items"; |
|
330 | + } |
|
331 | + |
|
332 | + |
|
333 | + /** |
|
334 | + * Used with AJAX in the list view (datatables) to show extra information about that row that didn't fit in the table. |
|
335 | + * It defaults to showing all connected translations and their CRUD buttons. |
|
336 | + * |
|
337 | + * It's enabled by: |
|
338 | + * - setting the $crud['details_row'] variable to true; |
|
339 | + * - adding the details route for the entity; ex: Route::get('page/{id}/details', 'PageCrudController@showDetailsRow'); |
|
340 | + */ |
|
341 | + public function showDetailsRow($id) |
|
342 | + { |
|
343 | + // get the info for that entry |
|
344 | + $model = $this->crud['model']; |
|
345 | + $this->data['entry'] = $model::find($id); |
|
346 | + $this->data['entry']->addFakes($this->getFakeColumnsAsArray()); |
|
347 | + $this->data['original_entry'] = $this->data['entry']; |
|
348 | + $this->data['crud'] = $this->crud; |
|
349 | + |
|
350 | + if (property_exists($model, 'translatable')) |
|
351 | + { |
|
352 | + $this->data['translations'] = $this->data['entry']->translations(); |
|
353 | + |
|
354 | + // create a list of languages the item is not translated in |
|
355 | + $this->data['languages'] = \Backpack\LangFileManager\app\Models\Language::all(); |
|
356 | + $this->data['languages_already_translated_in'] = $this->data['entry']->translationLanguages(); |
|
357 | + $this->data['languages_to_translate_in'] = $this->data['languages']->diff($this->data['languages_already_translated_in']); |
|
358 | + $this->data['languages_to_translate_in'] = $this->data['languages_to_translate_in']->reject(function($item) { |
|
359 | + return $item->abbr == \Lang::locale(); |
|
360 | + }); |
|
361 | + } |
|
362 | + |
|
363 | + // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
|
364 | + return view('crud::details_row', $this->data); |
|
365 | + } |
|
366 | + |
|
367 | + |
|
368 | + /** |
|
369 | + * Duplicate an existing item into another language and open it for editing. |
|
370 | + */ |
|
371 | + public function translateItem($id, $lang) |
|
372 | + { |
|
373 | + $model = $this->crud['model']; |
|
374 | + $this->data['entry'] = $model::find($id); |
|
375 | + // check if there isn't a translation already |
|
376 | + $existing_translation = $this->data['entry']->translation($lang); |
|
377 | + |
|
378 | + if ($existing_translation) |
|
379 | + { |
|
380 | + $new_entry = $existing_translation; |
|
381 | + } else |
|
382 | + { |
|
383 | + // get the info for that entry |
|
384 | + $new_entry_attributes = $this->data['entry']->getAttributes(); |
|
385 | + $new_entry_attributes['translation_lang'] = $lang; |
|
386 | + $new_entry_attributes['translation_of'] = $id; |
|
387 | + $new_entry_attributes = array_except($new_entry_attributes, 'id'); |
|
388 | + |
|
389 | + $new_entry = $model::create($new_entry_attributes); |
|
390 | + } |
|
391 | + |
|
392 | + // redirect to the edit form for that translation |
|
393 | + return redirect(str_replace($id, $new_entry->id, str_replace('translate/'.$lang, 'edit', \Request::url()))); |
|
394 | + } |
|
395 | + |
|
396 | + |
|
397 | + |
|
398 | + /** |
|
399 | + * COMMODITY FUNCTIONS |
|
400 | + */ |
|
401 | + |
|
402 | + |
|
403 | + /** |
|
404 | + * Refactor the request array to something that can be passed to the model's create or update function. |
|
405 | + * The resulting array will only include the fields that are stored in the database and their values, |
|
406 | + * plus the '_token' and 'redirect_after_save' variables. |
|
407 | + * |
|
408 | + * @param Request $request - everything that was sent from the form, usually \Request::all() |
|
409 | + * @return array |
|
410 | + */ |
|
411 | + protected function compactFakeFields($request) { |
|
412 | + |
|
413 | + $this->prepareFields(); |
|
414 | + |
|
415 | + $fake_field_columns_to_encode = []; |
|
416 | + |
|
417 | + // go through each defined field |
|
418 | + foreach ($this->crud['fields'] as $k => $field) { |
|
419 | + // if it's a fake field |
|
420 | + if (isset($this->crud['fields'][$k]['fake']) && $this->crud['fields'][$k]['fake'] == true) { |
|
421 | + // add it to the request in its appropriate variable - the one defined, if defined |
|
422 | + if (isset($this->crud['fields'][$k]['store_in'])) { |
|
423 | + $request[$this->crud['fields'][$k]['store_in']][$this->crud['fields'][$k]['name']] = $request[$this->crud['fields'][$k]['name']]; |
|
424 | + |
|
425 | + $remove_fake_field = array_pull($request, $this->crud['fields'][$k]['name']); |
|
426 | + if (!in_array($this->crud['fields'][$k]['store_in'], $fake_field_columns_to_encode, true)) { |
|
427 | + array_push($fake_field_columns_to_encode, $this->crud['fields'][$k]['store_in']); |
|
428 | + } |
|
429 | + } else //otherwise in the one defined in the $crud variable |
|
430 | + { |
|
431 | + $request['extras'][$this->crud['fields'][$k]['name']] = $request[$this->crud['fields'][$k]['name']]; |
|
432 | + |
|
433 | + $remove_fake_field = array_pull($request, $this->crud['fields'][$k]['name']); |
|
434 | + if (!in_array('extras', $fake_field_columns_to_encode, true)) { |
|
435 | + array_push($fake_field_columns_to_encode, 'extras'); |
|
436 | + } |
|
437 | + } |
|
438 | + } |
|
439 | + } |
|
440 | + |
|
441 | + // json_encode all fake_value columns in the database, so they can be properly stored and interpreted |
|
442 | + if (count($fake_field_columns_to_encode)) { |
|
443 | + foreach ($fake_field_columns_to_encode as $key => $value) { |
|
444 | + $request[$value] = json_encode($request[$value]); |
|
445 | + } |
|
446 | + } |
|
447 | + |
|
448 | + // if there are no fake fields defined, this will just return the original Request in full |
|
449 | + // since no modifications or additions have been made to $request |
|
450 | + return $request; |
|
451 | + } |
|
452 | + |
|
453 | + |
|
454 | + /** |
|
455 | + * Returns an array of database columns names, that are used to store fake values. |
|
456 | + * Returns ['extras'] if no columns have been found. |
|
457 | + * |
|
458 | + */ |
|
459 | + protected function getFakeColumnsAsArray() { |
|
460 | + |
|
461 | + $this->prepareFields(); |
|
462 | + |
|
463 | + $fake_field_columns_to_encode = []; |
|
464 | + |
|
465 | + foreach ($this->crud['fields'] as $k => $field) { |
|
466 | + // if it's a fake field |
|
467 | + if (isset($this->crud['fields'][$k]['fake']) && $this->crud['fields'][$k]['fake'] == true) { |
|
468 | + // add it to the request in its appropriate variable - the one defined, if defined |
|
469 | + if (isset($this->crud['fields'][$k]['store_in'])) { |
|
470 | + if (!in_array($this->crud['fields'][$k]['store_in'], $fake_field_columns_to_encode, true)) { |
|
471 | + array_push($fake_field_columns_to_encode, $this->crud['fields'][$k]['store_in']); |
|
472 | + } |
|
473 | + } else //otherwise in the one defined in the $crud variable |
|
474 | + { |
|
475 | + if (!in_array('extras', $fake_field_columns_to_encode, true)) { |
|
476 | + array_push($fake_field_columns_to_encode, 'extras'); |
|
477 | + } |
|
478 | + } |
|
479 | + } |
|
480 | + } |
|
481 | + |
|
482 | + if (!count($fake_field_columns_to_encode)) { |
|
483 | + return ['extras']; |
|
484 | + } |
|
485 | + |
|
486 | + return $fake_field_columns_to_encode; |
|
487 | + } |
|
488 | + |
|
489 | + |
|
490 | + // If it's not an array of array and it's a simple array, create a proper array of arrays for it |
|
491 | + protected function prepareColumns() |
|
492 | + { |
|
493 | + // if the columns aren't set, we can't show this page |
|
494 | + // TODO: instead of dying, show the columns defined as visible on the model |
|
495 | + if (!isset($this->crud['columns'])) |
|
496 | + { |
|
497 | + abort(500, "CRUD columns are not defined."); |
|
498 | + } |
|
499 | + |
|
500 | + // if the columns are defined as a string, transform it to a proper array |
|
501 | + if (!is_array($this->crud['columns'])) |
|
502 | + { |
|
503 | + $current_columns_array = explode(",", $this->crud['columns']); |
|
504 | + $proper_columns_array = array(); |
|
505 | + |
|
506 | + foreach ($current_columns_array as $key => $col) { |
|
507 | + $proper_columns_array[] = [ |
|
508 | + 'name' => $col, |
|
509 | + 'label' => ucfirst($col) //TODO: also replace _ with space |
|
510 | + ]; |
|
511 | + } |
|
512 | + |
|
513 | + $this->crud['columns'] = $proper_columns_array; |
|
514 | + } |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * Prepare the fields to be shown, stored, updated or created. |
|
519 | + * |
|
520 | + * Makes sure $this->crud['fields'] is in the proper format (array of arrays); |
|
521 | + * Makes sure $this->crud['fields'] also contains the id of the current item; |
|
522 | + * Makes sure $this->crud['fields'] also contains the values for each field; |
|
523 | + * |
|
524 | + */ |
|
525 | + protected function prepareFields($entry = false) |
|
526 | + { |
|
527 | + // if the fields have been defined separately for create and update, use that |
|
528 | + if (!isset($this->crud['fields'])) |
|
529 | + { |
|
530 | + if (isset($this->crud['create_fields'])) |
|
531 | + { |
|
532 | + $this->crud['fields'] = $this->crud['create_fields']; |
|
533 | + } elseif (isset($this->crud['update_fields'])) |
|
534 | + { |
|
535 | + $this->crud['fields'] = $this->crud['update_fields']; |
|
536 | + } |
|
537 | + } |
|
538 | + |
|
539 | + // PREREQUISITES CHECK: |
|
540 | + // if the fields aren't set, trigger error |
|
541 | + if (!isset($this->crud['fields'])) |
|
542 | + { |
|
543 | + abort(500, "The CRUD fields are not defined."); |
|
544 | + } |
|
545 | + |
|
546 | + // if the fields are defined as a string, transform it to a proper array |
|
547 | + if (!is_array($this->crud['fields'])) |
|
548 | + { |
|
549 | + $current_fields_array = explode(",", $this->crud['fields']); |
|
550 | + $proper_fields_array = array(); |
|
551 | + |
|
552 | + foreach ($current_fields_array as $key => $field) { |
|
553 | + $proper_fields_array[] = [ |
|
554 | + 'name' => $field, |
|
555 | + 'label' => ucfirst($field), // TODO: also replace _ with space |
|
556 | + 'type' => 'text' // TODO: choose different types of fields depending on the MySQL column type |
|
557 | + ]; |
|
558 | + } |
|
559 | + |
|
560 | + $this->crud['fields'] = $proper_fields_array; |
|
561 | + } |
|
562 | + |
|
563 | + // if no field type is defined, assume the "text" field type |
|
564 | + foreach ($this->crud['fields'] as $k => $field) { |
|
565 | + if (!isset($this->crud['fields'][$k]['type'])) { |
|
566 | + $this->crud['fields'][$k]['type'] = 'text'; |
|
567 | + } |
|
568 | + } |
|
569 | + |
|
570 | + // if an entry was passed, we're preparing for the update form, not create |
|
571 | + if ($entry) { |
|
572 | + // put the values in the same 'fields' variable |
|
573 | + $fields = $this->crud['fields']; |
|
574 | + |
|
575 | + foreach ($fields as $k => $field) { |
|
576 | + // set the value |
|
577 | + if (!isset($this->crud['fields'][$k]['value'])) |
|
578 | + { |
|
579 | + $this->crud['fields'][$k]['value'] = $entry->$field['name']; |
|
580 | + } |
|
581 | + } |
|
582 | + |
|
583 | + // always have a hidden input for the entry id |
|
584 | + $this->crud['fields'][] = array( |
|
585 | + 'name' => 'id', |
|
586 | + 'value' => $entry->id, |
|
587 | + 'type' => 'hidden' |
|
588 | + ); |
|
589 | + } |
|
590 | + } |
|
591 | 591 | |
592 | 592 | } |
@@ -71,8 +71,7 @@ |
||
71 | 71 | if ($results && $results->count()) { |
72 | 72 | $results_array = $results->lists($column['attribute'], 'id'); |
73 | 73 | echo implode(', ', $results_array->toArray()); |
74 | - } |
|
75 | - else |
|
74 | + } else |
|
76 | 75 | { |
77 | 76 | echo '-'; |
78 | 77 | } |
@@ -12,14 +12,14 @@ |
||
12 | 12 | |-------------------------------------------------------------------------- |
13 | 13 | */ |
14 | 14 | |
15 | - public static function getPossibleEnumValues($field_name){ |
|
15 | + public static function getPossibleEnumValues($field_name) { |
|
16 | 16 | $instance = new static; // create an instance of the model to be able to get the table name |
17 | - $type = DB::select( DB::raw('SHOW COLUMNS FROM '.$instance->getTable().' WHERE Field = "'.$field_name.'"') )[0]->Type; |
|
17 | + $type = DB::select(DB::raw('SHOW COLUMNS FROM '.$instance->getTable().' WHERE Field = "'.$field_name.'"'))[0]->Type; |
|
18 | 18 | preg_match('/^enum\((.*)\)$/', $type, $matches); |
19 | 19 | $enum = array(); |
20 | 20 | $exploded = explode(',', $matches[1]); |
21 | - foreach($exploded as $value){ |
|
22 | - $v = trim( $value, "'" ); |
|
21 | + foreach ($exploded as $value) { |
|
22 | + $v = trim($value, "'"); |
|
23 | 23 | $enum[] = $v; |
24 | 24 | } |
25 | 25 | return $enum; |