Completed
Push — master ( ddf4ea...eacf6e )
by Aimeos
03:06
created

Standard::getAggregate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package Admin
7
 * @subpackage JsonAdm
8
 */
9
10
11
namespace Aimeos\Admin\JsonAdm;
12
13
14
/**
15
 * JSON API standard client
16
 *
17
 * @package Admin
18
 * @subpackage JsonAdm
19
 */
20
class Standard
21
	extends \Aimeos\Admin\JsonAdm\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\Admin\JsonAdm\Common\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	/**
25
	 * Deletes the resource or the resource list
26
	 *
27
	 * @param string $body Request body
28
	 * @param array &$header Variable which contains the HTTP headers and the new ones afterwards
29
	 * @param integer &$status Variable which contains the HTTP status afterwards
30
	 * @return string Content for response body
31
	 */
32
	public function delete( $body, array &$header, &$status )
33
	{
34
		$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' );
35
		$view = $this->getView();
36
37
		try
38
		{
39
			$view = $this->deleteItems( $view, $body );
40
			$status = 200;
41
		}
42
		catch( \Aimeos\Admin\JsonAdm\Exception $e )
43
		{
44
			$status = $e->getCode();
45
			$view->errors = array( array(
46
				'title' => $this->getContext()->getI18n()->dt( 'admin/jsonadm', $e->getMessage() ),
47
				'detail' => $e->getTraceAsString(),
48
			) );
49
		}
50
		catch( \Aimeos\MShop\Exception $e )
51
		{
52
			$status = 404;
53
			$view->errors = array( array(
54
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
55
				'detail' => $e->getTraceAsString(),
56
			) );
57
		}
58
		catch( \Exception $e )
59
		{
60
			$status = 500;
61
			$view->errors = array( array(
62
				'title' => $e->getMessage(),
63
				'detail' => $e->getTraceAsString(),
64
			) );
65
		}
66
67
		/** admin/jsonadm/standard/template-delete
68
		 * Relative path to the JSON API template for DELETE requests
69
		 *
70
		 * The template file contains the code and processing instructions
71
		 * to generate the result shown in the JSON API body. The
72
		 * configuration string is the path to the template file relative
73
		 * to the templates directory (usually in admin/jsonadm/templates).
74
		 *
75
		 * You can overwrite the template file configuration in extensions and
76
		 * provide alternative templates. These alternative templates should be
77
		 * named like the default one but with the string "standard" replaced by
78
		 * an unique name. You may use the name of your project for this. If
79
		 * you've implemented an alternative client class as well, "standard"
80
		 * should be replaced by the name of the new class.
81
		 *
82
		 * @param string Relative path to the template creating the body for the DELETE method of the JSON API
83
		 * @since 2015.12
84
		 * @category Developer
85
		 * @see admin/jsonadm/standard/template-aggregate
86
		 * @see admin/jsonadm/standard/template-get
87
		 * @see admin/jsonadm/standard/template-patch
88
		 * @see admin/jsonadm/standard/template-post
89
		 * @see admin/jsonadm/standard/template-put
90
		 * @see admin/jsonadm/standard/template-options
91
		 */
92
		$tplconf = 'admin/jsonadm/standard/template-delete';
93
		$default = 'delete-default.php';
94
95
		return $view->render( $view->config( $tplconf, $default ) );
96
	}
97
98
99
	/**
100
	 * Returns the requested resource or the resource list
101
	 *
102
	 * @param string $body Request body
103
	 * @param array &$header Variable which contains the HTTP headers and the new ones afterwards
104
	 * @param integer &$status Variable which contains the HTTP status afterwards
105
	 * @return string Content for response body
106
	 */
107
	public function get( $body, array &$header, &$status )
