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