Passed
Push — master ( 46dc3d...0e7e66 )
by Aimeos
03:47
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
	 * Copies a resource
40
	 *
41
	 * @return string|null HTML output
42
	 */
43
	public function copy() : ?string
44
	{
45
		$view = $this->addViewData( $this->getView() );
46
47
		$view->priceData = $this->toArray( $view->item, true );
48
		$view->priceBody = '';
49
50
		foreach( $this->getSubClients() as $client ) {
51
			$view->priceBody .= $client->copy();
52
		}
53
54
		return $this->render( $view );
55
	}
56
57
58
	/**
59
	 * Creates a new resource
60
	 *
61
	 * @return string|null HTML output
62
	 */
63
	public function create() : ?string
64
	{
65
		$view = $this->addViewData( $this->getView() );
66
		$siteid = $this->getContext()->getLocale()->getSiteId();
67
		$data = $view->param( 'price', [] );
68
69
		foreach( $data as $idx => $entry )
70
		{
71
			$data[$idx]['product.lists.siteid'] = $siteid;
72
			$data[$idx]['price.siteid'] = $siteid;
73
		}
74
75
		$view->priceData = $data;
76
		$view->priceBody = '';
77
78
		foreach( $this->getSubClients() as $client ) {
79
			$view->priceBody .= $client->create();
80
		}
81
82
		return $this->render( $view );
83
	}
84
85
86
	/**
87
	 * Deletes a resource
88
	 *
89
	 * @return string|null HTML output
90
	 */
91
	public function delete() : ?string
92
	{
93
		parent::delete();
94
95
		$item = $this->getView()->item;
96
		$item->deleteListItems( $item->getListItems( 'price', null, null, false )->toArray(), true );
97
98
		return null;
99
	}
100
101
102
	/**
103
	 * Returns a single resource
104
	 *
105
	 * @return string|null HTML output
106
	 */
107
	public function get() : ?string
108
	{
109
		$view = $this->addViewData( $this->getView() );
110
111
		$view->priceCustom = $this->isCustom( $view->item );
112
		$view->priceData = $this->toArray( $view->item );
113
		$view->priceBody = '';
114
115
		foreach( $this->getSubClients() as $client ) {
116
			$view->priceBody .= $client->get();
117
		}
118
119
		return $this->render( $view );
120
	}
121
122
123
	/**
124
	 * Saves the data
125
	 *
126
	 * @return string|null HTML output
127
	 */
128
	public function save() : ?string
129
	{
130
		$view = $this->getView();
131
132
		$view->item = $this->setCustom( $view->item, $view->param( 'pricecustom' ) );
133
		$view->item = $this->fromArray( $view->item, $view->param( 'price', [] ) );
134
		$view->priceBody = '';
135
136
		foreach( $this->getSubClients() as $client ) {
137
			$view->priceBody .= $client->save();
138
		}
139
140
		return null;
141
	}
142
143
144
	/**
145
	 * Returns the sub-client given by its name.
146
	 *
147
	 * @param string $type Name of the client type
148
	 * @param string|null $name Name of the sub-client (Default if null)
149
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
150
	 */
151
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
152
	{
153
		/** admin/jqadm/product/price/decorators/excludes
154
		 * Excludes decorators added by the "common" option from the product JQAdm client
155
		 *
156
		 * Decorators extend the functionality of a class by adding new aspects
157
		 * (e.g. log what is currently done), executing the methods of the underlying
158
		 * class only in certain conditions (e.g. only for logged in users) or
159
		 * modify what is returned to the caller.
160
		 *
161
		 * This option allows you to remove a decorator added via
162
		 * "admin/jqadm/common/decorators/default" before they are wrapped
163
		 * around the JQAdm client.
164
		 *
165
		 *  admin/jqadm/product/price/decorators/excludes = array( 'decorator1' )
166
		 *
167
		 * This would remove the decorator named "decorator1" from the list of
168
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
169
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
170
		 *
171
		 * @param array List of decorator names
172
		 * @since 2016.01
173
		 * @category Developer
174
		 * @see admin/jqadm/common/decorators/default
175
		 * @see admin/jqadm/product/price/decorators/global
176
		 * @see admin/jqadm/product/price/decorators/local
177
		 */
178
179
		/** admin/jqadm/product/price/decorators/global
180
		 * Adds a list of globally available decorators only to the product JQAdm client
181
		 *
182
		 * Decorators extend the functionality of a class by adding new aspects
183
		 * (e.g. log what is currently done), executing the methods of the underlying
184
		 * class only in certain conditions (e.g. only for logged in users) or
185
		 * modify what is returned to the caller.
186
		 *
187
		 * This option allows you to wrap global decorators
188
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
189
		 *
190
		 *  admin/jqadm/product/price/decorators/global = array( 'decorator1' )
191
		 *
192
		 * This would add the decorator named "decorator1" defined by
193
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
194
		 *
195
		 * @param array List of decorator names
196
		 * @since 2016.01
197
		 * @category Developer
198
		 * @see admin/jqadm/common/decorators/default
199
		 * @see admin/jqadm/product/price/decorators/excludes
200
		 * @see admin/jqadm/product/price/decorators/local
201
		 */
202
203
		/** admin/jqadm/product/price/decorators/local
204
		 * Adds a list of local decorators only to the product JQAdm client
205
		 *
206
		 * Decorators extend the functionality of a class by adding new aspects
207
		 * (e.g. log what is currently done), executing the methods of the underlying
208
		 * class only in certain conditions (e.g. only for logged in users) or
209
		 * modify what is returned to the caller.
210
		 *
211
		 * This option allows you to wrap local decorators
212
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
213
		 *
214
		 *  admin/jqadm/product/price/decorators/local = array( 'decorator2' )
215
		 *
216
		 * This would add the decorator named "decorator2" defined by
217
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
218
		 *
219
		 * @param array List of decorator names
220
		 * @since 2016.01
221
		 * @category Developer
222
		 * @see admin/jqadm/common/decorators/default
223
		 * @see admin/jqadm/product/price/decorators/excludes
224
		 * @see admin/jqadm/product/price/decorators/global
225
		 */
226
		return $this->createSubClient( 'product/price/' . $type, $name );
227
	}
