Standard   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 476
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 115
dl 0
loc 476
rs 9.44
c 0
b 0
f 0
wmc 37

15 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 8 1
B toArray() 0 37 6
A getSubClient() 0 73 1
A create() 0 17 2
A setCustom() 0 20 4
A data() 0 23 2
A getSubClientNames() 0 35 1
B fromArray() 0 33 7
A delete() 0 8 1
A isCustom() 0 3 1
A batch() 0 26 6
A copy() 0 8 1
A render() 0 24 1
A save() 0 9 1
A getAttributeItem() 0 8 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
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
	 */
35
36
37
	/**
38
	 * Adds the required data used in the price template
39
	 *
40
	 * @param \Aimeos\Base\View\Iface $view View object
41
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
42
	 */
43
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
44
	{
45
		$context = $this->context();
46
47
		$priceTypeManager = \Aimeos\MShop::create( $context, 'price/type' );
48
		$listTypeManager = \Aimeos\MShop::create( $context, 'product/lists/type' );
49
		$currencyManager = \Aimeos\MShop::create( $context, 'locale/currency' );
50
51
		$search = $priceTypeManager->filter( true )->order( 'price.type.code' )->slice( 0, 10000 );
52
		$listSearch = $listTypeManager->filter( true )->order( 'product.lists.type.code' )->slice( 0, 10000 );
53
54
		$view->priceTypes = $priceTypeManager->search( $search );
55
		$view->priceListTypes = $listTypeManager->search( $listSearch );
56
		$view->priceCurrencies = $currencyManager->search( $currencyManager->filter( true )->slice( 0, 10000 ) );
57
		$view->priceCustomItem = $this->getAttributeItem();
58
59
		if( $view->priceCurrencies->isEmpty() )
60
		{
61
			$msg = $context->translate( 'admin', 'No currencies available. Please enable at least one currency' );
62
			throw new \Aimeos\Admin\JQAdm\Exception( $msg );
63
		}
64
65
		return $view;
66
	}
67
68
69
	/**
70
	 * Batch update of a resource
71
	 *
72
	 * @return string|null Output to display
73
	 */
74
	public function batch() : ?string
75
	{
76
		$view = $this->view();
77
		$data = $view->param( 'price', [] );
78
79
		foreach( $view->get( 'items', [] ) as $item )
80
		{
81
			foreach( $item->getRefItems( 'price' ) as $price )
82
			{
83
				$temp = $data; $price->fromArray( $temp, true );
84
85
				if( isset( $data['valuepercent'] ) ) {
86
					$price->setValue( $price->getValue() + $price->getValue() * $data['valuepercent'] / 100 );
87
				}
88
89
				if( isset( $data['rebatepercent'] ) ) {
90
					$price->setRebate( $price->getRebate() + $price->getRebate() * $data['rebatepercent'] / 100 );
91
				}
92
93
				if( isset( $data['costspercent'] ) ) {
94
					$price->setCosts( $price->getCosts() + $price->getCosts() * $data['costspercent'] / 100 );
95
				}
96
			}
97
		}
98
99
		return null;
100
	}
101
102
103
	/**
104
	 * Copies a resource
105
	 *
106
	 * @return string|null HTML output
107
	 */
108
	public function copy() : ?string
109
	{
110
		$view = $this->object()->data( $this->view() );
111
		$view->priceCustom = $this->isCustom( $view->item );
112
		$view->priceData = $this->toArray( $view->item, true );
113
		$view->priceBody = parent::copy();
114
115
		return $this->render( $view );
116
	}
117
118
119
	/**
120
	 * Creates a new resource
121
	 *
122
	 * @return string|null HTML output
123
	 */
124
	public function create() : ?string
125
	{
126
		$view = $this->object()->data( $this->view() );
127
		$siteid = $this->context()->locale()->getSiteId();
128
		$data = $view->param( 'price', [] );
129
130
		foreach( $data as $idx => $entry )
131
		{
132
			$data[$idx]['product.lists.siteid'] = $siteid;
133
			$data[$idx]['price.siteid'] = $siteid;
134
		}
135
136
		$view->priceCustom = $this->isCustom( $view->item );
137
		$view->priceBody = parent::create();
138
		$view->priceData = $data;
139
140
		return $this->render( $view );
141
	}
