Completed
Push — master ( 626216...e78c82 )
by Aimeos
03:45
created

Standard::setCustom()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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

438
			/** @scrutinizer ignore-call */ 
439
   $attrItem = $attrManager->saveItem( $attrItem );

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...
439
		}
440
441
		if( $value )
442
		{
443
			if( $item->getListItem( 'attribute', 'custom', $attrItem->getId(), false ) === null )
444
			{
445
				$listItem = \Aimeos\MShop::create( $context, 'product' )->createListsItem();
0 ignored issues
show
Bug introduced by
The method createListsItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean createItem()? ( Ignorable by Annotation )

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

445
				$listItem = \Aimeos\MShop::create( $context, 'product' )->/** @scrutinizer ignore-call */ createListsItem();

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