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\DirectusMessagesTableGateway; |
16
|
|
|
use Directus\Database\TableGateway\RelationalTableGateway; |
17
|
|
|
use Directus\Database\TableSchema; |
18
|
|
|
use Directus\SDK\Response\EntryCollection; |
19
|
|
|
use Directus\SDK\Response\Entry; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Client Local |
23
|
|
|
* |
24
|
|
|
* Client to Interact with the database directly using Directus Database Component |
25
|
|
|
* |
26
|
|
|
* @author Welling Guzmán <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ClientLocal implements RequestsInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var BaseTableGateway[] |
32
|
|
|
*/ |
33
|
|
|
protected $tableGateways = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Connection |
37
|
|
|
*/ |
38
|
|
|
protected $connection = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* ClientLocal constructor. |
42
|
|
|
* |
43
|
|
|
* @param $connection |
44
|
|
|
*/ |
45
|
|
|
public function __construct($connection) |
46
|
|
|
{ |
47
|
|
|
$this->connection = $connection; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
53
|
|
|
public function getTables(array $params = []) |
54
|
|
|
{ |
55
|
|
|
return $this->createResponseFromData(TableSchema::getTablesSchema($params)); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
|
|
public function getTable($tableName) |
62
|
|
|
{ |
63
|
|
|
return $this->createResponseFromData(TableSchema::getSchemaArray($tableName)); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public function getColumns($tableName, array $params = []) |
70
|
|
|
{ |
71
|
|
|
return $this->createResponseFromData(TableSchema::getTableColumnsSchema($tableName, $params)); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
public function getColumn($tableName, $columnName) |
78
|
|
|
{ |
79
|
|
|
return $this->createResponseFromData(TableSchema::getColumnSchemaArray($tableName, $columnName)); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
|
|
public function getEntries($tableName, array $params = []) |
86
|
|
|
{ |
87
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
88
|
|
|
|
89
|
|
|
return $this->createResponseFromData($tableGateway->getEntries($params)); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc |
94
|
|
|
*/ |
95
|
|
|
public function getEntry($tableName, $id, array $params = []) |
96
|
|
|
{ |
97
|
|
|
// @TODO: Dynamic ID |
98
|
|
|
return $this->getEntries($tableName, array_merge($params, [ |
|
|
|
|
99
|
|
|
'id' => $id |
100
|
|
|
])); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritDoc |
105
|
|
|
*/ |
106
|
|
|
public function getUsers(array $params = []) |
107
|
|
|
{ |
108
|
|
|
// @TODO: store the directus tables somewhere (SchemaManager?) |
109
|
|
|
return $this->getEntries('directus_users', $params); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritDoc |
114
|
|
|
*/ |
115
|
|
|
public function getUser($id, array $params = []) |
116
|
|
|
{ |
117
|
|
|
return $this->getEntry('directus_users', $id, $params); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritDoc |
122
|
|
|
*/ |
123
|
|
|
public function getGroups(array $params = []) |
124
|
|
|
{ |
125
|
|
|
return $this->getEntries('directus_groups', $params); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritDoc |
130
|
|
|
*/ |
131
|
|
|
public function getGroup($id, array $params = []) |
132
|
|
|
{ |
133
|
|
|
return $this->getEntry('directus_groups', $id, $params); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @inheritDoc |
138
|
|
|
*/ |
139
|
|
|
public function getGroupPrivileges($groupID) |
140
|
|
|
{ |
141
|
|
|
$this->getEntries('directus_privileges', [ |
142
|
|
|
'filter' => [ |
143
|
|
|
'group_id' => ['eq' => $groupID] |
144
|
|
|
] |
145
|
|
|
]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @inheritDoc |
150
|
|
|
*/ |
151
|
|
|
public function getFiles(array $params = []) |
152
|
|
|
{ |
153
|
|
|
return $this->getEntries('directus_files', $params); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
|
|
public function getFile($id, array $params = []) |
160
|
|
|
{ |
161
|
|
|
return $this->getEntry('directus_files', $id, $params); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @inheritDoc |
166
|
|
|
*/ |
167
|
|
|
public function getSettings() |
168
|
|
|
{ |
169
|
|
|
return $this->getEntries('directus_settings'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritDoc |
174
|
|
|
*/ |
175
|
|
|
public function getSettingsByCollection($collectionName) |
176
|
|
|
{ |
177
|
|
|
return $this->getEntries('directus_settings', [ |
178
|
|
|
'filter' => [ |
179
|
|
|
'collection' => ['eq' => $collectionName] |
180
|
|
|
] |
181
|
|
|
]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @inheritDoc |
186
|
|
|
*/ |
187
|
|
|
public function getMessages($userId) |
188
|
|
|
{ |
189
|
|
|
$messagesTableGateway = new DirectusMessagesTableGateway($this->connection, null); |
|
|
|
|
190
|
|
|
$result = $messagesTableGateway->fetchMessagesInboxWithHeaders($userId); |
191
|
|
|
|
192
|
|
|
return $this->createResponseFromData($result); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @inheritDoc |
197
|
|
|
*/ |
198
|
|
|
public function createEntry($tableName, array $data) |
199
|
|
|
{ |
200
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
201
|
|
|
$newRecord = $tableGateway->manageRecordUpdate($tableName, $data); |
202
|
|
|
|
203
|
|
|
return $this->createResponseFromData($newRecord->toArray()); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @inheritDoc |
208
|
|
|
*/ |
209
|
|
|
public function updateEntry($tableName, $id, array $data) |
210
|
|
|
{ |
211
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
212
|
|
|
$record = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id])); |
213
|
|
|
|
214
|
|
|
return $this->createResponseFromData($record->toArray()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @inheritDoc |
219
|
|
|
*/ |
220
|
|
|
public function deleteEntry($tableName, $ids) |
221
|
|
|
{ |
222
|
|
|
// @TODO: Accept EntryCollection and Entry |
223
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
224
|
|
|
|
225
|
|
|
if (!is_array($ids)) { |
226
|
|
|
$ids = [$ids]; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $tableGateway->delete(function($delete) use ($ids) { |
230
|
|
|
return $delete->where->in('id', $ids); |
231
|
|
|
}); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @inheritDoc |
236
|
|
|
*/ |
237
|
|
|
public function createUser(array $data) |
238
|
|
|
{ |
239
|
|
|
return $this->createEntry('directus_users', $data); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @inheritDoc |
244
|
|
|
*/ |
245
|
|
|
public function updateUser($id, array $data) |
246
|
|
|
{ |
247
|
|
|
return $this->updateEntry('directus_users', $id, $data); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @inheritDoc |
252
|
|
|
*/ |
253
|
|
|
public function deleteUser($ids) |
254
|
|
|
{ |
255
|
|
|
return $this->deleteEntry('directus_users', $ids); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @inheritDoc |
260
|
|
|
*/ |
261
|
|
|
public function createFile(array $data) |
262
|
|
|
{ |
263
|
|
|
return $this->createEntry('directus_files', $data); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @inheritDoc |
268
|
|
|
*/ |
269
|
|
|
public function updateFile($id, array $data) |
270
|
|
|
{ |
271
|
|
|
return $this->updateEntry('directus_files', $id, $data); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @inheritDoc |
276
|
|
|
*/ |
277
|
|
|
public function deleteFile($ids) |
278
|
|
|
{ |
279
|
|
|
return $this->deleteEntry('directus_files', $ids); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Get a table gateway for the given table name |
285
|
|
|
* |
286
|
|
|
* @param $tableName |
287
|
|
|
* |
288
|
|
|
* @return RelationalTableGateway |
289
|
|
|
*/ |
290
|
|
|
protected function getTableGateway($tableName) |
291
|
|
|
{ |
292
|
|
|
if (!array_key_exists($tableName, $this->tableGateways)) { |
293
|
|
|
$acl = TableSchema::getAclInstance(); |
294
|
|
|
$this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return $this->tableGateways[$tableName]; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
// @TODO: move to a builder class |
301
|
|
|
protected function createResponseFromData($data) |
302
|
|
|
{ |
303
|
|
|
if (isset($data['rows'])) { |
304
|
|
|
$response = new EntryCollection($data); |
305
|
|
|
} else { |
306
|
|
|
$response = new Entry($data); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $response; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|