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
|
|
|
* @param string|null $prefix Form parameter prefix when nesting parameters is required |
52
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
53
|
|
|
*/ |
54
|
|
|
public function delete( ServerRequestInterface $request, ResponseInterface $response, $prefix = null ) |
55
|
|
|
{ |
56
|
|
|
$view = $this->getView(); |
57
|
|
|
$view->prefix = $prefix; |
58
|
|
|
|
59
|
|
|
try |
60
|
|
|
{ |
61
|
|
|
$relId = $view->param( 'relatedid' ); |
62
|
|
|
$body = (string) $request->getBody(); |
63
|
|
|
|
64
|
|
|
if( $relId === '' || $relId === null ) |
65
|
|
|
{ |
66
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
67
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if( !is_array( $payload->data ) ) { |
71
|
|
|
$payload->data = [$payload->data]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach( $payload->data as $entry ) |
75
|
|
|
{ |
76
|
|
|
if( !isset( $entry->id ) ) { |
77
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'ID is missing' ), 400 ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->controller->deleteListsItem( $entry->id ); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
else |
84
|
|
|
{ |
85
|
|
|
$this->controller->deleteListsItem( $relId ); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$status = 200; |
89
|
|
|
} |
90
|
|
|
catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
91
|
|
|
{ |
92
|
|
|
$status = 403; |
93
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
94
|
|
|
} |
95
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
96
|
|
|
{ |
97
|
|
|
$status = 404; |
98
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
99
|
|
|
} |
100
|
|
|
catch( \Exception $e ) |
101
|
|
|
{ |
102
|
|
|
$status = 500; |
103
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->render( $response, $view, $status ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the resource or the resource list |
112
|
|
|
* |
113
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
114
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
115
|
|
|
* @param string|null $prefix Form parameter prefix when nesting parameters is required |
116
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
117
|
|
|
*/ |
118
|
|
|
public function get( ServerRequestInterface $request, ResponseInterface $response, $prefix = null ) |
119
|
|
|
{ |
120
|
|
|
$view = $this->getView(); |
121
|
|
|
$view->prefix = $prefix; |
122
|
|
|
|
123
|
|
|
try |
124
|
|
|
{ |
125
|
|
|
$total = 1; |
126
|
|
|
$relId = $view->param( 'relatedid' ); |
127
|
|
|
$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' ); |
128
|
|
|
|
129
|
|
|
if( $relId == null ) |
130
|
|
|
{ |
131
|
|
|
$filter = $this->initCriteria( $cntl->createListsFilter(), $view->param( 'filter', [] ) ); |
132
|
|
|
$view->items = $cntl->searchListsItems( $filter, $total ); |
133
|
|
|
} |
134
|
|
|
else |
135
|
|
|
{ |
136
|
|
|
$view->items = $cntl->getListsItem( $relId ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$view->total = $total; |
140
|
|
|
$status = 200; |
141
|
|
|
} |
142
|
|
|
catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
143
|
|
|
{ |
144
|
|
|
$status = 403; |
145
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
146
|
|
|
} |
147
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
148
|
|
|
{ |
149
|
|
|
$status = 404; |
150
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
151
|
|
|
} |
152
|
|
|
catch( \Exception $e ) |
153
|
|
|
{ |
154
|
|
|
$status = 500; |
155
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->render( $response, $view, $status ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Updates the resource or the resource list partitially |
164
|
|
|
* |
165
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
166
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
167
|
|
|
* @param string|null $prefix Form parameter prefix when nesting parameters is required |
168
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
169
|
|
|
*/ |
170
|
|
|
public function patch( ServerRequestInterface $request, ResponseInterface $response, $prefix = null ) |
171
|
|
|
{ |
172
|
|
|
$view = $this->getView(); |
173
|
|
|
$view->prefix = $prefix; |
174
|
|
|
|
175
|
|
|
try |
176
|
|
|
{ |
177
|
|
|
$body = (string) $request->getBody(); |
178
|
|
|
|
179
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data->attributes ) ) { |
180
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Invalid JSON in body' ), 400 ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'customer' ); |
184
|
|
|
|
185
|
|
|
$view->items = $cntl->editListsItem( $view->param( 'relatedid' ), (array) $payload->data->attributes ); |
186
|
|
|
$view->total = 1; |
187
|
|
|
$status = 200; |
188
|
|
|
} |
189
|
|
|
catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
190
|
|
|
{ |
191
|
|
|
print_r( $e->getMessage() ); |
192
|
|
|
$status = 403; |
193
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
194
|
|
|
} |
195
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
196
|
|
|
{ |
197
|
|
|
$status = 404; |
198
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
199
|
|
|
} |
200
|
|
|
catch( \Exception $e ) |
201
|
|
|
{ |
202
|
|
|
$status = 500; |
203
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this->render( $response, $view, $status ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Creates or updates the resource or the resource list |
212
|
|
|
* |
213
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
214
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
215
|
|
|
* @param string|null $prefix Form parameter prefix when nesting parameters is required |
216
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
217
|
|
|
*/ |
218
|
|
|
public function post( ServerRequestInterface $request, ResponseInterface $response, $prefix = null ) |
219
|
|
|
{ |
220
|
|
|
$view = $this->getView(); |
221
|
|
|
$view->prefix = $prefix; |
222
|
|
|
|
223
|
|
|
try |
224
|
|
|
{ |
225
|
|
|
$list = []; |
226
|
|
|
$body = (string) $request->getBody(); |
227
|
|
|
|
228
|
|
|
if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
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( !isset( $entry->attributes ) ) { |
239
|
|
|
throw new \Aimeos\Client\JsonApi\Exception( sprintf( 'Attributes are missing' ) ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$list[] = $this->controller->addListsItem( (array) $entry->attributes ); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
$view->total = count( $list ); |
247
|
|
|
$view->items = $list; |
248
|
|
|
$status = 201; |
249
|
|
|
} |
250
|
|
|
catch( \Aimeos\Controller\Frontend\Customer\Exception $e ) |
251
|
|
|
{ |
252
|
|
|
$status = 403; |
253
|
|
|
$view->errors = $this->getErrorDetails( $e, 'controller/frontend' ); |
254
|
|
|
} |
255
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
256
|
|
|
{ |
257
|
|
|
$status = 404; |
258
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
259
|
|
|
} |
260
|
|
|
catch( \Exception $e ) |
261
|
|
|
{ |
262
|
|
|
$status = 500; |
263
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $this->render( $response, $view, $status ); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Returns the available REST verbs and the available parameters |
272
|
|
|
* |
273
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
274
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
275
|
|
|
* @param string|null $prefix Form parameter prefix when nesting parameters is required |
276
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
277
|
|
|
*/ |
278
|
|
|
public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null ) |
279
|
|
|
{ |
280
|
|
|
$view = $this->getView(); |
281
|
|
|
$view->prefix = $prefix; |
282
|
|
|
|
283
|
|
|
$view->attributes = [ |
284
|
|
|
'customer.lists.refid' => [ |
285
|
|
|
'label' => 'ID of the related domain item', |
286
|
|
|
'type' => 'string', 'default' => '', 'required' => true, |
287
|
|
|
], |
288
|
|
|
'customer.lists.domain' => [ |
289
|
|
|
'label' => 'Domain of the related item, e.g. "product"', |
290
|
|
|
'type' => 'string', 'default' => '', 'required' => true, |
291
|
|
|
], |
292
|
|
|
'customer.lists.type' => [ |
293
|
|
|
'label' => 'Customer relationship type, e.g. "favorite"', |
294
|
|
|
'type' => 'string', 'default' => '', 'required' => false, |
295
|
|
|
], |
296
|
|
|
'customer.lists.config' => [ |
297
|
|
|
'label' => 'Associative list of key/value configuration pairs', |
298
|
|
|
'type' => 'string', 'default' => '[]', 'required' => false, |
299
|
|
|
], |
300
|
|
|
'customer.lists.datestart' => [ |
301
|
|
|
'label' => 'Start date when the relationship is valied', |
302
|
|
|
'type' => 'string', 'default' => '', 'required' => false, |
303
|
|
|
], |
304
|
|
|
'customer.lists.dateend' => [ |
305
|
|
|
'label' => 'End date until the relationship is valid', |
306
|
|
|
'type' => 'string', 'default' => '', 'required' => false, |
307
|
|
|
], |
308
|
|
|
'customer.lists.status' => [ |
309
|
|
|
'label' => 'Status of the relationship (0=disable, 1=enabled)', |
310
|
|
|
'type' => 'string', 'default' => '1', 'required' => false, |
311
|
|
|
], |
312
|
|
|
]; |
313
|
|
|
|
314
|
|
|
$tplconf = 'client/jsonapi/standard/template-options'; |
315
|
|
|
$default = 'options-standard.php'; |
316
|
|
|
|
317
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
318
|
|
|
|
319
|
|
|
return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
320
|
|
|
->withHeader( 'Cache-Control', 'max-age=300' ) |
321
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
322
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
323
|
|
|
->withStatus( 200 ); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Returns the response object with the rendered header and body |
329
|
|
|
* |
330
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
331
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
332
|
|
|
* @param integer $status HTTP status code |
333
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
334
|
|
|
*/ |
335
|
|
|
protected function render( ResponseInterface $response, \Aimeos\MW\View\Iface $view, $status ) |
336
|
|
|
{ |
337
|
|
|
$tplconf = 'client/jsonapi/customer/relationships/standard/template'; |
338
|
|
|
$default = 'customer/relationships/standard.php'; |
339
|
|
|
|
340
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
341
|
|
|
|
342
|
|
|
return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
343
|
|
|
->withHeader( 'Cache-Control', 'no-cache, private' ) |
344
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
345
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
346
|
|
|
->withStatus( $status ); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.