Standard::data()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 11
rs 10
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), 2019-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Attribute\Price\Property;
12
13
sprintf( 'property' ); // 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/property/name
27
	 * Name of the property subpart used by the JQAdm attribute price implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Attribute\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 2019.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, $view->get( 'priceData', [] ), true );
46
		$view->propertyBody = 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->get( 'priceData', [] );
62
63
		foreach( $data as $index => $entry )
64
		{
65
			foreach( $view->value( $entry, 'property', [] ) as $idx => $y ) {
66
				$data[$index]['property'][$idx]['price.property.siteid'] = $siteid;
67
			}
68
		}
69
70
		$view->propertyData = $data;
71
		$view->propertyBody = parent::create();
72
73
		return $this->render( $view );
74
	}
75
76
77
	/**
78
	 * Returns a single resource
79
	 *
80
	 * @return string|null HTML output
81
	 */
82
	public function get() : ?string
83
	{
84
		$view = $this->object()->data( $this->view() );
85
		$view->priceData = $this->toArray( $view->item, $view->get( 'priceData', [] ) );
86
		$view->propertyBody = parent::get();
87
88
		return $this->render( $view );
89
	}
90
91
92
	/**
93
	 * Saves the data
94
	 *
95
	 * @return string|null HTML output
96
	 */
97
	public function save() : ?string
98
	{
99
		$view = $this->view();
100
101
		$view->item = $this->fromArray( $view->item, $view->param( 'price', [] ) );
102
		$view->propertyBody = parent::save();
103
104
		return null;
105
	}
106
107
108
	/**
109
	 * Returns the sub-client given by its name.
110
	 *
111
	 * @param string $type Name of the client type
112
	 * @param string|null $name Name of the sub-client (Default if null)
113
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
114
	 */
115
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
116
	{
117
		/** admin/jqadm/attribute/price/property/decorators/excludes
118
		 * Excludes decorators added by the "common" option from the attribute JQAdm client
119
		 *
120
		 * Decorators extend the functionality of a class by adding new aspects
121
		 * (e.g. log what is currently done), executing the methods of the underlying
122
		 * class only in certain conditions (e.g. only for logged in users) or
123
		 * modify what is returned to the caller.
124
		 *
125
		 * This option allows you to remove a decorator added via
126
		 * "admin/jqadm/common/decorators/default" before they are wrapped
127
		 * around the JQAdm client.
128
		 *
129
		 *  admin/jqadm/attribute/price/property/decorators/excludes = array( 'decorator1' )
130
		 *
131
		 * This would remove the decorator named "decorator1" from the list of
132
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
133
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
134
		 *
135
		 * @param array List of decorator names
136
		 * @since 2019.07
137
		 * @see admin/jqadm/common/decorators/default
138
		 * @see admin/jqadm/attribute/price/property/decorators/global
139
		 * @see admin/jqadm/attribute/price/property/decorators/local
140
		 */
141
142
		/** admin/jqadm/attribute/price/property/decorators/global
143
		 * Adds a list of globally available decorators only to the attribute JQAdm client
144
		 *
145
		 * Decorators extend the functionality of a class by adding new aspects
146
		 * (e.g. log what is currently done), executing the methods of the underlying
147
		 * class only in certain conditions (e.g. only for logged in users) or
148
		 * modify what is returned to the caller.
149
		 *
150
		 * This option allows you to wrap global decorators
151
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
152
		 *
153
		 *  admin/jqadm/attribute/price/property/decorators/global = array( 'decorator1' )
154
		 *
155
		 * This would add the decorator named "decorator1" defined by
156
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
157
		 *
158
		 * @param array List of decorator names
159
		 * @since 2019.07
160
		 * @see admin/jqadm/common/decorators/default
161
		 * @see admin/jqadm/attribute/price/property/decorators/excludes
162
		 * @see admin/jqadm/attribute/price/property/decorators/local
163
		 */
164
165
		/** admin/jqadm/attribute/price/property/decorators/local
166
		 * Adds a list of local decorators only to the attribute JQAdm client
167
		 *
168
		 * Decorators extend the functionality of a class by adding new aspects
169
		 * (e.g. log what is currently done), executing the methods of the underlying
170
		 * class only in certain conditions (e.g. only for logged in users) or
171
		 * modify what is returned to the caller.
172
		 *
173
		 * This option allows you to wrap local decorators
174
		 * ("\Aimeos\Admin\JQAdm\Attribute\Decorator\*") around the JQAdm client.
175
		 *
176
		 *  admin/jqadm/attribute/price/property/decorators/local = array( 'decorator2' )
177
		 *
178
		 * This would add the decorator named "decorator2" defined by
179
		 * "\Aimeos\Admin\JQAdm\Attribute\Decorator\Decorator2" only to the JQAdm client.
180
		 *
181
		 * @param array List of decorator names
182
		 * @since 2019.07
183
		 * @see admin/jqadm/common/decorators/default
184
		 * @see admin/jqadm/attribute/price/property/decorators/excludes
185
		 * @see admin/jqadm/attribute/price/property/decorators/global
186
		 */
187
		return $this->createSubClient( 'attribute/price/property/' . $type, $name );
188
	}
189
190
191
	/**
192
	 * Adds the required data used in the attribute template
193
	 *
194
	 * @param \Aimeos\Base\View\Iface $view View object
195
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
196
	 */
197
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
198
	{
199
		$manager = \Aimeos\MShop::create( $this->context(), 'price/property/type' );
200
201
		$search = $manager->filter( true )
202
			->order( 'price.property.type.code' )
203
			->slice( 0, 10000 );
204
205
		$view->propertyTypes = $manager->search( $search );
206
207
		return $view;
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/attribute/price/property/subparts
219
		 * List of JQAdm sub-clients rendered within the attribute 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 2019.07
249
		 */
250
		return $this->context()->config()->get( 'admin/jqadm/attribute/price/property/subparts', [] );
251
	}
252
253
254
	/**
255
	 * Creates new and updates existing items using the data array
256
	 *
257
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object without referenced domain items
258
	 * @param array $data Data array
259
	 * @return \Aimeos\MShop\Attribute\Item\Iface Modified attribute item
260
	 */
261
	protected function fromArray( \Aimeos\MShop\Attribute\Item\Iface $item, array $data ) : \Aimeos\MShop\Attribute\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\Attribute\Item\Iface $item Attribute 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\Attribute\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/attribute/price/property/template-item
340
		 * Relative path to the HTML body template of the price subpart for attributes.
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 2019.07
356
		 */
357
		$tplconf = 'admin/jqadm/attribute/price/property/template-item';
358
		$default = 'attribute/item-price-property';
359
360
		return $view->render( $view->config( $tplconf, $default ) );
361
	}
362
}
363