142
143
144
	/**
145
	 * Deletes a resource
146
	 *
147
	 * @return string|null HTML output
148
	 */
149
	public function delete() : ?string
150
	{
151
		parent::delete();
152
153
		$item = $this->view()->item;
154
		$item->deleteListItems( $item->getListItems( 'price', null, null, false ), true );
155
156
		return null;
157
	}
158
159
160
	/**
161
	 * Returns a single resource
162
	 *
163
	 * @return string|null HTML output
164
	 */
165
	public function get() : ?string
166
	{
167
		$view = $this->object()->data( $this->view() );
168
		$view->priceCustom = $this->isCustom( $view->item );
169
		$view->priceData = $this->toArray( $view->item );
170
		$view->priceBody = parent::get();
171
172
		return $this->render( $view );
173
	}
174
175
176
	/**
177
	 * Saves the data
178
	 *
179
	 * @return string|null HTML output
180
	 */
181
	public function save() : ?string
182
	{
183
		$view = $this->view();
184
185
		$view->item = $this->setCustom( $view->item, $view->param( 'pricecustom' ) );
186
		$view->item = $this->fromArray( $view->item, $view->param( 'price', [] ) );
187
		$view->priceBody = parent::save();
188
189
		return null;
190
	}
191
192
193
	/**
194
	 * Returns the sub-client given by its name.
195
	 *
196
	 * @param string $type Name of the client type
197
	 * @param string|null $name Name of the sub-client (Default if null)
198
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
199
	 */
200
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
201
	{
202
		/** admin/jqadm/product/price/decorators/excludes
203
		 * Excludes decorators added by the "common" option from the product JQAdm client
204
		 *
205
		 * Decorators extend the functionality of a class by adding new aspects
206
		 * (e.g. log what is currently done), executing the methods of the underlying
207
		 * class only in certain conditions (e.g. only for logged in users) or
208
		 * modify what is returned to the caller.
209
		 *
210
		 * This option allows you to remove a decorator added via
211
		 * "admin/jqadm/common/decorators/default" before they are wrapped
212
		 * around the JQAdm client.
213
		 *
214
		 *  admin/jqadm/product/price/decorators/excludes = array( 'decorator1' )
215
		 *
216
		 * This would remove the decorator named "decorator1" from the list of
217
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
218
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
219
		 *
220
		 * @param array List of decorator names
221
		 * @since 2016.01
222
		 * @see admin/jqadm/common/decorators/default
223
		 * @see admin/jqadm/product/price/decorators/global
224
		 * @see admin/jqadm/product/price/decorators/local
225
		 */
226
227
		/** admin/jqadm/product/price/decorators/global
228
		 * Adds a list of globally available decorators only to the product JQAdm client
229
		 *
230
		 * Decorators extend the functionality of a class by adding new aspects
231
		 * (e.g. log what is currently done), executing the methods of the underlying
232
		 * class only in certain conditions (e.g. only for logged in users) or
233
		 * modify what is returned to the caller.
234
		 *
235
		 * This option allows you to wrap global decorators
236
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
237
		 *
238
		 *  admin/jqadm/product/price/decorators/global = array( 'decorator1' )
239
		 *
240
		 * This would add the decorator named "decorator1" defined by
241
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
242
		 *
243
		 * @param array List of decorator names
244
		 * @since 2016.01
245
		 * @see admin/jqadm/common/decorators/default
246
		 * @see admin/jqadm/product/price/decorators/excludes
247
		 * @see admin/jqadm/product/price/decorators/local
248
		 */
249
250
		/** admin/jqadm/product/price/decorators/local
251
		 * Adds a list of local decorators only to the product JQAdm client
252
		 *
253
		 * Decorators extend the functionality of a class by adding new aspects
254
		 * (e.g. log what is currently done), executing the methods of the underlying
255
		 * class only in certain conditions (e.g. only for logged in users) or
256
		 * modify what is returned to the caller.
257
		 *
258
		 * This option allows you to wrap local decorators
259
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
260
		 *
261
		 *  admin/jqadm/product/price/decorators/local = array( 'decorator2' )
262
		 *
263
		 * This would add the decorator named "decorator2" defined by
264
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
265
		 *
266
		 * @param array List of decorator names
267
		 * @since 2016.01
268
		 * @see admin/jqadm/common/decorators/default
269
		 * @see admin/jqadm/product/price/decorators/excludes
270
		 * @see admin/jqadm/product/price/decorators/global
271
		 */
272
		return $this->createSubClient( 'product/price/' . $type, $name );
273
	}
