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