108
	{
109
		$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' );
110
111
		$view = $this->getView();
112
		$aggregate = $view->param( 'aggregate' );
113
114
		try
115
		{
116
			if( $aggregate !== null ) {
117
				$view = $this->getAggregate( $view );
118
			} else {
119
				$view = $this->getItems( $view );
120
			}
121
122
			$status = 200;
123
		}
124
		catch( \Aimeos\MShop\Exception $e )
125
		{
126
			$status = 404;
127
			$view->errors = array( array(
128
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
129
				'detail' => $e->getTraceAsString(),
130
			) );
131
		}
132
		catch( \Exception $e )
133
		{
134
			$status = 500;
135
			$view->errors = array( array(
136
				'title' => $e->getMessage(),
137
				'detail' => $e->getTraceAsString(),
138
			) );
139
		}
140
141
		if( $aggregate !== null )
142
		{
143
			/** admin/jsonadm/standard/template-aggregate
144
			 * Relative path to the JSON API template for GET aggregate requests
145
			 *
146
			 * The template file contains the code and processing instructions
147
			 * to generate the result shown in the JSON API body. The
148
			 * configuration string is the path to the template file relative
149
			 * to the templates directory (usually in admin/jsonadm/templates).
150
			 *
151
			 * You can overwrite the template file configuration in extensions and
152
			 * provide alternative templates. These alternative templates should be
153
			 * named like the default one but with the string "standard" replaced by
154
			 * an unique name. You may use the name of your project for this. If
155
			 * you've implemented an alternative client class as well, "standard"
156
			 * should be replaced by the name of the new class.
157
			 *
158
			 * @param string Relative path to the template creating the body for the GET aggregate request of the JSON API
159
			 * @since 2016.07
160
			 * @category Developer
161
			 * @see admin/jsonadm/standard/template-delete
162
			 * @see admin/jsonadm/standard/template-get
163
			 * @see admin/jsonadm/standard/template-patch
164
			 * @see admin/jsonadm/standard/template-post
165
			 * @see admin/jsonadm/standard/template-put
166
			 * @see admin/jsonadm/standard/template-options
167
			 */
168
			$tplconf = 'admin/jsonadm/standard/template-aggregate';
169
			$default = 'aggregate-default.php';
170
		}
171
		else
172
		{
173
			/** admin/jsonadm/standard/template-get
174
			 * Relative path to the JSON API template for GET requests
175
			 *
176
			 * The template file contains the code and processing instructions
177
			 * to generate the result shown in the JSON API body. The
178
			 * configuration string is the path to the template file relative
179
			 * to the templates directory (usually in admin/jsonadm/templates).
180
			 *
181
			 * You can overwrite the template file configuration in extensions and
182
			 * provide alternative templates. These alternative templates should be
183
			 * named like the default one but with the string "standard" replaced by
184
			 * an unique name. You may use the name of your project for this. If
185
			 * you've implemented an alternative client class as well, "standard"
186
			 * should be replaced by the name of the new class.
187
			 *
188
			 * @param string Relative path to the template creating the body for the GET method of the JSON API
189
			 * @since 2015.12
190
			 * @category Developer
191
			 * @see admin/jsonadm/standard/template-aggregate
192
			 * @see admin/jsonadm/standard/template-delete
193
			 * @see admin/jsonadm/standard/template-patch
194
			 * @see admin/jsonadm/standard/template-post
195
			 * @see admin/jsonadm/standard/template-put
196
			 * @see admin/jsonadm/standard/template-options
197
			 */
198
			$tplconf = 'admin/jsonadm/standard/template-get';
199
			$default = 'get-default.php';
200
		}
201
202
		return $view->render( $view->config( $tplconf, $default ) );
203
	}
204
205
206
	/**
207
	 * Updates the resource or the resource list partitially
208
	 *
209
	 * @param string $body Request body
210
	 * @param array &$header Variable which contains the HTTP headers and the new ones afterwards
211
	 * @param integer &$status Variable which contains the HTTP status afterwards
212
	 * @return string Content for response body
213
	 */
214
	public function patch( $body, array &$header, &$status )
