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