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