Passed
Push — master ( 5ba05b...c83ede )
by Aimeos
03:53
created

Standard   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 421
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 100
c 5
b 0
f 0
dl 0
loc 421
rs 9.92
wmc 31

12 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 1
A copy() 0 7 1
A getSubClient() 0 73 1
A save() 0 11 2
A create() 0 16 2
A getSubClientNames() 0 35 1
A fromArrayAttributes() 0 16 3
B fromArray() 0 35 6
A getDomains() 0 3 1
A render() 0 24 1
A fromArrayStocks() 0 28 5
B toArray() 0 48 7
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Selection;
12
13
sprintf( 'selection' ); // for translation
14
15
16
/**
17
 * Default implementation of product selection 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/selection/name
27
	 * Name of the selection subpart used by the JQAdm product implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Selection\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
	 */
35
36
37
	/**
38
	 * Copies a resource
39
	 *
40
	 * @return string|null HTML output
41
	 */
42
	public function copy() : ?string
43
	{
44
		$view = $this->object()->data( $this->view() );
45
		$view->selectionData = $this->toArray( $view->item, true );
46
		$view->selectionBody = parent::copy();
47
48
		return $this->render( $view );
49
	}
50
51
52
	/**
53
	 * Creates a new resource
54
	 *
55
	 * @return string|null HTML output
56
	 */
57
	public function create() : ?string
58
	{
59
		$view = $this->object()->data( $this->view() );
60
		$siteid = $this->context()->locale()->getSiteId();
61
		$data = $view->param( 'selection', [] );
62
63
		foreach( $data as $idx => $entry )
64
		{
65
			$data[$idx]['product.lists.siteid'] = $siteid;
66
			$data[$idx]['product.siteid'] = $siteid;
67
		}
68
69
		$view->selectionData = $data;
70
		$view->selectionBody = parent::create();
71
72
		return $this->render( $view );
73
	}
74
75
76
	/**
77
	 * Returns a single resource
78
	 *
79
	 * @return string|null HTML output
80
	 */
81
	public function get() : ?string
82
	{
83
		$view = $this->object()->data( $this->view() );
84
		$view->selectionData = $this->toArray( $view->item );
85
		$view->selectionBody = parent::get();
86
87
		return $this->render( $view );
88
	}
89
90
91
	/**
92
	 * Saves the data
93
	 *
94
	 * @return string|null HTML output
95
	 */
96
	public function save() : ?string
97
	{
98
		$view = $this->view();
99
100
		if( in_array( $view->item->getType(), ['group', 'select'] ) )
101
		{
102
			$this->fromArray( $view->item, $view->param( 'selection', [] ) );
103
			$view->selectionBody = parent::save();
104
		}
105
106
		return null;
107
	}
108
109
110
	/**
111
	 * Returns the sub-client given by its name.
112
	 *
113
	 * @param string $type Name of the client type
114
	 * @param string|null $name Name of the sub-client (Default if null)
115
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
116
	 */