215
	{
216
		$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' );
217
		$view = $this->getView();
218
219
		try
220
		{
221
			$view = $this->patchItems( $view, $body, $header );
222
			$status = 200;
223
		}
224
		catch( \Aimeos\Admin\JsonAdm\Exception $e )
225
		{
226
			$status = $e->getCode();
227
			$view->errors = array( array(
228
				'title' => $this->getContext()->getI18n()->dt( 'admin/jsonadm', $e->getMessage() ),
229
				'detail' => $e->getTraceAsString(),
230
			) );
231
		}
232
		catch( \Aimeos\MShop\Exception $e )
233
		{
234
			$status = 404;
235
			$view->errors = array( array(
236
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
237
				'detail' => $e->getTraceAsString(),
238
			) );
239
		}
240
		catch( \Exception $e )
241
		{
242
			$status = 500;
243
			$view->errors = array( array(
244
				'title' => $e->getMessage(),
245
				'detail' => $e->getTraceAsString(),
246
			) );
247
		}
248
249
		/** admin/jsonadm/standard/template-patch
250
		 * Relative path to the JSON API template for PATCH requests
251
		 *
252
		 * The template file contains the code and processing instructions
253
		 * to generate the result shown in the JSON API body. The
254
		 * configuration string is the path to the template file relative
255
		 * to the templates directory (usually in admin/jsonadm/templates).
256
		 *
257
		 * You can overwrite the template file configuration in extensions and
258
		 * provide alternative templates. These alternative templates should be
259
		 * named like the default one but with the string "standard" replaced by
260
		 * an unique name. You may use the name of your project for this. If
261
		 * you've implemented an alternative client class as well, "standard"
262
		 * should be replaced by the name of the new class.
263
		 *
264
		 * @param string Relative path to the template creating the body for the PATCH method of the JSON API
265
		 * @since 2015.12
266
		 * @category Developer
267
		 * @see admin/jsonadm/standard/template-aggregate
268
		 * @see admin/jsonadm/standard/template-get
269
		 * @see admin/jsonadm/standard/template-post
270
		 * @see admin/jsonadm/standard/template-delete
271
		 * @see admin/jsonadm/standard/template-put
272
		 * @see admin/jsonadm/standard/template-options
273
		 */
274
		$tplconf = 'admin/jsonadm/standard/template-patch';
275
		$default = 'patch-default.php';
276
277
		return $view->render( $view->config( $tplconf, $default ) );
278
	}
279
280
281
	/**
282
	 * Creates or updates the resource or the resource list
283
	 *
284
	 * @param string $body Request body
285
	 * @param array &$header Variable which contains the HTTP headers and the new ones afterwards
286
	 * @param integer &$status Variable which contains the HTTP status afterwards
287
	 * @return string Content for response body
288
	 */
289
	public function post( $body, array &$header, &$status )
290
	{
291
		$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' );
292
		$view = $this->getView();
293
294
		try
295
		{
296
			$view = $this->postItems( $view, $body, $header );
297
			$status = 201;
298
		}
299
		catch( \Aimeos\Admin\JsonAdm\Exception $e )
300
		{
301
			$status = $e->getCode();
302
			$view->errors = array( array(
303
				'title' => $this->getContext()->getI18n()->dt( 'admin/jsonadm', $e->getMessage() ),
304
				'detail' => $e->getTraceAsString(),
305
			) );
306
		}
307
		catch( \Aimeos\MShop\Exception $e )
308
		{
309
			$status = 404;
310
			$view->errors = array( array(
311
				'title' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ),
312
				'detail' => $e->getTraceAsString(),
313
			) );
314
		}
315
		catch( \Exception $e )
316
		{
317
			$status = 500;
318
			$view->errors = array( array(
319
				'title' => $e->getMessage(),
320
				'detail' => $e->getTraceAsString(),
321
			) );
322
		}
323
324
		/** admin/jsonadm/standard/template-post
325
		 * Relative path to the JSON API template for POST requests
326
		 *
327
		 * The template file contains the code and processing instructions
328
		 * to generate the result shown in the JSON API body. The
329
		 * configuration string is the path to the template file relative
330
		 * to the templates directory (usually in admin/jsonadm/templates).
331
		 *
332
		 * You can overwrite the template file configuration in extensions and
333
		 * provide alternative templates. These alternative templates should be
334
		 * named like the default one but with the string "standard" replaced by
335
		 * an unique name. You may use the name of your project for this. If
336
		 * you've implemented an alternative client class as well, "standard"
337
		 * should be replaced by the name of the new class.
338
		 *
339
		 * @param string Relative path to the template creating the body for the POST method of the JSON API
340
		 * @since 2015.12
341
		 * @category Developer
342
		 * @see admin/jsonadm/standard/template-aggregate
343
		 * @see admin/jsonadm/standard/template-get
344
		 * @see admin/jsonadm/standard/template-patch
345
		 * @see admin/jsonadm/standard/template-delete
346
		 * @see admin/jsonadm/standard/template-put
347
		 * @see admin/jsonadm/standard/template-options
348
		 */
