Standard::getItems()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 7
nop 3
dl 0
loc 37
rs 9.2568
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Admin
7
 * @subpackage JsonAdm
8
 */
9
10
11
namespace Aimeos\Admin\JsonAdm\Catalog;
12
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
17
/**
18
 * JSON API catalog client
19
 *
20
 * @package Admin
21
 * @subpackage JsonAdm
22
 */
23
class Standard
24
	extends \Aimeos\Admin\JsonAdm\Standard
25
	implements \Aimeos\Admin\JsonAdm\Common\Iface
26
{
27
	/** admin/jsonadm/catalog/decorators/excludes
28
	 * Excludes decorators added by the "common" option from the JSON API clients
29
	 *
30
	 * Decorators extend the functionality of a class by adding new aspects
31
	 * (e.g. log what is currently done), executing the methods of the underlying
32
	 * class only in certain conditions (e.g. only for logged in users) or
33
	 * modify what is returned to the caller.
34
	 *
35
	 * This option allows you to remove a decorator added via
36
	 * "admin/jsonadm/common/decorators/default" before they are wrapped
37
	 * around the Jsonadm client.
38
	 *
39
	 *  admin/jsonadm/decorators/excludes = array( 'decorator1' )
40
	 *
41
	 * This would remove the decorator named "decorator1" from the list of
42
	 * common decorators ("\Aimeos\Admin\JsonAdm\Common\Decorator\*") added via
43
	 * "admin/jsonadm/common/decorators/default" for the JSON API client.
44
	 *
45
	 * @param array List of decorator names
46
	 * @since 2016.01
47
	 * @category Developer
48
	 * @see admin/jsonadm/common/decorators/default
49
	 * @see admin/jsonadm/catalog/decorators/global
50
	 * @see admin/jsonadm/catalog/decorators/local
51
	 */
52
53
	/** admin/jsonadm/catalog/decorators/global
54
	 * Adds a list of globally available decorators only to the Jsonadm client
55
	 *
56
	 * Decorators extend the functionality of a class by adding new aspects
57
	 * (e.g. log what is currently done), executing the methods of the underlying
58
	 * class only in certain conditions (e.g. only for logged in users) or
59
	 * modify what is returned to the caller.
60
	 *
61
	 * This option allows you to wrap global decorators
62
	 * ("\Aimeos\Admin\Jsonadm\Common\Decorator\*") around the Jsonadm
63
	 * client.
64
	 *
65
	 *  admin/jsonadm/catalog/decorators/global = array( 'decorator1' )
66
	 *
67
	 * This would add the decorator named "decorator1" defined by
68
	 * "\Aimeos\Admin\Jsonadm\Common\Decorator\Decorator1" only to the
69
	 * "catalog" Jsonadm client.
70
	 *
71
	 * @param array List of decorator names
72
	 * @since 2016.01
73
	 * @category Developer
74
	 * @see admin/jsonadm/common/decorators/default
75
	 * @see admin/jsonadm/catalog/decorators/excludes
76
	 * @see admin/jsonadm/catalog/decorators/local
77
	 */
78
79
	/** admin/jsonadm/catalog/decorators/local
80
	 * Adds a list of local decorators only to the Jsonadm client
81
	 *
82
	 * Decorators extend the functionality of a class by adding new aspects
83
	 * (e.g. log what is currently done), executing the methods of the underlying
84
	 * class only in certain conditions (e.g. only for logged in users) or
85
	 * modify what is returned to the caller.
86
	 *
87
	 * This option allows you to wrap local decorators
88
	 * ("\Aimeos\Admin\Jsonadm\Catalog\Decorator\*") around the Jsonadm
89
	 * client.
90
	 *
91
	 *  admin/jsonadm/catalog/decorators/local = array( 'decorator2' )
92
	 *
93
	 * This would add the decorator named "decorator2" defined by
94
	 * "\Aimeos\Admin\Jsonadm\Catalog\Decorator\Decorator2" only to the
95
	 * "catalog" Jsonadm client.
96
	 *
97
	 * @param array List of decorator names
98
	 * @since 2016.01
99
	 * @category Developer
100
	 * @see admin/jsonadm/common/decorators/default
101
	 * @see admin/jsonadm/catalog/decorators/excludes
102
	 * @see admin/jsonadm/catalog/decorators/global
103
	 */
104
105
106
	/**
107
	 * Returns the requested resource or the resource list
108
	 *
109
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
110
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
111
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
112
	 */
113
	public function get( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
114
	{
115
		/** admin/jsonadm/partials/catalog/template-data
116
		 * Relative path to the data partial template file for the catalog client
117
		 *
118
		 * Partials are templates which are reused in other templates and generate
119
		 * reoccuring blocks filled with data from the assigned values. The data
120
		 * partial creates the "data" part for the JSON API response.
121
		 *
122
		 * The partial template files are usually stored in the templates/partials/ folder
123
		 * of the core or the extensions. The configured path to the partial file must
124
		 * be relative to the templates/ folder, e.g. "partials/data".
125
		 *
126
		 * @param string Relative path to the template file
127
		 * @since 2016.07
128
		 * @category Developer
129
		 */
130
		$this->view()->assign( array( 'partial-data' => 'admin/jsonadm/partials/catalog/template-data' ) );
131
132
		return parent::get( $request, $response );
133
	}
134
135
136
	/**
137
	 * Returns the items with parent/child relationships
138
	 *
139
	 * @param \Aimeos\Map $items List of items implementing \Aimeos\MShop\Common\Item\Iface
140
	 * @param array $include List of resource types that should be fetched
141
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface
142
	 */
143
	protected function getChildItems( \Aimeos\Map $items, array $include ) : \Aimeos\Map
144
	{
145
		$list = map();
146
147
		if( in_array( 'catalog', $include ) )
148
		{
149
			foreach( $items as $item ) {
150
				$list = $list->push( $item )->merge( $this->getChildItems( map( $item->getChildren() ), $include ) );
151
			}
152
		}
153
154
		return $list;
155
	}
156
157
158
	/**
159
	 * Retrieves the item or items and adds the data to the view
160
	 *
161
	 * @param \Psr\Http\Message\ServerRequestInterface $request Request object
162
	 * @param \Psr\Http\Message\ResponseInterface $response Response object
163
	 * @return \Psr\Http\Message\ResponseInterface Modified response object
164
	 */
165
	protected function getItems( \Aimeos\Base\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface
166
	{
167
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
168
169
		if( ( $key = $view->param( 'aggregate' ) ) !== null )
170
		{
171
			$search = $this->initCriteria( $manager->filter(), $view->param() );
172
			$view->data = $manager->aggregate( $search, explode( ',', $key ) );
173
			return $response;
174
		}
175
176
		$include = ( ( $include = $view->param( 'include' ) ) !== null ? explode( ',', $include ) : [] );
177
		$search = $this->initCriteria( $manager->filter(), $view->param() );
178
		$total = 1;
179
180
		if( ( $id = $view->param( 'id' ) ) == null )
181
		{
182
			$view->data = $manager->search( $search, $include, $total );
183
			$view->listItems = $this->getListItems( $view->data, $include );
184
			$view->childItems = map();
185
		}
186
		else
187
		{
188
			$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE;
189
			if( in_array( 'catalog', $include ) ) {
190
				$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_LIST;
191
			}
192
193
			$view->data = $manager->getTree( $id, $include, $level, $search );
194
			$view->listItems = $this->getListItems( map( [$id => $view->data] ), $include );
195
			$view->childItems = $this->getChildItems( map( $view->data->getChildren() ), $include );
196
		}
197
198
		$view->refItems = $this->getRefItems( $view->listItems );
199
		$view->total = $total;
200
201
		return $response;
202
	}
203
204
205
	/**
206
	 * Returns the list items for association relationships
207
	 *
208
	 * @param \Aimeos\Map $items List of items implementing \Aimeos\MShop\Common\Item\Iface
209
	 * @param array $include List of resource types that should be fetched
210
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Lists\Iface
211
	 */
212
	protected function getListItems( \Aimeos\Map $items, array $include ) : \Aimeos\Map
213
	{
214
		return $items->getListItems( null, null, null, false )->collapse();
215
	}
216
217
218
	/**
219
	 * Saves and returns the new or updated item
220
	 *
221
	 * @param \Aimeos\MShop\Common\Manager\Iface $manager Manager responsible for the items
222
	 * @param \stdClass $entry Object including "id" and "attributes" elements
223
	 * @return \Aimeos\MShop\Common\Item\Iface New or updated item
224
	 */
225
	protected function saveEntry( \Aimeos\MShop\Common\Manager\Iface $manager, \stdClass $entry ) : \Aimeos\MShop\Common\Item\Iface
226
	{
227
		$targetId = ( isset( $entry->targetid ) ? $entry->targetid : null );
228
		$refId = ( isset( $entry->refid ) ? $entry->refid : null );
229
230
		if( isset( $entry->id ) )
231
		{
232
			$item = $manager->get( $entry->id );
233
234
			if( isset( $entry->attributes ) && ( $attr = (array) $entry->attributes ) ) {
235
				$item = $item->fromArray( $attr, true );
236
			}
237
238
			$item = $manager->save( $item );
239
240
			if( isset( $entry->parentid ) && $targetId !== null ) {
241
				$manager->move( $item->getId(), $entry->parentid, $targetId, $refId );
242
			}
243
		}
244
		else
245
		{
246
			$item = $manager->create();
247
248
			if( isset( $entry->attributes ) && ( $attr = (array) $entry->attributes ) ) {
249
				$item = $item->fromArray( $attr, true );
250
			}
251
252
			$item = $manager->insert( $item, $targetId, $refId );
253
		}
254
255
		if( isset( $entry->relationships ) ) {
256
			$this->saveRelationships( $manager, $item, $entry->relationships );
257
		}
258
259
		return $manager->get( $item->getId() );
260
	}
261
}
262