117
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
118
	{
119
		/** admin/jqadm/product/selection/decorators/excludes
120
		 * Excludes decorators added by the "common" option from the product JQAdm client
121
		 *
122
		 * Decorators extend the functionality of a class by adding new aspects
123
		 * (e.g. log what is currently done), executing the methods of the underlying
124
		 * class only in certain conditions (e.g. only for logged in users) or
125
		 * modify what is returned to the caller.
126
		 *
127
		 * This option allows you to remove a decorator added via
128
		 * "admin/jqadm/common/decorators/default" before they are wrapped
129
		 * around the JQAdm client.
130
		 *
131
		 *  admin/jqadm/product/selection/decorators/excludes = array( 'decorator1' )
132
		 *
133
		 * This would remove the decorator named "decorator1" from the list of
134
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
135
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
136
		 *
137
		 * @param array List of decorator names
138
		 * @since 2016.01
139
		 * @see admin/jqadm/common/decorators/default
140
		 * @see admin/jqadm/product/selection/decorators/global
141
		 * @see admin/jqadm/product/selection/decorators/local
142
		 */
143
144
		/** admin/jqadm/product/selection/decorators/global
145
		 * Adds a list of globally available decorators only to the product JQAdm client
146
		 *
147
		 * Decorators extend the functionality of a class by adding new aspects
148
		 * (e.g. log what is currently done), executing the methods of the underlying
149
		 * class only in certain conditions (e.g. only for logged in users) or
150
		 * modify what is returned to the caller.
151
		 *
152
		 * This option allows you to wrap global decorators
153
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
154
		 *
155
		 *  admin/jqadm/product/selection/decorators/global = array( 'decorator1' )
156
		 *
157
		 * This would add the decorator named "decorator1" defined by
158
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
159
		 *
160
		 * @param array List of decorator names
161
		 * @since 2016.01
162
		 * @see admin/jqadm/common/decorators/default
163
		 * @see admin/jqadm/product/selection/decorators/excludes
164
		 * @see admin/jqadm/product/selection/decorators/local
165
		 */
166
167
		/** admin/jqadm/product/selection/decorators/local
168
		 * Adds a list of local decorators only to the product JQAdm client
169
		 *
170
		 * Decorators extend the functionality of a class by adding new aspects
171
		 * (e.g. log what is currently done), executing the methods of the underlying
172
		 * class only in certain conditions (e.g. only for logged in users) or
173
		 * modify what is returned to the caller.
174
		 *
175
		 * This option allows you to wrap local decorators
176
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
177
		 *
178
		 *  admin/jqadm/product/selection/decorators/local = array( 'decorator2' )
179
		 *
180
		 * This would add the decorator named "decorator2" defined by
181
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
182
		 *
183
		 * @param array List of decorator names
184
		 * @since 2016.01
185
		 * @see admin/jqadm/common/decorators/default
186
		 * @see admin/jqadm/product/selection/decorators/excludes
187
		 * @see admin/jqadm/product/selection/decorators/global
188
		 */
189
		return $this->createSubClient( 'product/selection/' . $type, $name );
190
	}
191
192
193
	/**
194
	 * Returns the domain names whose items should be fetched too
195
	 *
196
	 * @return string[] List of domain names
197
	 */
198
	protected function getDomains() : array
199
	{
200
		return $this->context()->config()->get( 'admin/jqadm/product/domains', [] );
201
	}
202
203
204
	/**
205
	 * Returns the list of sub-client names configured for the client.
206
	 *
207
	 * @return array List of JQAdm client names
208
	 */
209
	protected function getSubClientNames() : array
210
	{
211
		/** admin/jqadm/product/selection/subparts
212
		 * List of JQAdm sub-clients rendered within the product selection section
213
		 *
214
		 * The output of the frontend is composed of the code generated by the JQAdm
215
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
216
		 * that are responsible for rendering certain sub-parts of the output. The
217
		 * sub-clients can contain JQAdm clients themselves and therefore a
218
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
219
		 * the output that is placed inside the container of its parent.
220
		 *
221
		 * At first, always the JQAdm code generated by the parent is printed, then
222
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
223
		 * determines the order of the output of these sub-clients inside the parent
224
		 * container. If the configured list of clients is
225
		 *
226
		 *  array( "subclient1", "subclient2" )
227
		 *
228
		 * you can easily change the order of the output by reordering the subparts:
229
		 *
230
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
231
		 *
232
		 * You can also remove one or more parts if they shouldn't be rendered:
233
		 *
234
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
235
		 *
236
		 * As the clients only generates structural JQAdm, the layout defined via CSS
237
		 * should support adding, removing or reordering content by a fluid like
238
		 * design.
239
		 *
240
		 * @param array List of sub-client names
241
		 * @since 2016.01
242
		 */
243
		return $this->context()->config()->get( 'admin/jqadm/product/selection/subparts', [] );
244
	}
