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\Customer\Relationships; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* JSON API customer/relationships 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 "customer/relationships" |
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\Customer\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
|
|
|
|
62
|
|
|
if( $relId === '' || $relId === null ) |
63
|
|
|
{ |
64
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
65
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if( !is_array( $payload->data ) ) { |
69
|
|
|
$payload->data = [$payload->data]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
foreach( $payload->data as $entry ) |
73
|
|
|
{ |
74
|
|
|
if( !isset( $entry->id ) ) { |
75
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->controller->deleteListItem( $entry->id ); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
else |
82
|
|
|
{ |
83
|
|
|
$this->controller->deleteListItem( $relId ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$status = 200; |
87
|
|
|
} |
88
|
|
|
catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
89
|
|
|
{ |
90
|
|
|
$status = 403; |
91
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
92
|
|
|
} |
93
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
94
|
|
|
{ |
95
|
|
|
$status = 404; |
96
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
97
|
|
|
} |
98
|
|
|
catch( \Exception $e ) |
99
|
|
|
{ |
100
|
|
|
$status = 500; |
101
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->render( $response, $view, $status ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the resource or the resource list |
110
|
|
|
* |
111
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
112
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
113
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
114
|
|
|
*/ |
115
|
|
|
public function get( ServerRequestInterface $request, ResponseInterface $response ) |
116
|
|
|
{ |
117
|
|
|
$view = $this->getView(); |
118
|
|
|
|
119
|
|
|
try |
120
|
|
|
{ |
121
|
|
|
$total = 1; |
122
|
|
|
$relId = $view->param( 'relatedid' ); |
123
|
|
|
$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' ); |
124
|
|
|
|
125
|
|
|
if( $relId == null ) |
126
|
|
|
{ |
127
|
|
|
$filter = $this->initCriteria( $cntl->createListsFilter(), $view->param( 'filter', [] ) ); |
128
|
|
|
$view->items = $cntl->searchListItems( $filter, $total ); |
129
|
|
|
} |
130
|
|
|
else |
131
|
|
|
{ |
132
|
|
|
$view->items = $cntl->getListItem( $relId ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$view->total = $total; |
136
|
|
|
$status = 200; |
137
|
|
|
} |
138
|
|
|
catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
139
|
|
|
{ |
140
|
|
|
$status = 403; |
141
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
142
|
|
|
} |
143
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
144
|
|
|
{ |
145
|
|
|
$status = 404; |
146
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
147
|
|
|
} |
148
|
|
|
catch( \Exception $e ) |
149
|
|
|
{ |
150
|
|
|
$status = 500; |
151
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->render( $response, $view, $status ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Updates the resource or the resource list partitially |
160
|
|
|
* |
161
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
162
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
163
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
164
|
|
|
*/ |
165
|
|
|
public function patch( ServerRequestInterface $request, ResponseInterface $response ) |
166
|
|
|
{ |
167
|
|
|
$view = $this->getView(); |
168
|
|
|
|
169
|
|
|
try |
170
|
|
|
{ |
171
|
|
|
$body = (string) $request->getBody(); |
172
|
|
|
|
173
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
174
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' ); |
178
|
|
|
|
179
|
|
|
$view->items = $cntl->editListItem( $view->param( 'relatedid' ), (array) $payload->data->attributes ); |
180
|
|
|
$view->total = 1; |
181
|
|
|
$status = 200; |
182
|
|
|
} |
183
|
|
|
catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
184
|
|
|
{ |
185
|
|
|
$status = 403; |
186
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
187
|
|
|
} |
188
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
189
|
|
|
{ |
190
|
|
|
$status = 404; |
191
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
192
|
|
|
} |
193
|
|
|
catch( \Exception $e ) |
194
|
|
|
{ |
195
|
|
|
$status = 500; |
196
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this->render( $response, $view, $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 ) |
211
|
|
|
{ |
212
|
|
|
$view = $this->getView(); |
213
|
|
|
|
214
|
|
|
try |
215
|
|
|
{ |
216
|
|
|
$list = []; |
217
|
|
|
$body = (string) $request->getBody(); |
218
|
|
|
|
219
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
220
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if( !is_array( $payload->data ) ) { |
224
|
|
|
$payload->data = [$payload->data]; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
foreach( $payload->data as $entry ) |
228
|
|
|
{ |
229
|
|
|
if( !isset( $entry->attributes ) ) { |
230
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing' ) ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$list[] = $this->controller->addListItem( (array) $entry->attributes ); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
$view->total = count( $list ); |
238
|
|
|
$view->items = $list; |
239
|
|
|
$status = 201; |
240
|
|
|
} |
241
|
|
|
catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
242
|
|
|
{ |
243
|
|
|
$status = 403; |
244
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
245
|
|
|
} |
246
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
247
|
|
|
{ |
248
|
|
|
$status = 404; |
249
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
250
|
|
|
} |
251
|
|
|
catch( \Exception $e ) |
252
|
|
|
{ |
253
|
|
|
$status = 500; |
254
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this->render( $response, $view, $status ); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Returns the available REST verbs and the available parameters |
263
|
|
|
* |
264
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
265
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
266
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
267
|
|
|
*/ |
268
|
|
|
public function options( ServerRequestInterface $request, ResponseInterface $response ) |
269
|
|
|
{ |
270
|
|
|
$view = $this->getView(); |
271
|
|
|
|
272
|
|
|
$view->attributes = [ |
273
|
|
|
'customer.lists.refid' => [ |
274
|
|
|
'label' => 'ID of the related domain item', |
275
|
|
|
'type' => 'string', 'default' => '', 'required' => true, |
276
|
|
|
], |
277
|
|
|
'customer.lists.domain' => [ |
278
|
|
|
'label' => 'Domain of the related item, e.g. "product"', |
279
|
|
|
'type' => 'string', 'default' => '', 'required' => true, |
280
|
|
|
], |
281
|
|
|
'customer.lists.type' => [ |
282
|
|
|
'label' => 'Customer relationship type, e.g. "favorite"', |
283
|
|
|
'type' => 'string', 'default' => '', 'required' => false, |
284
|
|
|
], |
285
|
|
|
'customer.lists.config' => [ |
286
|
|
|
'label' => 'Associative list of key/value configuration pairs', |
287
|
|
|
'type' => 'string', 'default' => '[]', 'required' => false, |
288
|
|
|
], |
289
|
|
|
'customer.lists.datestart' => [ |
290
|
|
|
'label' => 'Start date when the relationship is valied', |
291
|
|
|
'type' => 'string', 'default' => '', 'required' => false, |
292
|
|
|
], |
293
|
|
|
'customer.lists.dateend' => [ |
294
|
|
|
'label' => 'End date until the relationship is valid', |
295
|
|
|
'type' => 'string', 'default' => '', 'required' => false, |
296
|
|
|
], |
297
|
|
|
'customer.lists.status' => [ |
298
|
|
|
'label' => 'Status of the relationship (0=disable, 1=enabled)', |
299
|
|
|
'type' => 'string', 'default' => '1', 'required' => false, |
300
|
|
|
], |
301
|
|
|
]; |
302
|
|
|
|
303
|
|
|
$tplconf = 'client/jsonapi/standard/template-options'; |
304
|
|
|
$default = 'options-standard.php'; |
305
|
|
|
|
306
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
307
|
|
|
|
308
|
|
|
return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
309
|
|
|
->withHeader( 'Cache-Control', 'max-age=300' ) |
310
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
311
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
312
|
|
|
->withStatus( 200 ); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Returns the response object with the rendered header and body |
318
|
|
|
* |
319
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
320
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
321
|
|
|
* @param integer $status HTTP status code |
322
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
323
|
|
|
*/ |
324
|
|
|
protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) |
325
|
|
|
{ |
326
|
|
|
/** client/jsonapi/customer/relationships/standard/template |
327
|
|
|
* Relative path to the customer relationships JSON API template |
328
|
|
|
* |
329
|
|
|
* The template file contains the code and processing instructions |
330
|
|
|
* to generate the result shown in the JSON API body. The |
331
|
|
|
* configuration string is the path to the template file relative |
332
|
|
|
* to the templates directory (usually in client/jsonapi/templates). |
333
|
|
|
* |
334
|
|
|
* You can overwrite the template file configuration in extensions and |
335
|
|
|
* provide alternative templates. These alternative templates should be |
336
|
|
|
* named like the default one but with the string "standard" replaced by |
337
|
|
|
* an unique name. You may use the name of your project for this. If |
338
|
|
|
* you've implemented an alternative client class as well, "standard" |
339
|
|
|
* should be replaced by the name of the new class. |
340
|
|
|
* |
341
|
|
|
* @param string Relative path to the template creating the body for the JSON API |
342
|
|
|
* @since 2017.07 |
343
|
|
|
* @category Developer |
344
|
|
|
*/ |
345
|
|
|
$tplconf = 'client/jsonapi/customer/relationships/standard/template'; |
346
|
|
|
$default = 'customer/relationships/standard.php'; |
347
|
|
|
|
348
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
349
|
|
|
|
350
|
|
|
return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
351
|
|
|
->withHeader( 'Cache-Control', 'no-cache, private' ) |
352
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
353
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
354
|
|
|
->withStatus( $status ); |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|