349
		$tplconf = 'admin/jsonadm/standard/template-post';
350
		$default = 'post-default.php';
351
352
		return $view->render( $view->config( $tplconf, $default ) );
353
	}
354
355
356
	/**
357
	 * Creates or updates the resource or the resource list
358
	 *
359
	 * @param string $body Request body
360
	 * @param array &$header Variable which contains the HTTP headers and the new ones afterwards
361
	 * @param integer &$status Variable which contains the HTTP status afterwards
362
	 * @return string Content for response body
363
	 */
364
	public function put( $body, array &$header, &$status )
365
	{
366
		$status = 501;
367
		$header = array( 'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"' );
368
		$view = $this->getView();
369
370
		$view->errors = array( array(
371
			'title' => $this->getContext()->getI18n()->dt( 'admin/jsonadm', 'Not implemented, use PATCH instead' ),
372
		) );
373
374
		/** admin/jsonadm/standard/template-put
375
		 * Relative path to the JSON API template for PUT requests
376
		 *
377
		 * The template file contains the code and processing instructions
378
		 * to generate the result shown in the JSON API body. The
379
		 * configuration string is the path to the template file relative
380
		 * to the templates directory (usually in admin/jsonadm/templates).
381
		 *
382
		 * You can overwrite the template file configuration in extensions and
383
		 * provide alternative templates. These alternative templates should be
384
		 * named like the default one but with the string "standard" replaced by
385
		 * an unique name. You may use the name of your project for this. If
386
		 * you've implemented an alternative client class as well, "standard"
387
		 * should be replaced by the name of the new class.
388
		 *
389
		 * @param string Relative path to the template creating the body for the PUT method of the JSON API
390
		 * @since 2015.12
391
		 * @category Developer
392
		 * @see admin/jsonadm/standard/template-aggregate
393
		 * @see admin/jsonadm/standard/template-delete
394
		 * @see admin/jsonadm/standard/template-patch
395
		 * @see admin/jsonadm/standard/template-post
396
		 * @see admin/jsonadm/standard/template-get
397
		 * @see admin/jsonadm/standard/template-options
398
		 */
399
		$tplconf = 'admin/jsonadm/standard/template-put';
400
		$default = 'put-default.php';
401
402
		return $view->render( $view->config( $tplconf, $default ) );
403
	}
404
405
406
	/**
407
	 * Returns the available REST verbs and the available resources
408
	 *
409
	 * @param string $body Request body
410
	 * @param array &$header Variable which contains the HTTP headers and the new ones afterwards
411
	 * @param integer &$status Variable which contains the HTTP status afterwards
412
	 * @return string Content for response body
413
	 */
414
	public function options( $body, array &$header, &$status )