245
246
247
	/**
248
	 * Creates new and updates existing items using the data array
249
	 *
250
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
251
	 * @param array $data Data array
252
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
253
	 */
254
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
255
	{
256
		$context = $this->context();
257
		$manager = \Aimeos\MShop::create( $context, 'product' );
258
259
		$prodIds = map( $data )->col( 'product.id' )->toArray();
260
		$filter = $manager->filter()->add( ['product.id' => $prodIds] );
261
		$prodItems = $manager->search( $filter, $this->getDomains() );
262
263
		$listItems = $item->getListItems( 'product', 'default', null, false );
264
		$prodIds = [];
265
266
		foreach( $data as $idx => $entry )
267
		{
268
			$id = $this->val( $entry, 'product.id', '' );
269
270
			$litem = $item->getListItem( 'product', 'default', $id, false ) ?: $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

270
			$litem = $item->getListItem( 'product', 'default', $id, false ) ?: $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...
271
			$refItem = $prodItems->get( $id ) ?: ( $litem->getRefItem() ?: $manager->create() );
272
273
			$litem->fromArray( $entry, true )->setPosition( $idx );
274
			$refItem->fromArray( $entry, true );
275
276
			if( isset( $entry['attr'] ) ) {
277
				$refItem = $this->fromArrayAttributes( $refItem, $entry['attr'] );
278
			}
279
280
			$item->addListItem( 'product', $litem, $manager->save( $refItem ) );
0 ignored issues
show
Bug introduced by
It seems like $manager->save($refItem) can also be of type Aimeos\Map; however, parameter $refItem of Aimeos\MShop\Common\Item...ef\Iface::addListItem() does only seem to accept Aimeos\MShop\Common\Item\Iface|null, maybe add an additional type check? ( Ignorable by Annotation )

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

280
			$item->addListItem( 'product', $litem, /** @scrutinizer ignore-type */ $manager->save( $refItem ) );
Loading history...
281
282
			$prodIds[] = $data[$idx]['stock.productid'] = $refItem->getId();
283
			unset( $listItems[$litem->getId()] );
284
		}
285
286
		$this->fromArrayStocks( $prodIds, $data );
287
288
		return $item->deleteListItems( $listItems->toArray() );
289
	}
290
291
292
	/**
293
	 * Updates the variant attributes of the given product item
294
	 *
295
	 * @param \Aimeos\MShop\Product\Item\Iface $refItem Article item object
296
	 * @param array $data Associative list of key/values for product attribute references
297
	 * @return \Aimeos\MShop\Product\Item\Iface Updated artice item object
298
	 */
299
	protected function fromArrayAttributes( \Aimeos\MShop\Product\Item\Iface $refItem, array $data ) : \Aimeos\MShop\Product\Item\Iface
300
	{
301
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
302
		$listItems = $refItem->getListItems( 'attribute', 'variant', null, false );
303
		$pos = 0;
304
305
		foreach( $data as $attr )
306
		{
307
			$listid = $this->val( $attr, 'product.lists.id' );
308
			$litem = $listItems->pull( $listid ) ?: $manager->createListItem();
309
			$litem->setRefId( $this->val( $attr, 'product.lists.refid' ) )->setType( 'variant' )->setPosition( $pos++ );
310
311
			$refItem->addListItem( 'attribute', $litem );
312
		}
313
314
		return $refItem->deleteListItems( $listItems );
315
	}
316
317
318
	/**
319
	 * Updates the stocklevels for the products
320
	 *
321
	 * @param array $data List of product codes
322
	 * @param array $data Data array
323
	 */
324
	protected function fromArrayStocks( array $prodIds, array $data )
