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