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