|
1
|
|
|
<?php namespace Mascame\Artificer\Http\Controllers; |
|
2
|
|
|
|
|
3
|
|
|
use Event; |
|
4
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
5
|
|
|
use Input; |
|
6
|
|
|
use Mascame\Artificer\Options\AdminOption; |
|
7
|
|
|
use Redirect; |
|
8
|
|
|
use Request; |
|
9
|
|
|
use Response; |
|
10
|
|
|
use Session; |
|
11
|
|
|
use URL; |
|
12
|
|
|
use View; |
|
13
|
|
|
|
|
14
|
|
|
class ModelController extends BaseModelController |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Show the form for creating a new resource. |
|
19
|
|
|
* |
|
20
|
|
|
* @return Response |
|
21
|
|
|
*/ |
|
22
|
|
|
public function create() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->handleData($this->modelObject->schema->getInstance()); |
|
25
|
|
|
|
|
26
|
|
|
$form = array( |
|
27
|
|
|
'form_action_route' => 'admin.model.store', |
|
28
|
|
|
'form_method' => 'post' |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
return View::make($this->getView('edit'))->with('items', $this->data)->with($form); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $modelName |
|
36
|
|
|
* @return $this |
|
37
|
|
|
*/ |
|
38
|
|
|
public function filter($modelName) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->handleData($this->model->firstOrFail()); |
|
41
|
|
|
|
|
42
|
|
|
$sort = $this->getSort(); |
|
43
|
|
|
|
|
44
|
|
|
$data = $this->model->where(function ($query) { |
|
45
|
|
|
|
|
46
|
|
|
foreach (Input::all() as $name => $value) { |
|
47
|
|
|
if ($value != '' && isset($this->fields[$name])) { |
|
48
|
|
|
$this->fields[$name]->filter($query, $value); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $query; |
|
53
|
|
|
})->with($this->modelObject->getRelations())->orderBy($sort['column'], $sort['direction'])->paginate(); |
|
54
|
|
|
|
|
55
|
|
|
return parent::all($modelName, $data, $sort); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Todo: rethink the way relations are made |
|
60
|
|
|
* |
|
61
|
|
|
* Store a newly created resource in storage. |
|
62
|
|
|
* |
|
63
|
|
|
* @return Response |
|
64
|
|
|
*/ |
|
65
|
|
|
public function store() |
|
66
|
|
|
{ |
|
67
|
|
|
$data = $this->filterInputData(); |
|
68
|
|
|
|
|
69
|
|
|
$validator = $this->validator($data); |
|
70
|
|
|
if ($validator->fails()) { |
|
71
|
|
|
return $this->redirect($validator, 'admin.model.create'); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->handleData($data); |
|
75
|
|
|
|
|
76
|
|
|
$this->model->guard($this->modelObject->getOption('guarded', array())); |
|
77
|
|
|
|
|
78
|
|
|
$item = $this->model->create(with($this->handleFiles($data))); |
|
79
|
|
|
|
|
80
|
|
|
$relation_on_create = '_set_relation_on_create'; |
|
81
|
|
|
if (Input::has($relation_on_create)) { |
|
82
|
|
|
$relateds = array( |
|
83
|
|
|
'id' => $item->id, |
|
84
|
|
|
'modelClass' => $this->modelObject->class, |
|
85
|
|
|
'foreign' => Input::get('_set_relation_on_create_foreign') |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
Session::push($relation_on_create . '_' . Input::get($relation_on_create), $relateds); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (Session::has($relation_on_create . '_' . $this->modelObject->name)) { |
|
92
|
|
|
$relations = Session::get($relation_on_create . '_' . $this->modelObject->name); |
|
93
|
|
|
|
|
94
|
|
|
foreach ($relations as $relation) { |
|
95
|
|
|
$related_item = $relation['modelClass']::find($relation['id']); |
|
96
|
|
|
$related_item->$relation['foreign'] = $item->id; |
|
97
|
|
|
$related_item->save(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
Session::forget($relation_on_create . '_' . $this->modelObject->name); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (Request::ajax()) { |
|
104
|
|
|
return $this->handleAjaxResponse($item); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return Redirect::route('admin.model.all', array('slug' => $this->modelObject->getRouteName())); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Display the specified resource. |
|
112
|
|
|
* |
|
113
|
|
|
* @param int $id |
|
114
|
|
|
* @return Response |
|
115
|
|
|
*/ |
|
116
|
|
|
public function show($modelName, $id) |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
$this->handleData($this->model->findOrFail($id)); |
|
119
|
|
|
|
|
120
|
|
|
return View::make($this->getView('show'))->with('items', $this->data); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Display the specified post. |
|
125
|
|
|
* |
|
126
|
|
|
* @return Response |
|
127
|
|
|
*/ |
|
128
|
|
|
public function all($modelName, $data = null, $sort = null) |
|
129
|
|
|
{ |
|
130
|
|
|
$sort = $this->getSort(); |
|
131
|
|
|
|
|
132
|
|
|
$data = $this->model->with($this->modelObject->getRelations())->orderBy($sort['column'], |
|
133
|
|
|
$sort['direction'])->get(); |
|
134
|
|
|
|
|
135
|
|
|
return parent::all($modelName, $data, $sort); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Show the form for editing the specified post. |
|
140
|
|
|
* |
|
141
|
|
|
* @param int $id |
|
142
|
|
|
* @return Response |
|
143
|
|
|
*/ |
|
144
|
|
|
public function edit($modelName, $id) |
|
|
|
|
|
|
145
|
|
|
{ |
|
146
|
|
|
$this->handleData($this->model->with($this->modelObject->getRelations())->findOrFail($id)); |
|
147
|
|
|
|
|
148
|
|
|
$form = array( |
|
149
|
|
|
'form_action_route' => 'admin.model.update', |
|
150
|
|
|
'form_method' => 'put' |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
return View::make($this->getView('edit'))->with('items', $this->data)->with($form); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function field($modelName, $id, $field) |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
$this->handleData($this->model->with($this->modelObject->getRelations())->findOrFail($id)); |
|
159
|
|
|
|
|
160
|
|
|
$this->fields[$field]->showFullField = true; |
|
161
|
|
|
|
|
162
|
|
|
return \HTML::field($this->fields[$field], AdminOption::get('icons')); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
protected function handleAjaxResponse($item) |
|
166
|
|
|
{ |
|
167
|
|
|
return Response::json(array( |
|
168
|
|
|
'item' => $item->toArray(), |
|
169
|
|
|
'refresh' => URL::route('admin.model.field.edit', array( |
|
170
|
|
|
'slug' => Input::get('_standalone_origin'), |
|
171
|
|
|
'id' => Input::get('_standalone_origin_id'), |
|
172
|
|
|
'field' => ':fieldName:' |
|
173
|
|
|
)) |
|
174
|
|
|
) |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Update the specified resource in storage. |
|
180
|
|
|
* |
|
181
|
|
|
* @param int $id |
|
182
|
|
|
* @return Response |
|
183
|
|
|
*/ |
|
184
|
|
|
|
|
185
|
|
|
public function update($modelName, $id) |
|
|
|
|
|
|
186
|
|
|
{ |
|
187
|
|
|
$item = $this->model->findOrFail($id); |
|
188
|
|
|
|
|
189
|
|
|
$data = $this->filterInputData(); |
|
190
|
|
|
|
|
191
|
|
|
$validator = $this->validator($data); |
|
192
|
|
|
if ($validator->fails()) { |
|
193
|
|
|
return $this->redirect($validator, 'admin.model.edit', $id); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
$item->update(with($this->handleFiles($data))); |
|
197
|
|
|
|
|
198
|
|
|
if (Request::ajax()) { |
|
199
|
|
|
return $this->handleAjaxResponse($item); |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return Redirect::route('admin.model.all', array('slug' => $this->modelObject->getRouteName())); |
|
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Remove the specified resource from storage. |
|
207
|
|
|
* |
|
208
|
|
|
* @param int $id |
|
209
|
|
|
* @return Response |
|
210
|
|
|
*/ |
|
211
|
|
|
public function destroy($modelName, $id) |
|
212
|
|
|
{ |
|
213
|
|
|
$event_info = array( |
|
214
|
|
|
array( |
|
215
|
|
|
"model" => $modelName, |
|
216
|
|
|
"id" => $id |
|
217
|
|
|
) |
|
218
|
|
|
); |
|
219
|
|
|
|
|
220
|
|
|
Event::fire('artificer.model.before.destroy', $event_info); |
|
221
|
|
|
|
|
222
|
|
|
if ($this->model->destroy($id)) { |
|
223
|
|
|
Notification::success('<b>Success!</b> The record has been deleted!', true); |
|
224
|
|
|
Event::fire('artificer.model.after.destroy', $event_info); |
|
225
|
|
|
} else { |
|
226
|
|
|
Notification::danger('<b>Failed!</b> The record could not be deleted!'); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
if (Request::ajax()) { |
|
230
|
|
|
// todo |
|
231
|
|
|
return Response::json(array()); |
|
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return Redirect::back(); |
|
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
// return Redirect::route('admin.model.all', array('slug' => $this->modelObject->getRouteName())); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
} |
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 theSoncalls the wrong method in the parent class.