Standard   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 429
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 105
c 5
b 0
f 0
dl 0
loc 429
rs 9.84
wmc 32

12 Methods

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

277
			$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...
278
			$refItem = $prodItems->get( $id ) ?: ( $litem->getRefItem() ?: $manager->create() );
279
280
			$litem->fromArray( $entry, true )->setPosition( $idx )->setConfigValue( 'quantity', $qty );
281
			$refItem->fromArray( $entry, true );
282
283
			if( isset( $entry['attr'] ) ) {
284
				$refItem = $this->fromArrayAttributes( $refItem, $entry['attr'] );
285
			}
286
287
			$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

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