228
229
230
	/**
231
	 * Returns the list of sub-client names configured for the client.
232
	 *
233
	 * @return array List of JQAdm client names
234
	 */
235
	protected function getSubClientNames() : array
236
	{
237
		/** admin/jqadm/product/price/standard/subparts
238
		 * List of JQAdm sub-clients rendered within the product price section
239
		 *
240
		 * The output of the frontend is composed of the code generated by the JQAdm
241
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
242
		 * that are responsible for rendering certain sub-parts of the output. The
243
		 * sub-clients can contain JQAdm clients themselves and therefore a
244
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
245
		 * the output that is placed inside the container of its parent.
246
		 *
247
		 * At first, always the JQAdm code generated by the parent is printed, then
248
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
249
		 * determines the order of the output of these sub-clients inside the parent
250
		 * container. If the configured list of clients is
251
		 *
252
		 *  array( "subclient1", "subclient2" )
253
		 *
254
		 * you can easily change the order of the output by reordering the subparts:
255
		 *
256
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
257
		 *
258
		 * You can also remove one or more parts if they shouldn't be rendered:
259
		 *
260
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
261
		 *
262
		 * As the clients only generates structural JQAdm, the layout defined via CSS
263
		 * should support adding, removing or reordering content by a fluid like
264
		 * design.
265
		 *
266
		 * @param array List of sub-client names
267
		 * @since 2016.01
268
		 * @category Developer
269
		 */
270
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/price/standard/subparts', [] );
271
	}
272
273
274
	/**
275
	 * Adds the required data used in the price template
276
	 *
277
	 * @param \Aimeos\MW\View\Iface $view View object
278
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
279
	 */
280
	protected function addViewData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
281
	{
282
		$context = $this->getContext();
283
284
		$priceTypeManager = \Aimeos\MShop::create( $context, 'price/type' );
285
		$listTypeManager = \Aimeos\MShop::create( $context, 'product/lists/type' );
286
		$currencyManager = \Aimeos\MShop::create( $context, 'locale/currency' );
287
288
		$search = $priceTypeManager->createSearch( true )->setSlice( 0, 10000 );
289
		$search->setConditions( $search->compare( '==', 'price.type.domain', 'product' ) );
290
		$search->setSortations( array( $search->sort( '+', 'price.type.position' ) ) );
291
292
		$listSearch = $listTypeManager->createSearch( true )->setSlice( 0, 10000 );
293
		$listSearch->setConditions( $listSearch->compare( '==', 'product.lists.type.domain', 'price' ) );
294
		$listSearch->setSortations( array( $listSearch->sort( '+', 'product.lists.type.position' ) ) );
295
296
		$view->priceTypes = $priceTypeManager->searchItems( $search );
297
		$view->priceListTypes = $listTypeManager->searchItems( $listSearch );
298
		$view->priceCurrencies = $currencyManager->searchItems( $currencyManager->createSearch( true )->setSlice( 0, 10000 ) );
299
300
		if( $view->priceCurrencies->isEmpty() ) {
301
			throw new \Aimeos\Admin\JQAdm\Exception( 'No currencies available. Please enable at least one currency' );
302
		}
303
304
		return $view;
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
	protected function isCustom( \Aimeos\MShop\Product\Item\Iface $item ) : bool
407
	{
408
		return $item->getRefItems( 'attribute', 'price', 'custom', false )->isEmpty() ? false : true;
409
	}
410
411
412
	protected function setCustom( \Aimeos\MShop\Product\Item\Iface $item, $value ) : \Aimeos\MShop\Product\Item\Iface
413
	{
414
		$context = $this->getContext();
415
416
		try
417
		{
418
			$attrManager = \Aimeos\MShop::create( $context, 'attribute' );
419
			$attrItem = $attrManager->findItem( 'custom', [], 'product', 'price' );
420
		}
421
		catch( \Aimeos\MShop\Exception $e )
422
		{
423
			$attrItem = $attrManager->createItem()->setDomain( 'product' )->setType( 'price' )->setCode( 'custom' );
424
			$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

424
			/** @scrutinizer ignore-call */ 
425
   $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...
425
		}
426
427
		if( $value )
428
		{
429
			if( $item->getListItem( 'attribute', 'custom', $attrItem->getId(), false ) === null )
430
			{
431
				$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

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