415
	{
416
		$context = $this->getContext();
417
		$view = $this->getView();
418
419
		try
420
		{
421
			$resources = $attributes = array();
422
423
			foreach( $this->getDomains( $view ) as $domain )
424
			{
425
				$manager = \Aimeos\MShop\Factory::createManager( $context, $domain );
426
				$resources = array_merge( $resources, $manager->getResourceType( true ) );
427
				$attributes = array_merge( $attributes, $manager->getSearchAttributes( true ) );
428
			}
429
430
			$view->resources = $resources;
431
			$view->attributes = $attributes;
432
433
			$header = array(
434
				'Content-Type' => 'application/vnd.api+json; supported-ext="bulk"',
435
				'Allow' => 'DELETE,GET,PATCH,POST,OPTIONS'
436
			);
437
			$status = 200;
438
		}
439
		catch( \Aimeos\MShop\Exception $e )
440
		{
441
			$status = 404;
442
			$view->errors = array( array(
443
				'title' => $context->getI18n()->dt( 'mshop', $e->getMessage() ),
444
				'detail' => $e->getTraceAsString(),
445
			) );
446
		}
447
		catch( \Exception $e )
448
		{
449
			$status = 500;
450
			$view->errors = array( array(
451
				'title' => $e->getMessage(),
452
				'detail' => $e->getTraceAsString(),
453
			) );
454
		}
455
456
		/** admin/jsonadm/standard/template-options
457
		 * Relative path to the JSON API template for OPTIONS requests
458
		 *
459
		 * The template file contains the code and processing instructions
460
		 * to generate the result shown in the JSON API body. The
461
		 * configuration string is the path to the template file relative
462
		 * to the templates directory (usually in admin/jsonadm/templates).
463
		 *
464
		 * You can overwrite the template file configuration in extensions and
465
		 * provide alternative templates. These alternative templates should be
466
		 * named like the default one but with the string "standard" replaced by
467
		 * an unique name. You may use the name of your project for this. If
468
		 * you've implemented an alternative client class as well, "standard"
469
		 * should be replaced by the name of the new class.
470
		 *
471
		 * @param string Relative path to the template creating the body for the OPTIONS method of the JSON API
472
		 * @since 2015.12
473
		 * @category Developer
474
		 * @see admin/jsonadm/standard/template-aggregate
475
		 * @see admin/jsonadm/standard/template-delete
476
		 * @see admin/jsonadm/standard/template-patch
477
		 * @see admin/jsonadm/standard/template-post
478
		 * @see admin/jsonadm/standard/template-get
479
		 * @see admin/jsonadm/standard/template-put
480
		 */
481
		$tplconf = 'admin/jsonadm/standard/template-options';
482
		$default = 'options-default.php';
483
484
		return $view->render( $view->config( $tplconf, $default ) );
485
	}
486
487
488
	/**
489
	 * Deletes one or more items
490
	 *
491
	 * @param \Aimeos\MW\View\Iface $view View instance with "param" view helper
492
	 * @param string $body Request body
493
	 * @return \Aimeos\MW\View\Iface $view View object that will contain the "total" property afterwards
494
	 * @throws \Aimeos\Admin\JsonAdm\Exception If the request body is invalid
495
	 */
496
	protected function deleteItems( \Aimeos\MW\View\Iface $view, $body )
497
	{
498
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() );
499
500
		if( ( $id = $view->param( 'id' ) ) == null )
501
		{
502
			if( ( $request = json_decode( $body ) ) === null || !isset( $request->data ) || !is_array( $request->data ) ) {
503
				throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid JSON in body' ), 400 );
504
			}
505
506
			$ids = $this->getIds( $request );
507
			$manager->deleteItems( $ids );
508
			$view->total = count( $ids );
509
		}
510
		else
511
		{
512
			$manager->deleteItem( $id );
513
			$view->total = 1;
514
		}
515
516
		return $view;
517
	}
518
519
520
	/**
521
	 * Retrieves the aggregation and adds the data to the view
522
	 *
523
	 * @param \Aimeos\MW\View\Iface $view View instance
524
	 * @return \Aimeos\MW\View\Iface View instance with additional data assigned
525
	 */
526
	protected function getAggregate( \Aimeos\MW\View\Iface $view )
527
	{
528
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() );
529
530
		$key = $view->param( 'aggregate' );
531
532
		$search = $this->initCriteria( $manager->createSearch(), $view->param() );
533
		$view->data = $manager->aggregate( $search, $key );
534
535
		return $view;
536
	}
537
538
539
	/**
540
	 * Retrieves the item or items and adds the data to the view
541
	 *
542
	 * @param \Aimeos\MW\View\Iface $view View instance
543
	 * @return \Aimeos\MW\View\Iface View instance with additional data assigned
544
	 */
545
	protected function getItems( \Aimeos\MW\View\Iface $view )
546
	{
547
		$total = 1;
548
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() );
549
		$include = ( ( $include = $view->param( 'include' ) ) !== null ? explode( ',', $include ) : array() );
550
551
		if( ( $id = $view->param( 'id' ) ) == null )
