Passed
Push — master ( 0bcc8c...56fe4f )
by Aimeos
03:08
created

Standard::toArray()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 41
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 41
rs 8.6666
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 = '';
48
49
		foreach( $this->getSubClients() as $client ) {
50
			$view->selectionBody .= $client->copy();
51
		}
52
53
		return $this->render( $view );
54
	}
55
56
57
	/**
58
	 * Creates a new resource
59
	 *
60
	 * @return string|null HTML output
61
	 */
62
	public function create() : ?string
63
	{
64
		$view = $this->getObject()->addData( $this->getView() );
65
		$siteid = $this->getContext()->getLocale()->getSiteId();
66
		$data = $view->param( 'selection', [] );
67
68
		foreach( $data as $idx => $entry )
69
		{
70
			$data[$idx]['product.lists.siteid'] = $siteid;
71
			$data[$idx]['product.siteid'] = $siteid;
72
		}
73
74
		$view->selectionData = $data;
75
		$view->selectionBody = '';
76
77
		foreach( $this->getSubClients() as $client ) {
78
			$view->selectionBody .= $client->create();
79
		}
80
81
		return $this->render( $view );
82
	}
83
84
85
	/**
86
	 * Returns a single resource
87
	 *
88
	 * @return string|null HTML output
89
	 */
90
	public function get() : ?string
91
	{
92
		$view = $this->getObject()->addData( $this->getView() );
93
		$view->selectionData = $this->toArray( $view->item );
94
		$view->selectionBody = '';
95
96
		foreach( $this->getSubClients() as $client ) {
97
			$view->selectionBody .= $client->get();
98
		}
99
100
		return $this->render( $view );
101
	}
102
103
104
	/**
105
	 * Saves the data
106
	 *
107
	 * @return string|null HTML output
108
	 */
109
	public function save() : ?string
110
	{
111
		$view = $this->getView();
112
113
		if( $view->item->getType() === 'select' )
114
		{
115
			$this->fromArray( $view->item, $view->param( 'selection', [] ) );
116
			$view->selectionBody = '';
117
118
			foreach( $this->getSubClients() as $client ) {
119
				$view->selectionBody .= $client->save();
120
			}
121
		}
122
123
		return null;
124
	}
125
126
127
	/**
128
	 * Returns the sub-client given by its name.
129
	 *
130
	 * @param string $type Name of the client type
131
	 * @param string|null $name Name of the sub-client (Default if null)
132
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
133
	 */
134
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
135
	{
136
		/** admin/jqadm/product/selection/decorators/excludes
137
		 * Excludes decorators added by the "common" option from the product JQAdm client
138
		 *
139
		 * Decorators extend the functionality of a class by adding new aspects
140
		 * (e.g. log what is currently done), executing the methods of the underlying
141
		 * class only in certain conditions (e.g. only for logged in users) or
142
		 * modify what is returned to the caller.
143
		 *
144
		 * This option allows you to remove a decorator added via
145
		 * "admin/jqadm/common/decorators/default" before they are wrapped
146
		 * around the JQAdm client.
147
		 *
148
		 *  admin/jqadm/product/selection/decorators/excludes = array( 'decorator1' )
149
		 *
150
		 * This would remove the decorator named "decorator1" from the list of
151
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
152
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
153
		 *
154
		 * @param array List of decorator names
155
		 * @since 2016.01
156
		 * @category Developer
157
		 * @see admin/jqadm/common/decorators/default
158
		 * @see admin/jqadm/product/selection/decorators/global
159
		 * @see admin/jqadm/product/selection/decorators/local
160
		 */
161
162
		/** admin/jqadm/product/selection/decorators/global
163
		 * Adds a list of globally available decorators only to the product JQAdm client
164
		 *
165
		 * Decorators extend the functionality of a class by adding new aspects
166
		 * (e.g. log what is currently done), executing the methods of the underlying
167
		 * class only in certain conditions (e.g. only for logged in users) or
168
		 * modify what is returned to the caller.
169
		 *
170
		 * This option allows you to wrap global decorators
171
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
172
		 *
173
		 *  admin/jqadm/product/selection/decorators/global = array( 'decorator1' )
174
		 *
175
		 * This would add the decorator named "decorator1" defined by
176
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
177
		 *
178
		 * @param array List of decorator names
179
		 * @since 2016.01
180
		 * @category Developer
181
		 * @see admin/jqadm/common/decorators/default
182
		 * @see admin/jqadm/product/selection/decorators/excludes
183
		 * @see admin/jqadm/product/selection/decorators/local
184
		 */
185
186
		/** admin/jqadm/product/selection/decorators/local
187
		 * Adds a list of local decorators only to the product JQAdm client
188
		 *
189
		 * Decorators extend the functionality of a class by adding new aspects
190
		 * (e.g. log what is currently done), executing the methods of the underlying
191
		 * class only in certain conditions (e.g. only for logged in users) or
192
		 * modify what is returned to the caller.
193
		 *
194
		 * This option allows you to wrap local decorators
195
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
196
		 *
197
		 *  admin/jqadm/product/selection/decorators/local = array( 'decorator2' )
198
		 *
199
		 * This would add the decorator named "decorator2" defined by
200
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
201
		 *
202
		 * @param array List of decorator names
203
		 * @since 2016.01
204
		 * @category Developer
205
		 * @see admin/jqadm/common/decorators/default
206
		 * @see admin/jqadm/product/selection/decorators/excludes
207
		 * @see admin/jqadm/product/selection/decorators/global
208
		 */
209
		return $this->createSubClient( 'product/selection/' . $type, $name );
210
	}
