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