Passed
Push — master ( d0aa0f...2addb8 )
by Aimeos
04:10
created

Standard::fromArray()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 3
nop 2
dl 0
loc 36
rs 8.9617
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-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 list of sub-client names configured for the client.
195
	 *
196
	 * @return array List of JQAdm client names
197
	 */
198
	protected function getSubClientNames() : array
199
	{
200
		/** admin/jqadm/product/selection/subparts
201
		 * List of JQAdm sub-clients rendered within the product selection section
202
		 *
203
		 * The output of the frontend is composed of the code generated by the JQAdm
204
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
205
		 * that are responsible for rendering certain sub-parts of the output. The
206
		 * sub-clients can contain JQAdm clients themselves and therefore a
207
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
208
		 * the output that is placed inside the container of its parent.
209
		 *
210
		 * At first, always the JQAdm code generated by the parent is printed, then
211
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
212
		 * determines the order of the output of these sub-clients inside the parent
213
		 * container. If the configured list of clients is
214
		 *
215
		 *  array( "subclient1", "subclient2" )
216
		 *
217
		 * you can easily change the order of the output by reordering the subparts:
218
		 *
219
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
220
		 *
221
		 * You can also remove one or more parts if they shouldn't be rendered:
222
		 *
223
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
224
		 *
225
		 * As the clients only generates structural JQAdm, the layout defined via CSS
226
		 * should support adding, removing or reordering content by a fluid like
227
		 * design.
228
		 *
229
		 * @param array List of sub-client names
230
		 * @since 2016.01
231
		 */
232
		return $this->context()->config()->get( 'admin/jqadm/product/selection/subparts', [] );
233
	}
234
235
236
	/**
237
	 * Creates new and updates existing items using the data array
238
	 *
239
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
240
	 * @param array $data Data array
241
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
242
	 */
243
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
244
	{
245
		$context = $this->context();
246
		$manager = \Aimeos\MShop::create( $context, 'product' );
247
		$listManager = \Aimeos\MShop::create( $context, 'product/lists' );
248
249
		$prodIds = map( $data )->col( 'product.id' )->toArray();
250
		$filter = $manager->filter()->add( ['product.id' => $prodIds] );
251
		$prodItems = $manager->search( $filter, ['attribute' => ['variant']] );
252
253
		$listItems = $item->getListItems( 'product', 'default', null, false );
254
		$prodIds = [];
255
256
		foreach( $data as $idx => $entry )
257
		{
258
			$id = $this->val( $entry, 'product.id', '' );
259
260
			$litem = $item->getListItem( 'product', 'default', $id, false ) ?: $listManager->create();
261
			$refItem = $prodItems->get( $id ) ?: ( $litem->getRefItem() ?: $manager->create() );
262
263
			$litem->fromArray( $entry, true )->setPosition( $idx );
264
			$refItem->fromArray( $entry, true );
265
266
			if( isset( $entry['attr'] ) ) {
267
				$refItem = $this->fromArrayAttributes( $refItem, $entry['attr'] );
268
			}
269
270
			$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

270
			$item->addListItem( 'product', $litem, /** @scrutinizer ignore-type */ $manager->save( $refItem ) );
Loading history...
271
272
			$prodIds[] = $data[$idx]['stock.productid'] = $refItem->getId();
273
			unset( $listItems[$litem->getId()] );
274
		}
275
276
		$this->fromArrayStocks( $prodIds, $data );
277
278
		return $item->deleteListItems( $listItems->toArray() );
279
	}
280
281
282
	/**
283
	 * Updates the variant attributes of the given product item
284
	 *
285
	 * @param \Aimeos\MShop\Product\Item\Iface $refItem Article item object
286
	 * @param array $data Associative list of key/values for product attribute references
287
	 * @return \Aimeos\MShop\Product\Item\Iface Updated artice item object
288
	 */
289
	protected function fromArrayAttributes( \Aimeos\MShop\Product\Item\Iface $refItem, array $data ) : \Aimeos\MShop\Product\Item\Iface
290
	{
291
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
292
		$listItems = $refItem->getListItems( 'attribute', 'variant', null, false );
293
		$pos = 0;
294
295
		foreach( $data as $attr )
296
		{
297
			$listid = $this->val( $attr, 'product.lists.id' );
298
			$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

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