Issues (87)

templates/client/jsonapi/basket/standard.php (7 issues)

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2026
6
 * @package Client
7
 * @subpackage JsonApi
8
 */
9
10
11
$enc = $this->encoder();
12
13
$target = $this->config( 'client/jsonapi/url/target' );
14
$cntl = $this->config( 'client/jsonapi/url/controller', 'jsonapi' );
15
$action = $this->config( 'client/jsonapi/url/action', 'get' );
16
$config = $this->config( 'client/jsonapi/url/config', [] );
17
18
19
$basketId = ( isset( $this->item ) && $this->item->getId() ? $this->item->getId() : ( $this->param( 'id' ) ?: 'default' ) );
20
$ref = array( 'resource', 'id', 'filter', 'page', 'sort', 'include', 'fields' ); // no related/relatedid for basket self URL
21
$params = array_intersect_key( $this->param(), array_flip( $ref ) );
22
23
$pretty = $this->param( 'pretty' ) ? JSON_PRETTY_PRINT : 0;
24
$fields = $this->param( 'fields', [] );
25
26
foreach( (array) $fields as $resource => $list ) {
27
	$fields[$resource] = array_flip( explode( ',', $list ) );
28
}
29
30
31
$entryFcn = function( \Aimeos\MShop\Order\Item\Iface $item, $basketId ) use ( $fields, $target, $cntl, $action, $config )
32
{
33
	$allow = array( 'GET' );
34
	$attributes = $item->toArray();
35
	$params = ['resource' => 'basket', 'id' => $basketId];
36
37
	if( ( $filter = $this->param( 'filter', [] ) ) !== [] ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
38
		$params['filter'] = $filter;
39
	}
40
41
	if( $item->getId() === null ) {
42
		$allow = array( 'DELETE', 'GET', 'PATCH', 'POST' );
43
	}
44
45
	if( isset( $fields['basket'] ) ) {
46
		$attributes = array_intersect_key( $attributes, $fields['basket'] );
47
	}
48
49
50
	$relationships = [];
51
	$types = explode( ',', $this->param( 'include', 'basket.product,basket.service,basket.address,basket.coupon' ) );
52
53
	if( in_array( 'basket.product', $types ) )
54
	{
55
		foreach( $item->getProducts() as $position => $list ) {
56
			$relationships['basket.product']['data'][] = ['type' => 'basket.product', 'id' => $position];
57
		}
58
	}
59
60
	if( in_array( 'basket.service', $types ) )
61
	{
62
		foreach( $item->getServices() as $type => $list )
63
		{
64
			if( count( $list ) > 0 ) {
65
				$relationships['basket.service']['data'][] = ['type' => 'basket.service', 'id' => $type];
66
			}
67
		}
68
	}
69
70
	if( in_array( 'basket.address', $types ) )
71
	{
72
		foreach( $item->getAddresses() as $type => $list ) {
73
			$relationships['basket.address']['data'][] = ['type' => 'basket.address', 'id' => $type];
74
		}
75
	}
76
77
	if( in_array( 'basket.coupon', $types ) )
78
	{
79
		foreach( $item->getCoupons() as $code => $list ) {
80
			$relationships['basket.coupon']['data'][] = ['type' => 'basket.coupon', 'id' => $code];
81
		}
82
	}
83
84
	if( $customer = $item->getCustomerItem() ) {
85
		$relationships['customer']['data'][] = ['type' => 'customer', 'id' => $customer->getId()];
86
	}
87
88
89
	return array(
90
		'id' => $basketId,
91
		'type' => 'basket',
92
		'links' => array(
93
			'self' => array(
94
				'href' => $this->url( $target, $cntl, $action, $params, [], $config ),
95
				'allow' => $allow,
96
			),
97
		),
98
		'attributes' => $attributes,
99
		'relationships' => (object) $relationships,
100
	);
101
};
102
103
104
$productFcn = function( \Aimeos\MShop\Order\Item\Iface $item, $basketId ) use ( $fields, $target, $cntl, $action, $config )
105
{
106
	$result = [];
107
108
	foreach( $item->getProducts() as $position => $orderProduct )
109
	{
110
		$entry = ['id' => $position, 'type' => 'basket.product'];
111
		$entry['attributes'] = $orderProduct->toArray();
112
113
		if( isset( $fields['basket.product'] ) ) {
114
			$entry['attributes'] = array_intersect_key( $entry['attributes'], $fields['basket.product'] );
115
		}
116
117
		if( $item->getId() === null && $orderProduct->getFlags() !== \Aimeos\MShop\Order\Item\Product\Base::FLAG_IMMUTABLE )
118
		{
119
			$params = ['resource' => 'basket', 'id' => $basketId, 'related' => 'product', 'relatedid' => $position];
120
			$entry['links'] = array(
121
				'self' => array(
122
					'href' => $this->url( $target, $cntl, $action, $params, [], $config ),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
123
					'allow' => ['DELETE', 'PATCH'],
124
				),
125
			);
126
		}
127
128
		foreach( $orderProduct->getProducts() as $subProduct )
129
		{
130
			$subEntry = $subProduct->toArray();
131
132
			foreach( $subProduct->getAttributeItems() as $attribute ) {
133
				$subEntry['attribute'][] = $attribute->toArray();
134
			}
135
136
			$entry['attributes']['product'][] = $subEntry;
137
		}
138
139
		foreach( $orderProduct->getAttributeItems() as $attribute ) {
140
			$entry['attributes']['attribute'][] = $attribute->toArray();
141
		}
142
143
		if( $product = $orderProduct->getProductItem() )
144
		{
145
			$entry['relationships']['product']['data'][] = ['type' => 'product', 'id' => $product->getId()];
146
			$result = array_merge( $result, $this->jincluded( $product, $fields ) );
147
		}
148
149
		$result['order.product'][] = $entry;
150
	}
151
152
	return $result;
153
};
154
155
156
$serviceFcn = function( \Aimeos\MShop\Order\Item\Iface $item, $basketId ) use ( $fields, $target, $cntl, $action, $config )
157
{
158
	$result = [];
159
160
	foreach( $item->getServices() as $type => $list )
161
	{
162
		foreach( $list as $orderService )
163
		{
164
			$entry = ['id' => $type, 'type' => 'basket.service'];
165
			$entry['attributes'] = $orderService->toArray();
166
167
			if( isset( $fields['basket.service'] ) ) {
168
				$entry['attributes'] = array_intersect_key( $entry['attributes'], $fields['basket.service'] );
169
			}
170
171
			if( $item->getId() === null )
172
			{
173
				$params = ['resource' => 'basket', 'id' => $basketId, 'related' => 'service', 'relatedid' => $type];
174
				$entry['links'] = array(
175
					'self' => array(
176
						'href' => $this->url( $target, $cntl, $action, $params, [], $config ),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
177
						'allow' => ['DELETE', 'PATCH'],
178
					),
179
				);
180
			}
181
182
			foreach( $orderService->getAttributeItems() as $attribute ) {
183
				$entry['attributes']['attribute'][] = $attribute->toArray();
184
			}
185
186
			if( $service = $orderService->getServiceItem() )
187
			{
188
				$entry['relationships']['service']['data'][] = ['type' => 'service', 'id' => $service->getId()];
189
				$result = array_merge( $result, $this->jincluded( $service, $fields ) );
190
			}
191
192
			$result['order.service'][] = $entry;
193
		}
194
	}
195
196
	return $result;
197
};
198
199
200
$addressFcn = function( \Aimeos\MShop\Order\Item\Iface $item, $basketId ) use ( $fields, $target, $cntl, $action, $config )
201
{
202
	$list = [];
203
204
	foreach( $item->getAddresses() as $type => $addresses )
205
	{
206
		foreach( $addresses as $address )
207
		{
208
			$entry = ['id' => $type, 'type' => 'basket.address'];
209
			$entry['attributes'] = $address->toArray();
210
211
			if( isset( $fields['basket.address'] ) ) {
212
				$entry['attributes'] = array_intersect_key( $entry['attributes'], $fields['basket.address'] );
213
			}
214
215
			if( $item->getId() === null )
216
			{
217
				$params = ['resource' => 'basket', 'id' => $basketId, 'related' => 'address', 'relatedid' => $type];
218
				$entry['links'] = array(
219
					'self' => array(
220
						'href' => $this->url( $target, $cntl, $action, $params, [], $config ),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
221
						'allow' => ['DELETE', 'PATCH'],
222
					),
223
				);
224
			}
225
226
			$list['order.address'][] = $entry;
227
		}
228
	}
229
230
	return $list;
231
};
232
233
234
$couponFcn = function( \Aimeos\MShop\Order\Item\Iface $item, $basketId ) use ( $fields, $target, $cntl, $action, $config )
0 ignored issues
show
The import $fields is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
235
{
236
	$coupons = [];
237
238
	foreach( $item->getCoupons() as $code => $list )
239
	{
240
		$entry = ['id' => $code, 'type' => 'basket.coupon'];
241
242
		if( $item->getId() === null )
243
		{
244
			$params = ['resource' => 'basket', 'id' => $basketId, 'related' => 'coupon', 'relatedid' => $code];
245
			$entry['links'] = array(
246
				'self' => array(
247
					'href' => $this->url( $target, $cntl, $action, $params, [], $config ),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
248
					'allow' => ['DELETE'],
249
				),
250
			);
251
		}
252
253
		$coupons['order.coupon'][] = $entry;
254
	}
255
256
	return $coupons;
257
};
258
259
260
$customerFcn = function( \Aimeos\MShop\Order\Item\Iface $item ) use ( $fields, $target, $cntl, $action, $config )
261
{
262
	$result = [];
263
	$customer = $this->item->getCustomerItem();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
264
265
	if( $customer && $customer->isAvailable() )
266
	{
267
		$params = ['resource' => 'customer', 'id' => $customer->getId()];
268
		$entry = ['id' => $customer->getId(), 'type' => 'customer'];
269
		$entry['attributes'] = $customer->toArray();
270
271
		if( isset( $fields['customer'] ) ) {
272
			$entry['attributes'] = array_intersect_key( $entry['attributes'], $fields['customer'] );
273
		}
274
275
		$entry['links'] = array(
276
			'self' => array(
277
				'href' => $this->url( $target, $cntl, $action, $params, [], $config ),
278
				'allow' => ['GET'],
279
			),
280
		);
281
282
		$result['customer'][$customer->getId()] = $entry;
283
		$result = array_replace_recursive( $result, $this->jincluded( $customer, $fields ) );
284
	}
285
286
	return $result;
287
};
288
289
290
?>
291
{
292
	"meta": {
293
		"total": <?= ( isset( $this->item ) ? 1 : 0 ); ?>,
294
		"prefix": <?= json_encode( $this->get( 'prefix' ) ); ?>,
295
		"content-baseurl": "<?= $this->config( 'resource/fs/baseurl' ); ?>",
296
		"content-baseurls": {
297
			"fs-media": "<?= $this->config( 'resource/fs-media/baseurl' ) ?>",
298
			"fs-mimeicon": "<?= $this->config( 'resource/fs-mimeicon/baseurl' ) ?>",
299
			"fs-theme": "<?= $this->config( 'resource/fs-theme/baseurl' ) ?>"
300
		}
301
		<?php if( $this->csrf()->name() != '' ) : ?>
302
			, "csrf": {
303
				"name": "<?= $this->csrf()->name(); ?>",
304
				"value": "<?= $this->csrf()->value(); ?>"
305
			}
306
		<?php endif; ?>
307
308
	},
309
	"links": {
310
		"self": {
311
			"href": "<?= $this->url( $target, $cntl, $action, $params, [], $config ); ?>",
312
			"allow": <?= isset( $this->item ) && $this->item->getId() ? '["GET"]' : '["DELETE","GET","PATCH","POST"]' ?>
313
314
		}
315
		<?php if( isset( $this->item ) ) : ?>
316
			<?php if( $this->item->getId() === null ) : ?>
317
				,
318
				"basket.product": {
319
					"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'product'], [], $config ); ?>",
320
					"allow": ["DELETE", "POST"]
321
				},
322
				"basket.service": {
323
					"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'service'], [], $config ); ?>",
324
					"allow": ["DELETE", "POST"]
325
				},
326
				"basket.address": {
327
					"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'address'], [], $config ); ?>",
328
					"allow": ["DELETE", "POST"]
329
				},
330
				"basket.coupon": {
331
					"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'basket', 'id' => $basketId, 'related' => 'coupon'], [], $config ); ?>",
332
					"allow": ["DELETE", "POST"]
333
				}
334
			<?php else : ?>
335
				,
336
				"order": {
337
					"href": "<?= $this->url( $target, $cntl, $action, ['resource' => 'order'], [], $config ); ?>",
338
					"allow": ["POST"]
339
				}
340
			<?php endif; ?>
341
		<?php endif; ?>
342
343
	}
344
	<?php if( isset( $this->errors ) ) : ?>
345
		,"errors": <?= json_encode( $this->errors, $pretty ); ?>
346
347
	<?php elseif( isset( $this->item ) ) : ?>
348
		<?php
349
			$included = [];
350
			$types = explode( ',', $this->param( 'include', 'basket.product,basket.service,basket.address,basket.coupon' ) );
351
352
			if( in_array( 'basket.product', $types ) ) {
353
				$included = array_replace_recursive( $included, $productFcn( $this->item, $basketId ) );
354
			}
355
356
			if( in_array( 'basket.service', $types ) ) {
357
				$included = array_replace_recursive( $included, $serviceFcn( $this->item, $basketId ) );
358
			}
359
360
			if( in_array( 'basket.address', $types ) ) {
361
				$included = array_replace_recursive( $included, $addressFcn( $this->item, $basketId ) );
362
			}
363
364
			if( in_array( 'basket.coupon', $types ) ) {
365
				$included = array_replace_recursive( $included, $couponFcn( $this->item, $basketId ) );
366
			}
367
368
			if( in_array( 'customer', $types ) ) {
369
				$included = array_replace_recursive( $included, $customerFcn( $this->item ) );
370
			}
371
		?>
372
373
		,"data": <?= json_encode( $entryFcn( $this->item, $basketId ), $pretty ); ?>
374
375
		,"included": <?= map( $included )->flat( 1 )->toJson( $pretty ) ?>
376
377
	<?php endif; ?>
378
379
}
380