Passed
Push — master ( 718b20...41c9aa )
by Aimeos
08:12
created

Standard::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 14
rs 10
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-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Category;
12
13
sprintf( 'category' ); // for translation
14
15
16
/**
17
 * Default implementation of product category JQAdm client.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
class Standard
23
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
24
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
25
{
26
	/** admin/jqadm/product/category/name
27
	 * Name of the category subpart used by the JQAdm product implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Category\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the JQAdm class name
33
	 * @since 2016.04
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Adds the required data used in the template
40
	 *
41
	 * @param \Aimeos\Base\View\Iface $view View object
42
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
43
	 */
44
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
45
	{
46
		if( $item = $view->get( 'item' ) ) {
47
			$view->categorySites = $this->getSites( $item );
48
		}
49
50
		return $view;
51
	}
52
53
54
	/**
55
	 * Copies a resource
56
	 *
57
	 * @return string|null HTML output
58
	 */
59
	public function copy() : ?string
60
	{
61
		$view = $this->object()->data( $this->view() );
62
63
		$view->categoryData = $this->toArray( $view->item, true );
64
		$view->categoryBody = parent::copy();
65
66
		return $this->render( $view );
67
	}
68
69
70
	/**
71
	 * Creates a new resource
72
	 *
73
	 * @return string|null HTML output
74
	 */
75
	public function create() : ?string
76
	{
77
		$view = $this->object()->data( $this->view() );
78
		$siteid = $this->context()->locale()->getSiteId();
79
		$data = $view->param( 'category', [] );
80
81
		foreach( $view->value( $data, 'product.lists.id', [] ) as $idx => $value ) {
82
			$data['product.lists.siteid'][$idx] = $siteid;
83
		}
84
85
		$view->categoryData = $data;
86
		$view->categoryBody = parent::create();
87
88
		return $this->render( $view );
89
	}
90
91
92
	/**
93
	 * Returns a single resource
94
	 *
95
	 * @return string|null HTML output
96
	 */
97
	public function get() : ?string
98
	{
99
		$view = $this->object()->data( $this->view() );
100
		$view->categoryData = $this->toArray( $view->item );
101
		$view->categoryBody = parent::get();
102
103
		return $this->render( $view );
104
	}
105
106
107
	/**
108
	 * Saves the data
109
	 *
110
	 * @return string|null HTML output
111
	 */
112
	public function save() : ?string
113
	{
114
		$view = $this->view();
115
116
		$this->fromArray( $view->item, $view->param( 'category', [] ) );
117
		$view->categoryBody = parent::save();
118
119
		return null;
120
	}
121
122
123
	/**
124
	 * Returns the site items for the site IDs used in the list items
125
	 *
126
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item
127
	 * @return \Aimeos\Map List of site items
128
	 */
129
	protected function getSites( \Aimeos\MShop\Product\Item\Iface $item ) : \Aimeos\Map
130
	{
131
		$siteIds = $item->getListItems( 'catalog' )->getSiteId();
132
133
		$manager = \Aimeos\MShop::create( $this->context(), 'locale/site' );
134
		$filter = $manager->filter()->add( 'locale.site.siteid', '==', $siteIds );
135
136
		return $manager->search( $filter )->col( 'locale.site.label', 'locale.site.siteid' );
137
	}
138
139
140
	/**
141
	 * Returns the sub-client given by its name.
142
	 *
143
	 * @param string $type Name of the client type
144
	 * @param string|null $name Name of the sub-client (Default if null)
145
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
146
	 */
147
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
148
	{
149
		/** admin/jqadm/product/category/decorators/excludes
150
		 * Excludes decorators added by the "common" option from the product JQAdm client
151
		 *
152
		 * Decorators extend the functionality of a class by adding new aspects
153
		 * (e.g. log what is currently done), executing the methods of the underlying
154
		 * class only in certain conditions (e.g. only for logged in users) or
155
		 * modify what is returned to the caller.
156
		 *
157
		 * This option allows you to remove a decorator added via
158
		 * "admin/jqadm/common/decorators/default" before they are wrapped
159
		 * around the JQAdm client.
160
		 *
161
		 *  admin/jqadm/product/category/decorators/excludes = array( 'decorator1' )
162
		 *
163
		 * This would remove the decorator named "decorator1" from the list of
164
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
165
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
166
		 *
167
		 * @param array List of decorator names
168
		 * @since 2016.01
169
		 * @category Developer
170
		 * @see admin/jqadm/common/decorators/default
171
		 * @see admin/jqadm/product/category/decorators/global
172
		 * @see admin/jqadm/product/category/decorators/local
173
		 */
174
175
		/** admin/jqadm/product/category/decorators/global
176
		 * Adds a list of globally available decorators only to the product JQAdm client
177
		 *
178
		 * Decorators extend the functionality of a class by adding new aspects
179
		 * (e.g. log what is currently done), executing the methods of the underlying
180
		 * class only in certain conditions (e.g. only for logged in users) or
181
		 * modify what is returned to the caller.
182
		 *
183
		 * This option allows you to wrap global decorators
184
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
185
		 *
186
		 *  admin/jqadm/product/category/decorators/global = array( 'decorator1' )
187
		 *
188
		 * This would add the decorator named "decorator1" defined by
189
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
190
		 *
191
		 * @param array List of decorator names
192
		 * @since 2016.01
193
		 * @category Developer
194
		 * @see admin/jqadm/common/decorators/default
195
		 * @see admin/jqadm/product/category/decorators/excludes
196
		 * @see admin/jqadm/product/category/decorators/local
197
		 */
198
199
		/** admin/jqadm/product/category/decorators/local
200
		 * Adds a list of local decorators only to the product JQAdm client
201
		 *
202
		 * Decorators extend the functionality of a class by adding new aspects
203
		 * (e.g. log what is currently done), executing the methods of the underlying
204
		 * class only in certain conditions (e.g. only for logged in users) or
205
		 * modify what is returned to the caller.
206
		 *
207
		 * This option allows you to wrap local decorators
208
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
209
		 *
210
		 *  admin/jqadm/product/category/decorators/local = array( 'decorator2' )
211
		 *
212
		 * This would add the decorator named "decorator2" defined by
213
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
214
		 *
215
		 * @param array List of decorator names
216
		 * @since 2016.01
217
		 * @category Developer
218
		 * @see admin/jqadm/common/decorators/default
219
		 * @see admin/jqadm/product/category/decorators/excludes
220
		 * @see admin/jqadm/product/category/decorators/global
221
		 */
222
		return $this->createSubClient( 'product/category/' . $type, $name );
223
	}
