Passed
Push — master ( 64b209...d4bb0c )
by Aimeos
04:10
created

Standard::fromArray()   A

Complexity

Conditions 6
Paths 13

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 13
nop 2
dl 0
loc 37
rs 9.0444
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), 2017-2021
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Price;
12
13
sprintf( 'price' ); // for translation
14
15
16
/**
17
 * Default implementation of product price 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/price/name
27
	 * Name of the price subpart used by the JQAdm product implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Price\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 2017.07
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Adds the required data used in the price template
40
	 *
41
	 * @param \Aimeos\MW\View\Iface $view View object
42
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
43
	 */
44
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
45
	{
46
		$context = $this->getContext();
47
48
		$priceTypeManager = \Aimeos\MShop::create( $context, 'price/type' );
49
		$listTypeManager = \Aimeos\MShop::create( $context, 'product/lists/type' );
50
		$currencyManager = \Aimeos\MShop::create( $context, 'locale/currency' );
51
52
		$search = $priceTypeManager->filter( true )->slice( 0, 10000 );
53
		$search->setConditions( $search->compare( '==', 'price.type.domain', 'product' ) );
54
		$search->setSortations( array( $search->sort( '+', 'price.type.position' ) ) );
55
56
		$listSearch = $listTypeManager->filter( true )->slice( 0, 10000 );
57
		$listSearch->setConditions( $listSearch->compare( '==', 'product.lists.type.domain', 'price' ) );
58
		$listSearch->setSortations( array( $listSearch->sort( '+', 'product.lists.type.position' ) ) );
59
60
		$view->priceTypes = $priceTypeManager->search( $search );
61
		$view->priceListTypes = $listTypeManager->search( $listSearch );
62
		$view->priceCurrencies = $currencyManager->search( $currencyManager->filter( true )->slice( 0, 10000 ) );
63
64
		if( $view->priceCurrencies->isEmpty() )
65
		{
66
			$msg = $context->translate( 'admin', 'No currencies available. Please enable at least one currency' );
67
			throw new \Aimeos\Admin\JQAdm\Exception( $msg );
68
		}
69
70
		return $view;
71
	}
72
73
74
	/**
75
	 * Copies a resource
76
	 *
77
	 * @return string|null HTML output
78
	 */
79
	public function copy() : ?string
80
	{
81
		$view = $this->getObject()->addData( $this->getView() );
82
		$view->priceCustom = $this->isCustom( $view->item );
83
		$view->priceData = $this->toArray( $view->item, true );
84
		$view->priceBody = parent::copy();
85
86
		return $this->render( $view );
87
	}
88
89
90
	/**
91
	 * Creates a new resource
92
	 *
93
	 * @return string|null HTML output
94
	 */
95
	public function create() : ?string
96
	{
97
		$view = $this->getObject()->addData( $this->getView() );
98
		$siteid = $this->getContext()->getLocale()->getSiteId();
99
		$data = $view->param( 'price', [] );
100
101
		foreach( $data as $idx => $entry )
102
		{
103
			$data[$idx]['product.lists.siteid'] = $siteid;
104
			$data[$idx]['price.siteid'] = $siteid;
105
		}
106
107
		$view->priceCustom = $this->isCustom( $view->item );
108
		$view->priceBody = parent::create();
109
		$view->priceData = $data;
110
111
		return $this->render( $view );
112
	}
113
114
115
	/**
116
	 * Deletes a resource
117
	 *
118
	 * @return string|null HTML output
119
	 */
120
	public function delete() : ?string
121
	{
122
		parent::delete();
123
124
		$item = $this->getView()->item;
125
		$item->deleteListItems( $item->getListItems( 'price', null, null, false )->toArray(), true );
126
127
		return null;
128
	}
129
130
131
	/**
132
	 * Returns a single resource
133
	 *
134
	 * @return string|null HTML output
135
	 */
136
	public function get() : ?string