274
275
276
	/**
277
	 * Returns the custom price attribute item
278
	 *
279
	 * @return \Aimeos\MShop\Attribute\Item\Iface Custom price attribute item
280
	 */
281
	protected function getAttributeItem() : \Aimeos\MShop\Attribute\Item\Iface
282
	{
283
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute' );
284
285
		try {
286
			return $manager->find( 'custom', [], 'product', 'price' );
287
		} catch( \Aimeos\MShop\Exception $e ) {
288
			return $manager->save( $manager->create()->setDomain( 'product' )->setType( 'price' )->setCode( 'custom' ) );
289
		}
290
	}
291
292
293
	/**
294
	 * Returns the list of sub-client names configured for the client.
295
	 *
296
	 * @return array List of JQAdm client names
297
	 */
298
	protected function getSubClientNames() : array
299
	{
300
		/** admin/jqadm/product/price/subparts
301
		 * List of JQAdm sub-clients rendered within the product price section
302
		 *
303
		 * The output of the frontend is composed of the code generated by the JQAdm
304
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
305
		 * that are responsible for rendering certain sub-parts of the output. The
306
		 * sub-clients can contain JQAdm clients themselves and therefore a
307
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
308
		 * the output that is placed inside the container of its parent.
309
		 *
310
		 * At first, always the JQAdm code generated by the parent is printed, then
311
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
312
		 * determines the order of the output of these sub-clients inside the parent
313
		 * container. If the configured list of clients is
314
		 *
315
		 *  array( "subclient1", "subclient2" )
316
		 *
317
		 * you can easily change the order of the output by reordering the subparts:
318
		 *
319
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
320
		 *
321
		 * You can also remove one or more parts if they shouldn't be rendered:
322
		 *
323
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
324
		 *
325
		 * As the clients only generates structural JQAdm, the layout defined via CSS
326
		 * should support adding, removing or reordering content by a fluid like
327
		 * design.
328
		 *
329
		 * @param array List of sub-client names
330
		 * @since 2016.01
331
		 */
332
		return $this->context()->config()->get( 'admin/jqadm/product/price/subparts', [] );
333
	}
334
335
336
	/**
337
	 * Creates new and updates existing items using the data array
338
	 *
339
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
340
	 * @param array $data Data array
341
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
342
	 */
343
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
344
	{
345
		$context = $this->context();
346
347
		$priceManager = \Aimeos\MShop::create( $context, 'price' );
348
		$manager = \Aimeos\MShop::create( $context, 'product' );
349
350
		$listItems = $item->getListItems( 'price', null, null, false );
351
352
353
		foreach( $data as $idx => $entry )
354
		{
355
			$id = $this->val( $entry, 'price.id', '' );
356
			$type = $this->val( $entry, 'product.lists.type', 'default' );
357
358
			$listItem = $item->getListItem( 'price', $type, $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

358
			$listItem = $item->getListItem( 'price', $type, $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...
359
			$refItem = $listItem->getRefItem() ?: $priceManager->create();
360
361
			$refItem->fromArray( $entry, true );
362
			$listItem->fromArray( $entry, true )->setPosition( $idx )->setConfig( [] );
363
364
			foreach( (array) $this->val( $entry, 'config', [] ) as $cfg )
365
			{
366
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) {
367
					$listItem->setConfigValue( $key, json_decode( $val, true ) ?? $val );
368
				}
369
			}
370
371
			$item->addListItem( 'price', $listItem, $refItem );
372
			unset( $listItems[$listItem->getId()] );
373
		}
374
375
		return $item->deleteListItems( $listItems, true );
376
	}
377
378
379
	/**
380
	 * Constructs the data array for the view from the given item
381
	 *
382
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
383
	 * @param bool $copy True if items should be copied, false if not
384
	 * @return string[] Multi-dimensional associative list of item data
385
	 */
386
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
387
	{
388
		$data = [];
389
		$siteId = $this->context()->locale()->getSiteId();
390
391
		foreach( $item->getListItems( 'price', null, null, false ) as $listItem )
392
		{
393
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
394
				continue;
395
			}
396
397
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
398
399
			if( $copy === true )
400
			{
401
				$list['product.lists.siteid'] = $siteId;
402
				$list['product.lists.id'] = '';
403
				$list['price.siteid'] = $siteId;
404
				$list['price.id'] = null;
405
			}
406
407
			$list['product.lists.datestart'] = str_replace( ' ', 'T', $list['product.lists.datestart'] ?? '' );
408
			$list['product.lists.dateend'] = str_replace( ' ', 'T', $list['product.lists.dateend'] ?? '' );
409
			$list['config'] = [];
410
411
			foreach( $listItem->getConfig() as $key => $value ) {
412
				$list['config'][] = ['key' => $key, 'val' => $value];
413
			}
414
415
			if( empty( $refItem->getTaxRates() ) ) {
416
				$list['price.taxrates'] = ['' => ''];
417
			}
418
419
			$data[] = $list;
420
		}
421
422
		return $data;
423
	}
