|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2020-2025 |
|
6
|
|
|
* @package Admin |
|
7
|
|
|
* @subpackage JsonAdm |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Admin\JsonAdm\Index; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* JSON API index client |
|
19
|
|
|
* |
|
20
|
|
|
* @package Admin |
|
21
|
|
|
* @subpackage JsonAdm |
|
22
|
|
|
*/ |
|
23
|
|
|
class Standard |
|
24
|
|
|
extends \Aimeos\Admin\JsonAdm\Standard |
|
25
|
|
|
implements \Aimeos\Admin\JsonAdm\Common\Iface |
|
26
|
|
|
{ |
|
27
|
|
|
/** admin/jsonadm/index/decorators/excludes |
|
28
|
|
|
* Excludes decorators added by the "common" option from the JSON API clients |
|
29
|
|
|
* |
|
30
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
31
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
32
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
33
|
|
|
* modify what is returned to the caller. |
|
34
|
|
|
* |
|
35
|
|
|
* This option allows you to remove a decorator added via |
|
36
|
|
|
* "admin/jsonadm/common/decorators/default" before they are wrapped |
|
37
|
|
|
* around the Jsonadm client. |
|
38
|
|
|
* |
|
39
|
|
|
* admin/jsonadm/decorators/excludes = array( 'decorator1' ) |
|
40
|
|
|
* |
|
41
|
|
|
* This would remove the decorator named "decorator1" from the list of |
|
42
|
|
|
* common decorators ("\Aimeos\Admin\JsonAdm\Common\Decorator\*") added via |
|
43
|
|
|
* "admin/jsonadm/common/decorators/default" for the JSON API client. |
|
44
|
|
|
* |
|
45
|
|
|
* @param array List of decorator names |
|
46
|
|
|
* @since 2020.10 |
|
47
|
|
|
* @category Developer |
|
48
|
|
|
* @see admin/jsonadm/common/decorators/default |
|
49
|
|
|
* @see admin/jsonadm/index/decorators/global |
|
50
|
|
|
* @see admin/jsonadm/index/decorators/local |
|
51
|
|
|
*/ |
|
52
|
|
|
|
|
53
|
|
|
/** admin/jsonadm/index/decorators/global |
|
54
|
|
|
* Adds a list of globally available decorators only to the Jsonadm client |
|
55
|
|
|
* |
|
56
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
57
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
58
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
59
|
|
|
* modify what is returned to the caller. |
|
60
|
|
|
* |
|
61
|
|
|
* This option allows you to wrap global decorators |
|
62
|
|
|
* ("\Aimeos\Admin\Jsonadm\Common\Decorator\*") around the Jsonadm |
|
63
|
|
|
* client. |
|
64
|
|
|
* |
|
65
|
|
|
* admin/jsonadm/index/decorators/global = array( 'decorator1' ) |
|
66
|
|
|
* |
|
67
|
|
|
* This would add the decorator named "decorator1" defined by |
|
68
|
|
|
* "\Aimeos\Admin\Jsonadm\Common\Decorator\Decorator1" only to the |
|
69
|
|
|
* "index" Jsonadm client. |
|
70
|
|
|
* |
|
71
|
|
|
* @param array List of decorator names |
|
72
|
|
|
* @since 2020.10 |
|
73
|
|
|
* @category Developer |
|
74
|
|
|
* @see admin/jsonadm/common/decorators/default |
|
75
|
|
|
* @see admin/jsonadm/index/decorators/excludes |
|
76
|
|
|
* @see admin/jsonadm/index/decorators/local |
|
77
|
|
|
*/ |
|
78
|
|
|
|
|
79
|
|
|
/** admin/jsonadm/index/decorators/local |
|
80
|
|
|
* Adds a list of local decorators only to the Jsonadm client |
|
81
|
|
|
* |
|
82
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
83
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
84
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
85
|
|
|
* modify what is returned to the caller. |
|
86
|
|
|
* |
|
87
|
|
|
* This option allows you to wrap local decorators |
|
88
|
|
|
* ("\Aimeos\Admin\Jsonadm\Index\Decorator\*") around the Jsonadm |
|
89
|
|
|
* client. |
|
90
|
|
|
* |
|
91
|
|
|
* admin/jsonadm/index/decorators/local = array( 'decorator2' ) |
|
92
|
|
|
* |
|
93
|
|
|
* This would add the decorator named "decorator2" defined by |
|
94
|
|
|
* "\Aimeos\Admin\Jsonadm\Index\Decorator\Decorator2" only to the |
|
95
|
|
|
* "index" Jsonadm client. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array List of decorator names |
|
98
|
|
|
* @since 2020.10 |
|
99
|
|
|
* @category Developer |
|
100
|
|
|
* @see admin/jsonadm/common/decorators/default |
|
101
|
|
|
* @see admin/jsonadm/index/decorators/excludes |
|
102
|
|
|
* @see admin/jsonadm/index/decorators/global |
|
103
|
|
|
*/ |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Deletes the resource or the resource list |
|
108
|
|
|
* |
|
109
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
110
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
111
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
112
|
|
|
*/ |
|
113
|
|
|
public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
|
114
|
|
|
{ |
|
115
|
|
|
$view = $this->view(); |
|
116
|
|
|
|
|
117
|
|
|
try |
|
118
|
|
|
{ |
|
119
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'index' ); |
|
120
|
|
|
|
|
121
|
|
|
if( ( $id = $view->param( 'id' ) ) == null ) |
|
122
|
|
|
{ |
|
123
|
|
|
$body = (string) $request->getBody(); |
|
124
|
|
|
|
|
125
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) || !is_array( $payload->data ) ) { |
|
126
|
|
|
throw new \Aimeos\Admin\JsonAdm\Exception( 'Invalid JSON in body', 400 ); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$view->total = count( $payload->data ); |
|
130
|
|
|
$view->data = $payload->data; |
|
131
|
|
|
$id = $payload->data; |
|
132
|
|
|
} |
|
133
|
|
|
else |
|
134
|
|
|
{ |
|
135
|
|
|
$view->total = 1; |
|
136
|
|
|
$view->data = $id; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$manager->remove( $id ); |
|
140
|
|
|
$status = 200; |
|
141
|
|
|
} |
|
142
|
|
|
catch( \Aimeos\Admin\JsonAdm\Exception $e ) |
|
143
|
|
|
{ |
|
144
|
|
|
$status = $e->getCode(); |
|
145
|
|
|
$view->errors = array( array( |
|
146
|
|
|
'title' => $this->context()->translate( 'admin/jsonadm', $e->getMessage() ), |
|
147
|
|
|
'detail' => $e->getTraceAsString(), |
|
148
|
|
|
) ); |
|
149
|
|
|
} |
|
150
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
151
|
|
|
{ |
|
152
|
|
|
$status = 404; |
|
153
|
|
|
$view->errors = array( array( |
|
154
|
|
|
'title' => $this->context()->translate( 'mshop', $e->getMessage() ), |
|
155
|
|
|
'detail' => $e->getTraceAsString(), |
|
156
|
|
|
) ); |
|
157
|
|
|
} |
|
158
|
|
|
catch( \Exception $e ) |
|
159
|
|
|
{ |
|
160
|
|
|
$status = 500; |
|
161
|
|
|
$view->errors = array( array( |
|
162
|
|
|
'title' => $e->getMessage(), |
|
163
|
|
|
'detail' => $e->getTraceAsString(), |
|
164
|
|
|
) ); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** admin/jsonadm/template-delete |
|
168
|
|
|
* Relative path to the JSON API template for DELETE requests |
|
169
|
|
|
* |
|
170
|
|
|
* The template file contains the code and processing instructions |
|
171
|
|
|
* to generate the result shown in the JSON API body. The |
|
172
|
|
|
* configuration string is the path to the template file relative |
|
173
|
|
|
* to the templates directory (usually in templates/admin/jsonadm). |
|
174
|
|
|
* |
|
175
|
|
|
* You can overwrite the template file configuration in extensions and |
|
176
|
|
|
* provide alternative templates. These alternative templates should be |
|
177
|
|
|
* named like the default one but with the string "standard" replaced by |
|
178
|
|
|
* an unique name. You may use the name of your project for this. If |
|
179
|
|
|
* you've implemented an alternative client class as well, "standard" |
|
180
|
|
|
* should be replaced by the name of the new class. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string Relative path to the template creating the body for the DELETE method of the JSON API |
|
183
|
|
|
* @since 2020.10 |
|
184
|
|
|
* @category Developer |
|
185
|
|
|
* @see admin/jsonadm/template-aggregate |
|
186
|
|
|
* @see admin/jsonadm/template-get |
|
187
|
|
|
* @see admin/jsonadm/template-patch |
|
188
|
|
|
* @see admin/jsonadm/template-post |
|
189
|
|
|
* @see admin/jsonadm/template-put |
|
190
|
|
|
* @see admin/jsonadm/template-options |
|
191
|
|
|
*/ |
|
192
|
|
|
$tplconf = 'admin/jsonadm/template-delete'; |
|
193
|
|
|
$default = 'delete'; |
|
194
|
|
|
|
|
195
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
|
196
|
|
|
|
|
197
|
|
|
return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' ) |
|
198
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
|
199
|
|
|
->withStatus( $status ); |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Creates or updates the resource or the resource list |
|
205
|
|
|
* |
|
206
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
207
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
208
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
209
|
|
|
*/ |
|
210
|
|
|
public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
|
211
|
|
|
{ |
|
212
|
|
|
$view = $this->view(); |
|
213
|
|
|
|
|
214
|
|
|
try |
|
215
|
|
|
{ |
|
216
|
|
|
$body = (string) $request->getBody(); |
|
217
|
|
|
|
|
218
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
|
219
|
|
|
throw new \Aimeos\Admin\JsonAdm\Exception( 'Invalid JSON in body', 400 ); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
if( is_array( $payload->data ) ) |
|
223
|
|
|
{ |
|
224
|
|
|
$response = $response->withHeader( 'Content-Type', 'application/vnd.api+json; ext="bulk"; supported-ext="bulk"' ); |
|
225
|
|
|
$ids = $payload->data; |
|
226
|
|
|
} |
|
227
|
|
|
else |
|
228
|
|
|
{ |
|
229
|
|
|
$ids = [$payload->data]; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
$context = $this->context(); |
|
233
|
|
|
$domains = $context->config()->get( 'mshop/index/manager/domains', [] ); |
|
234
|
|
|
|
|
235
|
|
|
$manager = \Aimeos\MShop::create( $context, 'product' ); |
|
236
|
|
|
$items = $manager->search( $manager->filter()->add( 'product.id', '==', $ids ), $domains ); |
|
237
|
|
|
|
|
238
|
|
|
\Aimeos\MShop::create( $context, 'index' )->rebuild( $items->toArray() ); |
|
239
|
|
|
|
|
240
|
|
|
$status = 201; |
|
241
|
|
|
$view->total = count( $ids ); |
|
242
|
|
|
} |
|
243
|
|
|
catch( \Aimeos\Admin\JsonAdm\Exception $e ) |
|
244
|
|
|
{ |
|
245
|
|
|
$status = $e->getCode(); |
|
246
|
|
|
$view->errors = array( array( |
|
247
|
|
|
'title' => $this->context()->translate( 'admin/jsonadm', $e->getMessage() ), |
|
248
|
|
|
'detail' => $e->getTraceAsString(), |
|
249
|
|
|
) ); |
|
250
|
|
|
} |
|
251
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
252
|
|
|
{ |
|
253
|
|
|
$status = 404; |
|
254
|
|
|
$view->errors = array( array( |
|
255
|
|
|
'title' => $this->context()->translate( 'mshop', $e->getMessage() ), |
|
256
|
|
|
'detail' => $e->getTraceAsString(), |
|
257
|
|
|
) ); |
|
258
|
|
|
} |
|
259
|
|
|
catch( \Exception $e ) |
|
260
|
|
|
{ |
|
261
|
|
|
$status = 500; |
|
262
|
|
|
$view->errors = array( array( |
|
263
|
|
|
'title' => $e->getMessage(), |
|
264
|
|
|
'detail' => $e->getTraceAsString(), |
|
265
|
|
|
) ); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** admin/jsonadm/template-post |
|
269
|
|
|
* Relative path to the JSON API template for POST requests |
|
270
|
|
|
* |
|
271
|
|
|
* The template file contains the code and processing instructions |
|
272
|
|
|
* to generate the result shown in the JSON API body. The |
|
273
|
|
|
* configuration string is the path to the template file relative |
|
274
|
|
|
* to the templates directory (usually in templates/admin/jsonadm). |
|
275
|
|
|
* |
|
276
|
|
|
* You can overwrite the template file configuration in extensions and |
|
277
|
|
|
* provide alternative templates. These alternative templates should be |
|
278
|
|
|
* named like the default one but with the string "standard" replaced by |
|
279
|
|
|
* an unique name. You may use the name of your project for this. If |
|
280
|
|
|
* you've implemented an alternative client class as well, "standard" |
|
281
|
|
|
* should be replaced by the name of the new class. |
|
282
|
|
|
* |
|
283
|
|
|
* @param string Relative path to the template creating the body for the POST method of the JSON API |
|
284
|
|
|
* @since 2020.10 |
|
285
|
|
|
* @category Developer |
|
286
|
|
|
* @see admin/jsonadm/template-aggregate |
|
287
|
|
|
* @see admin/jsonadm/template-get |
|
288
|
|
|
* @see admin/jsonadm/template-patch |
|
289
|
|
|
* @see admin/jsonadm/template-delete |
|
290
|
|
|
* @see admin/jsonadm/template-put |
|
291
|
|
|
* @see admin/jsonadm/template-options |
|
292
|
|
|
*/ |
|
293
|
|
|
$tplconf = 'admin/jsonadm/template-post'; |
|
294
|
|
|
$default = 'post'; |
|
295
|
|
|
|
|
296
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
|
297
|
|
|
|
|
298
|
|
|
return $response->withHeader( 'Content-Type', 'application/vnd.api+json; supported-ext="bulk"' ) |
|
299
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
|
300
|
|
|
->withStatus( $status ); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|