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