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