1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
6
|
|
|
* @package Client |
7
|
|
|
* @subpackage JsonApi |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Client\JsonApi\Basket\Product; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* JSON API basket/product client |
19
|
|
|
* |
20
|
|
|
* @package Client |
21
|
|
|
* @subpackage JsonApi |
22
|
|
|
*/ |
23
|
|
|
class Standard |
24
|
|
|
extends \Aimeos\Client\JsonApi\Base |
25
|
|
|
implements \Aimeos\Client\JsonApi\Iface |
26
|
|
|
{ |
27
|
|
|
private $controller; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Initializes the client |
32
|
|
|
* |
33
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context MShop context object |
34
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
35
|
|
|
* @param array $templatePaths List of file system paths where the templates are stored |
36
|
|
|
* @param string $path Name of the client, e.g "basket/product" |
37
|
|
|
*/ |
38
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MW\View\Iface $view, array $templatePaths, $path ) |
39
|
|
|
{ |
40
|
|
|
parent::__construct( $context, $view, $templatePaths, $path ); |
41
|
|
|
|
42
|
|
|
$this->controller = \Aimeos\Controller\Frontend\Basket\Factory::createController( $this->getContext() ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Deletes the resource or the resource list |
48
|
|
|
* |
49
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
50
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
51
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
52
|
|
|
*/ |
53
|
|
|
public function delete( ServerRequestInterface $request, ResponseInterface $response ) |
54
|
|
|
{ |
55
|
|
|
$view = $this->getView(); |
56
|
|
|
|
57
|
|
|
try |
58
|
|
|
{ |
59
|
|
|
$relId = $view->param( 'relatedid' ); |
60
|
|
|
$body = (string) $request->getBody(); |
61
|
|
|
$this->controller->setType( $view->param( 'id', 'default' ) ); |
62
|
|
|
|
63
|
|
|
if( $relId === '' || $relId === null ) |
64
|
|
|
{ |
65
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
66
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if( !is_array( $payload->data ) ) { |
70
|
|
|
$payload->data = [$payload->data]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
foreach( $payload->data as $entry ) |
74
|
|
|
{ |
75
|
|
|
if( !isset( $entry->id ) ) { |
76
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Position (ID) is missing' ) ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->controller->deleteProduct( $entry->id ); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
else |
83
|
|
|
{ |
84
|
|
|
$this->controller->deleteProduct( $relId ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
$view->items = $this->controller->get(); |
89
|
|
|
$view->total = 1; |
90
|
|
|
|
91
|
|
|
$status = 200; |
92
|
|
|
} |
93
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
94
|
|
|
{ |
95
|
|
|
$status = 404; |
96
|
|
|
$view->errors = array( array( |
97
|
|
|
'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ), |
98
|
|
|
'detail' => $e->getTraceAsString(), |
99
|
|
|
) ); |
100
|
|
|
} |
101
|
|
|
catch( \Exception $e ) |
102
|
|
|
{ |
103
|
|
|
$status = 500; |
104
|
|
|
$view->errors = array( array( |
105
|
|
|
'title' => $e->getMessage(), |
106
|
|
|
'detail' => $e->getTraceAsString(), |
107
|
|
|
) ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->render( $response, $view, $status ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the resource or the resource list |
116
|
|
|
* |
117
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
118
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
119
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
120
|
|
|
*/ |
121
|
|
|
public function get( ServerRequestInterface $request, ResponseInterface $response ) |
122
|
|
|
{ |
123
|
|
|
$methods = 'GET'; |
124
|
|
|
$view = $this->getView(); |
125
|
|
|
$relId = $view->param( 'relatedid' ); |
126
|
|
|
$id = $view->param( 'id', 'default' ); |
127
|
|
|
|
128
|
|
|
try |
129
|
|
|
{ |
130
|
|
|
try |
131
|
|
|
{ |
132
|
|
|
$basket = $this->controller->load( $id, \Aimeos\MShop\Order\Manager\Base\Base::PARTS_PRODUCT ); |
133
|
|
|
|
134
|
|
|
if( $relId !== '' && $relId !== null ) |
135
|
|
|
{ |
136
|
|
|
foreach( $basket->getProducts() as $orderProduct ) |
137
|
|
|
{ |
138
|
|
|
if( $orderProduct->getId() == $relId ) |
139
|
|
|
{ |
140
|
|
|
$view->items = $orderProduct; |
141
|
|
|
break; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
147
|
|
|
{ |
148
|
|
|
$basket = $this->controller->setType( $id )->get(); |
149
|
|
|
$methods = 'DELETE,GET,PATCH,POST'; |
150
|
|
|
|
151
|
|
|
if( $relId !== '' && $relId !== null ) { |
152
|
|
|
$view->items = $basket->getProduct( $relId ); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if( $relId === '' || $relId === null ) { |
157
|
|
|
$view->items = $basket->getProducts(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$view->total = count( $view->items ); |
161
|
|
|
$status = 200; |
162
|
|
|
} |
163
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
164
|
|
|
{ |
165
|
|
|
$status = 404; |
166
|
|
|
$view->errors = array( array( |
167
|
|
|
'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ), |
168
|
|
|
'detail' => $e->getTraceAsString(), |
169
|
|
|
) ); |
170
|
|
|
} |
171
|
|
|
catch( \Exception $e ) |
172
|
|
|
{ |
173
|
|
|
$status = 500; |
174
|
|
|
$view->errors = array( array( |
175
|
|
|
'title' => $e->getMessage(), |
176
|
|
|
'detail' => $e->getTraceAsString(), |
177
|
|
|
) ); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** client/jsonapi/basket/product/standard/template |
181
|
|
|
* Relative path to the basket/product JSON API template |
182
|
|
|
* |
183
|
|
|
* The template file contains the code and processing instructions |
184
|
|
|
* to generate the result shown in the JSON API body. The |
185
|
|
|
* configuration string is the path to the template file relative |
186
|
|
|
* to the templates directory (usually in client/jsonapi/templates). |
187
|
|
|
* |
188
|
|
|
* You can overwrite the template file configuration in extensions and |
189
|
|
|
* provide alternative templates. These alternative templates should be |
190
|
|
|
* named like the default one but with the string "default" replaced by |
191
|
|
|
* an unique name. You may use the name of your project for this. If |
192
|
|
|
* you've implemented an alternative client class as well, "standard" |
193
|
|
|
* should be replaced by the name of the new class. |
194
|
|
|
* |
195
|
|
|
* @param string Relative path to the template creating the body for the JSON API |
196
|
|
|
* @since 2017.04 |
197
|
|
|
* @category Developer |
198
|
|
|
*/ |
199
|
|
|
$tplconf = 'client/jsonapi/basket/product/standard/template'; |
200
|
|
|
$default = 'basket/product-default.php'; |
201
|
|
|
|
202
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
203
|
|
|
|
204
|
|
|
return $response->withHeader( 'Allow', $methods ) |
205
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
206
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
207
|
|
|
->withStatus( $status ); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Updates the resource or the resource list partitially |
213
|
|
|
* |
214
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
215
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
216
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
217
|
|
|
*/ |
218
|
|
|
public function patch( ServerRequestInterface $request, ResponseInterface $response ) |
219
|
|
|
{ |
220
|
|
|
$view = $this->getView(); |
221
|
|
|
|
222
|
|
|
try |
223
|
|
|
{ |
224
|
|
|
$body = (string) $request->getBody(); |
225
|
|
|
$relId = $view->param( 'relatedid' ); |
226
|
|
|
$this->controller->setType( $view->param( 'id', 'default' ) ); |
227
|
|
|
|
228
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) || !isset( $payload->data->attributes ) ) { |
229
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if( !is_array( $payload->data ) ) { |
233
|
|
|
$payload->data = [$payload->data]; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
foreach( $payload->data as $entry ) |
237
|
|
|
{ |
238
|
|
|
if( $relId !== '' && $relId !== null ) { |
239
|
|
|
$entry->id = $relId; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if( !isset( $entry->id ) ) { |
243
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Position (ID) is missing' ) ); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
$qty = ( isset( $entry->attributes->quantity ) ? $entry->attributes->quantity : 1 ); |
247
|
|
|
$cfgAttrCodes = ( isset( $entry->attributes->codes ) ? (array) $entry->attributes->codes : [] ); |
248
|
|
|
|
249
|
|
|
$this->controller->editProduct( $entry->id, $qty, $cfgAttrCodes ); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$view->items = $this->controller->get(); |
253
|
|
|
$view->total = 1; |
254
|
|
|
|
255
|
|
|
$status = 200; |
256
|
|
|
} |
257
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
258
|
|
|
{ |
259
|
|
|
$status = 404; |
260
|
|
|
$view->errors = array( array( |
261
|
|
|
'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ), |
262
|
|
|
'detail' => $e->getTraceAsString(), |
263
|
|
|
) ); |
264
|
|
|
} |
265
|
|
|
catch( \Exception $e ) |
266
|
|
|
{ |
267
|
|
|
$status = 500; |
268
|
|
|
$view->errors = array( array( |
269
|
|
|
'title' => $e->getMessage(), |
270
|
|
|
'detail' => $e->getTraceAsString(), |
271
|
|
|
) ); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return $this->render( $response, $view, $status ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Creates or updates the resource or the resource list |
280
|
|
|
* |
281
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
282
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
283
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
284
|
|
|
*/ |
285
|
|
|
public function post( ServerRequestInterface $request, ResponseInterface $response ) |
286
|
|
|
{ |
287
|
|
|
$view = $this->getView(); |
288
|
|
|
|
289
|
|
|
try |
290
|
|
|
{ |
291
|
|
|
$body = (string) $request->getBody(); |
292
|
|
|
$this->controller->setType( $view->param( 'id', 'default' ) ); |
293
|
|
|
|
294
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
295
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if( !is_array( $payload->data ) ) { |
299
|
|
|
$payload->data = [$payload->data]; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
foreach( $payload->data as $entry ) |
303
|
|
|
{ |
304
|
|
|
if( !isset( $entry->attributes ) || !isset( $entry->attributes->productid ) ) { |
305
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Product ID is missing' ) ); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$qty = ( isset( $entry->attributes->quantity ) ? $entry->attributes->quantity : 1 ); |
309
|
|
|
$stocktype = ( isset( $entry->attributes->stocktype ) ? $entry->attributes->stocktype : 'default' ); |
310
|
|
|
$variantAttrIds = ( isset( $entry->attributes->variant ) ? (array) $entry->attributes->variant : [] ); |
311
|
|
|
$configAttrIds = ( isset( $entry->attributes->config ) ? (array) $entry->attributes->config : [] ); |
312
|
|
|
$hiddenAttrIds = ( isset( $entry->attributes->hidden ) ? (array) $entry->attributes->hidden : [] ); |
313
|
|
|
$customAttrIds = ( isset( $entry->attributes->custom ) ? (array) $entry->attributes->custom : [] ); |
314
|
|
|
|
315
|
|
|
$this->controller->addProduct( $entry->attributes->productid, $qty, [], |
316
|
|
|
$variantAttrIds, $configAttrIds, $hiddenAttrIds, $customAttrIds, $stocktype ); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
|
320
|
|
|
$view->items = $this->controller->get(); |
321
|
|
|
$view->total = 1; |
322
|
|
|
|
323
|
|
|
$status = 201; |
324
|
|
|
} |
325
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
326
|
|
|
{ |
327
|
|
|
$status = 404; |
328
|
|
|
$view->errors = array( array( |
329
|
|
|
'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ), |
330
|
|
|
'detail' => $e->getTraceAsString(), |
331
|
|
|
) ); |
332
|
|
|
} |
333
|
|
|
catch( \Exception $e ) |
334
|
|
|
{ |
335
|
|
|
$status = 500; |
336
|
|
|
$view->errors = array( array( |
337
|
|
|
'title' => $e->getMessage(), |
338
|
|
|
'detail' => $e->getTraceAsString(), |
339
|
|
|
) ); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
return $this->render( $response, $view, $status ); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Returns the response object with the rendered header and body |
348
|
|
|
* |
349
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
350
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
351
|
|
|
* @param integer $status HTTP status code |
352
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
353
|
|
|
*/ |
354
|
|
|
protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) |
355
|
|
|
{ |
356
|
|
|
$tplconf = 'client/jsonapi/basket/standard/template'; |
357
|
|
|
$default = 'basket/default.php'; |
358
|
|
|
|
359
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
360
|
|
|
|
361
|
|
|
return $response->withHeader( 'Allow', 'DELETE,GET,PATCH,POST' ) |
362
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
363
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
364
|
|
|
->withStatus( $status ); |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|