552
		{
553
			$search = $this->initCriteria( $manager->createSearch(), $view->param() );
554
			$view->data = $manager->searchItems( $search, array(), $total );
555
			$view->childItems = $this->getChildItems( $view->data, $include );
556
			$view->listItems = $this->getListItems( $view->data, $include );
557
		}
558
		else
559
		{
560
			$view->data = $manager->getItem( $id, array() );
561
			$view->childItems = $this->getChildItems( array( $id => $view->data ), $include );
562
			$view->listItems = $this->getListItems( array( $id => $view->data ), $include );
563
		}
564
565
		$view->refItems = $this->getRefItems( $view->listItems );
566
		$view->total = $total;
567
568
		return $view;
569
	}
570
571
572
	/**
573
	 * Retrieves the item or items and adds the data to the view
574
	 *
575
	 * @param \Aimeos\MW\View\Iface $view View instance
576
	 * @return \Aimeos\MW\View\Iface View instance with additional data assigned
577
	 * @deprecated 2016.06 Use getItems() instead
578
	 */
579
	protected function getItem( \Aimeos\MW\View\Iface $view )
580
	{
581
		return $this->getItems( $view );
582
	}
583
584
	/**
585
	 * Saves new attributes for one or more items
586
	 *
587
	 * @param \Aimeos\MW\View\Iface $view View that will contain the "data" and "total" properties afterwards
588
	 * @param string $body Request body
589
	 * @param array &$header Associative list of HTTP headers as value/result parameter
590
	 * @throws \Aimeos\Admin\JsonAdm\Exception If "id" parameter isn't available or the body is invalid
591
	 * @return \Aimeos\MW\View\Iface Updated view instance
592
	 */
593
	protected function patchItems( \Aimeos\MW\View\Iface $view, $body, array &$header )
594
	{
595
		if( ( $request = json_decode( $body ) ) === null || !isset( $request->data ) ) {
596
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid JSON in body' ), 400 );
597
		}
598
599
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() );
600
601
		if( is_array( $request->data ) )
602
		{
603
			$data = $this->saveData( $manager, $request );
604
605
			$view->data = $data;
606
			$view->total = count( $data );
607
			$header['Content-Type'] = 'application/vnd.api+json; ext="bulk"; supported-ext="bulk"';
608
		}
609
		elseif( ( $id = $view->param( 'id' ) ) != null )
610
		{
611
			$request->data->id = $id;
612
			$data = $this->saveEntry( $manager, $request->data );
613
614
			$view->data = $data;
615
			$view->total = 1;
616
		}
617
		else
618
		{
619
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'No ID given' ), 400 );
620
		}
621
622
		return $view;
623
	}
624
625
626
	/**
627
	 * Creates one or more new items
628
	 *
629
	 * @param \Aimeos\MW\View\Iface $view View that will contain the "data" and "total" properties afterwards
630
	 * @param string $body Request body
631
	 * @param array &$header Associative list of HTTP headers as value/result parameter
632
	 * @return \Aimeos\MW\View\Iface Updated view instance
633
	 */
634
	protected function postItems( \Aimeos\MW\View\Iface $view, $body, array &$header )
635
	{
636
		if( ( $request = json_decode( $body ) ) === null || !isset( $request->data ) ) {
637
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Invalid JSON in body' ), 400 );
638
		}
639
640
		if( isset( $request->data->id ) || $view->param( 'id' ) != null ) {
641
			throw new \Aimeos\Admin\JsonAdm\Exception( sprintf( 'Client generated IDs are not supported' ), 403 );
642
		}
643
644
645
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), $this->getPath() );
646
647
		if( is_array( $request->data ) )
648
		{
649
			$data = $this->saveData( $manager, $request );
650
651
			$view->data = $data;
652
			$view->total = count( $data );
653
			$header['Content-Type'] = 'application/vnd.api+json; ext="bulk"; supported-ext="bulk"';
654
		}
655
		else
656
		{
657
			$request->data->id = null;
658
			$data = $this->saveEntry( $manager, $request->data );
659
660
			$view->data = $data;
661
			$view->total = 1;
662
		}
663
664
		return $view;
665
	}
666
}
667