1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015 |
6
|
|
|
* @package Admin |
7
|
|
|
* @subpackage JsonAdm |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Admin\JsonAdm; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* JSON API common client |
16
|
|
|
* |
17
|
|
|
* @package Admin |
18
|
|
|
* @subpackage JsonAdm |
19
|
|
|
*/ |
20
|
|
|
class Base |
21
|
|
|
{ |
22
|
|
|
private $view; |
23
|
|
|
private $context; |
24
|
|
|
private $templatePaths; |
25
|
|
|
private $path; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initializes the client |
30
|
|
|
* |
31
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context MShop context object |
32
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
33
|
|
|
* @param array $templatePaths List of file system paths where the templates are stored |
34
|
|
|
* @param string $path Name of the client separated by slashes, e.g "product/stock" |
35
|
|
|
* @return void |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MW\View\Iface $view, array $templatePaths, $path ) |
38
|
|
|
{ |
39
|
|
|
$this->view = $view; |
40
|
|
|
$this->context = $context; |
41
|
|
|
$this->templatePaths = $templatePaths; |
42
|
|
|
$this->path = $path; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Deletes the resource or the resource list |
48
|
|
|
* |
49
|
|
|
* @param string $body Request body |
50
|
|
|
* @param array &$header Variable which contains the HTTP headers and the new ones afterwards |
51
|
|
|
* @param integer &$status Variable which contains the HTTP status afterwards |
52
|
|
|
* @return string Content for response body |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function delete( $body, array &$header, &$status ) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' ); |
57
|
|
|
$context = $this->getContext(); |
58
|
|
|
$view = $this->getView(); |
59
|
|
|
|
60
|
|
|
try |
61
|
|
|
{ |
62
|
|
|
$view = $this->deleteItems( $view, $body ); |
63
|
|
|
$status = 200; |
64
|
|
|
} |
65
|
|
|
catch( \Aimeos\Admin\JsonAdm\Exception $e ) |
66
|
|
|
{ |
67
|
|
|
$status = $e->getCode(); |
68
|
|
|
$view->errors = array( array( |
|
|
|
|
69
|
|
|
'title' => $context->getI18n()->dt( 'admin/jsonadm', $e->getMessage() ), |
70
|
|
|
'detail' => $e->getTraceAsString(), |
71
|
|
|
) ); |
72
|
|
|
} |
73
|
|
|
catch( \Aimeos\MAdmin\Exception $e ) |
74
|
|
|
{ |
75
|
|
|
$status = 404; |
76
|
|
|
$view->errors = array( array( |
|
|
|
|
77
|
|
|
'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ), |
78
|
|
|
'detail' => $e->getTraceAsString(), |
79
|
|
|
) ); |
80
|
|
|
} |
81
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
82
|
|
|
{ |
83
|
|
|
$status = 404; |
84
|
|
|
$view->errors = array( array( |
|
|
|
|
85
|
|
|
'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ), |
86
|
|
|
'detail' => $e->getTraceAsString(), |
87
|
|
|
) ); |
88
|
|
|
} |
89
|
|
|
catch( \Exception $e ) |
90
|
|
|
{ |
91
|
|
|
$status = 500; |
92
|
|
|
$view->errors = array( array( |
|
|
|
|
93
|
|
|
'title' => $e->getMessage(), |
94
|
|
|
'detail' => $e->getTraceAsString(), |
95
|
|
|
) ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** admin/jsonadm/standard/template-delete |
99
|
|
|
* Relative path to the JSON API template for DELETE requests |
100
|
|
|
* |
101
|
|
|
* The template file contains the code and processing instructions |
102
|
|
|
* to generate the result shown in the JSON API body. The |
103
|
|
|
* configuration string is the path to the template file relative |
104
|
|
|
* to the templates directory (usually in admin/jsonadm/templates). |
105
|
|
|
* |
106
|
|
|
* You can overwrite the template file configuration in extensions and |
107
|
|
|
* provide alternative templates. These alternative templates should be |
108
|
|
|
* named like the default one but with the string "standard" replaced by |
109
|
|
|
* an unique name. You may use the name of your project for this. If |
110
|
|
|
* you've implemented an alternative client class as well, "standard" |
111
|
|
|
* should be replaced by the name of the new class. |
112
|
|
|
* |
113
|
|
|
* @param string Relative path to the template creating the body for the DELETE method of the JSON API |
114
|
|
|
* @since 2015.12 |
115
|
|
|
* @category Developer |
116
|
|
|
* @see admin/jsonadm/standard/template-get |
117
|
|
|
* @see admin/jsonadm/standard/template-patch |
118
|
|
|
* @see admin/jsonadm/standard/template-post |
119
|
|
|
* @see admin/jsonadm/standard/template-put |
120
|
|
|
* @see admin/jsonadm/standard/template-options |
121
|
|
|
*/ |
122
|
|
|
$tplconf = 'admin/jsonadm/standard/template-delete'; |
123
|
|
|
$default = 'delete-default.php'; |
124
|
|
|
|
125
|
|
|
return $view->render( $view->config( $tplconf, $default ) ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the requested resource or the resource list |
131
|
|
|
* |
132
|
|
|
* @param string $body Request body |
133
|
|
|
* @param array &$header Variable which contains the HTTP headers and the new ones afterwards |
134
|
|
|
* @param integer &$status Variable which contains the HTTP status afterwards |
135
|
|
|
* @return string Content for response body |
136
|
|
|
*/ |
137
|
|
|
public function get( $body, array &$header, &$status ) |
138
|
|
|
{ |
139
|
|
|
$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' ); |
140
|
|
|
$context = $this->getContext(); |
141
|
|
|
$view = $this->getView(); |
142
|
|
|
$total = 1; |
143
|
|
|
|
144
|
|
|
try |
145
|
|
|
{ |
146
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, $this->getPath() ); |
147
|
|
|
$include = ( ( $include = $view->param( 'include' ) ) !== null ? explode( ',', $include ) : array() ); |
148
|
|
|
|
149
|
|
|
if( ( $id = $view->param( 'id' ) ) == null ) |
150
|
|
|
{ |
151
|
|
|
$search = $this->initCriteria( $manager->createSearch(), $view->param() ); |
152
|
|
|
$view->data = $manager->searchItems( $search, array(), $total ); |
|
|
|
|
153
|
|
|
$view->childItems = $this->getChildItems( $view->data, $include ); |
|
|
|
|
154
|
|
|
$view->listItems = $this->getListItems( $view->data, $include ); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
else |
157
|
|
|
{ |
158
|
|
|
$view->data = $manager->getItem( $id, array() ); |
|
|
|
|
159
|
|
|
$view->childItems = $this->getChildItems( array( $id => $view->data ), $include ); |
|
|
|
|
160
|
|
|
$view->listItems = $this->getListItems( array( $id => $view->data ), $include ); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$view->refItems = $this->getRefItems( $view->listItems ); |
|
|
|
|
164
|
|
|
|
165
|
|
|
$view->total = $total; |
|
|
|
|
166
|
|
|
$status = 200; |
167
|
|
|
} |
168
|
|
|
catch( \Aimeos\MAdmin\Exception $e ) |
169
|
|
|
{ |
170
|
|
|
$status = 404; |
171
|
|
|
$view->errors = array( array( |
|
|
|
|
172
|
|
|
'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ), |
173
|
|
|
'detail' => $e->getTraceAsString(), |
174
|
|
|
) ); |
175
|
|
|
} |
176
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
177
|
|
|
{ |
178
|
|
|
$status = 404; |
179
|
|
|
$view->errors = array( array( |
|
|
|
|
180
|
|
|
'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ), |
181
|
|
|
'detail' => $e->getTraceAsString(), |
182
|
|
|
) ); |
183
|
|
|
} |
184
|
|
|
catch( \Exception $e ) |
185
|
|
|
{ |
186
|
|
|
$status = 500; |
187
|
|
|
$view->errors = array( array( |
|
|
|
|
188
|
|
|
'title' => $e->getMessage(), |
189
|
|
|
'detail' => $e->getTraceAsString(), |
190
|
|
|
) ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** admin/jsonadm/standard/template-get |
194
|
|
|
* Relative path to the JSON API template for GET requests |
195
|
|
|
* |
196
|
|
|
* The template file contains the code and processing instructions |
197
|
|
|
* to generate the result shown in the JSON API body. The |
198
|
|
|
* configuration string is the path to the template file relative |
199
|
|
|
* to the templates directory (usually in admin/jsonadm/templates). |
200
|
|
|
* |
201
|
|
|
* You can overwrite the template file configuration in extensions and |
202
|
|
|
* provide alternative templates. These alternative templates should be |
203
|
|
|
* named like the default one but with the string "standard" replaced by |
204
|
|
|
* an unique name. You may use the name of your project for this. If |
205
|
|
|
* you've implemented an alternative client class as well, "standard" |
206
|
|
|
* should be replaced by the name of the new class. |
207
|
|
|
* |
208
|
|
|
* @param string Relative path to the template creating the body for the GET method of the JSON API |
209
|
|
|
* @since 2015.12 |
210
|
|
|
* @category Developer |
211
|
|
|
* @see admin/jsonadm/standard/template-delete |
212
|
|
|
* @see admin/jsonadm/standard/template-patch |
213
|
|
|
* @see admin/jsonadm/standard/template-post |
214
|
|
|
* @see admin/jsonadm/standard/template-put |
215
|
|
|
* @see admin/jsonadm/standard/template-options |
216
|
|
|
*/ |
217
|
|
|
$tplconf = 'admin/jsonadm/standard/template-get'; |
218
|
|
|
$default = 'get-default.php'; |
219
|
|
|
|
220
|
|
|
return $view->render( $view->config( $tplconf, $default ) ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Updates the resource or the resource list partitially |
226
|
|
|
* |
227
|
|
|
* @param string $body Request body |
228
|
|
|
* @param array &$header Variable which contains the HTTP headers and the new ones afterwards |
229
|
|
|
* @param integer &$status Variable which contains the HTTP status afterwards |
230
|
|
|
* @return string Content for response body |
231
|
|
|
*/ |
232
|
|
View Code Duplication |
public function patch( $body, array &$header, &$status ) |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' ); |
235
|
|
|
$context = $this->getContext(); |
236
|
|
|
$view = $this->getView(); |
237
|
|
|
|
238
|
|
|
try |
239
|
|
|
{ |
240
|
|
|
$view = $this->patchItems( $view, $body, $header ); |
241
|
|
|
$status = 200; |
242
|
|
|
} |
243
|
|
|
catch( \Aimeos\Admin\JsonAdm\Exception $e ) |
244
|
|
|
{ |
245
|
|
|
$status = $e->getCode(); |
246
|
|
|
$view->errors = array( array( |
|
|
|
|
247
|
|
|
'title' => $context->getI18n()->dt( 'admin/jsonadm', $e->getMessage() ), |
248
|
|
|
'detail' => $e->getTraceAsString(), |
249
|
|
|
) ); |
250
|
|
|
} |
251
|
|
|
catch( \Aimeos\MAdmin\Exception $e ) |
252
|
|
|
{ |
253
|
|
|
$status = 404; |
254
|
|
|
$view->errors = array( array( |
|
|
|
|
255
|
|
|
'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ), |
256
|
|
|
'detail' => $e->getTraceAsString(), |
257
|
|
|
) ); |
258
|
|
|
} |
259
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
260
|
|
|
{ |
261
|
|
|
$status = 404; |
262
|
|
|
$view->errors = array( array( |
|
|
|
|
263
|
|
|
'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ), |
264
|
|
|
'detail' => $e->getTraceAsString(), |
265
|
|
|
) ); |
266
|
|
|
} |
267
|
|
|
catch( \Aimeos\MW\Exception $e ) |
268
|
|
|
{ |
269
|
|
|
$status = 500; |
270
|
|
|
$view->errors = array( array( |
|
|
|
|
271
|
|
|
'title' => $e->getMessage(), |
272
|
|
|
'detail' => $e->getTraceAsString(), |
273
|
|
|
) ); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** admin/jsonadm/standard/template-patch |
277
|
|
|
* Relative path to the JSON API template for PATCH requests |
278
|
|
|
* |
279
|
|
|
* The template file contains the code and processing instructions |
280
|
|
|
* to generate the result shown in the JSON API body. The |
281
|
|
|
* configuration string is the path to the template file relative |
282
|
|
|
* to the templates directory (usually in admin/jsonadm/templates). |
283
|
|
|
* |
284
|
|
|
* You can overwrite the template file configuration in extensions and |
285
|
|
|
* provide alternative templates. These alternative templates should be |
286
|
|
|
* named like the default one but with the string "standard" replaced by |
287
|
|
|
* an unique name. You may use the name of your project for this. If |
288
|
|
|
* you've implemented an alternative client class as well, "standard" |
289
|
|
|
* should be replaced by the name of the new class. |
290
|
|
|
* |
291
|
|
|
* @param string Relative path to the template creating the body for the PATCH method of the JSON API |
292
|
|
|
* @since 2015.12 |
293
|
|
|
* @category Developer |
294
|
|
|
* @see admin/jsonadm/standard/template-get |
295
|
|
|
* @see admin/jsonadm/standard/template-post |
296
|
|
|
* @see admin/jsonadm/standard/template-delete |
297
|
|
|
* @see admin/jsonadm/standard/template-put |
298
|
|
|
* @see admin/jsonadm/standard/template-options |
299
|
|
|
*/ |
300
|
|
|
$tplconf = 'admin/jsonadm/standard/template-patch'; |
301
|
|
|
$default = 'patch-default.php'; |
302
|
|
|
|
303
|
|
|
return $view->render( $view->config( $tplconf, $default ) ); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Creates or updates the resource or the resource list |
309
|
|
|
* |
310
|
|
|
* @param string $body Request body |
311
|
|
|
* @param array &$header Variable which contains the HTTP headers and the new ones afterwards |
312
|
|
|
* @param integer &$status Variable which contains the HTTP status afterwards |
313
|
|
|
* @return string Content for response body |
314
|
|
|
*/ |
315
|
|
View Code Duplication |
public function post( $body, array &$header, &$status ) |
|
|
|
|
316
|
|
|
{ |
317
|
|
|
$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' ); |
318
|
|
|
$context = $this->getContext(); |
319
|
|
|
$view = $this->getView(); |
320
|
|
|
|
321
|
|
|
try |
322
|
|
|
{ |
323
|
|
|
$view = $this->postItems( $view, $body, $header ); |
324
|
|
|
$status = 201; |
325
|
|
|
} |
326
|
|
|
catch( \Aimeos\Admin\JsonAdm\Exception $e ) |
327
|
|
|
{ |
328
|
|
|
$status = $e->getCode(); |
329
|
|
|
$view->errors = array( array( |
|
|
|
|
330
|
|
|
'title' => $context->getI18n()->dt( 'admin/jsonadm', $e->getMessage() ), |
331
|
|
|
'detail' => $e->getTraceAsString(), |
332
|
|
|
) ); |
333
|
|
|
} |
334
|
|
|
catch( \Aimeos\MAdmin\Exception $e ) |
335
|
|
|
{ |
336
|
|
|
$status = 404; |
337
|
|
|
$view->errors = array( array( |
|
|
|
|
338
|
|
|
'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ), |
339
|
|
|
'detail' => $e->getTraceAsString(), |
340
|
|
|
) ); |
341
|
|
|
} |
342
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
343
|
|
|
{ |
344
|
|
|
$status = 404; |
345
|
|
|
$view->errors = array( array( |
|
|
|
|
346
|
|
|
'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ), |
347
|
|
|
'detail' => $e->getTraceAsString(), |
348
|
|
|
) ); |
349
|
|
|
} |
350
|
|
|
catch( \Exception $e ) |
351
|
|
|
{ |
352
|
|
|
$status = 500; |
353
|
|
|
$view->errors = array( array( |
|
|
|
|
354
|
|
|
'title' => $e->getMessage(), |
355
|
|
|
'detail' => $e->getTraceAsString(), |
356
|
|
|
) ); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** admin/jsonadm/standard/template-post |
360
|
|
|
* Relative path to the JSON API template for POST requests |
361
|
|
|
* |
362
|
|
|
* The template file contains the code and processing instructions |
363
|
|
|
* to generate the result shown in the JSON API body. The |
364
|
|
|
* configuration string is the path to the template file relative |
365
|
|
|
* to the templates directory (usually in admin/jsonadm/templates). |
366
|
|
|
* |
367
|
|
|
* You can overwrite the template file configuration in extensions and |
368
|
|
|
* provide alternative templates. These alternative templates should be |
369
|
|
|
* named like the default one but with the string "standard" replaced by |
370
|
|
|
* an unique name. You may use the name of your project for this. If |
371
|
|
|
* you've implemented an alternative client class as well, "standard" |
372
|
|
|
* should be replaced by the name of the new class. |
373
|
|
|
* |
374
|
|
|
* @param string Relative path to the template creating the body for the POST method of the JSON API |
375
|
|
|
* @since 2015.12 |
376
|
|
|
* @category Developer |
377
|
|
|
* @see admin/jsonadm/standard/template-get |
378
|
|
|
* @see admin/jsonadm/standard/template-patch |
379
|
|
|
* @see admin/jsonadm/standard/template-delete |
380
|
|
|
* @see admin/jsonadm/standard/template-put |
381
|
|
|
* @see admin/jsonadm/standard/template-options |
382
|
|
|
*/ |
383
|
|
|
$tplconf = 'admin/jsonadm/standard/template-post'; |
384
|
|
|
$default = 'post-default.php'; |
385
|
|
|
|
386
|
|
|
return $view->render( $view->config( $tplconf, $default ) ); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Creates or updates the resource or the resource list |
392
|
|
|
* |
393
|
|
|
* @param string $body Request body |
394
|
|
|
* @param array &$header Variable which contains the HTTP headers and the new ones afterwards |
395
|
|
|
* @param integer &$status Variable which contains the HTTP status afterwards |
396
|
|
|
* @return string Content for response body |
397
|
|
|
*/ |
398
|
|
|
public function put( $body, array &$header, &$status ) |
399
|
|
|
{ |
400
|
|
|
$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' ); |
401
|
|
|
$status = 501; |
402
|
|
|
|
403
|
|
|
$context = $this->getContext(); |
404
|
|
|
$view = $this->getView(); |
405
|
|
|
|
406
|
|
|
$view->errors = array( array( |
|
|
|
|
407
|
|
|
'title' => $context->getI18n()->dt( 'admin/jsonadm', 'Not implemented, use PATCH instead' ), |
408
|
|
|
) ); |
409
|
|
|
|
410
|
|
|
/** admin/jsonadm/standard/template-put |
411
|
|
|
* Relative path to the JSON API template for PUT requests |
412
|
|
|
* |
413
|
|
|
* The template file contains the code and processing instructions |
414
|
|
|
* to generate the result shown in the JSON API body. The |
415
|
|
|
* configuration string is the path to the template file relative |
416
|
|
|
* to the templates directory (usually in admin/jsonadm/templates). |
417
|
|
|
* |
418
|
|
|
* You can overwrite the template file configuration in extensions and |
419
|
|
|
* provide alternative templates. These alternative templates should be |
420
|
|
|
* named like the default one but with the string "standard" replaced by |
421
|
|
|
* an unique name. You may use the name of your project for this. If |
422
|
|
|
* you've implemented an alternative client class as well, "standard" |
423
|
|
|
* should be replaced by the name of the new class. |
424
|
|
|
* |
425
|
|
|
* @param string Relative path to the template creating the body for the PUT method of the JSON API |
426
|
|
|
* @since 2015.12 |
427
|
|
|
* @category Developer |
428
|
|
|
* @see admin/jsonadm/standard/template-delete |
429
|
|
|
* @see admin/jsonadm/standard/template-patch |
430
|
|
|
* @see admin/jsonadm/standard/template-post |
431
|
|
|
* @see admin/jsonadm/standard/template-get |
432
|
|
|
* @see admin/jsonadm/standard/template-options |
433
|
|
|
*/ |
434
|
|
|
$tplconf = 'admin/jsonadm/standard/template-put'; |
435
|
|
|
$default = 'put-default.php'; |
436
|
|
|
|
437
|
|
|
return $view->render( $view->config( $tplconf, $default ) ); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Returns the available REST verbs and the available resources |
443
|
|
|
* |
444
|
|
|
* @param string $body Request body |
445
|
|
|
* @param array &$header Variable which contains the HTTP headers and the new ones afterwards |
446
|
|
|
* @param integer &$status Variable which contains the HTTP status afterwards |
447
|
|
|
* @return string Content for response body |
448
|
|
|
*/ |
449
|
|
|
public function options( $body, array &$header, &$status ) |
450
|
|
|
{ |
451
|
|
|
$context = $this->getContext(); |
452
|
|
|
$view = $this->getView(); |
453
|
|
|
|
454
|
|
|
try |
455
|
|
|
{ |
456
|
|
|
$resources = $attributes = array(); |
457
|
|
|
|
458
|
|
|
if( ( $domains = $view->param( 'resource' ) ) == '' ) |
459
|
|
|
{ |
460
|
|
|
/** admin/jsonadm/domains |
461
|
|
|
* A list of domain names whose clients are available for the JSON API |
462
|
|
|
* |
463
|
|
|
* The HTTP OPTIONS method returns a list of resources known by the |
464
|
|
|
* JSON API including their URLs. The list of available resources |
465
|
|
|
* can be exteded dynamically be implementing a new Jsonadm client |
466
|
|
|
* class handling request for this new domain. |
467
|
|
|
* |
468
|
|
|
* To add the new domain client to the list of resources returned |
469
|
|
|
* by the HTTP OPTIONS method, you have to add its name in lower case |
470
|
|
|
* to the existing configuration. |
471
|
|
|
* |
472
|
|
|
* @param array List of domain names |
473
|
|
|
* @since 2016.01 |
474
|
|
|
* @category Developer |
475
|
|
|
*/ |
476
|
|
|
$default = array( |
477
|
|
|
'attribute', 'catalog', 'coupon', 'customer', 'locale', 'media', |
478
|
|
|
'order', 'plugin', 'price', 'product', 'service', 'supplier', 'tag', 'text' |
479
|
|
|
); |
480
|
|
|
$domains = $context->getConfig()->get( 'admin/jsonadm/domains', $default ); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
foreach( (array) $domains as $domain ) |
484
|
|
|
{ |
485
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, $domain ); |
486
|
|
|
$resources = array_merge( $resources, $manager->getResourceType( true ) ); |
487
|
|
|
$attributes = array_merge( $attributes, $manager->getSearchAttributes( true ) ); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
$view->resources = $resources; |
|
|
|
|
491
|
|
|
$view->attributes = $attributes; |
|
|
|
|
492
|
|
|
|
493
|
|
|
$header = array( |
494
|
|
|
'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"', |
495
|
|
|
'Allow' => 'DELETE,GET,POST,OPTIONS' |
496
|
|
|
); |
497
|
|
|
$status = 200; |
498
|
|
|
} |
499
|
|
|
catch( \Aimeos\MAdmin\Exception $e ) |
500
|
|
|
{ |
501
|
|
|
$status = 404; |
502
|
|
|
$view->errors = array( array( |
|
|
|
|
503
|
|
|
'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ), |
504
|
|
|
'detail' => $e->getTraceAsString(), |
505
|
|
|
) ); |
506
|
|
|
} |
507
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
508
|
|
|
{ |
509
|
|
|
$status = 404; |
510
|
|
|
$view->errors = array( array( |
|
|
|
|
511
|
|
|
'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ), |
512
|
|
|
'detail' => $e->getTraceAsString(), |
513
|
|
|
) ); |
514
|
|
|
} |
515
|
|
|
catch( \Exception $e ) |
516
|
|
|
{ |
517
|
|
|
$status = 500; |
518
|
|
|
$view->errors = array( array( |
|
|
|
|
519
|
|
|
'title' => $e->getMessage(), |
520
|
|
|
'detail' => $e->getTraceAsString(), |
521
|
|
|
) ); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** admin/jsonadm/standard/template-options |
525
|
|
|
* Relative path to the JSON API template for OPTIONS requests |
526
|
|
|
* |
527
|
|
|
* The template file contains the code and processing instructions |
528
|
|
|
* to generate the result shown in the JSON API body. The |
529
|
|
|
* configuration string is the path to the template file relative |
530
|
|
|
* to the templates directory (usually in admin/jsonadm/templates). |
531
|
|
|
* |
532
|
|
|
* You can overwrite the template file configuration in extensions and |
533
|
|
|
* provide alternative templates. These alternative templates should be |
534
|
|
|
* named like the default one but with the string "standard" replaced by |
535
|
|
|
* an unique name. You may use the name of your project for this. If |
536
|
|
|
* you've implemented an alternative client class as well, "standard" |
537
|
|
|
* should be replaced by the name of the new class. |
538
|
|
|
* |
539
|
|
|
* @param string Relative path to the template creating the body for the OPTIONS method of the JSON API |
540
|
|
|
* @since 2015.12 |
541
|
|
|
* @category Developer |
542
|
|
|
* @see admin/jsonadm/standard/template-delete |
543
|
|
|
* @see admin/jsonadm/standard/template-patch |
544
|
|
|
* @see admin/jsonadm/standard/template-post |
545
|
|
|
* @see admin/jsonadm/standard/template-get |
546
|
|
|
* @see admin/jsonadm/standard/template-put |
547
|
|
|
*/ |
548
|
|
|
$tplconf = 'admin/jsonadm/standard/template-options'; |
549
|
|
|
$default = 'options-default.php'; |
550
|
|
|
|
551
|
|
|
return $view->render( $view->config( $tplconf, $default ) ); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* Deletes one or more items |
557
|
|
|
* |
558
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance with "param" view helper |
559
|
|
|
* @param string $body Request body |
560
|
|
|
* @return \Aimeos\MW\View\Iface $view View object that will contain the "total" property afterwards |
561
|
|
|
* @throws \Aimeos\Admin\JsonAdm\Exception If the request body is invalid |
562
|
|
|
*/ |
563
|
|
|
protected function deleteItems( \Aimeos\MW\View\Iface $view, $body ) |
564
|
|
|
{ |
565
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() ); |
566
|
|
|
|
567
|
|
|
if( ( $id = $view->param( 'id' ) ) == null ) |
568
|
|
|
{ |
569
|
|
View Code Duplication |
if( ( $request = json_decode( $body ) ) === null || !isset( $request->data ) || !is_array( $request->data ) ) { |
|
|
|
|
570
|
|
|
throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
$ids = array(); |
574
|
|
|
|
575
|
|
|
foreach( $request->data as $entry ) |
576
|
|
|
{ |
577
|
|
|
if( isset( $entry->id ) ) { |
578
|
|
|
$ids[] = $entry->id; |
579
|
|
|
} |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
$manager->deleteItems( $ids ); |
583
|
|
|
$view->total = count( $ids ); |
|
|
|
|
584
|
|
|
} |
585
|
|
|
else |
586
|
|
|
{ |
587
|
|
|
$manager->deleteItem( $id ); |
588
|
|
|
$view->total = 1; |
|
|
|
|
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
return $view; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Returns the view object |
597
|
|
|
* |
598
|
|
|
* @return \Aimeos\MW\View\Iface View object |
599
|
|
|
*/ |
600
|
|
|
protected function getView() |
601
|
|
|
{ |
602
|
|
|
return $this->view; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* Initializes the criteria object based on the given parameter |
608
|
|
|
* |
609
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
610
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
611
|
|
|
* @return \Aimeos\MW\Criteria\Iface Initialized criteria object |
612
|
|
|
*/ |
613
|
|
|
protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
614
|
|
|
{ |
615
|
|
|
$this->initCriteriaConditions( $criteria, $params ); |
616
|
|
|
$this->initCriteriaSortations( $criteria, $params ); |
617
|
|
|
$this->initCriteriaSlice( $criteria, $params ); |
618
|
|
|
|
619
|
|
|
return $criteria; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* Initializes the criteria object with conditions based on the given parameter |
625
|
|
|
* |
626
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
627
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
628
|
|
|
*/ |
629
|
|
|
private function initCriteriaConditions( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
630
|
|
|
{ |
631
|
|
|
if( isset( $params['filter'] ) && is_array( $params['filter'] ) ) |
632
|
|
|
{ |
633
|
|
|
$existing = $criteria->getConditions(); |
634
|
|
|
$criteria->setConditions( $criteria->toConditions( (array) $params['filter'] ) ); |
635
|
|
|
|
636
|
|
|
$expr = array( $criteria->getConditions(), $existing ); |
637
|
|
|
$criteria->setConditions( $criteria->combine( '&&', $expr ) ); |
638
|
|
|
} |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Initializes the criteria object with the slice based on the given parameter. |
644
|
|
|
* |
645
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
646
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
647
|
|
|
*/ |
648
|
|
|
private function initCriteriaSlice( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
649
|
|
|
{ |
650
|
|
|
$start = ( isset( $params['page']['offset'] ) ? $params['page']['offset'] : 0 ); |
651
|
|
|
$size = ( isset( $params['page']['limit'] ) ? $params['page']['limit'] : 25 ); |
652
|
|
|
|
653
|
|
|
$criteria->setSlice( $start, $size ); |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* Initializes the criteria object with sortations based on the given parameter |
659
|
|
|
* |
660
|
|
|
* @param \Aimeos\MW\Criteria\Iface $criteria Criteria object |
661
|
|
|
* @param array $params List of criteria data with condition, sorting and paging |
662
|
|
|
*/ |
663
|
|
|
private function initCriteriaSortations( \Aimeos\MW\Criteria\Iface $criteria, array $params ) |
664
|
|
|
{ |
665
|
|
|
if( !isset( $params['sort'] ) ) { |
666
|
|
|
return; |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
$sortation = array(); |
670
|
|
|
|
671
|
|
|
foreach( explode( ',', $params['sort'] ) as $sort ) |
672
|
|
|
{ |
673
|
|
|
if( $sort[0] === '-' ) { |
674
|
|
|
$sortation[] = $criteria->sort( '-', substr( $sort, 1 ) ); |
675
|
|
|
} else { |
676
|
|
|
$sortation[] = $criteria->sort( '+', $sort ); break; |
677
|
|
|
} |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
$criteria->setSortations( $sortation ); |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* Returns the items with parent/child relationships |
686
|
|
|
* |
687
|
|
|
* @param array $items List of items implementing \Aimeos\MShop\Common\Item\Iface |
688
|
|
|
* @param array $include List of resource types that should be fetched |
689
|
|
|
* @return array List of items implementing \Aimeos\MShop\Common\Item\Iface |
690
|
|
|
*/ |
691
|
|
|
protected function getChildItems( array $items, array $include ) |
692
|
|
|
{ |
693
|
|
|
return array(); |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
|
697
|
|
|
/** |
698
|
|
|
* Returns the list items for association relationships |
699
|
|
|
* |
700
|
|
|
* @param array $items List of items implementing \Aimeos\MShop\Common\Item\Iface |
701
|
|
|
* @param array $include List of resource types that should be fetched |
702
|
|
|
* @return array List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface |
703
|
|
|
*/ |
704
|
|
|
protected function getListItems( array $items, array $include ) |
705
|
|
|
{ |
706
|
|
|
return array(); |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
|
710
|
|
|
/** |
711
|
|
|
* Returns the items associated via a lists table |
712
|
|
|
* |
713
|
|
|
* @param array $listItems List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface |
714
|
|
|
* @return array List of items implementing \Aimeos\MShop\Common\Item\Iface |
715
|
|
|
*/ |
716
|
|
|
protected function getRefItems( array $listItems ) |
717
|
|
|
{ |
718
|
|
|
$list = $map = array(); |
719
|
|
|
|
720
|
|
|
foreach( $listItems as $listItem ) { |
721
|
|
|
$map[$listItem->getDomain()][] = $listItem->getRefId(); |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
foreach( $map as $domain => $ids ) |
725
|
|
|
{ |
726
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $domain ); |
727
|
|
|
|
728
|
|
|
$search = $manager->createSearch(); |
729
|
|
|
$search->setConditions( $search->compare( '==', $domain . '.id', $ids ) ); |
730
|
|
|
|
731
|
|
|
$list = array_merge( $list, $manager->searchItems( $search ) ); |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
return $list; |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* Returns the context item object |
740
|
|
|
* |
741
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Context object |
742
|
|
|
*/ |
743
|
|
|
protected function getContext() |
744
|
|
|
{ |
745
|
|
|
return $this->context; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* Returns the paths to the template files |
751
|
|
|
* |
752
|
|
|
* @return array List of file system paths |
753
|
|
|
*/ |
754
|
|
|
protected function getTemplatePaths() |
755
|
|
|
{ |
756
|
|
|
return $this->templatePaths; |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* Returns the path to the client |
762
|
|
|
* |
763
|
|
|
* @return string Client path, e.g. "product/stock" |
764
|
|
|
*/ |
765
|
|
|
protected function getPath() |
766
|
|
|
{ |
767
|
|
|
return $this->path; |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
|
771
|
|
|
/** |
772
|
|
|
* Saves new attributes for one or more items |
773
|
|
|
* |
774
|
|
|
* @param \Aimeos\MW\View\Iface $view View that will contain the "data" and "total" properties afterwards |
775
|
|
|
* @param string $body Request body |
776
|
|
|
* @param array &$header Associative list of HTTP headers as value/result parameter |
777
|
|
|
* @throws \Aimeos\Admin\JsonAdm\Exception If "id" parameter isn't available or the body is invalid |
778
|
|
|
* @return \Aimeos\MW\View\Iface Updated view instance |
779
|
|
|
*/ |
780
|
|
|
protected function patchItems( \Aimeos\MW\View\Iface $view, $body, array &$header ) |
781
|
|
|
{ |
782
|
|
View Code Duplication |
if( ( $request = json_decode( $body ) ) === null || !isset( $request->data ) ) { |
|
|
|
|
783
|
|
|
throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() ); |
787
|
|
|
|
788
|
|
|
if( is_array( $request->data ) ) |
789
|
|
|
{ |
790
|
|
|
$data = $this->saveData( $manager, $request ); |
791
|
|
|
|
792
|
|
|
$view->data = $data; |
|
|
|
|
793
|
|
|
$view->total = count( $data ); |
|
|
|
|
794
|
|
|
$header['Content-Type'] = 'application/vnd.api+json; ext="bulk"; supported-ext="bulk"'; |
795
|
|
|
} |
796
|
|
|
else |
797
|
|
|
{ |
798
|
|
|
if( ( $id = $view->param( 'id' ) ) == null ) { |
799
|
|
|
throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'No ID given' ), 400 ); |
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
$request->data->id = $id; |
803
|
|
|
$data = $this->saveEntry( $manager, $request->data ); |
804
|
|
|
|
805
|
|
|
$view->data = $data; |
|
|
|
|
806
|
|
|
$view->total = 1; |
|
|
|
|
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
return $view; |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
|
813
|
|
|
/** |
814
|
|
|
* Creates one or more new items |
815
|
|
|
* |
816
|
|
|
* @param \Aimeos\MW\View\Iface $view View that will contain the "data" and "total" properties afterwards |
817
|
|
|
* @param string $body Request body |
818
|
|
|
* @param array &$header Associative list of HTTP headers as value/result parameter |
819
|
|
|
* @return \Aimeos\MW\View\Iface Updated view instance |
820
|
|
|
*/ |
821
|
|
|
protected function postItems( \Aimeos\MW\View\Iface $view, $body, array &$header ) |
822
|
|
|
{ |
823
|
|
View Code Duplication |
if( ( $request = json_decode( $body ) ) === null || !isset( $request->data ) ) { |
|
|
|
|
824
|
|
|
throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
825
|
|
|
} |
826
|
|
|
|
827
|
|
|
if( isset( $request->data->id ) || $view->param( 'id' ) != null ) { |
828
|
|
|
throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Client generated IDs are not supported' ), 403 ); |
829
|
|
|
} |
830
|
|
|
|
831
|
|
|
|
832
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() ); |
833
|
|
|
|
834
|
|
|
if( is_array( $request->data ) ) |
835
|
|
|
{ |
836
|
|
|
$data = $this->saveData( $manager, $request ); |
837
|
|
|
|
838
|
|
|
$view->data = $data; |
|
|
|
|
839
|
|
|
$view->total = count( $data ); |
|
|
|
|
840
|
|
|
$header['Content-Type'] = 'application/vnd.api+json; ext="bulk"; supported-ext="bulk"'; |
841
|
|
|
} |
842
|
|
|
else |
843
|
|
|
{ |
844
|
|
|
$request->data->id = null; |
845
|
|
|
$data = $this->saveEntry( $manager, $request->data ); |
846
|
|
|
|
847
|
|
|
$view->data = $data; |
|
|
|
|
848
|
|
|
$view->total = 1; |
|
|
|
|
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
return $view; |
852
|
|
|
} |
853
|
|
|
|
854
|
|
|
|
855
|
|
|
/** |
856
|
|
|
* Creates of updates several items at once |
857
|
|
|
* |
858
|
|
|
* @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
859
|
|
|
* @param \stdClass $request Object with request body data |
860
|
|
|
* @return array List of items |
861
|
|
|
*/ |
862
|
|
|
protected function saveData( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $request ) |
863
|
|
|
{ |
864
|
|
|
$data = array(); |
865
|
|
|
|
866
|
|
|
if( isset( $request->data ) ) |
867
|
|
|
{ |
868
|
|
|
foreach( (array) $request->data as $entry ) { |
869
|
|
|
$data[] = $this->saveEntry( $manager, $entry ); |
870
|
|
|
} |
871
|
|
|
} |
872
|
|
|
|
873
|
|
|
return $data; |
874
|
|
|
} |
875
|
|
|
|
876
|
|
|
|
877
|
|
|
/** |
878
|
|
|
* Saves and returns the new or updated item |
879
|
|
|
* |
880
|
|
|
* @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items |
881
|
|
|
* @param \stdClass $entry Object including "id" and "attributes" elements |
882
|
|
|
* @return \Aimeos\MShop\Common\Item\Iface New or updated item |
883
|
|
|
*/ |
884
|
|
|
protected function saveEntry( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $entry ) |
885
|
|
|
{ |
886
|
|
|
$attr = ( isset( $entry->attributes ) ? (array) $entry->attributes : array() ); |
887
|
|
|
|
888
|
|
|
if( isset( $entry->id ) && $entry->id !== null ) { |
889
|
|
|
$item = $manager->getItem( $entry->id ); |
890
|
|
|
} else { |
891
|
|
|
$item = $manager->createItem(); |
892
|
|
|
} |
893
|
|
|
|
894
|
|
|
$item->fromArray( $attr ); |
895
|
|
|
$manager->saveItem( $item ); |
896
|
|
|
|
897
|
|
|
return $manager->getItem( $item->getId() ); |
898
|
|
|
} |
899
|
|
|
} |
900
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.