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
|
|
|
$this->crud->allowAccess('reorder'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function index() |
23
|
|
|
{ |
24
|
|
|
$this->crud->hasAccessOrFail('list'); |
25
|
|
|
|
26
|
|
|
$this->data['crud'] = $this->crud; |
27
|
|
|
$this->data['title'] = ucfirst($this->crud->entity_name_plural); |
28
|
|
|
|
29
|
|
|
$entries = $this->data['crud']->getEntries(); |
30
|
|
|
$this->data['entries'] = $this->crud->model::loadTree($entries); |
|
|
|
|
31
|
|
|
|
32
|
|
|
return view($this->crud->getListView(), $this->data); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function create() |
36
|
|
|
{ |
37
|
|
|
$request = \Request::instance(); |
38
|
|
|
|
39
|
|
|
if ($parent = $request->get('parent')) { |
40
|
|
|
$this->crud->addField([ |
41
|
|
|
'name' => 'parent_id', |
42
|
|
|
'type' => 'hidden', |
43
|
|
|
'value' => $parent |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($request->wantsJson()) { |
48
|
|
|
return $this->ajaxCreate($request); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return parent::create(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function ajaxCreate(Request $request) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->crud->hasAccessOrFail('create'); |
57
|
|
|
|
58
|
|
|
// prepare the fields you need to show |
59
|
|
|
$this->data['crud'] = $this->crud; |
60
|
|
|
$this->data['saveAction'] = $this->getSaveAction(); |
61
|
|
|
$this->data['fields'] = $this->crud->getCreateFields(); |
62
|
|
|
$this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name; |
63
|
|
|
|
64
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
65
|
|
|
return view('nestedmodels::ajax.create', $this->data); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function storeCrud(StoreRequest $request = null) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if ($request->wantsJson()) { |
|
|
|
|
71
|
|
|
return $this->ajaxStore($request); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// your additional operations before save here |
75
|
|
|
$redirect_location = parent::storeCrud($request); |
76
|
|
|
// your additional operations after save here |
77
|
|
|
// use $this->data['entry'] or $this->crud->entry |
|
|
|
|
78
|
|
|
return $redirect_location; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function ajaxStore(StoreRequest $request) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$this->crud->hasAccessOrFail('create'); |
84
|
|
|
|
85
|
|
|
// fallback to global request instance |
86
|
|
|
if (is_null($request)) { |
87
|
|
|
$request = \Request::instance(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// replace empty values with NULL, so that it will work with MySQL strict mode on |
91
|
|
|
foreach ($request->input() as $key => $value) { |
|
|
|
|
92
|
|
|
if (empty($value) && $value !== '0') { |
93
|
|
|
$request->request->set($key, null); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// insert item in the db |
98
|
|
|
return $this->crud->create($request->except(['save_action', '_token', '_method'])); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function saveReorder() |
102
|
|
|
{ |
103
|
|
|
$this->crud->hasAccess('reorder'); |
104
|
|
|
$request = \Request::instance(); |
105
|
|
|
|
106
|
|
|
$this->crud->model::rebuildTree($request->all()); |
|
|
|
|
107
|
|
|
|
108
|
|
|
return (string) true; |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.