1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
5 | * @copyright Aimeos (aimeos.org), 2016-2024 |
||
6 | * @package Admin |
||
7 | * @subpackage Jsonadm |
||
8 | */ |
||
9 | |||
10 | |||
11 | $options = 0; |
||
12 | if( defined( 'JSON_PRETTY_PRINT' ) ) { |
||
13 | $options = JSON_PRETTY_PRINT; |
||
14 | } |
||
15 | |||
16 | |||
17 | $fields = $this->param( 'fields', [] ); |
||
18 | |||
19 | foreach( (array) $fields as $resource => $list ) { |
||
20 | $fields[$resource] = array_flip( explode( ',', $list ) ); |
||
21 | } |
||
22 | |||
23 | |||
24 | $build = function( \Aimeos\MShop\Catalog\Item\Iface $item, \Aimeos\Map $listItems ) use ( $fields ) |
||
25 | { |
||
26 | $id = $item->getId(); |
||
27 | $type = $item->getResourceType(); |
||
28 | $params = array( 'resource' => $type, 'id' => $id ); |
||
29 | $attributes = $item->toArray( true ); |
||
30 | |||
31 | $target = $this->config( 'admin/jsonadm/url/target' ); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
32 | $cntl = $this->config( 'admin/jsonadm/url/controller', 'jsonadm' ); |
||
33 | $action = $this->config( 'admin/jsonadm/url/action', 'get' ); |
||
34 | $config = $this->config( 'admin/jsonadm/url/config', [] ); |
||
35 | |||
36 | if( isset( $fields[$type] ) ) { |
||
37 | $attributes = array_intersect_key( $attributes, $fields[$type] ); |
||
38 | } |
||
39 | |||
40 | $result = array( |
||
41 | 'id' => $id, |
||
42 | 'type' => $type, |
||
43 | 'attributes' => $attributes, |
||
44 | 'links' => array( |
||
45 | 'self' => $this->url( $target, $cntl, $action, $params, [], $config ) |
||
46 | ), |
||
47 | 'relationships' => [] |
||
48 | ); |
||
49 | |||
50 | foreach( $item->getChildren() as $childItem ) |
||
51 | { |
||
52 | $type = $childItem->getResourceType(); |
||
53 | $result['relationships'][$type]['data'][] = ['id' => $childItem->getId(), 'type' => $type]; |
||
54 | } |
||
55 | |||
56 | foreach( $listItems as $listId => $listItem ) |
||
57 | { |
||
58 | if( $listItem->getParentId() == $id ) |
||
59 | { |
||
60 | $type = $listItem->getDomain(); |
||
61 | $params = array( 'resource' => $listItem->getResourceType(), 'id' => $listId ); |
||
62 | |||
63 | $result['relationships'][$type]['data'][] = [ |
||
64 | 'id' => $listItem->getRefId(), |
||
65 | 'type' => $type, |
||
66 | 'attributes' => $listItem->toArray( true ), |
||
67 | 'links' => array( |
||
68 | 'self' => $this->url( $target, $cntl, $action, $params, [], $config ) |
||
69 | ) |
||
70 | ]; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | return $result; |
||
75 | }; |
||
76 | |||
77 | |||
78 | $data = $this->get( 'data', [] ); |
||
79 | $listItems = $this->get( 'listItems', map() ); |
||
80 | |||
81 | if( is_map( $data ) ) |
||
82 | { |
||
83 | $response = []; |
||
84 | |||
85 | foreach( $data as $item ) { |
||
86 | $response[] = $build( $item, $listItems ); |
||
87 | } |
||
88 | } |
||
89 | elseif( $data !== null ) |
||
90 | { |
||
91 | $response = $build( $data, $listItems ); |
||
92 | } |
||
93 | else |
||
94 | { |
||
95 | $response = null; |
||
96 | } |
||
97 | |||
98 | |||
99 | echo json_encode( $response, $options ); |
||
100 |