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