|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Zemit Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Zemit Team <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Zemit\Modules\Api\Controllers; |
|
12
|
|
|
|
|
13
|
|
|
use Phalcon\Mvc\Model\MetaData; |
|
14
|
|
|
use Phalcon\Mvc\ModelInterface; |
|
15
|
|
|
use Phalcon\Support\Collection; |
|
16
|
|
|
use Zemit\Models\Table; |
|
17
|
|
|
use Zemit\Modules\Api\Controller; |
|
18
|
|
|
use Zemit\Mvc\Model\Dynamic; |
|
19
|
|
|
|
|
20
|
|
|
class RecordController extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
protected ?int $limit = 10000; |
|
23
|
|
|
protected ?int $maxLimit = 10000; |
|
24
|
|
|
protected Collection $columnMap; |
|
25
|
|
|
protected array $metaData = []; |
|
26
|
|
|
protected string $source = 'dynamic'; |
|
27
|
|
|
|
|
28
|
|
|
public function initialize() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->initializeSource(); |
|
31
|
|
|
$this->initializeTableColumns(); |
|
32
|
|
|
|
|
33
|
|
|
parent::initialize(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function initializeSource() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->source = $this->getParam('advanced')['tableUuid'] ?? 'dynamic'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function initializeTableColumns() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->columnMap = new Collection([ |
|
44
|
|
|
'id' => 'id', |
|
45
|
|
|
'uuid' => 'uuid', |
|
46
|
|
|
'deleted' => 'deleted', |
|
47
|
|
|
]); |
|
48
|
|
|
|
|
49
|
|
|
$table = Table::findFirst([ |
|
50
|
|
|
'uuid = :uuid:', |
|
51
|
|
|
'bind' => ['uuid' => $this->getSource()], |
|
52
|
|
|
]); |
|
53
|
|
|
if ($table) { |
|
54
|
|
|
$columns = $table->getColumnList(); |
|
55
|
|
|
foreach ($columns as $column) { |
|
56
|
|
|
$this->columnMap->set($column->getUuid(), $column->getUuid()); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function listExpose(iterable $items, ?array $expose = null): array |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
return (array)$items; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function initializeSearchFields(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->setSearchFields($this->columnMap); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function initializeFilterFields(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->setFilterFields(new Collection([ |
|
74
|
|
|
'workspaceId', |
|
75
|
|
|
'tableId', |
|
76
|
|
|
])); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function hasAdvanced(string $key) |
|
80
|
|
|
{ |
|
81
|
|
|
$advanced = $this->getParam('advanced') ?? []; |
|
82
|
|
|
return isset($advanced[$key]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getModelName(): ?string |
|
86
|
|
|
{ |
|
87
|
|
|
if (!isset($this->modelName)) { |
|
88
|
|
|
// $source = $this->getSource(); |
|
89
|
|
|
// $sourceClass = '_' . md5($source); |
|
90
|
|
|
// $sourceFullClass = '\\Zemit\\Models\\' . $sourceClass; |
|
91
|
|
|
// eval('namespace Zemit\Models; class ' . $sourceClass . ' extends \\' . Dynamic::class . ' {}'); |
|
92
|
|
|
// $this->modelName = $sourceFullClass; |
|
93
|
|
|
$this->modelName = Dynamic::class; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->modelName; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function loadModel(?string $modelName = null): ModelInterface |
|
100
|
|
|
{ |
|
101
|
|
|
$modelName ??= $this->getModelName() ?? ''; |
|
102
|
|
|
$modelInstance = Dynamic::createInstance($this->getSource(), $this->getColumnMap()); |
|
103
|
|
|
// $modelInstance = new $modelName(); |
|
104
|
|
|
// $modelInstance = $this->modelsManager->load($modelName); |
|
105
|
|
|
if ($modelInstance instanceof Dynamic) { |
|
|
|
|
|
|
106
|
|
|
$modelInstance->setDynamicSource($this->getSource()); |
|
107
|
|
|
$modelInstance->setDynamicMetaData($this->getMetaData()); |
|
108
|
|
|
$modelInstance->setDynamicColumnMap($this->getColumnMap()); |
|
109
|
|
|
} |
|
110
|
|
|
assert($modelInstance instanceof ModelInterface); |
|
111
|
|
|
return $modelInstance; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getColumnMap(): array |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->columnMap->toArray(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getSource(): string |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->source; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getMetaData(): array |
|
125
|
|
|
{ |
|
126
|
|
|
/** |
|
127
|
|
|
* Initialize meta-data |
|
128
|
|
|
*/ |
|
129
|
|
|
$attributes = []; |
|
130
|
|
|
$primaryKeys = []; |
|
131
|
|
|
$nonPrimaryKeys = []; |
|
132
|
|
|
$numericTyped = []; |
|
133
|
|
|
$notNull = []; |
|
134
|
|
|
$fieldTypes = []; |
|
135
|
|
|
$fieldBindTypes = []; |
|
136
|
|
|
$automaticDefault = []; |
|
137
|
|
|
$identityField = false; |
|
138
|
|
|
$defaultValues = []; |
|
139
|
|
|
$emptyStringValues = []; |
|
140
|
|
|
|
|
141
|
|
|
$columns = $this->dbd->describeColumns($this->getSource()); |
|
|
|
|
|
|
142
|
|
|
foreach ($columns as $column) { |
|
143
|
|
|
$fieldName = $column->getName(); |
|
144
|
|
|
$attributes[] = $fieldName; |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* To mark fields as primary keys |
|
148
|
|
|
*/ |
|
149
|
|
|
if ($column->isPrimary()) { |
|
150
|
|
|
$primaryKeys[] = $fieldName; |
|
151
|
|
|
} |
|
152
|
|
|
else { |
|
153
|
|
|
$nonPrimaryKeys[] = $fieldName; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* To mark fields as numeric |
|
158
|
|
|
*/ |
|
159
|
|
|
if ($column->isNumeric()) { |
|
160
|
|
|
$numericTyped[$fieldName] = true; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* To mark fields as not null |
|
165
|
|
|
*/ |
|
166
|
|
|
if ($column->isNotNull()) { |
|
167
|
|
|
$notNull[] = $fieldName; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* To mark fields as identity $columns |
|
172
|
|
|
*/ |
|
173
|
|
|
if ($column->isAutoIncrement()) { |
|
174
|
|
|
$identityField = $fieldName; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* To get the internal types |
|
179
|
|
|
*/ |
|
180
|
|
|
$fieldTypes[$fieldName] = $column->getType(); |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* To mark how the fields must be escaped |
|
184
|
|
|
*/ |
|
185
|
|
|
$fieldBindTypes[$fieldName] = $column->getBindType(); |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* If $column has default value or $column is nullable and default value is null |
|
189
|
|
|
*/ |
|
190
|
|
|
$defaultValue = $column->getDefault(); |
|
191
|
|
|
|
|
192
|
|
|
if ($defaultValue !== null || !$column->isNotNull()) { |
|
193
|
|
|
if (!$column->isAutoIncrement()) { |
|
194
|
|
|
$defaultValues[$fieldName] = $defaultValue; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Create an array using the MODELS_* constants as indexes |
|
201
|
|
|
*/ |
|
202
|
|
|
return [ |
|
203
|
|
|
MetaData::MODELS_ATTRIBUTES => $attributes, |
|
204
|
|
|
MetaData::MODELS_PRIMARY_KEY => $primaryKeys, |
|
205
|
|
|
MetaData::MODELS_NON_PRIMARY_KEY => $nonPrimaryKeys, |
|
206
|
|
|
MetaData::MODELS_NOT_NULL => $notNull, |
|
207
|
|
|
MetaData::MODELS_DATA_TYPES => $fieldTypes, |
|
208
|
|
|
MetaData::MODELS_DATA_TYPES_NUMERIC => $numericTyped, |
|
209
|
|
|
MetaData::MODELS_IDENTITY_COLUMN => $identityField, |
|
210
|
|
|
MetaData::MODELS_DATA_TYPES_BIND => $fieldBindTypes, |
|
211
|
|
|
MetaData::MODELS_AUTOMATIC_DEFAULT_INSERT => $automaticDefault, |
|
212
|
|
|
MetaData::MODELS_AUTOMATIC_DEFAULT_UPDATE => $automaticDefault, |
|
213
|
|
|
MetaData::MODELS_DEFAULT_VALUES => $defaultValues, |
|
214
|
|
|
MetaData::MODELS_EMPTY_STRING_VALUES => $emptyStringValues, |
|
215
|
|
|
]; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.