1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Directus – <http://getdirectus.com> |
5
|
|
|
* |
6
|
|
|
* @link The canonical repository – <https://github.com/directus/directus> |
7
|
|
|
* @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com> |
8
|
|
|
* @license GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Directus\SDK; |
12
|
|
|
|
13
|
|
|
use Directus\Database\Connection; |
14
|
|
|
use Directus\Database\TableGateway\BaseTableGateway; |
15
|
|
|
use Directus\Database\TableGateway\DirectusMessagesTableGateway; |
16
|
|
|
use Directus\Database\TableGateway\RelationalTableGateway; |
17
|
|
|
use Directus\Database\TableSchema; |
18
|
|
|
use Directus\Util\ArrayUtils; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Client Local |
22
|
|
|
* |
23
|
|
|
* Client to Interact with the database directly using Directus Database Component |
24
|
|
|
* |
25
|
|
|
* @author Welling Guzmán <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ClientLocal extends AbstractClient |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var BaseTableGateway[] |
31
|
|
|
*/ |
32
|
|
|
protected $tableGateways = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Connection |
36
|
|
|
*/ |
37
|
|
|
protected $connection = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ClientLocal constructor. |
41
|
|
|
* |
42
|
|
|
* @param $connection |
43
|
|
|
*/ |
44
|
|
|
public function __construct($connection) |
45
|
|
|
{ |
46
|
|
|
$this->connection = $connection; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
|
|
public function getTables(array $params = []) |
53
|
|
|
{ |
54
|
|
|
return $this->createResponseFromData(TableSchema::getTablesSchema($params)); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
|
|
public function getTable($tableName) |
61
|
|
|
{ |
62
|
|
|
return $this->createResponseFromData(TableSchema::getSchemaArray($tableName)); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function getColumns($tableName, array $params = []) |
69
|
|
|
{ |
70
|
|
|
return $this->createResponseFromData(TableSchema::getColumnSchemaArray($tableName, $params)); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
|
|
public function getColumn($tableName, $columnName) |
77
|
|
|
{ |
78
|
|
|
return $this->createResponseFromData(TableSchema::getColumnSchemaArray($tableName, $columnName)); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritDoc |
83
|
|
|
*/ |
84
|
|
|
public function getEntries($tableName, array $params = []) |
85
|
|
|
{ |
86
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
87
|
|
|
|
88
|
|
|
return $this->createResponseFromData($tableGateway->getEntries($params)); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
|
|
public function getEntry($tableName, $id, array $params = []) |
95
|
|
|
{ |
96
|
|
|
// @TODO: Dynamic ID |
97
|
|
|
return $this->getEntries($tableName, array_merge($params, [ |
|
|
|
|
98
|
|
|
'id' => $id |
99
|
|
|
])); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
105
|
|
|
public function getUsers(array $params = []) |
106
|
|
|
{ |
107
|
|
|
// @TODO: store the directus tables somewhere (SchemaManager?) |
108
|
|
|
return $this->getEntries('directus_users', $params); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritDoc |
113
|
|
|
*/ |
114
|
|
|
public function getUser($id, array $params = []) |
115
|
|
|
{ |
116
|
|
|
return $this->getEntry('directus_users', $id, $params); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritDoc |
121
|
|
|
*/ |
122
|
|
|
public function getGroups(array $params = []) |
123
|
|
|
{ |
124
|
|
|
return $this->getEntries('directus_groups', $params); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritDoc |
129
|
|
|
*/ |
130
|
|
|
public function getGroup($id, array $params = []) |
131
|
|
|
{ |
132
|
|
|
return $this->getEntry('directus_groups', $id, $params); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
|
|
public function getGroupPrivileges($groupID) |
139
|
|
|
{ |
140
|
|
|
$this->getEntries('directus_privileges', [ |
141
|
|
|
'filter' => [ |
142
|
|
|
'group_id' => ['eq' => $groupID] |
143
|
|
|
] |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @inheritDoc |
149
|
|
|
*/ |
150
|
|
|
public function getFiles(array $params = []) |
151
|
|
|
{ |
152
|
|
|
return $this->getEntries('directus_files', $params); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @inheritDoc |
157
|
|
|
*/ |
158
|
|
|
public function getFile($id, array $params = []) |
159
|
|
|
{ |
160
|
|
|
return $this->getEntry('directus_files', $id, $params); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @inheritDoc |
165
|
|
|
*/ |
166
|
|
|
public function getSettings() |
167
|
|
|
{ |
168
|
|
|
return $this->getEntries('directus_settings'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @inheritDoc |
173
|
|
|
*/ |
174
|
|
|
public function getSettingsByCollection($collectionName) |
175
|
|
|
{ |
176
|
|
|
return $this->getEntries('directus_settings', [ |
177
|
|
|
'filter' => [ |
178
|
|
|
'collection' => ['eq' => $collectionName] |
179
|
|
|
] |
180
|
|
|
]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritDoc |
185
|
|
|
*/ |
186
|
|
|
public function getMessages($userId) |
187
|
|
|
{ |
188
|
|
|
$messagesTableGateway = new DirectusMessagesTableGateway($this->connection, null); |
|
|
|
|
189
|
|
|
$result = $messagesTableGateway->fetchMessagesInboxWithHeaders($userId); |
190
|
|
|
|
191
|
|
|
return $this->createResponseFromData($result); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @inheritDoc |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function createEntry($tableName, array $data) |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
200
|
|
|
$data = $this->processData($tableName, $data); |
201
|
|
|
|
202
|
|
|
foreach($data as $key => $value) { |
203
|
|
|
if ($value instanceof File) { |
204
|
|
|
$data[$key] = $this->processFile($value); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$newRecord = $tableGateway->manageRecordUpdate($tableName, $data); |
209
|
|
|
|
210
|
|
|
return $this->getEntry($tableName, $newRecord[$tableGateway->primaryKeyFieldName]); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @inheritDoc |
215
|
|
|
*/ |
216
|
|
View Code Duplication |
public function updateEntry($tableName, $id, array $data) |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
219
|
|
|
$data = $this->processData($tableName, $data); |
220
|
|
|
|
221
|
|
|
foreach($data as $key => $value) { |
222
|
|
|
if ($value instanceof File) { |
223
|
|
|
$data[$key] = $this->processFile($value); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$updatedRecord = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id])); |
228
|
|
|
|
229
|
|
|
return $this->getEntry($tableName, $updatedRecord[$tableGateway->primaryKeyFieldName]); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @inheritDoc |
234
|
|
|
*/ |
235
|
|
|
public function deleteEntry($tableName, $ids) |
236
|
|
|
{ |
237
|
|
|
// @TODO: Accept EntryCollection and Entry |
238
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
239
|
|
|
|
240
|
|
|
if (!is_array($ids)) { |
241
|
|
|
$ids = [$ids]; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $tableGateway->delete(function($delete) use ($ids) { |
245
|
|
|
return $delete->where->in('id', $ids); |
246
|
|
|
}); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @inheritDoc |
251
|
|
|
*/ |
252
|
|
|
public function createUser(array $data) |
253
|
|
|
{ |
254
|
|
|
return $this->createEntry('directus_users', $data); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @inheritDoc |
259
|
|
|
*/ |
260
|
|
|
public function updateUser($id, array $data) |
261
|
|
|
{ |
262
|
|
|
return $this->updateEntry('directus_users', $id, $data); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @inheritDoc |
267
|
|
|
*/ |
268
|
|
|
public function deleteUser($ids) |
269
|
|
|
{ |
270
|
|
|
return $this->deleteEntry('directus_users', $ids); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @inheritDoc |
275
|
|
|
*/ |
276
|
|
|
public function createFile(File $file) |
277
|
|
|
{ |
278
|
|
|
$data = $this->processFile($file); |
279
|
|
|
|
280
|
|
|
return $this->createEntry('directus_files', $data); |
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @inheritDoc |
285
|
|
|
*/ |
286
|
|
|
public function updateFile($id, array $data) |
287
|
|
|
{ |
288
|
|
|
return $this->updateEntry('directus_files', $id, $data); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @inheritDoc |
293
|
|
|
*/ |
294
|
|
|
public function deleteFile($ids) |
295
|
|
|
{ |
296
|
|
|
return $this->deleteEntry('directus_files', $ids); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function createPreferences($data) |
300
|
|
|
{ |
301
|
|
|
if (!ArrayUtils::contains($data, ['title', 'table_name'])) { |
302
|
|
|
throw new \Exception('title and table_name are required'); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$acl = $this->container->get('acl'); |
306
|
|
|
$data['user'] = $acl->getUserId(); |
307
|
|
|
|
308
|
|
|
return $this->createEntry('directus_preferences', $data); |
|
|
|
|
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @inheritdoc |
313
|
|
|
*/ |
314
|
|
|
public function createBookmark($data) |
315
|
|
|
{ |
316
|
|
|
$acl = $this->container->get('acl'); |
317
|
|
|
$data['user'] = $acl->getUserId(); |
318
|
|
|
|
319
|
|
|
$preferences = $this->createPreferences(ArrayUtils::pick($data, [ |
320
|
|
|
'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible', 'user' |
321
|
|
|
])); |
322
|
|
|
|
323
|
|
|
$title = $preferences->title; |
|
|
|
|
324
|
|
|
$tableName = $preferences->table_name; |
|
|
|
|
325
|
|
|
$bookmarkData = [ |
326
|
|
|
'section' => 'search', |
327
|
|
|
'title' => $title, |
328
|
|
|
'url' => 'tables/' . $tableName . '/pref/' . $title, |
329
|
|
|
'user' => $data['user'] |
330
|
|
|
]; |
331
|
|
|
|
332
|
|
|
return $this->createEntry('directus_bookmarks', $bookmarkData); |
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @inheritdoc |
337
|
|
|
*/ |
338
|
|
|
public function createColumn($data) |
339
|
|
|
{ |
340
|
|
|
$requiredAttributes = ['name', 'table', 'type', 'ui']; |
341
|
|
|
if (!ArrayUtils::contains($data, $requiredAttributes)) { |
342
|
|
|
throw new \Exception(sprintf('%s are required', implode(',', $requiredAttributes))); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$data = ArrayUtils::aliasKeys($data, [ |
|
|
|
|
346
|
|
|
'table_name' => 'table', |
347
|
|
|
'column_name' => 'name', |
348
|
|
|
'data_type' => 'type', |
349
|
|
|
'char_length' => 'length' |
350
|
|
|
]); |
351
|
|
|
|
352
|
|
|
$tableGateway = $this->getTableGateway($data['table_name']); |
353
|
|
|
|
354
|
|
|
$tableGateway->addColumn($data['table_name'], ArrayUtils::omit($data, ['table_name'])); |
355
|
|
|
|
356
|
|
|
return $this->getColumn($data['table_name'], $data['column_name']); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Get a table gateway for the given table name |
361
|
|
|
* |
362
|
|
|
* @param $tableName |
363
|
|
|
* |
364
|
|
|
* @return RelationalTableGateway |
365
|
|
|
*/ |
366
|
|
|
protected function getTableGateway($tableName) |
367
|
|
|
{ |
368
|
|
|
if (!array_key_exists($tableName, $this->tableGateways)) { |
369
|
|
|
$acl = TableSchema::getAclInstance(); |
370
|
|
|
$this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return $this->tableGateways[$tableName]; |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|