|
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
|
|
|
use Zend\Db\Sql\Select; |
|
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 implements RequestsInterface |
|
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
|
|
|
* Gets the list of tables name in the database |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $params |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getTables(array $params = []) |
|
59
|
|
|
{ |
|
60
|
|
|
return TableSchema::getTablesSchema($params); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Gets all the columns in the database |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $params |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getColumns(array $params = []) |
|
71
|
|
|
{ |
|
72
|
|
|
return TableSchema::getColumnsSchema($params); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Gets table columns |
|
77
|
|
|
* |
|
78
|
|
|
* @param $tableName |
|
79
|
|
|
* @param array $params |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Directus\Database\Object\Column[] |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getTableColumns($tableName, array $params = []) |
|
84
|
|
|
{ |
|
85
|
|
|
$tables = TableSchema::getTableColumnsSchema($tableName, $params); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
return $tables; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Gets all the entries in the given table name |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $tableName |
|
94
|
|
|
* @param array $params |
|
95
|
|
|
* |
|
96
|
|
|
* @return Entry|EntryCollection |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getEntries($tableName, array $params = []) |
|
99
|
|
|
{ |
|
100
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
|
101
|
|
|
|
|
102
|
|
|
return $this->createResponseFromData($tableGateway->getEntries($params)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Gets an entry in the given table name with the given id |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $tableName |
|
109
|
|
|
* @param mixed $id |
|
110
|
|
|
* @param array $params |
|
111
|
|
|
* |
|
112
|
|
|
* @return array|mixed |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getEntry($tableName, $id, array $params = []) |
|
115
|
|
|
{ |
|
116
|
|
|
// @TODO: Dynamic ID |
|
117
|
|
|
return $this->getEntries($tableName, array_merge($params, [ |
|
|
|
|
|
|
118
|
|
|
'id' => $id |
|
119
|
|
|
])); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Gets the list of users |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $params |
|
126
|
|
|
* |
|
127
|
|
|
* @return array|mixed |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getUsers(array $params = []) |
|
130
|
|
|
{ |
|
131
|
|
|
// @TODO: store the directus tables somewhere (SchemaManager?) |
|
132
|
|
|
return $this->getEntries('directus_users', $params); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Gets an user by the given id |
|
137
|
|
|
* |
|
138
|
|
|
* @param $id |
|
139
|
|
|
* @param array $params |
|
140
|
|
|
* |
|
141
|
|
|
* @return array|mixed |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getUser($id, array $params = []) |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->getEntry('directus_users', $id, $params); |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @inheritDoc |
|
150
|
|
|
*/ |
|
151
|
|
|
public function fetchTables() |
|
152
|
|
|
{ |
|
153
|
|
|
// TODO: Implement fetchTables() method. |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @inheritDoc |
|
158
|
|
|
*/ |
|
159
|
|
|
public function fetchTableInfo($tableName) |
|
160
|
|
|
{ |
|
161
|
|
|
// TODO: Implement fetchTableInfo() method. |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @inheritDoc |
|
166
|
|
|
*/ |
|
167
|
|
|
public function fetchColumns($tableName) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
// TODO: Implement fetchColumns() method. |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @inheritDoc |
|
174
|
|
|
*/ |
|
175
|
|
|
public function fetchColumnInfo($tableName, $columnName) |
|
176
|
|
|
{ |
|
177
|
|
|
// TODO: Implement fetchColumnInfo() method. |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @inheritDoc |
|
182
|
|
|
*/ |
|
183
|
|
|
public function fetchItems($tableName = null, $conditions = []) |
|
184
|
|
|
{ |
|
185
|
|
|
if ($tableName == null) { |
|
186
|
|
|
$tableName = $this->getTable(); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$select = new Select($tableName); |
|
190
|
|
|
|
|
191
|
|
|
// Conditional to honor the active column, (does not check if column exists) |
|
192
|
|
|
if (isset($conditions['active'])) { |
|
193
|
|
|
$select->where->equalTo('active', $conditions['active']); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
// Order by "id desc" by default or by a parameter value |
|
197
|
|
|
if (isset($conditions['sort'])) { |
|
198
|
|
|
$select->order($conditions['sort']); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $this->selectWith($select); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @inheritDoc |
|
206
|
|
|
*/ |
|
207
|
|
|
public function fetchItem($tableName, $itemID) |
|
|
|
|
|
|
208
|
|
|
{ |
|
209
|
|
|
// TODO: Implement fetchItem() method. |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @inheritDoc |
|
214
|
|
|
*/ |
|
215
|
|
|
public function fetchGroups() |
|
216
|
|
|
{ |
|
217
|
|
|
// TODO: Implement fetchGroups() method. |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @inheritDoc |
|
222
|
|
|
*/ |
|
223
|
|
|
public function fetchGroupInfo($groupID) |
|
224
|
|
|
{ |
|
225
|
|
|
// TODO: Implement fetchGroupInfo() method. |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @inheritDoc |
|
230
|
|
|
*/ |
|
231
|
|
|
public function fetchGroupPrivileges($groupID) |
|
232
|
|
|
{ |
|
233
|
|
|
// TODO: Implement fetchGroupPrivileges() method. |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @inheritDoc |
|
238
|
|
|
*/ |
|
239
|
|
|
public function getFiles(array $params = []) |
|
240
|
|
|
{ |
|
241
|
|
|
return $this->getEntries('directus_files', $params); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @inheritDoc |
|
246
|
|
|
*/ |
|
247
|
|
|
public function getFile($id, array $params = []) |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->getEntry('directus_files', $id, $params); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @inheritDoc |
|
254
|
|
|
*/ |
|
255
|
|
|
public function fetchSettings() |
|
256
|
|
|
{ |
|
257
|
|
|
// TODO: Implement fetchSettings() method. |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @inheritDoc |
|
262
|
|
|
*/ |
|
263
|
|
|
public function fetchSettingCollection($collectionName) |
|
264
|
|
|
{ |
|
265
|
|
|
// TODO: Implement fetchSettingCollection() method. |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @inheritDoc |
|
270
|
|
|
*/ |
|
271
|
|
|
public function getMessages($userId) |
|
272
|
|
|
{ |
|
273
|
|
|
$messagesTableGateway = new DirectusMessagesTableGateway($this->connection, null); |
|
|
|
|
|
|
274
|
|
|
$result = $messagesTableGateway->fetchMessagesInboxWithHeaders($userId); |
|
275
|
|
|
|
|
276
|
|
|
return $this->createResponseFromData($result); |
|
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @inheritDoc |
|
281
|
|
|
*/ |
|
282
|
|
|
public function createEntry($tableName, array $data) |
|
283
|
|
|
{ |
|
284
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
|
285
|
|
|
$newRecord = $tableGateway->manageRecordUpdate($tableName, $data); |
|
286
|
|
|
|
|
287
|
|
|
return $this->createResponseFromData($newRecord->toArray()); |
|
|
|
|
|
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @inheritDoc |
|
292
|
|
|
*/ |
|
293
|
|
|
public function updateEntry($tableName, $id, array $data) |
|
294
|
|
|
{ |
|
295
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
|
296
|
|
|
$record = $tableGateway->manageRecordUpdate($tableName, array_merge($data, ['id' => $id])); |
|
297
|
|
|
|
|
298
|
|
|
return $this->createResponseFromData($record->toArray()); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* @inheritDoc |
|
303
|
|
|
*/ |
|
304
|
|
|
public function deleteEntry($tableName, $ids) |
|
305
|
|
|
{ |
|
306
|
|
|
// @TODO: Accept EntryCollection and Entry |
|
307
|
|
|
$tableGateway = $this->getTableGateway($tableName); |
|
308
|
|
|
|
|
309
|
|
|
if (!is_array($ids)) { |
|
310
|
|
|
$ids = [$ids]; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
return $tableGateway->delete(function($delete) use ($ids) { |
|
314
|
|
|
return $delete->where->in('id', $ids); |
|
315
|
|
|
}); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Get a table gateway for the given table name |
|
320
|
|
|
* |
|
321
|
|
|
* @param $tableName |
|
322
|
|
|
* |
|
323
|
|
|
* @return RelationalTableGateway |
|
324
|
|
|
*/ |
|
325
|
|
|
protected function getTableGateway($tableName) |
|
326
|
|
|
{ |
|
327
|
|
|
if (!array_key_exists($tableName, $this->tableGateways)) { |
|
328
|
|
|
$acl = TableSchema::getAclInstance(); |
|
329
|
|
|
$this->tableGateways[$tableName] = new RelationalTableGateway($tableName, $this->connection, $acl); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
return $this->tableGateways[$tableName]; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
// @TODO: move to a builder class |
|
336
|
|
|
protected function createResponseFromData($data) |
|
337
|
|
|
{ |
|
338
|
|
|
if (isset($data['rows'])) { |
|
339
|
|
|
$response = new EntryCollection($data); |
|
340
|
|
|
} else { |
|
341
|
|
|
$response = new Entry($data); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
return $response; |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.