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\StringUtils; |
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_ENDPOINT, $collectionName); |
156
|
|
|
|
157
|
2 |
|
return $this->performRequest('GET', $path); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritdoc |
162
|
|
|
*/ |
163
|
|
|
public function getMessages($userId) |
164
|
|
|
{ |
165
|
|
|
$path = $this->buildPath(static::MESSAGES_USER_ENDPOINT, $userId); |
166
|
|
|
|
167
|
|
|
return $this->performRequest('GET', $path); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritdoc |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
public function createItem($tableName, array $data) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$path = $this->buildPath(static::TABLE_ENTRY_CREATE_ENDPOINT, $tableName); |
176
|
|
|
$data = $this->processData($tableName, $data); |
177
|
|
|
|
178
|
|
|
return $this->performRequest('POST', $path, ['body' => $data]); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @inheritdoc |
183
|
|
|
*/ |
184
|
|
View Code Duplication |
public function updateItem($tableName, $id, array $data) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$path = $this->buildPath(static::TABLE_ENTRY_UPDATE_ENDPOINT, [$tableName, $id]); |
187
|
|
|
$data = $this->processData($tableName, $data); |
188
|
|
|
|
189
|
|
|
return $this->performRequest('PUT', $path, ['body' => $data]); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @inheritdoc |
194
|
|
|
*/ |
195
|
|
|
public function deleteItem($tableName, $id) |
196
|
|
|
{ |
197
|
|
|
$path = $this->buildPath(static::TABLE_ENTRY_DELETE_ENDPOINT, [$tableName, $id]); |
198
|
|
|
|
199
|
|
|
return $this->performRequest('DELETE', $path); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @inheritdoc |
204
|
|
|
*/ |
205
|
|
|
public function createUser(array $data) |
206
|
|
|
{ |
207
|
|
|
return $this->createItem('directus_users', $data); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @inheritdoc |
212
|
|
|
*/ |
213
|
|
|
public function updateUser($id, array $data) |
214
|
|
|
{ |
215
|
|
|
return $this->updateItem('directus_users', $id, $data); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @inheritdoc |
220
|
|
|
*/ |
221
|
|
|
public function deleteUser($ids) |
222
|
|
|
{ |
223
|
|
|
return $this->deleteItem('directus_users', $ids); |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @inheritdoc |
228
|
|
|
*/ |
229
|
|
|
public function createFile(File $file) |
230
|
|
|
{ |
231
|
|
|
$data = $this->processFile($file); |
232
|
|
|
|
233
|
|
|
return $this->performRequest('POST', static::FILE_CREATE_ENDPOINT, ['body' => $data]); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @inheritdoc |
238
|
|
|
*/ |
239
|
|
|
public function updateFile($id, $data) |
240
|
|
|
{ |
241
|
|
|
if ($data instanceof File) { |
242
|
|
|
$data = $data->toArray(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$data['id'] = $id; |
246
|
|
|
$path = $this->buildPath(static::FILE_UPDATE_ENDPOINT, $id); |
247
|
|
|
$data = $this->processData('directus_files', $data); |
248
|
|
|
|
249
|
|
|
return $this->performRequest('POST', $path, ['body' => $data]); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @inheritdoc |
254
|
|
|
*/ |
255
|
|
|
public function deleteFile($id) |
256
|
|
|
{ |
257
|
|
|
return $this->deleteItem('directus_files', $id); |
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function createPreferences($data) |
261
|
|
|
{ |
262
|
|
|
$this->requiredAttributes(['title', 'table_name'], $data); |
263
|
|
|
|
264
|
|
|
$tableName = ArrayUtils::get($data, 'table_name'); |
265
|
|
|
$path = $this->buildPath(static::TABLE_PREFERENCES_ENDPOINT, $tableName); |
266
|
|
|
$data = $this->processData($tableName, $data); |
267
|
|
|
|
268
|
|
|
return $this->performRequest('POST', $path, ['body' => $data]); |
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @inheritdoc |
273
|
|
|
*/ |
274
|
|
|
public function createBookmark($data) |
275
|
|
|
{ |
276
|
|
|
$preferences = $this->createPreferences(ArrayUtils::pick($data, [ |
277
|
|
|
'title', 'table_name', 'sort', 'status', 'search_string', 'sort_order', 'columns_visible' |
278
|
|
|
])); |
279
|
|
|
|
280
|
|
|
$title = $preferences->title; |
|
|
|
|
281
|
|
|
$tableName = $preferences->table_name; |
|
|
|
|
282
|
|
|
$bookmarkData = [ |
283
|
|
|
'section' => 'search', |
284
|
|
|
'title' => $title, |
285
|
|
|
'url' => 'tables/' . $tableName . '/pref/' . $title |
286
|
|
|
]; |
287
|
|
|
|
288
|
|
|
$path = $this->buildPath(static::TABLE_BOOKMARKS_CREATE_ENDPOINT); |
289
|
|
|
$bookmarkData = $this->processData($tableName, $bookmarkData); |
290
|
|
|
|
291
|
|
|
return $this->performRequest('POST', $path, ['body' => $bookmarkData]); |
|
|
|
|
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @inheritdoc |
296
|
|
|
*/ |
297
|
|
|
public function createColumn($data) |
298
|
|
|
{ |
299
|
|
|
$data = $this->parseColumnData($data); |
300
|
|
|
|
301
|
|
|
return $this->performRequest('POST', $this->buildPath(static::COLUMN_CREATE_ENDPOINT, $data['table_name']), [ |
|
|
|
|
302
|
|
|
'body' => $data |
303
|
|
|
]); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @inheritdoc |
308
|
|
|
*/ |
309
|
|
|
public function createGroup(array $data) |
310
|
|
|
{ |
311
|
|
|
return $this->performRequest('POST', static::GROUP_CREATE_ENDPOINT, [ |
|
|
|
|
312
|
|
|
'body' => $data |
313
|
|
|
]); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @inheritdoc |
318
|
|
|
*/ |
319
|
|
|
public function createMessage(array $data) |
320
|
|
|
{ |
321
|
|
|
$this->requiredAttributes(['from', 'message', 'subject'], $data); |
322
|
|
|
$this->requiredOneAttribute(['to', 'toGroup'], $data); |
323
|
|
|
|
324
|
|
|
$data['recipients'] = $this->getMessagesTo($data); |
325
|
|
|
ArrayUtils::remove($data, ['to', 'toGroup']); |
326
|
|
|
|
327
|
|
|
return $this->performRequest('POST', static::MESSAGES_CREATE_ENDPOINT, [ |
|
|
|
|
328
|
|
|
'body' => $data |
329
|
|
|
]); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @inheritdoc |
334
|
|
|
*/ |
335
|
|
|
public function sendMessage(array $data) |
336
|
|
|
{ |
337
|
|
|
return $this->createMessage($data); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @inheritdoc |
342
|
|
|
*/ |
343
|
|
|
public function createPrivileges(array $data) |
344
|
|
|
{ |
345
|
|
|
$this->requiredAttributes(['group_id', 'table_name'], $data); |
346
|
|
|
|
347
|
|
|
return $this->performRequest('POST', static::GROUP_PRIVILEGES_CREATE_ENDPOINT, [ |
|
|
|
|
348
|
|
|
'body' => $data |
349
|
|
|
]); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
public function createTable($name, array $params = []) |
353
|
|
|
{ |
354
|
|
|
$data = [ |
355
|
|
|
'addTable' => true, |
356
|
|
|
'table_name' => $name |
357
|
|
|
]; |
358
|
|
|
|
359
|
|
|
return $this->performRequest('POST', static::TABLE_CREATE_ENDPOINT, [ |
|
|
|
|
360
|
|
|
'body' => $data |
361
|
|
|
]); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @inheritdoc |
366
|
|
|
*/ |
367
|
|
|
public function createColumnUIOptions(array $data) |
368
|
|
|
{ |
369
|
|
|
$this->requiredAttributes(['table', 'column', 'ui', 'options'], $data); |
370
|
|
|
|
371
|
|
|
$path = $this->buildPath(static::COLUMN_OPTIONS_CREATE_ENDPOINT, [ |
372
|
|
|
$data['table'], |
373
|
|
|
$data['column'], |
374
|
|
|
$data['ui'] |
375
|
|
|
]); |
376
|
|
|
|
377
|
|
|
$data = ArrayUtils::get($data, 'options'); |
378
|
|
|
|
379
|
|
|
return $this->performRequest('POST', $path, [ |
|
|
|
|
380
|
|
|
'body' => $data |
381
|
|
|
]); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @inheritdoc |
386
|
|
|
*/ |
387
|
|
|
public function getPreferences($table, $user = null) |
388
|
|
|
{ |
389
|
|
|
return $this->performRequest('POST', $this->buildPath(static::TABLE_PREFERENCES_ENDPOINT, $table)); |
|
|
|
|
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* @inheritdoc |
394
|
|
|
*/ |
395
|
|
|
public function deleteBookmark($id) |
396
|
|
|
{ |
397
|
|
|
return $this->deleteItem('directus_bookmarks', $id); |
|
|
|
|
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* @inheritdoc |
402
|
|
|
*/ |
403
|
|
|
public function deleteColumn($name, $table) |
404
|
|
|
{ |
405
|
|
|
$path = $this->buildPath(static::COLUMN_DELETE_ENDPOINT, [$name, $table]); |
406
|
|
|
|
407
|
|
|
return $this->performRequest('DELETE', $path); |
|
|
|
|
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @inheritdoc |
412
|
|
|
*/ |
413
|
|
|
public function deleteGroup($id) |
414
|
|
|
{ |
415
|
|
|
return $this->deleteItem('directus_groups', $id); |
|
|
|
|
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* @inheritdoc |
420
|
|
|
*/ |
421
|
|
|
public function deleteTable($name) |
422
|
|
|
{ |
423
|
|
|
$path = $this->buildPath(static::TABLE_DELETE_ENDPOINT, $name); |
424
|
|
|
|
425
|
|
|
return $this->performRequest('DELETE', $path); |
|
|
|
|
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|