137
	{
138
		$view = $this->getObject()->addData( $this->getView() );
139
		$view->priceCustom = $this->isCustom( $view->item );
140
		$view->priceData = $this->toArray( $view->item );
141
		$view->priceBody = parent::get();
142
143
		return $this->render( $view );
144
	}
145
146
147
	/**
148
	 * Saves the data
149
	 *
150
	 * @return string|null HTML output
151
	 */
152
	public function save() : ?string
153
	{
154
		$view = $this->getView();
155
156
		$view->item = $this->setCustom( $view->item, $view->param( 'pricecustom' ) );
157
		$view->item = $this->fromArray( $view->item, $view->param( 'price', [] ) );
158
		$view->priceBody = parent::save();
159
160
		return null;
161
	}
162
163
164
	/**
165
	 * Returns the sub-client given by its name.
166
	 *
167
	 * @param string $type Name of the client type
168
	 * @param string|null $name Name of the sub-client (Default if null)
169
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
170
	 */
171
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
172
	{
173
		/** admin/jqadm/product/price/decorators/excludes
174
		 * Excludes decorators added by the "common" option from 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 remove a decorator added via
182
		 * "admin/jqadm/common/decorators/default" before they are wrapped
183
		 * around the JQAdm client.
184
		 *
185
		 *  admin/jqadm/product/price/decorators/excludes = array( 'decorator1' )
186
		 *
187
		 * This would remove the decorator named "decorator1" from the list of
188
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
189
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
190
		 *
191
		 * @param array List of decorator names
192
		 * @since 2016.01
193
		 * @category Developer
194
		 * @see admin/jqadm/common/decorators/default
195
		 * @see admin/jqadm/product/price/decorators/global
196
		 * @see admin/jqadm/product/price/decorators/local
197
		 */
198
199
		/** admin/jqadm/product/price/decorators/global
200
		 * Adds a list of globally available decorators only to the product JQAdm client
201
		 *
202
		 * Decorators extend the functionality of a class by adding new aspects
203
		 * (e.g. log what is currently done), executing the methods of the underlying
204
		 * class only in certain conditions (e.g. only for logged in users) or
205
		 * modify what is returned to the caller.
206
		 *
207
		 * This option allows you to wrap global decorators
208
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
209
		 *
210
		 *  admin/jqadm/product/price/decorators/global = array( 'decorator1' )
211
		 *
212
		 * This would add the decorator named "decorator1" defined by
213
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
214
		 *
215
		 * @param array List of decorator names
216
		 * @since 2016.01
217
		 * @category Developer
218
		 * @see admin/jqadm/common/decorators/default
219
		 * @see admin/jqadm/product/price/decorators/excludes
220
		 * @see admin/jqadm/product/price/decorators/local
221
		 */
222
223
		/** admin/jqadm/product/price/decorators/local
224
		 * Adds a list of local decorators only to the product JQAdm client
225
		 *
226
		 * Decorators extend the functionality of a class by adding new aspects
227
		 * (e.g. log what is currently done), executing the methods of the underlying
228
		 * class only in certain conditions (e.g. only for logged in users) or
229
		 * modify what is returned to the caller.
230
		 *
231
		 * This option allows you to wrap local decorators
232
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
233
		 *
234
		 *  admin/jqadm/product/price/decorators/local = array( 'decorator2' )
235
		 *
236
		 * This would add the decorator named "decorator2" defined by
237
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
238
		 *
239
		 * @param array List of decorator names
240
		 * @since 2016.01
241
		 * @category Developer
242
		 * @see admin/jqadm/common/decorators/default
243
		 * @see admin/jqadm/product/price/decorators/excludes
244
		 * @see admin/jqadm/product/price/decorators/global
245
		 */
246
		return $this->createSubClient( 'product/price/' . $type, $name );
247
	}
