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