|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use CodexShaper\DBM\Database\Drivers\MongoDB\Type; |
|
6
|
|
|
use CodexShaper\DBM\Facades\Driver; |
|
7
|
|
|
use CodexShaper\DBM\Models\DBM_Collection; |
|
8
|
|
|
use Illuminate\Support\Facades\DB; |
|
9
|
|
|
use Illuminate\Support\Facades\Validator; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
|
|
12
|
|
|
trait RecordTrait |
|
13
|
|
|
{ |
|
14
|
|
|
protected $name; |
|
15
|
|
|
protected $function_name; |
|
16
|
|
|
protected $localModel; |
|
17
|
|
|
protected $create; |
|
18
|
|
|
protected $relatedPivotKey; |
|
19
|
|
|
protected $parentPivotKey; |
|
20
|
|
|
protected $relationType; |
|
21
|
|
|
protected $details; |
|
22
|
|
|
protected $rules; |
|
23
|
|
|
protected $update; |
|
24
|
|
|
protected $type; |
|
25
|
|
|
protected $settings; |
|
26
|
|
|
protected $validation; |
|
27
|
|
|
protected $pivotTable; |
|
28
|
|
|
protected $foreignModel; |
|
29
|
|
|
protected $localTable; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Store files in storage. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Illuminate\Http\Request $request |
|
35
|
|
|
* @param string $column |
|
36
|
|
|
* @param string $tableName |
|
37
|
|
|
* |
|
38
|
|
|
* @return array|string |
|
39
|
|
|
*/ |
|
40
|
|
|
public function saveFiles($request, $column, $tableName) |
|
41
|
|
|
{ |
|
42
|
|
|
$files = $request->file($column); |
|
43
|
|
|
$values = []; |
|
44
|
|
|
foreach ($files as $file) { |
|
45
|
|
|
$fileName = Str::random(config('dbm.filesystem.random_length')).'.'.$file->getClientOriginalExtension(); |
|
46
|
|
|
$path = trim(config('dbm.filesystem.dir'), '/').DIRECTORY_SEPARATOR.$tableName; |
|
47
|
|
|
$file->storeAs($path, $fileName); |
|
48
|
|
|
$values[] = $fileName; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (count($values) > 1) { |
|
52
|
|
|
$value = $values; |
|
53
|
|
|
if (! Driver::isMongoDB()) { |
|
54
|
|
|
$value = json_encode($values); |
|
55
|
|
|
} |
|
56
|
|
|
} elseif (count($values) == 1) { |
|
57
|
|
|
$value = $values[0]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $value; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Prepare fields to store. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array|string|int|float|bool $request |
|
67
|
|
|
* @param string $tableName |
|
68
|
|
|
* @param string $column |
|
69
|
|
|
* |
|
70
|
|
|
* @return array|string|int|float|bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function prepareStoreField($value, $tableName, $column) |
|
73
|
|
|
{ |
|
74
|
|
|
$value = is_array($value) ? json_encode($value) : $value; |
|
75
|
|
|
|
|
76
|
|
|
if (Driver::isMongoDB()) { |
|
77
|
|
|
$fieldType = $this->getFieldType($tableName, $column); |
|
78
|
|
|
|
|
79
|
|
|
if (! in_array($fieldType, Type::getTypes())) { |
|
80
|
|
|
$this->generateError([$fieldType.' type not supported.']); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ($fieldType != 'timestamp') { |
|
84
|
|
|
$value = Type::$fieldType($value); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $value; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Prepare record fields. |
|
93
|
|
|
* |
|
94
|
|
|
* @param \Illuminate\Support\Collection $fields |
|
95
|
|
|
* |
|
96
|
|
|
* @return \Illuminate\Support\Collection |
|
97
|
|
|
*/ |
|
98
|
|
|
public function prepareRecordFields($fields) |
|
99
|
|
|
{ |
|
100
|
|
|
foreach ($fields as $key => $field) { |
|
101
|
|
|
if ($field->type == 'relationship') { |
|
102
|
|
|
$relationship = $field->settings; |
|
103
|
|
|
$foreignModel = $relationship['foreignModel']; |
|
104
|
|
|
$foreignKey = $relationship['foreignKey']; |
|
105
|
|
|
$fields = $this->removeRelationshipKeyForBelongsTo($fields, $foreignKey); |
|
106
|
|
|
$field->foreignTableData = $foreignModel::all(); |
|
107
|
|
|
$field->relationship = $relationship; |
|
108
|
|
|
continue; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (isset($field->settings['options'])) { |
|
112
|
|
|
$options = $this->getSettingOptions($field); |
|
113
|
|
|
if (is_array($options)) { |
|
114
|
|
|
$fields[$key]->options = $options; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $fields; |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Remove field when belongs_to relation. |
|
124
|
|
|
* |
|
125
|
|
|
* @param \Illuminate\Support\Collection $fields |
|
126
|
|
|
* @param string $foreignKey |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
public function removeRelationshipKeyForBelongsTo($fields, $foreignKey) |
|
131
|
|
|
{ |
|
132
|
|
|
$results = []; |
|
133
|
|
|
|
|
134
|
|
|
foreach ($fields as $key => $field) { |
|
135
|
|
|
if ($field->name == $foreignKey) { |
|
136
|
|
|
unset($fields[$key]); |
|
137
|
|
|
continue; |
|
138
|
|
|
} |
|
139
|
|
|
$results[] = $field; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $results; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Prepare Record Details. |
|
147
|
|
|
* |
|
148
|
|
|
* @param mixed $records |
|
149
|
|
|
* @param \Illuminate\Support\Collection $fields |
|
150
|
|
|
* @param \CodexShaper\DBM\Models\DBM_Object|\CodexShaper\DBM\Models\DBM_MongoObject $object |
|
151
|
|
|
* @param string $findValue |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
|
|
public function prepareRecordDetails($records, $fields, $object, $findValue) |
|
156
|
|
|
{ |
|
157
|
|
|
$newRecords = []; |
|
158
|
|
|
$newRecord = new \stdClass(); |
|
159
|
|
|
|
|
160
|
|
|
foreach ($records as $item => $record) { |
|
161
|
|
|
foreach ($fields as $key => &$field) { |
|
162
|
|
|
if (isset($record->{$field->name})) { |
|
163
|
|
|
$record->{$field->name} = is_json($record->{$field->name}) ? json_decode($record->{$field->name}, true) : $record->{$field->name}; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if ($findValue && $record->{$object->details['findColumn']} == $findValue) { |
|
168
|
|
|
$newRecord = $record; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$newRecords[] = $record; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return [ |
|
175
|
|
|
'records' => $newRecords, |
|
176
|
|
|
'record' => $newRecord, |
|
177
|
|
|
]; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Get options for Dropdown, Selectbox, Radio button and other fields via controller. |
|
182
|
|
|
* |
|
183
|
|
|
* @param object $field |
|
184
|
|
|
* |
|
185
|
|
|
* @return array|mixed |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getSettingOptions($field) |
|
188
|
|
|
{ |
|
189
|
|
|
$options = $field->settings['options']; |
|
190
|
|
|
if (isset($options['controller'])) { |
|
191
|
|
|
$partials = explode('@', $options['controller']); |
|
192
|
|
|
$controllerName = $partials[0]; |
|
193
|
|
|
$methodName = $partials[1]; |
|
194
|
|
|
|
|
195
|
|
|
return app($controllerName)->{$methodName}(); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Check validation and return errors if fails. |
|
201
|
|
|
* |
|
202
|
|
|
* @param array $fields |
|
203
|
|
|
* @param object $columns |
|
204
|
|
|
* @param string $action |
|
205
|
|
|
* |
|
206
|
|
|
* @return array |
|
207
|
|
|
*/ |
|
208
|
|
|
public function validation($fields, $columns, $action = 'create') |
|
209
|
|
|
{ |
|
210
|
|
|
$errors = []; |
|
211
|
|
|
foreach ($fields as $field) { |
|
212
|
|
|
$name = $field->name; |
|
213
|
|
|
|
|
214
|
|
|
if (is_object($field->settings) && property_exists($field->settings, 'validation') !== false) { |
|
215
|
|
|
$validationSettings = $field->settings->validation; |
|
216
|
|
|
$rules = $this->prepareRules($columns, $action, $validationSettings); |
|
217
|
|
|
$data = [$name => $columns->{$name}]; |
|
218
|
|
|
$validator = Validator::make($data, [$name => $rules]); |
|
219
|
|
|
if ($validator->fails()) { |
|
220
|
|
|
foreach ($validator->errors()->all() as $error) { |
|
221
|
|
|
$errors[] = $error; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return $errors; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Prepare validation rules. |
|
232
|
|
|
* |
|
233
|
|
|
* @param object $columns |
|
234
|
|
|
* @param string $action |
|
235
|
|
|
* @param string|object $settings |
|
236
|
|
|
* |
|
237
|
|
|
* @return array|string |
|
238
|
|
|
*/ |
|
239
|
|
|
public function prepareRules($columns, $action, $settings) |
|
240
|
|
|
{ |
|
241
|
|
|
$rules = ''; |
|
242
|
|
|
|
|
243
|
|
|
if (is_string($settings)) { |
|
244
|
|
|
$rules = $settings; |
|
245
|
|
|
} elseif ($action == 'create' && isset($settings->create)) { |
|
246
|
|
|
$createSettings = $settings->create; |
|
247
|
|
|
$rules = $createSettings->rules; |
|
248
|
|
|
} elseif ($action == 'update' && isset($settings->update)) { |
|
249
|
|
|
$updateSettings = $settings->update; |
|
250
|
|
|
$rules = $updateSettings->rules; |
|
251
|
|
|
if (isset($updateSettings->localKey)) { |
|
252
|
|
|
$localKey = $updateSettings->localKey; |
|
253
|
|
|
$rules = $updateSettings->rules.','.$columns->{$localKey}; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return $rules; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Get field name. |
|
262
|
|
|
* |
|
263
|
|
|
* @param string $collectionName |
|
264
|
|
|
* @param string $fieldName |
|
265
|
|
|
* |
|
266
|
|
|
* @return string |
|
267
|
|
|
*/ |
|
268
|
|
|
public function getFieldType($collectionName, $fieldName) |
|
269
|
|
|
{ |
|
270
|
|
|
$collection = DBM_Collection::where('name', $collectionName)->first(); |
|
271
|
|
|
|
|
272
|
|
|
return $collection->fields()->where('name', $fieldName)->first()->type; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Generate errors and return response. |
|
277
|
|
|
* |
|
278
|
|
|
* @param array $errors |
|
279
|
|
|
* |
|
280
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
281
|
|
|
*/ |
|
282
|
|
|
public function generateError($errors) |
|
283
|
|
|
{ |
|
284
|
|
|
return response()->json([ |
|
285
|
|
|
'success' => false, |
|
286
|
|
|
'errors' => $errors, |
|
287
|
|
|
], 400); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Check Function name is available for MySQL. |
|
292
|
|
|
* |
|
293
|
|
|
* @param array $fields |
|
294
|
|
|
* @param string $column |
|
295
|
|
|
* |
|
296
|
|
|
* @return string|false |
|
297
|
|
|
*/ |
|
298
|
|
|
public function hasFunction($fields, $column) |
|
299
|
|
|
{ |
|
300
|
|
|
foreach ($fields as $field) { |
|
301
|
|
|
if ($field->name == $column && ($field->function_name != null || $field->function_name != '')) { |
|
302
|
|
|
return $field->function_name; |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
return false; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Execute MySQL Function. |
|
311
|
|
|
* |
|
312
|
|
|
* @param string $functionName |
|
313
|
|
|
* @param string|int|float|bool|null $column |
|
314
|
|
|
* |
|
315
|
|
|
* @return string|int|float|bool |
|
316
|
|
|
*/ |
|
317
|
|
|
public function executeFunction($functionName, $value = null) |
|
318
|
|
|
{ |
|
319
|
|
|
$signature = ($value != null) ? "{$functionName}('{$value}')" : "{$functionName}()"; |
|
320
|
|
|
$result = DB::raw("{$signature}"); |
|
321
|
|
|
|
|
322
|
|
|
return $result; |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|