248
249
250
	/**
251
	 * Returns the list of sub-client names configured for the client.
252
	 *
253
	 * @return array List of JQAdm client names
254
	 */
255
	protected function getSubClientNames() : array
256
	{
257
		/** admin/jqadm/product/price/subparts
258
		 * List of JQAdm sub-clients rendered within the product price section
259
		 *
260
		 * The output of the frontend is composed of the code generated by the JQAdm
261
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
262
		 * that are responsible for rendering certain sub-parts of the output. The
263
		 * sub-clients can contain JQAdm clients themselves and therefore a
264
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
265
		 * the output that is placed inside the container of its parent.
266
		 *
267
		 * At first, always the JQAdm code generated by the parent is printed, then
268
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
269
		 * determines the order of the output of these sub-clients inside the parent
270
		 * container. If the configured list of clients is
271
		 *
272
		 *  array( "subclient1", "subclient2" )
273
		 *
274
		 * you can easily change the order of the output by reordering the subparts:
275
		 *
276
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
277
		 *
278
		 * You can also remove one or more parts if they shouldn't be rendered:
279
		 *
280
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
281
		 *
282
		 * As the clients only generates structural JQAdm, the layout defined via CSS
283
		 * should support adding, removing or reordering content by a fluid like
284
		 * design.
285
		 *
286
		 * @param array List of sub-client names
287
		 * @since 2016.01
288
		 * @category Developer
289
		 */
290
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/price/subparts', [] );
291
	}
292
293
294
	/**
295
	 * Creates new and updates existing items using the data array
296
	 *
297
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
298
	 * @param array $data Data array
299
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
300
	 */
301
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
302
	{
303
		$context = $this->getContext();
304
305
		$priceManager = \Aimeos\MShop::create( $context, 'price' );
306
		$listManager = \Aimeos\MShop::create( $context, 'product/lists' );
307
308
		$listItems = $item->getListItems( 'price', null, null, false );
309
310
311
		foreach( $data as $idx => $entry )
312
		{
313
			$listType = $entry['product.lists.type'] ?? 'default';
314
315
			if( ( $listItem = $item->getListItem( 'price', $listType, $entry['price.id'], false ) ) === null ) {
316
				$listItem = $listManager->create();
317
			}
318
319
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
320
				$refItem = $priceManager->create();
321
			}
322
323
			$refItem->fromArray( $entry, true );
324
			$listItem->fromArray( $entry, true )->setPosition( $idx )->setConfig( [] );
325
326
			foreach( (array) $this->getValue( $entry, 'config', [] ) as $cfg )
327
			{
328
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' ) {
329
					$listItem->setConfigValue( $key, trim( $cfg['val'] ?? '' ) );
330
				}
331
			}
332
333
			$item->addListItem( 'price', $listItem, $refItem );
334
			unset( $listItems[$listItem->getId()] );
335
		}
336
337
		return $item->deleteListItems( $listItems->toArray(), true );
338
	}
339
340
341
	/**
342
	 * Constructs the data array for the view from the given item
343
	 *
344
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
345
	 * @param bool $copy True if items should be copied, false if not
346
	 * @return string[] Multi-dimensional associative list of item data
347
	 */
348
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
349
	{
350
		$data = [];
351
		$siteId = $this->getContext()->getLocale()->getSiteId();
352
353
		foreach( $item->getListItems( 'price', null, null, false ) as $listItem )
354
		{
355
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
356
				continue;
357
			}
358
359
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
360
361
			if( $copy === true )
362
			{
363
				$list['product.lists.siteid'] = $siteId;
364
				$list['product.lists.id'] = '';
365
				$list['price.siteid'] = $siteId;
366
				$list['price.id'] = null;
367
			}
368
369
			$list['product.lists.datestart'] = str_replace( ' ', 'T', $list['product.lists.datestart'] );
370
			$list['product.lists.dateend'] = str_replace( ' ', 'T', $list['product.lists.dateend'] );
371
			$list['config'] = [];
372
373
			foreach( $listItem->getConfig() as $key => $value ) {
374
				$list['config'][] = ['key' => $key, 'val' => $value];
375
			}
376
377
			if( empty( $refItem->getTaxRates() ) ) {
378
				$list['price.taxrates'] = ['' => ''];
379
			}
380
381
			$data[] = $list;
382
		}