424
425
426
	/**
427
	 * Returns if the prices can be chosen by the customers themselves
428
	 *
429
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item including attribute items
430
	 * @return bool True if price value can be entered by the customer, false if not
431
	 */
432
	protected function isCustom( \Aimeos\MShop\Product\Item\Iface $item ) : bool
433
	{
434
		return !$item->getRefItems( 'attribute', 'price', 'custom', false )->isEmpty();
435
	}
436
437
438
	/**
439
	 * Sets the flag if the price is customizable by the customer
440
	 *
441
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item including attribute items
442
	 * @param mixed $value Zero, empty, null or false to remove the flag, otherwise add the flag
443
	 * @return \Aimeos\MShop\Product\Item\Iface $item Modified product item
444
	 */
445
	protected function setCustom( \Aimeos\MShop\Product\Item\Iface $item, $value ) : \Aimeos\MShop\Product\Item\Iface
446
	{
447
		$attrItem = $this->getAttributeItem();
448
449
		if( $value )
450
		{
451
			if( $item->getListItem( 'attribute', 'custom', $attrItem->getId(), false ) === null )
0 ignored issues
show
Bug introduced by
It seems like $attrItem->getId() can also be of type null; however, parameter $refId of Aimeos\MShop\Common\Item...ef\Iface::getListItem() does only seem to accept string, 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

451
			if( $item->getListItem( 'attribute', 'custom', /** @scrutinizer ignore-type */ $attrItem->getId(), false ) === null )
Loading history...
452
			{
453
				$listItem = \Aimeos\MShop::create( $this->context(), 'product' )->createListItem();
454
				$item = $item->addListItem( 'attribute', $listItem->setType( 'custom' ), $attrItem );
455
			}
456
		}
457
		else
458
		{
459
			if( ( $litem = $item->getListItem( 'attribute', 'custom', $attrItem->getId(), false ) ) !== null ) {
460
				$item = $item->deleteListItem( 'attribute', $litem );
461
			}
462
		}
463
464
		return $item;
465
	}
466
467
468
	/**
469
	 * Returns the rendered template including the view data
470
	 *
471
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
472
	 * @return string HTML output
473
	 */
474
	protected function render( \Aimeos\Base\View\Iface $view ) : string
475
	{
476
		/** admin/jqadm/product/price/template-item
477
		 * Relative path to the HTML body template of the price subpart for products.
478
		 *
479
		 * The template file contains the HTML code and processing instructions
480
		 * to generate the result shown in the body of the frontend. The
481
		 * configuration string is the path to the template file relative
482
		 * to the templates directory (usually in templates/admin/jqadm).
483
		 *
484
		 * You can overwrite the template file configuration in extensions and
485
		 * provide alternative templates. These alternative templates should be
486
		 * named like the default one but with the string "default" replaced by
487
		 * an unique name. You may use the name of your project for this. If
488
		 * you've implemented an alternative client class as well, "default"
489
		 * should be replaced by the name of the new class.
490
		 *
491
		 * @param string Relative path to the template creating the HTML code
492
		 * @since 2016.04
493
		 */
494
		$tplconf = 'admin/jqadm/product/price/template-item';
495
		$default = 'product/item-price';
496
497
		return $view->render( $view->config( $tplconf, $default ) );
498
	}
499
}
500