1
|
|
|
<?php namespace Mascame\Artificer\Http\Controllers; |
2
|
|
|
|
3
|
|
|
use App; |
4
|
|
|
use Auth; |
5
|
|
|
use File; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Routing\Redirector; |
8
|
|
|
use Illuminate\Routing\Router; |
9
|
|
|
use Input; |
10
|
|
|
use Mascame\Artificer\Fields\Field; |
11
|
|
|
use Mascame\Artificer\Fields\FieldFactory; |
12
|
|
|
use Mascame\Formality\Factory\Factory; |
13
|
|
|
use Mascame\Formality\Parser\Parser; |
14
|
|
|
use Mascame\Artificer\Permit\ModelPermit; |
15
|
|
|
use Mascame\Formality\Manager\Manager; |
16
|
|
|
use Redirect; |
17
|
|
|
use Route; |
18
|
|
|
use Session; |
19
|
|
|
use Validator; |
20
|
|
|
use View; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class BaseModelController extends BaseController |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The Eloquent model instance |
28
|
|
|
* @var \Eloquent |
29
|
|
|
*/ |
30
|
|
|
protected $model; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
parent::__construct(); |
38
|
|
|
|
39
|
|
|
// Todo: Do Sth with this |
40
|
|
|
if (false) { |
41
|
|
|
if (! Auth::check() || ! ModelPermit::access()) { |
42
|
|
|
App::abort('403', 'Forbidden access'); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->model = $this->modelObject->model; |
47
|
|
|
|
48
|
|
|
$this->checkPermissions(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
|
|
protected function checkPermissions() |
55
|
|
|
{ |
56
|
|
|
$permit = array( |
57
|
|
|
'read' => ModelPermit::to('read'), |
58
|
|
|
'create' => ModelPermit::to('create'), |
59
|
|
|
'update' => ModelPermit::to('update'), |
60
|
|
|
'delete' => ModelPermit::to('delete'), |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
ModelPermit::routeAction(Route::currentRouteName()); |
64
|
|
|
|
65
|
|
|
View::share('permit', $permit); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $data |
70
|
|
|
*/ |
71
|
|
|
protected function handleData($data) |
72
|
|
|
{ |
73
|
|
|
$this->data = $data; |
74
|
|
|
|
75
|
|
|
$this->getFields($data); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $data |
80
|
|
|
* @return null |
81
|
|
|
*/ |
82
|
|
|
protected function getFields($data) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
if ($this->fields) return $this->fields; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var $data Collection |
88
|
|
|
*/ |
89
|
|
|
// Todo: try to avoid parsing all columns each time... |
90
|
|
|
|
91
|
|
|
$fieldFactory = new FieldFactory(new Parser(config('admin.fields.types')), $this->modelObject->columns, config('admin.fields.classmap')); |
92
|
|
|
$this->fields = $fieldFactory->makeFields(); |
93
|
|
|
|
94
|
|
|
View::share('fields', $this->fields); |
95
|
|
|
|
96
|
|
|
return $this->fields; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Prepares fields for factory |
100
|
|
|
// protected function prepareFields($data) { |
101
|
|
|
// $fields = []; |
102
|
|
|
// |
103
|
|
|
// foreach ($data as $key => $item) { |
104
|
|
|
// foreach ($this->modelObject->columns as $column) { |
105
|
|
|
// $fields[$key][$column] = $item->$column; |
106
|
|
|
// } |
107
|
|
|
// } |
108
|
|
|
// |
109
|
|
|
// return $fields; |
110
|
|
|
// } |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
protected function getSort() |
116
|
|
|
{ |
117
|
|
|
$sort = array(); |
118
|
|
|
|
119
|
|
|
if (Input::has('sort_by')) { |
120
|
|
|
$sort['column'] = Input::get('sort_by'); |
121
|
|
|
$sort['direction'] = Input::get('direction'); |
122
|
|
|
} else { |
123
|
|
|
|
124
|
|
|
if ($this->modelObject->schema->hasColumn('sort_id')) { |
125
|
|
|
$sort['column'] = 'sort_id'; |
126
|
|
|
} else { |
127
|
|
|
$sort['column'] = 'id'; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$sort['direction'] = 'asc'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $sort; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
protected function getRules() |
140
|
|
|
{ |
141
|
|
|
if (isset($this->options['rules'])) { |
142
|
|
|
return $this->options['rules']; |
143
|
|
|
} else { |
144
|
|
|
if (isset($this->model->rules)) { |
145
|
|
|
return $this->model->rules; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return array(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $items |
154
|
|
|
* @return null |
155
|
|
|
*/ |
156
|
|
|
public static function getCurrentModelId($items) |
157
|
|
|
{ |
158
|
|
|
return (isset($items->id)) ? $items->id : null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array|mixed |
163
|
|
|
*/ |
164
|
|
|
protected function filterInputData() |
165
|
|
|
{ |
166
|
|
|
if ($this->modelObject->hasGuarded()) { |
167
|
|
|
$input = Input::all(); |
168
|
|
|
$filtered_input = array(); |
169
|
|
|
|
170
|
|
|
foreach ($input as $key => $value) { |
171
|
|
|
if (in_array($key, $this->modelObject->columns)) { |
172
|
|
|
$filtered_input[$key] = $value; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->except($this->modelObject->options['guarded'], $filtered_input); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return Input::except('id'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param $keys |
184
|
|
|
* @param $values |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
|
|
protected function except($keys, $values) |
188
|
|
|
{ |
189
|
|
|
$keys = is_array($keys) ? $keys : func_get_args(); |
190
|
|
|
|
191
|
|
|
$results = $values; |
192
|
|
|
|
193
|
|
|
array_forget($results, $keys); |
194
|
|
|
|
195
|
|
|
return $results; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param $data |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
protected function handleFiles($data) |
203
|
|
|
{ |
204
|
|
|
$new_data = array(); |
205
|
|
|
$fields = $this->getFields($data); |
|
|
|
|
206
|
|
|
|
207
|
|
|
if (!is_null($fields)) { |
208
|
|
|
foreach ($fields as $field) { |
209
|
|
|
if ($this->isFileInput($field->type)) { |
210
|
|
|
if (Input::hasFile($field->name)) { |
211
|
|
|
$new_data[$field->name] = $this->uploadFile($field->name); |
212
|
|
|
} else { |
213
|
|
|
unset($data[$field->name]); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return array_merge($data, $new_data); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param $type |
224
|
|
|
* @return bool |
225
|
|
|
*/ |
226
|
|
|
protected function isFileInput($type) |
227
|
|
|
{ |
228
|
|
|
return ($type == 'file' || $type == 'image'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* This is used for simple upload (no plugins) |
233
|
|
|
* |
234
|
|
|
* @param $fieldName |
235
|
|
|
* @param null $path |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
|
protected function uploadFile($fieldName, $path = null) |
239
|
|
|
{ |
240
|
|
|
if (!$path) { |
241
|
|
|
$path = public_path() . '/uploads/'; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$file = Input::file($fieldName); |
245
|
|
|
|
246
|
|
|
if (!file_exists($path)) { |
247
|
|
|
File::makeDirectory($path); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$name = uniqid() . '-' . Str::slug($file->getFilename()) . '.' . $file->guessExtension(); |
251
|
|
|
|
252
|
|
|
$file->move($path, $name); |
253
|
|
|
|
254
|
|
|
return $name; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param $modelName |
259
|
|
|
* @param $id |
260
|
|
|
* @param $field |
261
|
|
|
* @return null |
262
|
|
|
*/ |
263
|
|
|
protected function getRelatedFieldOutput($modelName, $id, $field) |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
if ($id != 0) { |
266
|
|
|
$this->handleData($this->model->with($this->modelObject->getRelations())->findOrFail($id)); |
267
|
|
|
} else { |
268
|
|
|
if (Session::has('_set_relation_on_create_' . $this->modelObject->name)) { |
269
|
|
|
$relateds = Session::get('_set_relation_on_create_' . $this->modelObject->name); |
270
|
|
|
$related_ids = array(); |
271
|
|
|
foreach ($relateds as $related) { |
272
|
|
|
$related_ids[] = $related['id']; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$data = $relateds[0]['modelClass']::whereIn('id', $related_ids)->get(); |
276
|
|
|
|
277
|
|
|
$this->handleData($data); |
278
|
|
|
} else { |
279
|
|
|
return null; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return $this->fields[$field]->output(); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param $validator |
288
|
|
|
* @param string $route |
289
|
|
|
* @return $this |
290
|
|
|
*/ |
291
|
|
|
protected function redirect($validator, $route, $id = null) |
292
|
|
|
{ |
293
|
|
|
if (Input::has('_standalone')) { |
294
|
|
|
$routeParams = array('slug' => Input::get('_standalone')); |
295
|
|
|
|
296
|
|
|
if ($id) { |
297
|
|
|
$routeParams['id'] = $id; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return Redirect::route($route, $routeParams) |
|
|
|
|
301
|
|
|
->withErrors($validator) |
302
|
|
|
->withInput(); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return Redirect::back()->withErrors($validator)->withInput(); |
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param $data |
310
|
|
|
* @return \Illuminate\Validation\Validator |
311
|
|
|
*/ |
312
|
|
|
protected function validator($data) |
313
|
|
|
{ |
314
|
|
|
return Validator::make($data, $this->getRules()); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param $modelName |
319
|
|
|
* @param null $data |
320
|
|
|
* @param $sort |
321
|
|
|
* @return $this |
322
|
|
|
*/ |
323
|
|
|
protected function all($modelName, $data = null, $sort) |
324
|
|
|
{ |
325
|
|
|
$this->handleData($data); |
326
|
|
|
|
327
|
|
|
return View::make($this->getView('all')) |
328
|
|
|
->with('items', $this->data) |
329
|
|
|
->with('sort', $sort); |
330
|
|
|
} |
331
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.