383
384
		return $data;
385
	}
386
387
388
	/**
389
	 * Returns if the prices can be chosen by the customers themselves
390
	 *
391
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item including attribute items
392
	 * @return bool True if price value can be entered by the customer, false if not
393
	 */
394
	protected function isCustom( \Aimeos\MShop\Product\Item\Iface $item ) : bool
395
	{
396
		return !$item->getRefItems( 'attribute', 'price', 'custom', false )->isEmpty();
397
	}
398
399
400
	/**
401
	 * Sets the flag if the price is customizable by the customer
402
	 *
403
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item including attribute items
404
	 * @param mixed $value Zero, empty, null or false to remove the flag, otherwise add the flag
405
	 * @return \Aimeos\MShop\Product\Item\Iface $item Modified product item
406
	 */
407
	protected function setCustom( \Aimeos\MShop\Product\Item\Iface $item, $value ) : \Aimeos\MShop\Product\Item\Iface
408
	{
409
		$context = $this->getContext();
410
411
		try
412
		{
413
			$attrManager = \Aimeos\MShop::create( $context, 'attribute' );
414
			$attrItem = $attrManager->find( 'custom', [], 'product', 'price' );
415
		}
416
		catch( \Aimeos\MShop\Exception $e )
417
		{
418
			$attrItem = $attrManager->create()->setDomain( 'product' )->setType( 'price' )->setCode( 'custom' );
419
			$attrItem = $attrManager->save( $attrItem );
420
		}
421
422
		if( $value )
423
		{
424
			if( $item->getListItem( 'attribute', 'custom', $attrItem->getId(), false ) === null )
425
			{
426
				$listItem = \Aimeos\MShop::create( $context, 'product' )->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

426
				$listItem = \Aimeos\MShop::create( $context, 'product' )->/** @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...
427
				$item = $item->addListItem( 'attribute', $listItem->setType( 'custom' ), $attrItem );
428
			}
429
		}
430
		else
431
		{
432
			if( ( $litem = $item->getListItem( 'attribute', 'custom', $attrItem->getId(), false ) ) !== null ) {
433
				$item = $item->deleteListItem( 'attribute', $litem );
434
			}
435
		}
436
437
		return $item;
438
	}
439
440
441
	/**
442
	 * Returns the rendered template including the view data
443
	 *
444
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
445
	 * @return string HTML output
446
	 */
447
	protected function render( \Aimeos\MW\View\Iface $view ) : string
448
	{
449
		/** admin/jqadm/product/price/template-item
450
		 * Relative path to the HTML body template of the price subpart for products.
451
		 *
452
		 * The template file contains the HTML code and processing instructions
453
		 * to generate the result shown in the body of the frontend. The
454
		 * configuration string is the path to the template file relative
455
		 * to the templates directory (usually in admin/jqadm/templates).
456
		 *
457
		 * You can overwrite the template file configuration in extensions and
458
		 * provide alternative templates. These alternative templates should be
459
		 * named like the default one but with the string "default" replaced by
460
		 * an unique name. You may use the name of your project for this. If
461
		 * you've implemented an alternative client class as well, "default"
462
		 * should be replaced by the name of the new class.
463
		 *
464
		 * @param string Relative path to the template creating the HTML code
465
		 * @since 2016.04
466
		 * @category Developer
467
		 */
468
		$tplconf = 'admin/jqadm/product/price/template-item';
469
		$default = 'product/item-price-standard';
470
471
		return $view->render( $view->config( $tplconf, $default ) );
472
	}
473
}
474