325
	{
326
		$manager = \Aimeos\MShop::create( $this->context(), 'stock' );
327
328
		$search = $manager->filter()->slice( 0, count( $prodIds ) )->add( [
329
			'stock.productid' => $prodIds,
330
			'stock.type' => 'default',
331
		] );
332
333
		$stockItems = $manager->search( $search );
334
		$map = $stockItems->col( 'stock.id', 'stock.productid' );
335
		$list = [];
336
337
		foreach( $data as $entry )
338
		{
339
			if( !isset( $entry['stock.id'] ) && !isset( $entry['stock.stocklevel'] ) ) {
340
				continue;
341
			}
342
343
			$stockItem = $stockItems->get( $map[$entry['stock.productid']] ?? null ) ?: $manager->create();
344
			$stockItem->fromArray( $entry, true )->setType( 'default' );
345
346
			$list[] = $stockItem;
347
		}
348
349
		$manager->begin();
350
		$manager->save( $list, false );
351
		$manager->commit();
352
	}
353
354
355
	/**
356
	 * Constructs the data array for the view from the given item
357
	 *
358
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
359
	 * @param bool $copy True if items should be copied
360
	 * @return string[] Multi-dimensional associative list of item data
361
	 */
362
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
363
	{
364
		if( !in_array( $item->getType(), ['group', 'select'] ) ) {
365
			return [];
366
		}
367
368
		$data = [];
369
		$siteId = $this->context()->locale()->getSiteId();
370
371
372
		foreach( $item->getListItems( 'product', 'default', null, false ) as $id => $listItem )
373
		{
374
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
375
				continue;
376
			}
377
378
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
379
380
			if( $copy === true )
381
			{
382
				$list['product.lists.siteid'] = $siteId;
383
				$list['product.lists.id'] = '';
384
				$list['product.siteid'] = $siteId;
385
				$list['product.id'] = '';
386
				$list['product.code'] = $list['product.code'] . '_' . substr( md5( microtime( true ) ), -5 );
387
388
				$list['stock.stocklevel'] = 0;
389
			}
390
			else
391
			{
392
				$list = array_merge( $list, $refItem->getStockItems( 'default' )->first( map() )->toArray() );
393
			}
394
395
			$idx = 0;
396
397
			foreach( $refItem->getListItems( 'attribute', 'variant', null, false ) as $litem )
398
			{
399
				if( ( $attrItem = $litem->getRefItem() ) !== null )
400
				{
401
					$label = $attrItem->getLabel() . ' (' . $attrItem->getType() . ')';
402
					$list['attr'][$idx++] = ['attribute.label' => $label] + $litem->toArray( true ) + $attrItem->toArray( true );
403
				}
404
			}
405
406
			$data[] = $list;
407
		}
408
409
		return $data;
410
	}
411
412
413
	/**
414
	 * Returns the rendered template including the view data
415
	 *
416
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
417
	 * @return string HTML output
418
	 */
419
	protected function render( \Aimeos\Base\View\Iface $view ) : string
420
	{
421
		/** admin/jqadm/product/selection/template-item
422
		 * Relative path to the HTML body template of the selection subpart for products.
423
		 *
424
		 * The template file contains the HTML code and processing instructions
425
		 * to generate the result shown in the body of the frontend. The
426
		 * configuration string is the path to the template file relative
427
		 * to the templates directory (usually in templates/admin/jqadm).
428
		 *
429
		 * You can overwrite the template file configuration in extensions and
430
		 * provide alternative templates. These alternative templates should be
431
		 * named like the default one but with the string "default" replaced by
432
		 * an unique name. You may use the name of your project for this. If
433
		 * you've implemented an alternative client class as well, "default"
434
		 * should be replaced by the name of the new class.
435
		 *
436
		 * @param string Relative path to the template creating the HTML code
437
		 * @since 2016.04
438
		 */
439
		$tplconf = 'admin/jqadm/product/selection/template-item';
440
		$default = 'product/item-selection';
441
442
		return $view->render( $view->config( $tplconf, $default ) );
443
	}
444
}
445