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