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\DirectusActivityTableGateway; |
16
|
|
|
use Directus\Database\TableGateway\DirectusMessagesTableGateway; |
17
|
|
|
use Directus\Database\TableGateway\DirectusPreferencesTableGateway; |
18
|
|
|
use Directus\Database\TableGateway\DirectusPrivilegesTableGateway; |
19
|
|
|
use Directus\Database\TableGateway\DirectusSettingsTableGateway; |
20
|
|
|
use Directus\Database\TableGateway\DirectusUiTableGateway; |
21
|
|
|
use Directus\Database\TableGateway\DirectusUsersTableGateway; |
22
|
|
|
use Directus\Database\TableGateway\RelationalTableGateway; |
23
|
|
|
use Directus\Database\TableSchema; |
24
|
|
|
use Directus\Util\ArrayUtils; |
25
|
|
|
use Directus\Util\SchemaUtils; |
26
|
|
|
use Directus\Util\StringUtils; |
27
|
|
|
use Zend\Db\Sql\Predicate\In; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Client Local |
31
|
|
|
* |
32
|
|
|
* Client to Interact with the database directly using Directus Database Component |
33
|
|
|
* |
34
|
|
|
* @author Welling Guzmán <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class ClientLocal extends AbstractClient |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var BaseTableGateway[] |
40
|
|
|
*/ |
41
|
|
|
protected $tableGateways = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Connection |
45
|
|
|
*/ |
46
|
|
|
protected $connection = null; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* ClientLocal constructor. |
50
|
|
|
* |
51
|
|
|
* @param $connection |
52
|
|
|
*/ |
53
|
|
|
public function __construct($connection) |
54
|
|
|
{ |
55
|
|
|
$this->connection = $connection; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
|
|
public function getTables(array $params = []) |
62
|
|
|
{ |
63
|
|
|
return $this->createResponseFromData(TableSchema::getTablesSchema($params)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public function getTable($tableName) |
70
|
|
|
{ |
71
|
|
|
return $this->createResponseFromData(TableSchema::getSchemaArray($tableName)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
public function getColumns($tableName, array $params = []) |
78
|
|
|
{ |
79
|
|
|
return $this->createResponseFromData(TableSchema::getColumnSchemaArray($tableName, $params)); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
|
|
public function getColumn($tableName, $columnName) |
86
|
|
|
{ |
87
|
|
|
return $this->createResponseFromData(TableSchema::getColumnSchema($tableName, $columnName)->toArray()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function getItems($tableName, array $params = []) |
94
|
|
|
{ |
95
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
96
|
|
|
|
97
|
|
|
return $this->createResponseFromData($tableGateway->getItems($params)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritDoc |
102
|
|
|
*/ |
103
|
|
|
public function getItem($tableName, $id, array $params = []) |
104
|
|
|
{ |
105
|
|
|
// @TODO: Dynamic ID |
106
|
|
|
return $this->getItems($tableName, array_merge($params, [ |
107
|
|
|
'id' => $id |
108
|
|
|
])); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritDoc |
113
|
|
|
*/ |
114
|
|
|
public function getUsers(array $params = []) |
115
|
|
|
{ |
116
|
|
|
// @TODO: store the directus tables somewhere (SchemaManager?) |
117
|
|
|
return $this->getItems('directus_users', $params); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritDoc |
122
|
|
|
*/ |
123
|
|
|
public function getUser($id, array $params = []) |
124
|
|
|
{ |
125
|
|
|
return $this->getItem('directus_users', $id, $params); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritDoc |
130
|
|
|
*/ |
131
|
|
|
public function getGroups(array $params = []) |
132
|
|
|
{ |
133
|
|
|
return $this->getItems('directus_groups', $params); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritDoc |
138
|
|
|
*/ |
139
|
|
|
public function getGroup($id, array $params = []) |
140
|
|
|
{ |
141
|
|
|
return $this->getItem('directus_groups', $id, $params); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritDoc |
146
|
|
|
*/ |
147
|
|
|
public function getGroupPrivileges($groupID) |
148
|
|
|
{ |
149
|
|
|
$this->getItems('directus_privileges', [ |
150
|
|
|
'filter' => [ |
151
|
|
|
'group_id' => ['eq' => $groupID] |
152
|
|
|
] |
153
|
|
|
]); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
|
|
public function getFiles(array $params = []) |
160
|
|
|
{ |
161
|
|
|
return $this->getItems('directus_files', $params); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @inheritDoc |
166
|
|
|
*/ |
167
|
|
|
public function getFile($id, array $params = []) |
168
|
|
|
{ |
169
|
|
|
return $this->getItem('directus_files', $id, $params); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritDoc |
174
|
|
|
*/ |
175
|
|
|
public function getSettings() |
176
|
|
|
{ |
177
|
|
|
return $this->getItems('directus_settings'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @inheritDoc |
182
|
|
|
*/ |
183
|
|
View Code Duplication |
public function getSettingsByCollection($collectionName) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
$connection = $this->container->get('connection'); |
186
|
|
|
$acl = $this->container->get('acl'); |
187
|
|
|
$tableGateway = new DirectusSettingsTableGateway($connection, $acl); |
188
|
|
|
|
189
|
|
|
$data = [ |
190
|
|
|
'meta' => [ |
191
|
|
|
'table' => 'directus_settings', |
192
|
|
|
'type' => 'entry', |
193
|
|
|
'settings_collection' => $collectionName |
194
|
|
|
], |
195
|
|
|
'data' => $tableGateway->fetchCollection($collectionName) |
196
|
|
|
]; |
197
|
|
|
|
198
|
|
|
return $this->createResponseFromData($data); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @inheritdoc |
203
|
|
|
*/ |
204
|
|
|
public function updateSettings($collection, array $data) |
205
|
|
|
{ |
206
|
|
|
$connection = $this->container->get('connection'); |
207
|
|
|
$acl = $this->container->get('acl'); |
208
|
|
|
$tableGateway = new DirectusSettingsTableGateway($connection, $acl); |
209
|
|
|
|
210
|
|
|
$tableGateway->setValues($collection, $data); |
211
|
|
|
|
212
|
|
|
return $this->getSettingsByCollection($collection); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @inheritDoc |
217
|
|
|
*/ |
218
|
|
View Code Duplication |
public function getMessages($userId = null) |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
$connection = $this->container->get('connection'); |
221
|
|
|
$acl = $this->container->get('acl'); |
222
|
|
|
|
223
|
|
|
if ($userId === null) { |
224
|
|
|
$userId = $acl->getUserId(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$messagesTableGateway = new DirectusMessagesTableGateway($connection, $acl); |
228
|
|
|
$result = $messagesTableGateway->fetchMessagesInboxWithHeaders($userId); |
229
|
|
|
|
230
|
|
|
return $this->createResponseFromData($result); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
View Code Duplication |
public function getMessage($id, $userId = null) |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
$connection = $this->container->get('connection'); |
236
|
|
|
$acl = $this->container->get('acl'); |
237
|
|
|
|
238
|
|
|
if ($userId === null) { |
239
|
|
|
$userId = $acl->getUserId(); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$messagesTableGateway = new DirectusMessagesTableGateway($connection, $acl); |
243
|
|
|
$message = $messagesTableGateway->fetchMessageWithRecipients($id, $userId); |
244
|
|
|
|
245
|
|
|
return $this->createResponseFromData($message); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @inheritDoc |
250
|
|
|
*/ |
251
|
|
View Code Duplication |
public function createItem($tableName, array $data) |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
254
|
|
|
$data = $this->processData($tableName, $data); |
255
|
|
|
|
256
|
|
|
foreach($data as $key => $value) { |
257
|
|
|
if ($value instanceof File) { |
258
|
|
|
$data[$key] = $this->processFile($value); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$newRecord = $tableGateway->manageRecordUpdate($tableName, $data); |
263
|
|
|
|
264
|
|
|
return $this->getItem($tableName, $newRecord[$tableGateway->primaryKeyFieldName]); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @inheritDoc |
269
|
|
|
*/ |
270
|
|
View Code Duplication |
public function updateItem($tableName, $id, array $data) |
|
|
|
|
271
|
|
|
{ |
272
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
273
|
|
|
$data = $this->processData($tableName, $data); |
274
|
|
|
|
275
|
|
|
foreach($data as $key => $value) { |
276
|
|
|
if ($value instanceof File) { |
277
|
|
|
$data[$key] = $this->processFile($value); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$updatedRecord = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id])); |
282
|
|
|
|
283
|
|
|
return $this->getItem($tableName, $updatedRecord[$tableGateway->primaryKeyFieldName]); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @inheritDoc |
288
|
|
|
*/ |
289
|
|
|
public function deleteItem($tableName, $ids, $hard = false) |
290
|
|
|
{ |
291
|
|
|
// @TODO: Accept EntryCollection and Entry |
292
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
293
|
|
|
|
294
|
|
|
if (!is_array($ids)) { |
295
|
|
|
$ids = [$ids]; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if ($hard === true) { |
299
|
|
|
return $tableGateway->delete(function($delete) use ($ids) { |
300
|
|
|
return $delete->where->in('id', $ids); |
301
|
|
|
}); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
if (!$tableGateway->getTableSchema()->hasStatusColumn()) { |
305
|
|
|
throw new \Exception('Cannot soft-delete. Table has not status column.'); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return $tableGateway->update([ |
309
|
|
|
$tableGateway->getStatusColumnName() => $tableGateway->getDeletedValue() |
310
|
|
|
], new In('id', $ids)); |
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @inheritDoc |
315
|
|
|
*/ |
316
|
|
|
public function createUser(array $data) |
317
|
|
|
{ |
318
|
|
|
return $this->createItem('directus_users', $data); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @inheritDoc |
323
|
|
|
*/ |
324
|
|
|
public function updateUser($id, array $data) |
325
|
|
|
{ |
326
|
|
|
return $this->updateItem('directus_users', $id, $data); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @inheritDoc |
331
|
|
|
*/ |
332
|
|
|
public function deleteUser($ids, $hard = false) |
333
|
|
|
{ |
334
|
|
|
return $this->deleteItem('directus_users', $ids, $hard); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @inheritDoc |
339
|
|
|
*/ |
340
|
|
|
public function createFile(File $file) |
341
|
|
|
{ |
342
|
|
|
$data = $this->processFile($file); |
343
|
|
|
|
344
|
|
|
return $this->createItem('directus_files', $data); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @inheritDoc |
349
|
|
|
*/ |
350
|
|
|
public function updateFile($id, $data) |
351
|
|
|
{ |
352
|
|
|
if ($data instanceof File) { |
353
|
|
|
$data = $this->processFile($data); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return $this->updateItem('directus_files', $id, $data); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @inheritDoc |
361
|
|
|
*/ |
362
|
|
|
public function deleteFile($ids, $hard = false) |
363
|
|
|
{ |
364
|
|
|
return $this->deleteItem('directus_files', $ids, $hard); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
public function createPreferences($data) |
368
|
|
|
{ |
369
|
|
|
if (!ArrayUtils::contains($data, ['title', 'table_name'])) { |
370
|
|
|
throw new \Exception('title and table_name are required'); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
$acl = $this->container->get('acl'); |
374
|
|
|
$data['user'] = $acl->getUserId(); |
375
|
|
|
|
376
|
|
|
return $this->createItem('directus_preferences', $data); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @inheritdoc |
381
|
|
|
*/ |
382
|
|
|
public function createBookmark($data) |
383
|
|
|
{ |
384
|
|
|
$acl = $this->container->get('acl'); |
385
|
|
|
$data['user'] = $acl->getUserId(); |
386
|
|
|
|
387
|
|
|
$preferences = $this->createPreferences(ArrayUtils::pick($data, [ |
388
|
|
|
'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible', 'user' |
389
|
|
|
])); |
390
|
|
|
|
391
|
|
|
$title = $preferences->title; |
|
|
|
|
392
|
|
|
$tableName = $preferences->table_name; |
|
|
|
|
393
|
|
|
$bookmarkData = [ |
394
|
|
|
'section' => 'search', |
395
|
|
|
'title' => $title, |
396
|
|
|
'url' => 'tables/' . $tableName . '/pref/' . $title, |
397
|
|
|
'user' => $data['user'] |
398
|
|
|
]; |
399
|
|
|
|
400
|
|
|
return $this->createItem('directus_bookmarks', $bookmarkData); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @inheritdoc |
405
|
|
|
*/ |
406
|
|
|
public function getBookmark($id) |
407
|
|
|
{ |
408
|
|
|
return $this->getItem('directus_bookmarks', $id); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @inheritdoc |
413
|
|
|
*/ |
414
|
|
|
public function getBookmarks($userId = null) |
415
|
|
|
{ |
416
|
|
|
$filters = []; |
417
|
|
|
if ($userId !== null) { |
418
|
|
|
$filters = [ |
419
|
|
|
'filters' => ['user' => ['eq' => $userId]] |
420
|
|
|
]; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
return $this->getItems('directus_bookmarks', $filters); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* @inheritdoc |
428
|
|
|
*/ |
429
|
|
|
public function createColumn($data) |
430
|
|
|
{ |
431
|
|
|
$data = $this->parseColumnData($data); |
432
|
|
|
|
433
|
|
|
$tableGateway = $this->getTableGateway($data['table_name']); |
434
|
|
|
|
435
|
|
|
$tableGateway->addColumn($data['table_name'], ArrayUtils::omit($data, ['table_name'])); |
436
|
|
|
|
437
|
|
|
return $this->getColumn($data['table_name'], $data['column_name']); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* @inheritdoc |
442
|
|
|
*/ |
443
|
|
|
public function createGroup(array $data) |
444
|
|
|
{ |
445
|
|
|
return $this->createItem('directus_groups', $data); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* @inheritdoc |
450
|
|
|
*/ |
451
|
|
|
public function createMessage(array $data) |
452
|
|
|
{ |
453
|
|
|
$this->requiredAttributes(['from', 'message', 'subject'], $data); |
454
|
|
|
$this->requiredOneAttribute(['to', 'toGroup'], $data); |
455
|
|
|
|
456
|
|
|
$recipients = $this->getMessagesTo($data); |
457
|
|
|
$recipients = explode(',', $recipients); |
458
|
|
|
ArrayUtils::remove($data, ['to', 'toGroup']); |
459
|
|
|
|
460
|
|
|
$groupRecipients = []; |
461
|
|
|
$userRecipients = []; |
462
|
|
|
foreach ($recipients as $recipient) { |
463
|
|
|
$typeAndId = explode('_', $recipient); |
464
|
|
|
if ($typeAndId[0] == 0) { |
465
|
|
|
$userRecipients[] = $typeAndId[1]; |
466
|
|
|
} else { |
467
|
|
|
$groupRecipients[] = $typeAndId[1]; |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
$ZendDb = $this->container->get('connection'); |
|
|
|
|
472
|
|
|
$acl = $this->container->get('acl'); |
473
|
|
|
if (count($groupRecipients) > 0) { |
474
|
|
|
$usersTableGateway = new DirectusUsersTableGateway($ZendDb, $acl); |
|
|
|
|
475
|
|
|
$result = $usersTableGateway->findActiveUserIdsByGroupIds($groupRecipients); |
476
|
|
|
foreach ($result as $item) { |
477
|
|
|
$userRecipients[] = $item['id']; |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
$userRecipients[] = $acl->getUserId(); |
482
|
|
|
|
483
|
|
|
$messagesTableGateway = new DirectusMessagesTableGateway($ZendDb, $acl); |
|
|
|
|
484
|
|
|
$id = $messagesTableGateway->sendMessage($data, array_unique($userRecipients), $acl->getUserId()); |
485
|
|
|
|
486
|
|
|
if ($id) { |
487
|
|
|
$Activity = new DirectusActivityTableGateway($ZendDb, $acl); |
|
|
|
|
488
|
|
|
$data['id'] = $id; |
489
|
|
|
$Activity->recordMessage($data, $acl->getUserId()); |
|
|
|
|
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
$message = $messagesTableGateway->fetchMessageWithRecipients($id, $acl->getUserId()); |
493
|
|
|
$response = [ |
494
|
|
|
'meta' => [ |
495
|
|
|
'type' => 'item', |
496
|
|
|
'table' => 'directus_messages' |
497
|
|
|
], |
498
|
|
|
'data' => $message |
499
|
|
|
]; |
500
|
|
|
|
501
|
|
|
return $this->createResponseFromData($response); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* @inheritdoc |
506
|
|
|
*/ |
507
|
|
|
public function sendMessage(array $data) |
508
|
|
|
{ |
509
|
|
|
return $this->createMessage($data); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
View Code Duplication |
public function createPrivileges(array $data) |
|
|
|
|
513
|
|
|
{ |
514
|
|
|
$connection = $this->container->get('connection'); |
515
|
|
|
$acl = $this->container->get('acl'); |
516
|
|
|
$privileges = new DirectusPrivilegesTableGateway($connection, $acl); |
517
|
|
|
|
518
|
|
|
$response = [ |
519
|
|
|
'meta' => [ |
520
|
|
|
'type' => 'item', |
521
|
|
|
'table' => 'directus_privileges' |
522
|
|
|
], |
523
|
|
|
'data' => $privileges->insertPrivilege($data) |
524
|
|
|
]; |
525
|
|
|
|
526
|
|
|
return $this->createResponseFromData($response); |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
public function createTable($name, array $data = []) |
530
|
|
|
{ |
531
|
|
|
$isTableNameAlphanumeric = preg_match("/[a-z0-9]+/i", $name); |
532
|
|
|
$zeroOrMoreUnderscoresDashes = preg_match("/[_-]*/i", $name); |
533
|
|
|
|
534
|
|
|
if (!($isTableNameAlphanumeric && $zeroOrMoreUnderscoresDashes)) { |
535
|
|
|
return $this->createResponseFromData(['error' => ['message' => 'invalid_table_name']]); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
$schema = $this->container->get('schemaManager'); |
539
|
|
|
$emitter = $this->container->get('emitter'); |
540
|
|
|
if (!$schema->tableExists($name)) { |
541
|
|
|
$emitter->run('table.create:before', $name); |
542
|
|
|
// Through API: |
543
|
|
|
// Remove spaces and symbols from table name |
544
|
|
|
// And in lowercase |
545
|
|
|
$name = SchemaUtils::cleanTableName($name); |
546
|
|
|
$schema->createTable($name); |
547
|
|
|
$emitter->run('table.create', $name); |
548
|
|
|
$emitter->run('table.create:after', $name); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
$connection = $this->container->get('connection'); |
552
|
|
|
$acl = $this->container->get('acl'); |
553
|
|
|
$privileges = new DirectusPrivilegesTableGateway($connection, $acl); |
554
|
|
|
|
555
|
|
|
return $this->createResponseFromData($privileges->insertPrivilege([ |
556
|
|
|
'group_id' => 1, |
557
|
|
|
'table_name' => $name |
558
|
|
|
])); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* @inheritdoc |
563
|
|
|
*/ |
564
|
|
|
public function createColumnUIOptions(array $data) |
565
|
|
|
{ |
566
|
|
|
$this->requiredAttributes(['table', 'column', 'ui', 'options'], $data); |
567
|
|
|
$tableGateway = $this->getTableGateway('directus_ui'); |
568
|
|
|
|
569
|
|
|
$table = $data['table']; |
570
|
|
|
$column = $data['column']; |
571
|
|
|
$ui = $data['ui']; |
572
|
|
|
|
573
|
|
|
$data = $data['options']; |
574
|
|
|
$keys = ['table_name' => $table, 'column_name' => $column, 'ui_name' => $ui]; |
575
|
|
|
$uis = to_name_value($data, $keys); |
576
|
|
|
|
577
|
|
|
$column_settings = []; |
|
|
|
|
578
|
|
|
foreach ($uis as $col) { |
579
|
|
|
$existing = $tableGateway->select(['table_name' => $table, 'column_name' => $column, 'ui_name' => $ui, 'name' => $col['name']])->toArray(); |
580
|
|
|
if (count($existing) > 0) { |
581
|
|
|
$col['id'] = $existing[0]['id']; |
582
|
|
|
} |
583
|
|
|
array_push($column_settings, $col); |
|
|
|
|
584
|
|
|
} |
585
|
|
|
$tableGateway->updateCollection($column_settings); |
|
|
|
|
586
|
|
|
|
587
|
|
|
$connection = $this->container->get('connection'); |
588
|
|
|
$acl = $this->container->get('acl'); |
589
|
|
|
$UiOptions = new DirectusUiTableGateway($connection, $acl); |
|
|
|
|
590
|
|
|
$response = $UiOptions->fetchOptions($table, $column, $ui); |
|
|
|
|
591
|
|
|
|
592
|
|
|
if (!$response) { |
593
|
|
|
$response = [ |
594
|
|
|
'error' => [ |
595
|
|
|
'message' => sprintf('unable_to_find_column_%s_options_for_%s', ['column' => $column, 'ui' => $ui]) |
596
|
|
|
], |
597
|
|
|
'success' => false |
598
|
|
|
]; |
599
|
|
|
} else { |
600
|
|
|
$response = [ |
601
|
|
|
'meta' => [ |
602
|
|
|
'type' => 'item', |
603
|
|
|
'table' => 'directus_ui' |
604
|
|
|
], |
605
|
|
|
'data' => $response |
606
|
|
|
]; |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
return $this->createResponseFromData($response); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
View Code Duplication |
public function getPreferences($table, $user) |
|
|
|
|
613
|
|
|
{ |
614
|
|
|
$acl = $this->container->get('acl'); |
615
|
|
|
$connection = $this->container->get('connection'); |
616
|
|
|
$preferencesTableGateway = new DirectusPreferencesTableGateway($connection, $acl); |
617
|
|
|
|
618
|
|
|
$response = [ |
619
|
|
|
'meta' => [ |
620
|
|
|
'type' => 'item', |
621
|
|
|
'table' => 'directus_preferences' |
622
|
|
|
], |
623
|
|
|
'data' => $preferencesTableGateway->fetchByUserAndTableAndTitle($user, $table) |
624
|
|
|
]; |
625
|
|
|
|
626
|
|
|
return $this->createResponseFromData($response); |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* @inheritdoc |
631
|
|
|
*/ |
632
|
|
|
public function deleteBookmark($id, $hard = false) |
633
|
|
|
{ |
634
|
|
|
return $this->deleteItem('directus_bookmarks', $id, $hard); |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
/** |
638
|
|
|
* @inheritdoc |
639
|
|
|
*/ |
640
|
|
View Code Duplication |
public function deleteColumn($name, $table) |
|
|
|
|
641
|
|
|
{ |
642
|
|
|
$tableGateway = $this->getTableGateway($table); |
643
|
|
|
$success = $tableGateway->dropColumn($name); |
644
|
|
|
|
645
|
|
|
$response = [ |
646
|
|
|
'success' => (bool) $success |
647
|
|
|
]; |
648
|
|
|
|
649
|
|
|
if (!$success) { |
650
|
|
|
$response['error'] = [ |
651
|
|
|
'message' => sprintf('unable_to_remove_column_%s', ['column_name' => $name]) |
652
|
|
|
]; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
return $this->createResponseFromData($response); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* @inheritdoc |
660
|
|
|
*/ |
661
|
|
|
public function deleteGroup($id, $hard = false) |
662
|
|
|
{ |
663
|
|
|
return $this->deleteItem('directus_groups', $id, $hard); |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
/** |
667
|
|
|
* @inheritdoc |
668
|
|
|
*/ |
669
|
|
View Code Duplication |
public function deleteTable($name) |
|
|
|
|
670
|
|
|
{ |
671
|
|
|
$tableGateway = $this->getTableGateway($name); |
672
|
|
|
$success = $tableGateway->drop(); |
673
|
|
|
|
674
|
|
|
$response = [ |
675
|
|
|
'success' => (bool) $success |
676
|
|
|
]; |
677
|
|
|
|
678
|
|
|
if (!$success) { |
679
|
|
|
$response['error'] = [ |
680
|
|
|
'message' => sprintf('unable_to_remove_table_%s', ['table_name' => $name]) |
681
|
|
|
]; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
return $this->createResponseFromData($response); |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
public function getActivity(array $params = []) |
688
|
|
|
{ |
689
|
|
|
$connection = $this->container->get('connection'); |
690
|
|
|
$acl = $this->container->get('acl'); |
691
|
|
|
$tableGateway = new DirectusActivityTableGateway($connection, $acl); |
692
|
|
|
|
693
|
|
|
$data = $tableGateway->fetchFeed($params); |
694
|
|
|
|
695
|
|
|
return $this->createResponseFromData($data); |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
/** |
699
|
|
|
* @inheritdoc |
700
|
|
|
*/ |
701
|
|
|
public function getRandom(array $options = []) |
702
|
|
|
{ |
703
|
|
|
$length = (int) ArrayUtils::get($options, 'length', 32); |
704
|
|
|
|
705
|
|
|
return $this->createResponseFromData([ |
706
|
|
|
'success' => true, |
707
|
|
|
'data' => [ |
708
|
|
|
'random' => StringUtils::randomString($length) |
709
|
|
|
] |
710
|
|
|
]); |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* @inheritdoc |
715
|
|
|
*/ |
716
|
|
|
public function getHash($string, array $options = []) |
717
|
|
|
{ |
718
|
|
|
$hashManager = $this->container->get('hashManager'); |
719
|
|
|
|
720
|
|
|
try { |
721
|
|
|
$hash = $hashManager->hash($string, $options); |
722
|
|
|
$data = [ |
723
|
|
|
'success' => true, |
724
|
|
|
'data' => [ |
725
|
|
|
'hash' => $hash |
726
|
|
|
] |
727
|
|
|
]; |
728
|
|
|
} catch (\Exception $e) { |
729
|
|
|
$data = [ |
730
|
|
|
'success' => false, |
731
|
|
|
'error' => [ |
732
|
|
|
'message' => $e->getMessage() |
733
|
|
|
] |
734
|
|
|
]; |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
return $this->createResponseFromData($data); |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* Get a table gateway for the given table name |
742
|
|
|
* |
743
|
|
|
* @param $tableName |
744
|
|
|
* |
745
|
|
|
* @return RelationalTableGateway |
746
|
|
|
*/ |
747
|
|
|
protected function getTableGateway($tableName) |
748
|
|
|
{ |
749
|
|
|
if (!array_key_exists($tableName, $this->tableGateways)) { |
750
|
|
|
$acl = TableSchema::getAclInstance(); |
751
|
|
|
$this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl); |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
return $this->tableGateways[$tableName]; |
755
|
|
|
} |
756
|
|
|
} |
757
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.