|
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\Facades\Manager as DBM; |
|
8
|
|
|
use CodexShaper\DBM\Models\DBM_Collection; |
|
9
|
|
|
use Illuminate\Support\Facades\DB; |
|
10
|
|
|
use Illuminate\Support\Facades\Validator; |
|
11
|
|
|
use Illuminate\Support\Str; |
|
12
|
|
|
|
|
13
|
|
|
trait RecordTrait |
|
14
|
|
|
{ |
|
15
|
|
|
protected $name; |
|
16
|
|
|
protected $function_name; |
|
17
|
|
|
protected $localModel; |
|
18
|
|
|
protected $create; |
|
19
|
|
|
protected $relatedPivotKey; |
|
20
|
|
|
protected $parentPivotKey; |
|
21
|
|
|
protected $relationType; |
|
22
|
|
|
protected $details; |
|
23
|
|
|
protected $rules; |
|
24
|
|
|
protected $update; |
|
25
|
|
|
protected $type; |
|
26
|
|
|
protected $settings; |
|
27
|
|
|
protected $validation; |
|
28
|
|
|
protected $pivotTable; |
|
29
|
|
|
protected $foreignModel; |
|
30
|
|
|
protected $localTable; |
|
31
|
|
|
|
|
32
|
|
|
public function saveFiles($request, $column, $tableName) |
|
33
|
|
|
{ |
|
34
|
|
|
$files = $request->file($column); |
|
35
|
|
|
$values = []; |
|
36
|
|
|
foreach ($files as $file) { |
|
37
|
|
|
$fileName = Str::random(config('dbm.filesystem.random_length')) . '.' . $file->getClientOriginalExtension(); |
|
38
|
|
|
$path = 'public/dbm/' . $tableName; |
|
39
|
|
|
$file->storeAs($path, $fileName); |
|
40
|
|
|
$values[] = $fileName; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (count($values) > 1) { |
|
44
|
|
|
$value = $values; |
|
45
|
|
|
if (!Driver::isMongoDB()) { |
|
46
|
|
|
$value = json_encode($values); |
|
47
|
|
|
} |
|
48
|
|
|
} else if (count($values) == 1) { |
|
49
|
|
|
$value = $values[0]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $value; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function prepareStoreField($value, $tableName, $column) |
|
56
|
|
|
{ |
|
57
|
|
|
$value = is_array($value) ? json_encode($value) : $value; |
|
58
|
|
|
|
|
59
|
|
|
if (Driver::isMongoDB()) { |
|
60
|
|
|
|
|
61
|
|
|
$fieldType = $this->getFieldType($tableName, $column); |
|
62
|
|
|
|
|
63
|
|
|
if (!in_array($fieldType, Type::getTypes())) { |
|
64
|
|
|
$this->generateError([$fieldType . " type not supported."]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($fieldType != 'timestamp') { |
|
68
|
|
|
$value = Type::$fieldType($value); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function prepareRecordFields($fields) |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($fields as $key => $field) { |
|
79
|
|
|
|
|
80
|
|
|
if ($field->type == 'relationship') { |
|
81
|
|
|
|
|
82
|
|
|
$relationship = $field->settings; |
|
83
|
|
|
$foreignModel = $relationship['foreignModel']; |
|
84
|
|
|
$foreignKey = $relationship['foreignKey']; |
|
85
|
|
|
$fields = $this->removeRelationshipKeyForBelongsTo($fields, $foreignKey); |
|
86
|
|
|
$field->foreignTableData = $foreignModel::all(); |
|
87
|
|
|
$field->relationship = $relationship; |
|
88
|
|
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (isset($field->settings['options'])) { |
|
92
|
|
|
$options = $this->getSettingOptions($field); |
|
93
|
|
|
if (is_array($options)) { |
|
94
|
|
|
$fields[$key]->options = $options; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $fields; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function removeRelationshipKeyForBelongsTo($fields, $foreignKey) |
|
103
|
|
|
{ |
|
104
|
|
|
$results = []; |
|
105
|
|
|
|
|
106
|
|
|
foreach ($fields as $key => $field) { |
|
107
|
|
|
if ($field->name == $foreignKey) { |
|
108
|
|
|
unset($fields[$key]); |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
$results[] = $field; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $results; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function prepareJsonFieldData($records, $fields, $object, $findValue) |
|
118
|
|
|
{ |
|
119
|
|
|
$newRecords = []; |
|
120
|
|
|
$newRecord = new \stdClass(); |
|
121
|
|
|
|
|
122
|
|
|
foreach ($records as $item => $record) { |
|
123
|
|
|
|
|
124
|
|
|
foreach ($fields as $key => &$field) { |
|
125
|
|
|
|
|
126
|
|
|
if (isset($record->{$field->name})) { |
|
127
|
|
|
|
|
128
|
|
|
$record->{$field->name} = is_json($record->{$field->name}) ? json_decode($record->{$field->name}, true) : $record->{$field->name}; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($findValue && $record->{$object->details['findColumn']} == $findValue) { |
|
133
|
|
|
$newRecord = $record; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$newRecords[] = $record; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return [ |
|
140
|
|
|
"records" => $newRecords, |
|
141
|
|
|
"record" => $newRecord, |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function getSettingOptions($field) |
|
146
|
|
|
{ |
|
147
|
|
|
$options = $field->settings['options']; |
|
148
|
|
|
if (isset($options['controller'])) { |
|
149
|
|
|
$partials = explode('@', $options['controller']); |
|
150
|
|
|
$controllerName = $partials[0]; |
|
151
|
|
|
$methodName = $partials[1]; |
|
152
|
|
|
|
|
153
|
|
|
return app($controllerName)->{$methodName}(); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function validation($fields, $columns, $action = "create") |
|
158
|
|
|
{ |
|
159
|
|
|
$errors = []; |
|
160
|
|
|
foreach ($fields as $field) { |
|
161
|
|
|
$name = $field->name; |
|
162
|
|
|
|
|
163
|
|
|
if (is_object($field->settings) && property_exists($field->settings, 'validation') !== false) { |
|
164
|
|
|
|
|
165
|
|
|
$validationSettings = $field->settings->validation; |
|
166
|
|
|
$rules = $this->prepareRules($columns, $action, $validationSettings); |
|
167
|
|
|
$data = [$name => $columns->{$name}]; |
|
168
|
|
|
$validator = Validator::make($data, [$name => $rules]); |
|
169
|
|
|
if ($validator->fails()) { |
|
170
|
|
|
foreach ($validator->errors()->all() as $error) { |
|
171
|
|
|
$errors[] = $error; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $errors; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function prepareRules($columns, $action, $settings) |
|
181
|
|
|
{ |
|
182
|
|
|
$rules = ''; |
|
183
|
|
|
|
|
184
|
|
|
if (is_string($settings)) { |
|
185
|
|
|
$rules = $settings; |
|
186
|
|
|
} else if ($action == 'create' && isset($settings->create)) { |
|
187
|
|
|
$createSettings = $settings->create; |
|
188
|
|
|
$rules = $createSettings->rules; |
|
189
|
|
|
} else if ($action == 'update' && isset($settings->update)) { |
|
190
|
|
|
$updateSettings = $settings->update; |
|
191
|
|
|
$localKey = $updateSettings->localKey; |
|
192
|
|
|
$rules = $updateSettings->rules . ',' . $columns->{$localKey}; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return $rules; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function getFieldType($collectionName, $fieldName) |
|
199
|
|
|
{ |
|
200
|
|
|
$collection = DBM_Collection::where('name', $collectionName)->first(); |
|
201
|
|
|
|
|
202
|
|
|
return $collection->fields()->where('name', $fieldName)->first()->type; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function generateError($errors) |
|
206
|
|
|
{ |
|
207
|
|
|
return response()->json([ |
|
208
|
|
|
'success' => false, |
|
209
|
|
|
'errors' => $errors, |
|
210
|
|
|
], 400); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function hasFunction($fields, $column) |
|
214
|
|
|
{ |
|
215
|
|
|
foreach ($fields as $field) { |
|
216
|
|
|
if ($field->name == $column && ($field->function_name != null || $field->function_name != "")) { |
|
217
|
|
|
return $field->function_name; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return false; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function executeFunction($functionName, $value = null) |
|
225
|
|
|
{ |
|
226
|
|
|
$signature = ($value != null) ? "{$functionName}('{$value}')" : "{$functionName}()"; |
|
227
|
|
|
|
|
228
|
|
|
$result = DB::raw("{$signature}"); |
|
229
|
|
|
|
|
230
|
|
|
return $result; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|