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
|
|
|
use Directus\Util\ArrayUtils; |
13
|
|
|
use Directus\Util\DateUtils; |
14
|
|
|
use Directus\Util\StringUtils; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Client Remote |
18
|
|
|
* |
19
|
|
|
* @author Welling Guzmán <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ClientRemote extends BaseClientRemote |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
2 |
|
public function getTables(array $params = []) |
27
|
|
|
{ |
28
|
2 |
|
return $this->performRequest('GET', static::TABLE_LIST_ENDPOINT); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
4 |
|
public function getTable($tableName) |
35
|
|
|
{ |
36
|
4 |
|
$path = $this->buildPath(static::TABLE_INFORMATION_ENDPOINT, $tableName); |
37
|
|
|
|
38
|
4 |
|
return $this->performRequest('GET', $path); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
2 |
|
public function getColumns($tableName, array $params = []) |
45
|
|
|
{ |
46
|
2 |
|
$path = $this->buildPath(static::COLUMN_LIST_ENDPOINT, $tableName); |
47
|
|
|
|
48
|
2 |
|
return $this->performRequest('GET', $path); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
2 |
|
public function getColumn($tableName, $columnName) |
55
|
|
|
{ |
56
|
2 |
|
$path = $this->buildPath(static::COLUMN_INFORMATION_ENDPOINT, [$tableName, $columnName]); |
57
|
|
|
|
58
|
2 |
|
return $this->performRequest('GET', $path); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
2 |
|
public function getItems($tableName, array $options = []) |
65
|
|
|
{ |
66
|
2 |
|
$path = $this->buildPath(static::TABLE_ENTRIES_ENDPOINT, $tableName); |
|
|
|
|
67
|
|
|
|
68
|
2 |
|
return $this->performRequest('GET', $path, ['query' => $options]); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
2 |
|
public function getItem($tableName, $id, array $options = []) |
75
|
|
|
{ |
76
|
2 |
|
$path = $this->buildPath(static::TABLE_ENTRY_ENDPOINT, [$tableName, $id]); |
77
|
|
|
|
78
|
2 |
|
return $this->performRequest('GET', $path, ['query' => $options]); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
2 |
|
public function getUsers(array $params = []) |
85
|
|
|
{ |
86
|
2 |
|
return $this->getItems('directus_users', $params); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
2 |
|
public function getUser($id, array $params = []) |
93
|
|
|
{ |
94
|
2 |
|
return $this->getItem($id, 'directus_users', $params); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
2 |
|
public function getGroups() |
101
|
|
|
{ |
102
|
2 |
|
return $this->performRequest('GET', static::GROUP_LIST_ENDPOINT); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
2 |
|
public function getGroup($groupID) |
109
|
|
|
{ |
110
|
2 |
|
$path = $this->buildPath(static::GROUP_INFORMATION_ENDPOINT, $groupID); |
111
|
|
|
|
112
|
2 |
|
return $this->performRequest('GET', $path); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
2 |
|
public function getGroupPrivileges($groupID) |
119
|
|
|
{ |
120
|
2 |
|
$path = $this->buildPath(static::GROUP_PRIVILEGES_ENDPOINT, $groupID); |
121
|
|
|
|
122
|
2 |
|
return $this->performRequest('GET', $path); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritdoc |
127
|
|
|
*/ |
128
|
2 |
|
public function getFiles() |
129
|
|
|
{ |
130
|
2 |
|
return $this->performRequest('GET', static::FILE_LIST_ENDPOINT); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @inheritdoc |
135
|
|
|
*/ |
136
|
2 |
|
public function getFile($fileID) |
137
|
|
|
{ |
138
|
2 |
|
$path = $this->buildPath(static::FILE_INFORMATION_ENDPOINT, $fileID); |
139
|
|
|
|
140
|
2 |
|
return $this->performRequest('GET', $path); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritdoc |
145
|
|
|
*/ |
146
|
2 |
|
public function getSettings() |
147
|
|
|
{ |
148
|
2 |
|
return $this->performRequest('GET', static::SETTING_LIST_ENDPOINT); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @inheritdoc |
153
|
|
|
*/ |
154
|
2 |
|
public function getSettingsByCollection($collectionName) |
155
|
|
|
{ |
156
|
2 |
|
$path = $this->buildPath(static::SETTING_COLLECTION_GET_ENDPOINT, $collectionName); |
157
|
|
|
|
158
|
2 |
|
return $this->performRequest('GET', $path); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritdoc |
163
|
|
|
*/ |
164
|
|
|
public function updateSettings($collection, array $data) |
165
|
|
|
{ |
166
|
|
|
$path = $this->buildPath(static::SETTING_COLLECTION_UPDATE_ENDPOINT, $collection); |
167
|
|
|
|
168
|
|
|
return $this->performRequest('PUT', $path, [ |
|
|
|
|
169
|
|
|
'body' => $data |
170
|
|
|
]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritdoc |
175
|
|
|
*/ |
176
|
|
|
public function getMessages($userId) |
177
|
|
|
{ |
178
|
|
|
$path = $this->buildPath(static::MESSAGES_USER_ENDPOINT, $userId); |
179
|
|
|
|
180
|
|
|
return $this->performRequest('GET', $path); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritdoc |
185
|
|
|
*/ |
186
|
|
View Code Duplication |
public function createItem($tableName, array $data) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$path = $this->buildPath(static::TABLE_ENTRY_CREATE_ENDPOINT, $tableName); |
189
|
|
|
$data = $this->processData($tableName, $data); |
190
|
|
|
|
191
|
|
|
return $this->performRequest('POST', $path, ['body' => $data]); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @inheritdoc |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function updateItem($tableName, $id, array $data) |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
$path = $this->buildPath(static::TABLE_ENTRY_UPDATE_ENDPOINT, [$tableName, $id]); |
200
|
|
|
$data = $this->processData($tableName, $data); |
201
|
|
|
|
202
|
|
|
return $this->performRequest('PUT', $path, ['body' => $data]); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @inheritdoc |
207
|
|
|
*/ |
208
|
|
|
public function deleteItem($tableName, $id) |
209
|
|
|
{ |
210
|
|
|
$path = $this->buildPath(static::TABLE_ENTRY_DELETE_ENDPOINT, [$tableName, $id]); |
211
|
|
|
|
212
|
|
|
return $this->performRequest('DELETE', $path); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @inheritdoc |
217
|
|
|
*/ |
218
|
|
|
public function createUser(array $data) |
219
|
|
|
{ |
220
|
|
|
return $this->createItem('directus_users', $data); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @inheritdoc |
225
|
|
|
*/ |
226
|
|
|
public function updateUser($id, array $data) |
227
|
|
|
{ |
228
|
|
|
return $this->updateItem('directus_users', $id, $data); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @inheritdoc |
233
|
|
|
*/ |
234
|
|
|
public function deleteUser($ids) |
235
|
|
|
{ |
236
|
|
|
return $this->deleteItem('directus_users', $ids); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @inheritdoc |
241
|
|
|
*/ |
242
|
|
|
public function createFile(File $file) |
243
|
|
|
{ |
244
|
|
|
$data = $this->processFile($file); |
245
|
|
|
|
246
|
|
|
return $this->performRequest('POST', static::FILE_CREATE_ENDPOINT, ['body' => $data]); |
|
|
|
|
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @inheritdoc |
251
|
|
|
*/ |
252
|
|
|
public function updateFile($id, $data) |
253
|
|
|
{ |
254
|
|
|
if ($data instanceof File) { |
255
|
|
|
$data = $data->toArray(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$data['id'] = $id; |
259
|
|
|
$path = $this->buildPath(static::FILE_UPDATE_ENDPOINT, $id); |
260
|
|
|
$data = $this->processData('directus_files', $data); |
261
|
|
|
|
262
|
|
|
return $this->performRequest('POST', $path, ['body' => $data]); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @inheritdoc |
267
|
|
|
*/ |
268
|
|
|
public function deleteFile($id) |
269
|
|
|
{ |
270
|
|
|
return $this->deleteItem('directus_files', $id); |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function createPreferences($data) |
274
|
|
|
{ |
275
|
|
|
$this->requiredAttributes(['title', 'table_name'], $data); |
276
|
|
|
|
277
|
|
|
$tableName = ArrayUtils::get($data, 'table_name'); |
278
|
|
|
$path = $this->buildPath(static::TABLE_PREFERENCES_ENDPOINT, $tableName); |
279
|
|
|
$data = $this->processData($tableName, $data); |
280
|
|
|
|
281
|
|
|
return $this->performRequest('POST', $path, ['body' => $data]); |
|
|
|
|
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @inheritdoc |
286
|
|
|
*/ |
287
|
|
|
public function createBookmark($data) |
288
|
|
|
{ |
289
|
|
|
$preferences = $this->createPreferences(ArrayUtils::pick($data, [ |
290
|
|
|
'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible' |
291
|
|
|
])); |
292
|
|
|
|
293
|
|
|
$title = $preferences->title; |
|
|
|
|
294
|
|
|
$tableName = $preferences->table_name; |
|
|
|
|
295
|
|
|
$bookmarkData = [ |
296
|
|
|
'section' => 'search', |
297
|
|
|
'title' => $title, |
298
|
|
|
'url' => 'tables/' . $tableName . '/pref/' . $title |
299
|
|
|
]; |
300
|
|
|
|
301
|
|
|
$path = $this->buildPath(static::BOOKMARKS_CREATE_ENDPOINT); |
302
|
|
|
$bookmarkData = $this->processData($tableName, $bookmarkData); |
303
|
|
|
|
304
|
|
|
return $this->performRequest('POST', $path, ['body' => $bookmarkData]); |
|
|
|
|
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @inheritdoc |
309
|
|
|
*/ |
310
|
|
|
public function getBookmark($id) |
311
|
|
|
{ |
312
|
|
|
$path = $this->buildPath(static::BOOKMARKS_READ_ENDPOINT, $id); |
|
|
|
|
313
|
|
|
|
314
|
|
|
return $this->performRequest('GET', $path); |
|
|
|
|
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @inheritdoc |
319
|
|
|
*/ |
320
|
|
|
public function getBookmarks($userId = null) |
321
|
|
|
{ |
322
|
|
|
if ($userId !== null) { |
323
|
|
|
$path = $this->buildPath(static::BOOKMARKS_USER_ENDPOINT, $userId); |
|
|
|
|
324
|
|
|
} else { |
325
|
|
|
$path = $this->buildPath(static::BOOKMARKS_ALL_ENDPOINT); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return $this->performRequest('GET', $path); |
|
|
|
|
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @inheritdoc |
333
|
|
|
*/ |
334
|
|
|
public function createColumn($data) |
335
|
|
|
{ |
336
|
|
|
$data = $this->parseColumnData($data); |
337
|
|
|
|
338
|
|
|
return $this->performRequest('POST', $this->buildPath(static::COLUMN_CREATE_ENDPOINT, $data['table_name']), [ |
|
|
|
|
339
|
|
|
'body' => $data |
340
|
|
|
]); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @inheritdoc |
345
|
|
|
*/ |
346
|
|
|
public function createGroup(array $data) |
347
|
|
|
{ |
348
|
|
|
return $this->performRequest('POST', static::GROUP_CREATE_ENDPOINT, [ |
|
|
|
|
349
|
|
|
'body' => $data |
350
|
|
|
]); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @inheritdoc |
355
|
|
|
*/ |
356
|
|
|
public function createMessage(array $data) |
357
|
|
|
{ |
358
|
|
|
$this->requiredAttributes(['from', 'message', 'subject'], $data); |
359
|
|
|
$this->requiredOneAttribute(['to', 'toGroup'], $data); |
360
|
|
|
|
361
|
|
|
$data['recipients'] = $this->getMessagesTo($data); |
362
|
|
|
ArrayUtils::remove($data, ['to', 'toGroup']); |
363
|
|
|
|
364
|
|
|
return $this->performRequest('POST', static::MESSAGES_CREATE_ENDPOINT, [ |
|
|
|
|
365
|
|
|
'body' => $data |
366
|
|
|
]); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* @inheritdoc |
371
|
|
|
*/ |
372
|
|
|
public function sendMessage(array $data) |
373
|
|
|
{ |
374
|
|
|
return $this->createMessage($data); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @inheritdoc |
379
|
|
|
*/ |
380
|
|
|
public function createPrivileges(array $data) |
381
|
|
|
{ |
382
|
|
|
$this->requiredAttributes(['group_id', 'table_name'], $data); |
383
|
|
|
|
384
|
|
|
return $this->performRequest('POST', static::GROUP_PRIVILEGES_CREATE_ENDPOINT, [ |
|
|
|
|
385
|
|
|
'body' => $data |
386
|
|
|
]); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
public function createTable($name, array $params = []) |
390
|
|
|
{ |
391
|
|
|
$data = [ |
392
|
|
|
'addTable' => true, |
393
|
|
|
'table_name' => $name |
394
|
|
|
]; |
395
|
|
|
|
396
|
|
|
return $this->performRequest('POST', static::TABLE_CREATE_ENDPOINT, [ |
|
|
|
|
397
|
|
|
'body' => $data |
398
|
|
|
]); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @inheritdoc |
403
|
|
|
*/ |
404
|
|
|
public function createColumnUIOptions(array $data) |
405
|
|
|
{ |
406
|
|
|
$this->requiredAttributes(['table', 'column', 'ui', 'options'], $data); |
407
|
|
|
|
408
|
|
|
$path = $this->buildPath(static::COLUMN_OPTIONS_CREATE_ENDPOINT, [ |
409
|
|
|
$data['table'], |
410
|
|
|
$data['column'], |
411
|
|
|
$data['ui'] |
412
|
|
|
]); |
413
|
|
|
|
414
|
|
|
$data = ArrayUtils::get($data, 'options'); |
415
|
|
|
|
416
|
|
|
return $this->performRequest('POST', $path, [ |
|
|
|
|
417
|
|
|
'body' => $data |
418
|
|
|
]); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @inheritdoc |
423
|
|
|
*/ |
424
|
|
|
public function getPreferences($table, $user = null) |
425
|
|
|
{ |
426
|
|
|
return $this->performRequest('POST', $this->buildPath(static::TABLE_PREFERENCES_ENDPOINT, $table)); |
|
|
|
|
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* @inheritdoc |
431
|
|
|
*/ |
432
|
|
|
public function deleteBookmark($id) |
433
|
|
|
{ |
434
|
|
|
return $this->deleteItem('directus_bookmarks', $id); |
|
|
|
|
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* @inheritdoc |
439
|
|
|
*/ |
440
|
|
|
public function deleteColumn($name, $table) |
441
|
|
|
{ |
442
|
|
|
$path = $this->buildPath(static::COLUMN_DELETE_ENDPOINT, [$name, $table]); |
443
|
|
|
|
444
|
|
|
return $this->performRequest('DELETE', $path); |
|
|
|
|
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @inheritdoc |
449
|
|
|
*/ |
450
|
|
|
public function deleteGroup($id) |
451
|
|
|
{ |
452
|
|
|
return $this->deleteItem('directus_groups', $id); |
|
|
|
|
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* @inheritdoc |
457
|
|
|
*/ |
458
|
|
|
public function deleteTable($name) |
459
|
|
|
{ |
460
|
|
|
$path = $this->buildPath(static::TABLE_DELETE_ENDPOINT, $name); |
461
|
|
|
|
462
|
|
|
return $this->performRequest('DELETE', $path); |
|
|
|
|
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* @inheritdoc |
467
|
|
|
*/ |
468
|
|
|
public function getActivity(array $params = []) |
469
|
2 |
|
{ |
470
|
|
View Code Duplication |
if (!ArrayUtils::has($params, 'filters.datetime')) { |
|
|
|
|
471
|
|
|
$params['filters']['datetime'] = ['>=' => DateUtils::daysAgo(30)]; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
$path = $this->buildPath(static::ACTIVITY_GET_ENDPOINT); |
475
|
|
|
|
476
|
|
|
return $this->performRequest('GET', $path, [ |
|
|
|
|
477
|
|
|
'query' => $params |
478
|
|
|
]); |
479
|
|
|
} |
480
|
|
|
} |
481
|
|
|
|