Passed
Push — master ( 98a0a0...f25a73 )
by Aimeos
18:48
created

Standard::fromArray()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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

306
			$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...
307
			$refItem = $listItem->getRefItem() ?: $priceManager->create();
308
309
			$config = array_column( (array) $this->val( $entry, 'config', [] ), 'val', 'key' );
310
			$config = array_map( fn( $val ) => json_decode( $val, true ) ?? $val, $config );
311
312
			$refItem->fromArray( $entry, true );
313
			$listItem->fromArray( $entry, true )->setPosition( $idx )->setConfigFlat( $config );
314
315
			$item->addListItem( 'price', $listItem, $refItem );
316
			unset( $listItems[$listItem->getId()] );
317
		}
318
319
		return $item->deleteListItems( $listItems, true );
320
	}
321
322
323
	/**
324
	 * Constructs the data array for the view from the given item
325
	 *
326
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object including referenced domain items
327
	 * @param bool $copy True if items should be copied, false if not
328
	 * @return string[] Multi-dimensional associative list of item data
329
	 */
330
	protected function toArray( \Aimeos\MShop\Attribute\Item\Iface $item, bool $copy = false ) : array
331
	{
332
		$data = [];
333
		$siteId = $this->context()->locale()->getSiteId();
334
335
		foreach( $item->getListItems( 'price', null, null, false ) as $listItem )
336
		{
337
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
338
				continue;
339
			}
340
341
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
342
343
			if( $copy === true )
344
			{
345
				$list['attribute.lists.siteid'] = $siteId;
346
				$list['attribute.lists.id'] = '';
347
				$list['price.siteid'] = $siteId;
348
				$list['price.id'] = null;
349
			}
350
351
			$list['attribute.lists.datestart'] = str_replace( ' ', 'T', $list['attribute.lists.datestart'] ?? '' );
352
			$list['attribute.lists.dateend'] = str_replace( ' ', 'T', $list['attribute.lists.dateend'] ?? '' );
353
			$list['config'] = [];
354
355
			foreach( $listItem->getConfig() as $key => $value ) {
356
				$list['config'][] = ['key' => $key, 'val' => $value];
357
			}
358
359
			if( empty( $refItem->getTaxRates() ) ) {
360
				$list['price.taxrates'] = ['' => ''];
361
			}
362
363
			$data[] = $list;
364
		}
365
366
		return $data;
367
	}
368
369
370
	/**
371
	 * Returns the rendered template including the view data
372
	 *
373
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
374
	 * @return string HTML output
375
	 */
376
	protected function render( \Aimeos\Base\View\Iface $view ) : string
377
	{
378
		/** admin/jqadm/attribute/price/template-item
379
		 * Relative path to the HTML body template of the price subpart for attributes.
380
		 *
381
		 * The template file contains the HTML code and processing instructions
382
		 * to generate the result shown in the body of the frontend. The
383
		 * configuration string is the path to the template file relative
384
		 * to the templates directory (usually in templates/admin/jqadm).
385
		 *
386
		 * You can overwrite the template file configuration in extensions and
387
		 * provide alternative templates. These alternative templates should be
388
		 * named like the default one but with the string "default" replaced by
389
		 * an unique name. You may use the name of your project for this. If
390
		 * you've implemented an alternative client class as well, "default"
391
		 * should be replaced by the name of the new class.
392
		 *
393
		 * @param string Relative path to the template creating the HTML code
394
		 * @since 2016.04
395
		 */
396
		$tplconf = 'admin/jqadm/attribute/price/template-item';
397
		$default = 'attribute/item-price';
398
399
		return $view->render( $view->config( $tplconf, $default ) );
400
	}
401
}
402