|
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; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* JSON API basket 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" |
|
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
|
|
|
$status = 200; |
|
60
|
|
|
$type = $view->param( 'id', 'default' ); |
|
61
|
|
|
$view->item = $this->controller->setType( $type )->clear()->get(); |
|
62
|
|
|
} |
|
63
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
64
|
|
|
{ |
|
65
|
|
|
$status = 404; |
|
66
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
|
67
|
|
|
} |
|
68
|
|
|
catch( \Exception $e ) |
|
69
|
|
|
{ |
|
70
|
|
|
$status = 500; |
|
71
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->render( $response, $view, $status ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns the resource or the resource list |
|
80
|
|
|
* |
|
81
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
82
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
83
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
84
|
|
|
*/ |
|
85
|
|
|
public function get( ServerRequestInterface $request, ResponseInterface $response ) |
|
86
|
|
|
{ |
|
87
|
|
|
$allow = false; |
|
88
|
|
|
$view = $this->getView(); |
|
89
|
|
|
$id = $view->param( 'id', 'default' ); |
|
90
|
|
|
|
|
91
|
|
|
try |
|
92
|
|
|
{ |
|
93
|
|
|
try |
|
94
|
|
|
{ |
|
95
|
|
|
$view->item = $this->controller->load( $id, $this->getParts( $view ) ); |
|
96
|
|
|
} |
|
97
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
98
|
|
|
{ |
|
99
|
|
|
$view->item = $this->controller->setType( $id )->get(); |
|
100
|
|
|
$allow = true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$status = 200; |
|
104
|
|
|
} |
|
105
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
106
|
|
|
{ |
|
107
|
|
|
$status = 404; |
|
108
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
|
109
|
|
|
} |
|
110
|
|
|
catch( \Exception $e ) |
|
111
|
|
|
{ |
|
112
|
|
|
$status = 500; |
|
113
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this->render( $response, $view, $status, $allow ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Updates the resource or the resource list partitially |
|
122
|
|
|
* |
|
123
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
124
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
125
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
126
|
|
|
*/ |
|
127
|
|
|
public function patch( ServerRequestInterface $request, ResponseInterface $response ) |
|
128
|
|
|
{ |
|
129
|
|
|
$view = $this->getView(); |
|
130
|
|
|
|
|
131
|
|
|
try |
|
132
|
|
|
{ |
|
133
|
|
|
$body = (string) $request->getBody(); |
|
134
|
|
|
|
|
135
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
|
136
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$basket = $this->controller->setType( $view->param( 'id', 'default' ) )->get(); |
|
140
|
|
|
|
|
141
|
|
|
if( isset( $payload->data->attributes->{'order.base.comment'} ) ) { |
|
142
|
|
|
$basket->setComment( $payload->data->attributes->{'order.base.comment'} ); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$view->item = $basket; |
|
146
|
|
|
$status = 200; |
|
147
|
|
|
} |
|
148
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
149
|
|
|
{ |
|
150
|
|
|
$status = 404; |
|
151
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
|
152
|
|
|
} |
|
153
|
|
|
catch( \Exception $e ) |
|
154
|
|
|
{ |
|
155
|
|
|
$status = 500; |
|
156
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $this->render( $response, $view, $status ); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Creates or updates the resource or the resource list |
|
165
|
|
|
* |
|
166
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
167
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
168
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
169
|
|
|
*/ |
|
170
|
|
|
public function post( ServerRequestInterface $request, ResponseInterface $response ) |
|
171
|
|
|
{ |
|
172
|
|
|
$view = $this->getView(); |
|
173
|
|
|
|
|
174
|
|
|
try |
|
175
|
|
|
{ |
|
176
|
|
|
$item = $this->controller->setType( $view->param( 'id', 'default' ) )->store(); |
|
177
|
|
|
$this->getContext()->getSession()->set( 'aimeos/order.baseid', $item->getId() ); |
|
178
|
|
|
|
|
179
|
|
|
$view->item = $item; |
|
180
|
|
|
$status = 200; |
|
181
|
|
|
} |
|
182
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
183
|
|
|
{ |
|
184
|
|
|
$status = 404; |
|
185
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
|
186
|
|
|
} |
|
187
|
|
|
catch( \Exception $e ) |
|
188
|
|
|
{ |
|
189
|
|
|
$status = 500; |
|
190
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $this->render( $response, $view, $status ); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Returns the available REST verbs and the available parameters |
|
199
|
|
|
* |
|
200
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
201
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
202
|
|
|
* @param string|null $prefix Form parameter prefix when nesting parameters is required |
|
203
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
204
|
|
|
*/ |
|
205
|
|
|
public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null ) |
|
206
|
|
|
{ |
|
207
|
|
|
$view = $this->getView(); |
|
208
|
|
|
$view->attributes = [ |
|
209
|
|
|
'order.base.comment' => [ |
|
210
|
|
|
'label' => 'Customer comment for the order', |
|
211
|
|
|
'type' => 'string', 'default' => '', 'required' => false, |
|
212
|
|
|
], |
|
213
|
|
|
]; |
|
214
|
|
|
|
|
215
|
|
|
$tplconf = 'client/jsonapi/standard/template-options'; |
|
216
|
|
|
$default = 'options-standard.php'; |
|
217
|
|
|
|
|
218
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
|
219
|
|
|
|
|
220
|
|
|
return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
|
221
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
|
222
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
|
223
|
|
|
->withStatus( 200 ); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Returns the integer constant for the basket parts that should be included |
|
229
|
|
|
* |
|
230
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
|
231
|
|
|
* @return integer Constant from Aimeos\MShop\Order\Manager\Base\Base |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function getParts( \Aimeos\MW\View\Iface $view ) |
|
234
|
|
|
{ |
|
235
|
|
|
$available = array( |
|
236
|
|
|
'basket/address' => \Aimeos\MShop\Order\Manager\Base\Base::PARTS_ADDRESS, |
|
237
|
|
|
'basket/coupon' => \Aimeos\MShop\Order\Manager\Base\Base::PARTS_COUPON, |
|
238
|
|
|
'basket/product' => \Aimeos\MShop\Order\Manager\Base\Base::PARTS_PRODUCT, |
|
239
|
|
|
'basket/service' => \Aimeos\MShop\Order\Manager\Base\Base::PARTS_SERVICE, |
|
240
|
|
|
); |
|
241
|
|
|
|
|
242
|
|
|
$included = explode( ',', $view->param( 'included', 'basket/address,basket/coupon,basket/product,basket/service' ) ); |
|
243
|
|
|
|
|
244
|
|
|
$parts = \Aimeos\MShop\Order\Manager\Base\Base::PARTS_NONE; |
|
245
|
|
|
foreach( $included as $type ) |
|
246
|
|
|
{ |
|
247
|
|
|
if( isset( $available[$type] ) ) { |
|
248
|
|
|
$parts |= $available[$type]; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return $parts; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Returns the response object with the rendered header and body |
|
258
|
|
|
* |
|
259
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
260
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
|
261
|
|
|
* @param integer $status HTTP status code |
|
262
|
|
|
* @param boolean $allow True to allow all HTTP methods, false for GET only |
|
263
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
264
|
|
|
*/ |
|
265
|
|
|
protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status, $allow = true ) |
|
266
|
|
|
{ |
|
267
|
|
|
/** client/jsonapi/basket/standard/template |
|
268
|
|
|
* Relative path to the basket JSON API template |
|
269
|
|
|
* |
|
270
|
|
|
* The template file contains the code and processing instructions |
|
271
|
|
|
* to generate the result shown in the JSON API body. The |
|
272
|
|
|
* configuration string is the path to the template file relative |
|
273
|
|
|
* to the templates directory (usually in client/jsonapi/templates). |
|
274
|
|
|
* |
|
275
|
|
|
* You can overwrite the template file configuration in extensions and |
|
276
|
|
|
* provide alternative templates. These alternative templates should be |
|
277
|
|
|
* named like the default one but with the string "standard" replaced by |
|
278
|
|
|
* an unique name. You may use the name of your project for this. If |
|
279
|
|
|
* you've implemented an alternative client class as well, "standard" |
|
280
|
|
|
* should be replaced by the name of the new class. |
|
281
|
|
|
* |
|
282
|
|
|
* @param string Relative path to the template creating the body for the JSON API |
|
283
|
|
|
* @since 2017.04 |
|
284
|
|
|
* @category Developer |
|
285
|
|
|
*/ |
|
286
|
|
|
$tplconf = 'client/jsonapi/basket/standard/template'; |
|
287
|
|
|
$default = 'basket/standard.php'; |
|
288
|
|
|
|
|
289
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
|
290
|
|
|
|
|
291
|
|
|
if( $allow === true ) { |
|
292
|
|
|
$methods = 'DELETE,GET,OPTIONS,PATCH,POST'; |
|
293
|
|
|
} else { |
|
294
|
|
|
$methods = 'GET'; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return $response->withHeader( 'Allow', $methods ) |
|
298
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
|
299
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
|
300
|
|
|
->withStatus( $status ); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|