Issues (87)

templates/client/jsonapi/catalog/standard.php (2 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
$offset = max( $this->param( 'page/offset', 0 ), 0 );
19
$limit = max( $this->param( 'page/limit', 100 ), 1 );
20
21
$ref = array( 'resource', 'id', 'related', 'relatedid', 'filter', 'page', 'sort', 'include', 'fields' );
22
$params = array_intersect_key( $this->param(), array_flip( $ref ) );
23
24
$pretty = $this->param( 'pretty' ) ? JSON_PRETTY_PRINT : 0;
25
$fields = $this->param( 'fields', [] );
26
27
foreach( (array) $fields as $resource => $list ) {
28
	$fields[$resource] = array_flip( explode( ',', $list ) );
29
}
30
31
32
$entryFcn = function( \Aimeos\MShop\Catalog\Item\Iface $item ) use ( $fields, $target, $cntl, $action, $config )
33
{
34
	if( $item->isAvailable() === false ) {
35
		return [];
36
	}
37
38
	$id = $item->getId();
39
	$type = $item->getResourceType();
40
	$params = array( 'resource' => $type, 'id' => $item->getId() );
41
	$attributes = $item->toArray();
42
43
	if( isset( $fields[$type] ) ) {
44
		$attributes = array_intersect_key( $attributes, $fields[$type] );
45
	}
46
47
	$entry = array(
48
		'id' => $id,
49
		'type' => $type,
50
		'links' => array(
51
			'self' => array(
52
				'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...
53
				'allow' => array( 'GET' ),
54
			),
55
		),
56
		'attributes' => $attributes,
57
	);
58
59
	foreach( $item->getChildren() as $catItem )
60
	{
61
		if( $catItem->isAvailable() ) {
62
			$entry['relationships']['catalog']['data'][] = array( 'id' => $catItem->getId(), 'type' => 'catalog' );
63
		}
64
	}
65
66
	foreach( $item->getListItems() as $listItem )
67
	{
68
		if( ( $refItem = $listItem->getRefItem() ) !== null && $refItem->isAvailable() )
69
		{
70
			$ltype = str_replace( '/', '.', $listItem->getResourceType() );
71
			$rtype = str_replace( '/', '.', $refItem->getResourceType() );
72
			$attributes = $listItem->toArray();
73
74
			if( isset( $fields[$ltype] ) ) {
75
				$attributes = array_intersect_key( $attributes, $fields[$ltype] );
76
			}
77
78
			$data = array( 'id' => $refItem->getId(), 'type' => $rtype, 'attributes' => $attributes );
79
			$entry['relationships'][$rtype]['data'][] = $data;
80
		}
81
	}
82
83
	return $entry;
84
};
85
86
87
$catFcn = function( \Aimeos\MShop\Catalog\Item\Iface $item, array $entry ) use ( $target, $cntl, $action, $config )
88
{
89
	$params = ['resource' => 'catalog', 'id' => $item->getId()];
90
	$entry['links'] = [
91
		'self' => [
92
			'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...
93
			'allow' => ['GET']
94
		]
95
	];
96
97
	return $entry;
98
};
99
100
101
?>
102
{
103
	"meta": {
104
		"total": <?= $this->get( 'total', 0 )  ?>,
105
		"prefix": <?= json_encode( $this->get( 'prefix' ) ); ?>,
106
		"content-baseurl": "<?= $this->config( 'resource/fs/baseurl' ); ?>",
107
		"content-baseurls": {
108
			"fs-media": "<?= $this->config( 'resource/fs-media/baseurl' ) ?>",
109
			"fs-mimeicon": "<?= $this->config( 'resource/fs-mimeicon/baseurl' ) ?>",
110
			"fs-theme": "<?= $this->config( 'resource/fs-theme/baseurl' ) ?>"
111
		}
112
		<?php if( $this->csrf()->name() != '' ) : ?>
113
			, "csrf": {
114
				"name": "<?= $this->csrf()->name(); ?>",
115
				"value": "<?= $this->csrf()->value(); ?>"
116
			}
117
		<?php endif; ?>
118
119
	},
120
	"links": {
121
		"self": "<?= $this->url( $target, $cntl, $action, $params, [], $config ); ?>"
122
	}
123
	<?php if( isset( $this->errors ) ) : ?>
124
		,"errors": <?= json_encode( $this->errors, $pretty ); ?>
125
126
	<?php elseif( isset( $this->items ) ) : ?>
127
		<?php
128
			$data = [];
129
			$items = $this->get( 'items', map() );
130
			$included = $this->jincluded( $items, $fields, ['catalog' => $catFcn] );
131
132
			if( is_map( $items ) )
133
			{
134
				foreach( $items as $item ) {
135
					$data[] = $entryFcn( $item );
136
				}
137
			}
138
			else
139
			{
140
				$data = $entryFcn( $items );
141
			}
142
		 ?>
143
144
		,"data": <?= json_encode( $data, $pretty ); ?>
145
146
		,"included": <?= map( $included )->flat( 1 )->toJson( $pretty ) ?>
147
148
	<?php endif; ?>
149
150
}
151