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

Standard   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 373
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 82
c 0
b 0
f 0
dl 0
loc 373
rs 10
wmc 21

11 Methods

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

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