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\DirectusPrivilegesTableGateway; |
18
|
|
|
use Directus\Database\TableGateway\DirectusUsersTableGateway; |
19
|
|
|
use Directus\Database\TableGateway\RelationalTableGateway; |
20
|
|
|
use Directus\Database\TableSchema; |
21
|
|
|
use Directus\Util\ArrayUtils; |
22
|
|
|
use Directus\Util\SchemaUtils; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Client Local |
26
|
|
|
* |
27
|
|
|
* Client to Interact with the database directly using Directus Database Component |
28
|
|
|
* |
29
|
|
|
* @author Welling Guzmán <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class ClientLocal extends AbstractClient |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var BaseTableGateway[] |
35
|
|
|
*/ |
36
|
|
|
protected $tableGateways = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Connection |
40
|
|
|
*/ |
41
|
|
|
protected $connection = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* ClientLocal constructor. |
45
|
|
|
* |
46
|
|
|
* @param $connection |
47
|
|
|
*/ |
48
|
|
|
public function __construct($connection) |
49
|
|
|
{ |
50
|
|
|
$this->connection = $connection; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function getTables(array $params = []) |
57
|
|
|
{ |
58
|
|
|
return $this->createResponseFromData(TableSchema::getTablesSchema($params)); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
|
|
public function getTable($tableName) |
65
|
|
|
{ |
66
|
|
|
return $this->createResponseFromData(TableSchema::getSchemaArray($tableName)); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
|
|
public function getColumns($tableName, array $params = []) |
73
|
|
|
{ |
74
|
|
|
return $this->createResponseFromData(TableSchema::getColumnSchemaArray($tableName, $params)); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
|
|
public function getColumn($tableName, $columnName) |
81
|
|
|
{ |
82
|
|
|
return $this->createResponseFromData(TableSchema::getColumnSchema($tableName, $columnName)->toArray()); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
88
|
|
|
public function getEntries($tableName, array $params = []) |
89
|
|
|
{ |
90
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
91
|
|
|
|
92
|
|
|
return $this->createResponseFromData($tableGateway->getEntries($params)); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
|
|
public function getEntry($tableName, $id, array $params = []) |
99
|
|
|
{ |
100
|
|
|
// @TODO: Dynamic ID |
101
|
|
|
return $this->getEntries($tableName, array_merge($params, [ |
|
|
|
|
102
|
|
|
'id' => $id |
103
|
|
|
])); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
|
|
public function getUsers(array $params = []) |
110
|
|
|
{ |
111
|
|
|
// @TODO: store the directus tables somewhere (SchemaManager?) |
112
|
|
|
return $this->getEntries('directus_users', $params); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritDoc |
117
|
|
|
*/ |
118
|
|
|
public function getUser($id, array $params = []) |
119
|
|
|
{ |
120
|
|
|
return $this->getEntry('directus_users', $id, $params); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritDoc |
125
|
|
|
*/ |
126
|
|
|
public function getGroups(array $params = []) |
127
|
|
|
{ |
128
|
|
|
return $this->getEntries('directus_groups', $params); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritDoc |
133
|
|
|
*/ |
134
|
|
|
public function getGroup($id, array $params = []) |
135
|
|
|
{ |
136
|
|
|
return $this->getEntry('directus_groups', $id, $params); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritDoc |
141
|
|
|
*/ |
142
|
|
|
public function getGroupPrivileges($groupID) |
143
|
|
|
{ |
144
|
|
|
$this->getEntries('directus_privileges', [ |
145
|
|
|
'filter' => [ |
146
|
|
|
'group_id' => ['eq' => $groupID] |
147
|
|
|
] |
148
|
|
|
]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritDoc |
153
|
|
|
*/ |
154
|
|
|
public function getFiles(array $params = []) |
155
|
|
|
{ |
156
|
|
|
return $this->getEntries('directus_files', $params); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritDoc |
161
|
|
|
*/ |
162
|
|
|
public function getFile($id, array $params = []) |
163
|
|
|
{ |
164
|
|
|
return $this->getEntry('directus_files', $id, $params); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
|
|
public function getSettings() |
171
|
|
|
{ |
172
|
|
|
return $this->getEntries('directus_settings'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritDoc |
177
|
|
|
*/ |
178
|
|
|
public function getSettingsByCollection($collectionName) |
179
|
|
|
{ |
180
|
|
|
return $this->getEntries('directus_settings', [ |
181
|
|
|
'filter' => [ |
182
|
|
|
'collection' => ['eq' => $collectionName] |
183
|
|
|
] |
184
|
|
|
]); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @inheritDoc |
189
|
|
|
*/ |
190
|
|
|
public function getMessages($userId) |
191
|
|
|
{ |
192
|
|
|
$messagesTableGateway = new DirectusMessagesTableGateway($this->connection, null); |
|
|
|
|
193
|
|
|
$result = $messagesTableGateway->fetchMessagesInboxWithHeaders($userId); |
194
|
|
|
|
195
|
|
|
return $this->createResponseFromData($result); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @inheritDoc |
200
|
|
|
*/ |
201
|
|
View Code Duplication |
public function createEntry($tableName, array $data) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
204
|
|
|
$data = $this->processData($tableName, $data); |
205
|
|
|
|
206
|
|
|
foreach($data as $key => $value) { |
207
|
|
|
if ($value instanceof File) { |
208
|
|
|
$data[$key] = $this->processFile($value); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$newRecord = $tableGateway->manageRecordUpdate($tableName, $data); |
213
|
|
|
|
214
|
|
|
return $this->getEntry($tableName, $newRecord[$tableGateway->primaryKeyFieldName]); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @inheritDoc |
219
|
|
|
*/ |
220
|
|
View Code Duplication |
public function updateEntry($tableName, $id, array $data) |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
223
|
|
|
$data = $this->processData($tableName, $data); |
224
|
|
|
|
225
|
|
|
foreach($data as $key => $value) { |
226
|
|
|
if ($value instanceof File) { |
227
|
|
|
$data[$key] = $this->processFile($value); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$updatedRecord = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id])); |
232
|
|
|
|
233
|
|
|
return $this->getEntry($tableName, $updatedRecord[$tableGateway->primaryKeyFieldName]); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @inheritDoc |
238
|
|
|
*/ |
239
|
|
|
public function deleteEntry($tableName, $ids) |
240
|
|
|
{ |
241
|
|
|
// @TODO: Accept EntryCollection and Entry |
242
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
243
|
|
|
|
244
|
|
|
if (!is_array($ids)) { |
245
|
|
|
$ids = [$ids]; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $tableGateway->delete(function($delete) use ($ids) { |
249
|
|
|
return $delete->where->in('id', $ids); |
250
|
|
|
}); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @inheritDoc |
255
|
|
|
*/ |
256
|
|
|
public function createUser(array $data) |
257
|
|
|
{ |
258
|
|
|
return $this->createEntry('directus_users', $data); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @inheritDoc |
263
|
|
|
*/ |
264
|
|
|
public function updateUser($id, array $data) |
265
|
|
|
{ |
266
|
|
|
return $this->updateEntry('directus_users', $id, $data); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @inheritDoc |
271
|
|
|
*/ |
272
|
|
|
public function deleteUser($ids) |
273
|
|
|
{ |
274
|
|
|
return $this->deleteEntry('directus_users', $ids); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @inheritDoc |
279
|
|
|
*/ |
280
|
|
|
public function createFile(File $file) |
281
|
|
|
{ |
282
|
|
|
$data = $this->processFile($file); |
283
|
|
|
|
284
|
|
|
return $this->createEntry('directus_files', $data); |
|
|
|
|
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @inheritDoc |
289
|
|
|
*/ |
290
|
|
|
public function updateFile($id, array $data) |
291
|
|
|
{ |
292
|
|
|
return $this->updateEntry('directus_files', $id, $data); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @inheritDoc |
297
|
|
|
*/ |
298
|
|
|
public function deleteFile($ids) |
299
|
|
|
{ |
300
|
|
|
return $this->deleteEntry('directus_files', $ids); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function createPreferences($data) |
304
|
|
|
{ |
305
|
|
|
if (!ArrayUtils::contains($data, ['title', 'table_name'])) { |
306
|
|
|
throw new \Exception('title and table_name are required'); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
$acl = $this->container->get('acl'); |
310
|
|
|
$data['user'] = $acl->getUserId(); |
311
|
|
|
|
312
|
|
|
return $this->createEntry('directus_preferences', $data); |
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @inheritdoc |
317
|
|
|
*/ |
318
|
|
|
public function createBookmark($data) |
319
|
|
|
{ |
320
|
|
|
$acl = $this->container->get('acl'); |
321
|
|
|
$data['user'] = $acl->getUserId(); |
322
|
|
|
|
323
|
|
|
$preferences = $this->createPreferences(ArrayUtils::pick($data, [ |
324
|
|
|
'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible', 'user' |
325
|
|
|
])); |
326
|
|
|
|
327
|
|
|
$title = $preferences->title; |
|
|
|
|
328
|
|
|
$tableName = $preferences->table_name; |
|
|
|
|
329
|
|
|
$bookmarkData = [ |
330
|
|
|
'section' => 'search', |
331
|
|
|
'title' => $title, |
332
|
|
|
'url' => 'tables/' . $tableName . '/pref/' . $title, |
333
|
|
|
'user' => $data['user'] |
334
|
|
|
]; |
335
|
|
|
|
336
|
|
|
return $this->createEntry('directus_bookmarks', $bookmarkData); |
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @inheritdoc |
341
|
|
|
*/ |
342
|
|
|
public function createColumn($data) |
343
|
|
|
{ |
344
|
|
|
$data = $this->parseColumnData($data); |
345
|
|
|
|
346
|
|
|
$tableGateway = $this->getTableGateway($data['table_name']); |
347
|
|
|
|
348
|
|
|
$tableGateway->addColumn($data['table_name'], ArrayUtils::omit($data, ['table_name'])); |
349
|
|
|
|
350
|
|
|
return $this->getColumn($data['table_name'], $data['column_name']); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @inheritdoc |
355
|
|
|
*/ |
356
|
|
|
public function createGroup(array $data) |
357
|
|
|
{ |
358
|
|
|
return $this->createEntry('directus_groups', $data); |
|
|
|
|
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @inheritdoc |
363
|
|
|
*/ |
364
|
|
|
public function createMessage(array $data) |
365
|
|
|
{ |
366
|
|
|
$this->requiredAttributes(['from', 'message', 'subject'], $data); |
367
|
|
|
$this->requiredOneAttribute(['to', 'toGroup'], $data); |
368
|
|
|
|
369
|
|
|
$recipients = $this->getMessagesTo($data); |
370
|
|
|
$recipients = explode(',', $recipients); |
371
|
|
|
ArrayUtils::remove($data, ['to', 'toGroup']); |
372
|
|
|
|
373
|
|
|
$groupRecipients = []; |
374
|
|
|
$userRecipients = []; |
375
|
|
|
foreach ($recipients as $recipient) { |
376
|
|
|
$typeAndId = explode('_', $recipient); |
377
|
|
|
if ($typeAndId[0] == 0) { |
378
|
|
|
$userRecipients[] = $typeAndId[1]; |
379
|
|
|
} else { |
380
|
|
|
$groupRecipients[] = $typeAndId[1]; |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
$ZendDb = $this->container->get('connection'); |
385
|
|
|
$acl = $this->container->get('acl'); |
386
|
|
|
if (count($groupRecipients) > 0) { |
387
|
|
|
$usersTableGateway = new DirectusUsersTableGateway($ZendDb, $acl); |
388
|
|
|
$result = $usersTableGateway->findActiveUserIdsByGroupIds($groupRecipients); |
389
|
|
|
foreach ($result as $item) { |
390
|
|
|
$userRecipients[] = $item['id']; |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
$userRecipients[] = $acl->getUserId(); |
395
|
|
|
|
396
|
|
|
$messagesTableGateway = new DirectusMessagesTableGateway($ZendDb, $acl); |
397
|
|
|
$id = $messagesTableGateway->sendMessage($data, array_unique($userRecipients), $acl->getUserId()); |
398
|
|
|
|
399
|
|
|
if ($id) { |
400
|
|
|
$Activity = new DirectusActivityTableGateway($ZendDb, $acl); |
401
|
|
|
$data['id'] = $id; |
402
|
|
|
$Activity->recordMessage($data, $acl->getUserId()); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
$message = $messagesTableGateway->fetchMessageWithRecipients($id, $acl->getUserId()); |
406
|
|
|
$response = [ |
407
|
|
|
'meta' => [ |
408
|
|
|
'type' => 'entry', |
409
|
|
|
'table' => 'directus_messages' |
410
|
|
|
], |
411
|
|
|
'data' => $message |
412
|
|
|
]; |
413
|
|
|
|
414
|
|
|
return $this->createResponseFromData($response); |
|
|
|
|
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @inheritdoc |
419
|
|
|
*/ |
420
|
|
|
public function sendMessage(array $data) |
421
|
|
|
{ |
422
|
|
|
return $this->createMessage($data); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
public function createPrivileges(array $data) |
426
|
|
|
{ |
427
|
|
|
$connection = $this->container->get('connection'); |
428
|
|
|
$acl = $this->container->get('acl'); |
429
|
|
|
$privileges = new DirectusPrivilegesTableGateway($connection, $acl); |
430
|
|
|
|
431
|
|
|
$response = [ |
432
|
|
|
'meta' => [ |
433
|
|
|
'type' => 'entry', |
434
|
|
|
'table' => 'directus_privileges' |
435
|
|
|
], |
436
|
|
|
'data' => $privileges->insertPrivilege($data) |
437
|
|
|
]; |
438
|
|
|
|
439
|
|
|
return $this->createResponseFromData($response); |
|
|
|
|
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
public function createTable($name, array $data = []) |
443
|
|
|
{ |
444
|
|
|
$isTableNameAlphanumeric = preg_match("/[a-z0-9]+/i", $name); |
445
|
|
|
$zeroOrMoreUnderscoresDashes = preg_match("/[_-]*/i", $name); |
446
|
|
|
|
447
|
|
|
if (!($isTableNameAlphanumeric && $zeroOrMoreUnderscoresDashes)) { |
448
|
|
|
return $this->createResponseFromData(['error' => ['message' => 'invalid_table_name']]); |
|
|
|
|
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$schema = $this->container->get('schemaManager'); |
452
|
|
|
$emitter = $this->container->get('emitter'); |
453
|
|
|
if (!$schema->tableExists($name)) { |
454
|
|
|
$emitter->run('table.create:before', $name); |
455
|
|
|
// Through API: |
456
|
|
|
// Remove spaces and symbols from table name |
457
|
|
|
// And in lowercase |
458
|
|
|
$name = SchemaUtils::cleanTableName($name); |
459
|
|
|
$schema->createTable($name); |
460
|
|
|
$emitter->run('table.create', $name); |
461
|
|
|
$emitter->run('table.create:after', $name); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
$connection = $this->container->get('connection'); |
465
|
|
|
$acl = $this->container->get('acl'); |
466
|
|
|
$privileges = new DirectusPrivilegesTableGateway($connection, $acl); |
467
|
|
|
|
468
|
|
|
return $this->createResponseFromData($privileges->insertPrivilege([ |
|
|
|
|
469
|
|
|
'group_id' => 1, |
470
|
|
|
'table_name' => $name |
471
|
|
|
])); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Get a table gateway for the given table name |
476
|
|
|
* |
477
|
|
|
* @param $tableName |
478
|
|
|
* |
479
|
|
|
* @return RelationalTableGateway |
480
|
|
|
*/ |
481
|
|
|
protected function getTableGateway($tableName) |
482
|
|
|
{ |
483
|
|
|
if (!array_key_exists($tableName, $this->tableGateways)) { |
484
|
|
|
$acl = TableSchema::getAclInstance(); |
485
|
|
|
$this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
return $this->tableGateways[$tableName]; |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|