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