Passed
Push — master ( e58325...6c2e3c )
by Aimeos
04:22
created

Standard::fromArray()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

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

274
			$item->addListItem( 'product', $litem, $manager->/** @scrutinizer ignore-call */ saveItem( $refItem ) );

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