224
225
226
	/**
227
	 * Returns the list of sub-client names configured for the client.
228
	 *
229
	 * @return array List of JQAdm client names
230
	 */
231
	protected function getSubClientNames() : array
232
	{
233
		/** admin/jqadm/product/category/subparts
234
		 * List of JQAdm sub-clients rendered within the product category section
235
		 *
236
		 * The output of the frontend is composed of the code generated by the JQAdm
237
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
238
		 * that are responsible for rendering certain sub-parts of the output. The
239
		 * sub-clients can contain JQAdm clients themselves and therefore a
240
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
241
		 * the output that is placed inside the container of its parent.
242
		 *
243
		 * At first, always the JQAdm code generated by the parent is printed, then
244
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
245
		 * determines the order of the output of these sub-clients inside the parent
246
		 * container. If the configured list of clients is
247
		 *
248
		 *  array( "subclient1", "subclient2" )
249
		 *
250
		 * you can easily change the order of the output by reordering the subparts:
251
		 *
252
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
253
		 *
254
		 * You can also remove one or more parts if they shouldn't be rendered:
255
		 *
256
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
257
		 *
258
		 * As the clients only generates structural JQAdm, the layout defined via CSS
259
		 * should support adding, removing or reordering content by a fluid like
260
		 * design.
261
		 *
262
		 * @param array List of sub-client names
263
		 * @since 2016.01
264
		 * @category Developer
265
		 */
266
		return $this->context()->config()->get( 'admin/jqadm/product/category/subparts', [] );
267
	}
268
269
270
	/**
271
	 * Creates new and updates existing items using the data array
272
	 *
273
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
274
	 * @param array $data Data array
275
	 */
276
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data )
277
	{
278
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
279
		$listItems = $item->getListItems( 'catalog' );
280
281
		foreach( $data as $entry )
282
		{
283
			$listid = $this->val( $entry, 'product.lists.id' );
284
			$litem = $listItems->pull( $listid ) ?: $manager->createListItem();
0 ignored issues
show
Bug introduced by
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

284
			$litem = $listItems->pull( $listid ) ?: $manager->/** @scrutinizer ignore-call */ createListItem();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
285
286
			$litem->setType( $this->val( $entry, 'product.lists.type' ) )
287
				->setRefId( $this->val( $entry, 'catalog.id' ) );
288
289
			$item->addListItem( 'catalog', $litem );
290
		}
291
292
		$item->deleteListItems( $listItems );
293
	}
294
295
296
	/**
297
	 * Constructs the data array for the view from the given item
298
	 *
299
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
300
	 * @param bool $copy True if items should be copied, false if not
301
	 * @return string[] Multi-dimensional associative list of item data
302
	 */
303
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
304
	{
305
		$data = [];
306
		$siteId = $this->context()->locale()->getSiteId();
307
308
		foreach( $item->getListItems( 'catalog' ) as $listItem )
309
		{
310
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
311
				continue;
312
			}
313
314
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
315
316
			if( $copy === true )
317
			{
318
				$list['product.lists.siteid'] = $siteId;
319
				$list['product.lists.id'] = '';
320
			}
321
322
			$data[] = $list;
323
		}
324
325
		return $data;
326
	}
327
328
329
	/**
330
	 * Returns the rendered template including the view data
331
	 *
332
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
333
	 * @return string HTML output
334
	 */
335
	protected function render( \Aimeos\Base\View\Iface $view ) : string
336
	{
337
		/** admin/jqadm/product/category/template-item
338
		 * Relative path to the HTML body template of the category subpart for products.
339
		 *
340
		 * The template file contains the HTML code and processing instructions
341
		 * to generate the result shown in the body of the frontend. The
342
		 * configuration string is the path to the template file relative
343
		 * to the templates directory (usually in admin/jqadm/templates).
344
		 *
345
		 * You can overwrite the template file configuration in extensions and
346
		 * provide alternative templates. These alternative templates should be
347
		 * named like the default one but with the string "default" replaced by
348
		 * an unique name. You may use the name of your project for this. If
349
		 * you've implemented an alternative client class as well, "default"
350
		 * should be replaced by the name of the new class.
351
		 *
352
		 * @param string Relative path to the template creating the HTML code
353
		 * @since 2016.04
354
		 * @category Developer
355
		 */
356
		$tplconf = 'admin/jqadm/product/category/template-item';
357
		$default = 'product/item-category';
358
359
		return $view->render( $view->config( $tplconf, $default ) );
360
	}
361
}
362