1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webfactor\Laravel\Backpack\NestedModels\Controllers; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
6
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as StoreRequest; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
|
9
|
|
|
class NestedModelsCrudController extends CrudController |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
public function setup() |
13
|
|
|
{ |
14
|
|
|
parent::setup(); |
15
|
|
|
|
16
|
|
|
$this->crud->setListView('nestedmodels::treecrud'); |
17
|
|
|
$this->crud->orderBy('lft'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function index() |
21
|
|
|
{ |
22
|
|
|
$this->crud->hasAccessOrFail('list'); |
23
|
|
|
|
24
|
|
|
$this->data['crud'] = $this->crud; |
25
|
|
|
$this->data['title'] = ucfirst($this->crud->entity_name_plural); |
26
|
|
|
|
27
|
|
|
$entries = $this->data['crud']->getEntries(); |
28
|
|
|
$this->data['entries'] = $this->crud->model::loadTree($entries); |
|
|
|
|
29
|
|
|
|
30
|
|
|
return view($this->crud->getListView(), $this->data); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function create() |
34
|
|
|
{ |
35
|
|
|
$request = Request::instance(); |
36
|
|
|
$this->addFieldsForType($request->get('type')); |
|
|
|
|
37
|
|
|
if ($parent = $request->get('parent')) { |
38
|
|
|
$this->crud->addField([ |
39
|
|
|
'name' => 'parent_id', |
40
|
|
|
'type' => 'hidden', |
41
|
|
|
'value' => $parent |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($request->wantsJson()) { |
46
|
|
|
return $this->ajaxCreate($request); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return parent::create(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function ajaxCreate(Request $request) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$this->crud->hasAccessOrFail('create'); |
55
|
|
|
|
56
|
|
|
// prepare the fields you need to show |
57
|
|
|
$this->data['crud'] = $this->crud; |
58
|
|
|
$this->data['saveAction'] = $this->getSaveAction(); |
59
|
|
|
$this->data['fields'] = $this->crud->getCreateFields(); |
60
|
|
|
$this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name; |
61
|
|
|
|
62
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
63
|
|
|
return view('nestedmodels::ajax.create', $this->data); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function store(StoreRequest $request) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if ($request->wantsJson()) { |
69
|
|
|
return $this->ajaxStore($request); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// your additional operations before save here |
73
|
|
|
$redirect_location = parent::storeCrud($request); |
|
|
|
|
74
|
|
|
// your additional operations after save here |
75
|
|
|
// use $this->data['entry'] or $this->crud->entry |
|
|
|
|
76
|
|
|
return $redirect_location; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function ajaxStore(StoreRequest $request) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$this->crud->hasAccessOrFail('create'); |
82
|
|
|
|
83
|
|
|
// fallback to global request instance |
84
|
|
|
if (is_null($request)) { |
85
|
|
|
$request = \Request::instance(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// replace empty values with NULL, so that it will work with MySQL strict mode on |
89
|
|
|
foreach ($request->input() as $key => $value) { |
|
|
|
|
90
|
|
|
if (empty($value) && $value !== '0') { |
91
|
|
|
$request->request->set($key, null); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// insert item in the db |
96
|
|
|
return $this->crud->create($request->except(['save_action', '_token', '_method'])); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.