211
212
213
	/**
214
	 * Returns the list of sub-client names configured for the client.
215
	 *
216
	 * @return array List of JQAdm client names
217
	 */
218
	protected function getSubClientNames() : array
219
	{
220
		/** admin/jqadm/product/selection/standard/subparts
221
		 * List of JQAdm sub-clients rendered within the product selection section
222
		 *
223
		 * The output of the frontend is composed of the code generated by the JQAdm
224
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
225
		 * that are responsible for rendering certain sub-parts of the output. The
226
		 * sub-clients can contain JQAdm clients themselves and therefore a
227
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
228
		 * the output that is placed inside the container of its parent.
229
		 *
230
		 * At first, always the JQAdm code generated by the parent is printed, then
231
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
232
		 * determines the order of the output of these sub-clients inside the parent
233
		 * container. If the configured list of clients is
234
		 *
235
		 *  array( "subclient1", "subclient2" )
236
		 *
237
		 * you can easily change the order of the output by reordering the subparts:
238
		 *
239
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
240
		 *
241
		 * You can also remove one or more parts if they shouldn't be rendered:
242
		 *
243
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
244
		 *
245
		 * As the clients only generates structural JQAdm, the layout defined via CSS
246
		 * should support adding, removing or reordering content by a fluid like
247
		 * design.
248
		 *
249
		 * @param array List of sub-client names
250
		 * @since 2016.01
251
		 * @category Developer
252
		 */
253
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/selection/standard/subparts', [] );
254
	}
255
256
257
	/**
258
	 * Creates new and updates existing items using the data array
259
	 *
260
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
261
	 * @param array $data Data array
262
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
263
	 */
264
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
265
	{
266
		$context = $this->getContext();
267
		$manager = \Aimeos\MShop::create( $context, 'product' );
268
		$listManager = \Aimeos\MShop::create( $context, 'product/lists' );
269
270
		$listItems = $item->getListItems( 'product', 'default', null, false );
271
		$codes = [];
272
273
		foreach( $data as $idx => $entry )
274
		{
275
			if( ( $litem = $item->getListItem( 'product', 'default', $entry['product.id'], false ) ) === null ) {
276
				$litem = $listManager->createItem()->setType( 'default' );
277
			}
278
279
			if( ( $refItem = $litem->getRefItem() ) === null ) {
280
				$refItem = $manager->createItem()->setType( 'default' );
281
			}
282
283
			$litem->fromArray( $entry, true );
284
			$litem->setPosition( $idx );
285
286
			$refItem->fromArray( $entry, true );
287
288
			if( isset( $entry['attr'] ) ) {
289
				$refItem = $this->fromArrayAttributes( $refItem, $entry['attr'] );
290
			}
291
292
			$item->addListItem( 'product', $litem, $refItem );
293
			unset( $listItems[$litem->getId()] );
294
295
			$codes[] = $refItem->getCode();
296
		}
297
298
		$this->fromArrayStocks( $codes, $data );
299
300
		return $item->deleteListItems( $listItems->toArray() );
301
	}
302
303
304
	/**
305
	 * Updates the variant attributes of the given product item
306
	 *
307
	 * @param \Aimeos\MShop\Product\Item\Iface $refItem Article item object
308
	 * @param array $entry Associative list of key/values for product attribute references
309
	 * @return \Aimeos\MShop\Product\Item\Iface Updated artice item object
310
	 */
311
	protected function fromArrayAttributes( \Aimeos\MShop\Product\Item\Iface $refItem, array $entry )
