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