1
|
|
|
<?php namespace Mascame\Artificer\Controllers; |
2
|
|
|
|
3
|
|
|
use Input; |
4
|
|
|
use Mascame\Artificer\Options\AdminOption; |
5
|
|
|
use Redirect; |
6
|
|
|
use Request; |
7
|
|
|
use Response; |
8
|
|
|
use URL; |
9
|
|
|
use View; |
10
|
|
|
|
11
|
|
|
class ModelController extends BaseModelController |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Show the form for creating a new resource. |
16
|
|
|
* |
17
|
|
|
* @return Response |
18
|
|
|
*/ |
19
|
|
|
public function create() |
20
|
|
|
{ |
21
|
|
|
$this->handleData($this->modelObject->schema->getInstance()); |
22
|
|
|
|
23
|
|
|
$form = array( |
24
|
|
|
'form_action_route' => 'admin.model.store', |
25
|
|
|
'form_method' => 'post' |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
return View::make($this->getView('edit'))->with('items', $this->data)->with($form); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $modelName |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function filter($modelName) |
36
|
|
|
{ |
37
|
|
|
$this->handleData($this->model->firstOrFail()); |
38
|
|
|
|
39
|
|
|
$sort = $this->getSort(); |
40
|
|
|
|
41
|
|
|
$data = $this->model->where(function ($query) { |
42
|
|
|
|
43
|
|
|
foreach (\Request::all() as $name => $value) { |
44
|
|
|
if ($value != '' && isset($this->fields[$name])) { |
45
|
|
|
$this->fields[$name]->filter($query, $value); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return null; |
50
|
|
|
}) |
51
|
|
|
->with($this->modelObject->getRelations()) |
52
|
|
|
->orderBy($sort['column'], $sort['direction']) |
53
|
|
|
->get(); |
54
|
|
|
|
55
|
|
|
return parent::all($modelName, $data, $sort); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Display the specified resource. |
60
|
|
|
* |
61
|
|
|
* @param int $id |
62
|
|
|
* @return Response |
63
|
|
|
*/ |
64
|
|
|
public function show($modelName, $id) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$this->handleData($this->model->findOrFail($id)); |
67
|
|
|
|
68
|
|
|
return View::make($this->getView('show'))->with('item', $this->data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Display the specified post. |
73
|
|
|
* |
74
|
|
|
* @return Response |
75
|
|
|
*/ |
76
|
|
|
public function all($modelName, $data = null, $sort = null) |
77
|
|
|
{ |
78
|
|
|
$sort = $this->getSort(); |
79
|
|
|
|
80
|
|
|
$data = $this->model->with($this->modelObject->getRelations())->orderBy($sort['column'], |
81
|
|
|
$sort['direction'])->get(); |
82
|
|
|
|
83
|
|
|
return parent::all($modelName, $data, $sort); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Show the form for editing the specified post. |
88
|
|
|
* |
89
|
|
|
* @param int $id |
90
|
|
|
* @return Response |
91
|
|
|
*/ |
92
|
|
|
public function edit($modelName, $id) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$this->handleData( |
95
|
|
|
$this->model->with($this->modelObject->getRelations())->findOrFail($id) |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$form = array( |
99
|
|
|
'form_action_route' => 'admin.model.update', |
100
|
|
|
'form_method' => 'put' |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return View::make($this->getView('edit')) |
104
|
|
|
->with('items', $this->data) |
105
|
|
|
->with($form); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function field($modelName, $id, $field) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$this->handleData($this->model->with($this->modelObject->getRelations())->findOrFail($id)); |
111
|
|
|
|
112
|
|
|
$this->fields[$field]->showFullField = true; |
113
|
|
|
|
114
|
|
|
return \HTML::field($this->fields[$field], AdminOption::get('icons')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function handleAjaxResponse($item) |
118
|
|
|
{ |
119
|
|
|
return Response::json(array( |
120
|
|
|
'item' => $item->toArray(), |
121
|
|
|
'refresh' => URL::route('admin.model.field.edit', array( |
122
|
|
|
'slug' => Input::get('_standalone_origin'), |
123
|
|
|
'id' => Input::get('_standalone_origin_id'), |
124
|
|
|
'field' => ':fieldName:' |
125
|
|
|
)) |
126
|
|
|
) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Update or create the specified resource in storage. |
132
|
|
|
* |
133
|
|
|
* @param int $id |
134
|
|
|
* @return Response |
135
|
|
|
*/ |
136
|
|
|
|
137
|
|
|
public function updateOrCreate($modelName, $id = null) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$isUpdating = $id; |
140
|
|
|
$item = null; |
141
|
|
|
|
142
|
|
|
if ($isUpdating) { |
|
|
|
|
143
|
|
|
$item = $this->model->findOrFail($id); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$data = $this->filterInputData(); |
147
|
|
|
|
148
|
|
|
$validator = $this->validator($data); |
149
|
|
|
|
150
|
|
|
if ($validator->fails()) { |
151
|
|
|
|
152
|
|
|
if ($isUpdating) { |
|
|
|
|
153
|
|
|
return $this->redirect($validator, 'admin.model.edit', $id); |
|
|
|
|
154
|
|
|
} else { |
155
|
|
|
return $this->redirect($validator, 'admin.model.create'); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$this->model->guard($this->modelObject->getGuarded()); |
160
|
|
|
$this->model->fillable($this->modelObject->getOption('fillable', [])); |
161
|
|
|
|
162
|
|
|
// $this->handleData($data); |
163
|
|
|
|
164
|
|
|
$data = $this->handleFiles($data); |
165
|
|
|
|
166
|
|
|
if ($isUpdating) { |
|
|
|
|
167
|
|
|
$item->update($data); |
168
|
|
|
} else { |
169
|
|
|
$item = $this->model->create($data); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (Request::ajax()) { |
173
|
|
|
return $this->handleAjaxResponse($item); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return Redirect::route('admin.model.all', array('slug' => $this->modelObject->getRouteName())); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Remove the specified resource from storage. |
181
|
|
|
* |
182
|
|
|
* @param $modelName |
183
|
|
|
* @param $id |
184
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
185
|
|
|
*/ |
186
|
|
|
public function destroy($modelName, $id) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
if ($this->model->destroy($id)) { |
189
|
|
|
// Todo Notify::success('<b>Success!</b> The record has been deleted!', true); |
190
|
|
|
} else { |
191
|
|
|
// Todo Notify::danger('<b>Failed!</b> The record could not be deleted!'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return Request::ajax() ? \Response::json([]) : Redirect::back(); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
} |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.