312
	{
313
		$listManager = \Aimeos\MShop::create( $this->getContext(), 'product/lists' );
314
		$litems = $refItem->getListItems( 'attribute', 'variant', null, false );
315
316
		foreach( $entry as $pos => $attr )
317
		{
318
			if( !isset( $attr['product.lists.refid'] ) || $attr['product.lists.refid'] == '' ) {
319
				continue;
320
			}
321
322
			if( ( $litem = $refItem->getListItem( 'attribute', 'variant', $attr['product.lists.refid'], false ) ) === null ) {
323
				$litem = $listManager->createItem()->setType( 'variant' );
324
			}
325
326
			$litem = $litem->fromArray( $attr, true )->setPosition( $pos );
327
328
			$refItem->addListItem( 'attribute', $litem, $litem->getRefItem() );
329
			unset( $litems[$litem->getId()] );
330
		}
331
332
		return $refItem->deleteListItems( $litems->toArray() );
333
	}
334
335
336
	/**
337
	 * Updates the stocklevels for the products
338
	 *
339
	 * @param array $data List of product codes
340
	 * @param array $data Data array
341
	 */
342
	protected function fromArrayStocks( array $codes, array $data )
343
	{
344
		$manager = \Aimeos\MShop::create( $this->getContext(), 'stock' );
345
346
		$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
347
		$search->setConditions( $search->combine( '&&', [
348
			$search->compare( '==', 'stock.productcode', $codes ),
349
			$search->compare( '==', 'stock.type', 'default' ),
350
		] ) );
351
352
		$stockItems = $manager->searchItems( $search );
353
		$map = $stockItems->col( 'stock.productcode', 'stock.id' );
354
		$list = [];
355
356
		foreach( $data as $entry )
357
		{
358
			$code = $entry['product.code'];
359
360
			if( ( $stockItem = $stockItems->get( $map[$code] ?? null ) ) === null ) {
361
				$stockItem = $manager->createItem();
362
			}
363
364
			$stockItem->fromArray( $entry, true )->setProductCode( $code )->setType( 'default' );
365
			unset( $stockItems[$stockItem->getId()] );
366
367
			$list[] = $stockItem;
368
		}
369
370
		try
371
		{
372
			$manager->begin();
373
			$manager->deleteItems( $stockItems->toArray() )->saveItems( $list, false );
374
			$manager->commit();
375
		}
376
		catch( \Exception $e )
377
		{
378
			$manager->rollback();
379
			throw $e;
380
		}
381
	}
382
383
384
	/**
385
	 * Constructs the data array for the view from the given item
386
	 *
387
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
388
	 * @param bool $copy True if items should be copied
389
	 * @return string[] Multi-dimensional associative list of item data
390
	 */
391
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
392
	{
393
		if( $item->getType() !== 'select' ) {
394
			return [];
395
		}
396
397
		$data = [];
398
		$siteId = $this->getContext()->getLocale()->getSiteId();
399
400
401
		foreach( $item->getListItems( 'product', 'default', null, false ) as $id => $listItem )
402
		{
403
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
404
				continue;
405
			}
406
407
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
408
409
			if( $copy === true )
410
			{
411
				$list['product.lists.siteid'] = $siteId;
412
				$list['product.lists.id'] = '';
413
				$list['product.siteid'] = $siteId;
414
				$list['product.id'] = null;
415
			}
416
417
			$idx = 0;
418
419
			foreach( $refItem->getListItems( 'attribute', 'variant', null, false ) as $litem )
420
			{
421
				if( ( $attrItem = $litem->getRefItem() ) !== null ) {
422
					$list['attr'][$idx++] = $litem->toArray( true ) + $attrItem->toArray( true );
423
				}
424
			}
425
426
			$list = array_merge( $list, $refItem->getStockItems( 'default' )->first( map() )->toArray() );
427
428
			$data[] = $list;
429
		}
430
431
		return $data;
432
	}
433
434
435
	/**
436
	 * Returns the rendered template including the view data
437
	 *
438
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
439
	 * @return string|null HTML output
440
	 */
441
	protected function render( \Aimeos\MW\View\Iface $view ) : string
442
	{
443
		/** admin/jqadm/product/selection/template-item
444
		 * Relative path to the HTML body template of the selection subpart for products.
445
		 *
446
		 * The template file contains the HTML code and processing instructions
447
		 * to generate the result shown in the body of the frontend. The
448
		 * configuration string is the path to the template file relative
449
		 * to the templates directory (usually in admin/jqadm/templates).
450
		 *
451
		 * You can overwrite the template file configuration in extensions and
452
		 * provide alternative templates. These alternative templates should be
453
		 * named like the default one but with the string "default" replaced by
454
		 * an unique name. You may use the name of your project for this. If
455
		 * you've implemented an alternative client class as well, "default"
456
		 * should be replaced by the name of the new class.
457
		 *
458
		 * @param string Relative path to the template creating the HTML code
459
		 * @since 2016.04
460
		 * @category Developer
461
		 */
462
		$tplconf = 'admin/jqadm/product/selection/template-item';
463
		$default = 'product/item-selection-standard';
464
465
		return $view->render( $view->config( $tplconf, $default ) );
466
	}
467
}
468