Standard   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 339
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 339
rs 10
c 0
b 0
f 0
wmc 18

10 Methods

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

279
					/** @scrutinizer ignore-call */ 
280
     $propItem = $manager->createPropertyItem();

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...
280
				}
281
282
				$propItem->fromArray( $entry, true );
283
				$refItem->addPropertyItem( $propItem );
284
			}
285
286
			$refItem->deletePropertyItems( $propItems );
287
			$index++;
288
		}
289
290
		return $item;
291
	}
292
293
294
	/**
295
	 * Constructs the data array for the view from the given item
296
	 *
297
	 * @param \Aimeos\MShop\Service\Item\Iface $item Service item object including referenced domain items
298
	 * @param array $data Associative list of price data
299
	 * @param bool $copy True if items should be copied, false if not
300
	 * @return string[] Multi-dimensional associative list of item data
301
	 */
302
	protected function toArray( \Aimeos\MShop\Service\Item\Iface $item, array $data, bool $copy = false ) : array
303
	{
304
		$idx = 0;
305
		$siteId = $this->context()->locale()->getSiteId();
306
307
		foreach( $item->getRefItems( 'price', null, null, false ) as $priceItem )
308
		{
309
			$data[$idx]['property'] = [];
310
311
			foreach( $priceItem->getPropertyItems( null, false ) as $propItem )
312
			{
313
				$list = $propItem->toArray( true );
314
315
				if( $copy === true )
316
				{
317
					$list['price.property.siteid'] = $siteId;
318
					$list['price.property.id'] = '';
319
				}
320
321
				$data[$idx]['property'][] = $list;
322
			}
323
324
			$idx++;
325
		}
326
327
		return $data;
328
	}
329
330
331
	/**
332
	 * Returns the rendered template including the view data
333
	 *
334
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
335
	 * @return string HTML output
336
	 */
337
	protected function render( \Aimeos\Base\View\Iface $view ) : string
338
	{
339
		/** admin/jqadm/service/price/property/template-item
340
		 * Relative path to the HTML body template of the price subpart for services.
341
		 *
342
		 * The template file contains the HTML code and processing instructions
343
		 * to generate the result shown in the body of the frontend. The
344
		 * configuration string is the path to the template file relative
345
		 * to the templates directory (usually in templates/admin/jqadm).
346
		 *
347
		 * You can overwrite the template file configuration in extensions and
348
		 * provide alternative templates. These alternative templates should be
349
		 * named like the default one but with the string "default" replaced by
350
		 * an unique name. You may use the name of your project for this. If
351
		 * you've implemented an alternative client class as well, "default"
352
		 * should be replaced by the name of the new class.
353
		 *
354
		 * @param string Relative path to the template creating the HTML code
355
		 * @since 2016.04
356
		 */
357
		$tplconf = 'admin/jqadm/service/price/property/template-item';
358
		$default = 'service/item-price-property';
359
360
		return $view->render( $view->config( $tplconf, $default ) );
361
	}
362
}
363