Passed
Push — master ( 85cd01...c58492 )
by Aimeos
03:18
created

Standard::data()   A

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-2021
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
	 * @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->getObject()->data( $this->view() );
46
		$view->priceData = $this->toArray( $view->item, $view->get( 'priceData', [] ), true );
47
		$view->propertyBody = parent::copy();
48
49
		return $this->render( $view );
50
	}
51
52
53
	/**
54
	 * Creates a new resource
55
	 *
56
	 * @return string|null HTML output
57
	 */
58
	public function create() : ?string
59
	{
60
		$view = $this->getObject()->data( $this->view() );
61
		$siteid = $this->getContext()->getLocale()->getSiteId();
62
		$data = $view->get( 'priceData', [] );
63
64
		foreach( $data as $index => $entry )
65
		{
66
			foreach( $view->value( $entry, 'property', [] ) as $idx => $y ) {
67
				$data[$index]['property'][$idx]['price.property.siteid'] = $siteid;
68
			}
69
		}
70
71
		$view->propertyData = $data;
72
		$view->propertyBody = parent::create();
73
74
		return $this->render( $view );
75
	}
76
77
78
	/**
79
	 * Returns a single resource
80
	 *
81
	 * @return string|null HTML output
82
	 */
83
	public function get() : ?string
84
	{
85
		$view = $this->getObject()->data( $this->view() );
86
		$view->priceData = $this->toArray( $view->item, $view->get( 'priceData', [] ) );
87
		$view->propertyBody = parent::get();
88
89
		return $this->render( $view );
90
	}
91
92
93
	/**
94
	 * Saves the data
95
	 *
96
	 * @return string|null HTML output
97
	 */
98
	public function save() : ?string
99
	{
100
		$view = $this->view();
101
102
		$view->item = $this->fromArray( $view->item, $view->param( 'price', [] ) );
103
		$view->propertyBody = parent::save();
104
105
		return null;
106
	}
107
108
109
	/**
110
	 * Returns the sub-client given by its name.
111
	 *
112
	 * @param string $type Name of the client type
113
	 * @param string|null $name Name of the sub-client (Default if null)
114
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
115
	 */
116
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
117
	{
118
		/** admin/jqadm/attribute/price/property/decorators/excludes
119
		 * Excludes decorators added by the "common" option from the attribute JQAdm client
120
		 *
121
		 * Decorators extend the functionality of a class by adding new aspects
122
		 * (e.g. log what is currently done), executing the methods of the underlying
123
		 * class only in certain conditions (e.g. only for logged in users) or
124
		 * modify what is returned to the caller.
125
		 *
126
		 * This option allows you to remove a decorator added via
127
		 * "admin/jqadm/common/decorators/default" before they are wrapped
128
		 * around the JQAdm client.
129
		 *
130
		 *  admin/jqadm/attribute/price/property/decorators/excludes = array( 'decorator1' )
131
		 *
132
		 * This would remove the decorator named "decorator1" from the list of
133
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
134
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
135
		 *
136
		 * @param array List of decorator names
137
		 * @since 2019.07
138
		 * @category Developer
139
		 * @see admin/jqadm/common/decorators/default
140
		 * @see admin/jqadm/attribute/price/property/decorators/global
141
		 * @see admin/jqadm/attribute/price/property/decorators/local
142
		 */
143
144
		/** admin/jqadm/attribute/price/property/decorators/global
145
		 * Adds a list of globally available decorators only to the attribute JQAdm client
146
		 *
147
		 * Decorators extend the functionality of a class by adding new aspects
148
		 * (e.g. log what is currently done), executing the methods of the underlying
149
		 * class only in certain conditions (e.g. only for logged in users) or
150
		 * modify what is returned to the caller.
151
		 *
152
		 * This option allows you to wrap global decorators
153
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
154
		 *
155
		 *  admin/jqadm/attribute/price/property/decorators/global = array( 'decorator1' )
156
		 *
157
		 * This would add the decorator named "decorator1" defined by
158
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
159
		 *
160
		 * @param array List of decorator names
161
		 * @since 2019.07
162
		 * @category Developer
163
		 * @see admin/jqadm/common/decorators/default
164
		 * @see admin/jqadm/attribute/price/property/decorators/excludes
165
		 * @see admin/jqadm/attribute/price/property/decorators/local
166
		 */
167
168
		/** admin/jqadm/attribute/price/property/decorators/local
169
		 * Adds a list of local decorators only to the attribute JQAdm client
170
		 *
171
		 * Decorators extend the functionality of a class by adding new aspects
172
		 * (e.g. log what is currently done), executing the methods of the underlying
173
		 * class only in certain conditions (e.g. only for logged in users) or
174
		 * modify what is returned to the caller.
175
		 *
176
		 * This option allows you to wrap local decorators
177
		 * ("\Aimeos\Admin\JQAdm\Attribute\Decorator\*") around the JQAdm client.
178
		 *
179
		 *  admin/jqadm/attribute/price/property/decorators/local = array( 'decorator2' )
180
		 *
181
		 * This would add the decorator named "decorator2" defined by
182
		 * "\Aimeos\Admin\JQAdm\Attribute\Decorator\Decorator2" only to the JQAdm client.
183
		 *
184
		 * @param array List of decorator names
185
		 * @since 2019.07
186
		 * @category Developer
187
		 * @see admin/jqadm/common/decorators/default
188
		 * @see admin/jqadm/attribute/price/property/decorators/excludes
189
		 * @see admin/jqadm/attribute/price/property/decorators/global
190
		 */
191
		return $this->createSubClient( 'attribute/price/property/' . $type, $name );
192
	}
193
194
195
	/**
196
	 * Adds the required data used in the attribute template
197
	 *
198
	 * @param \Aimeos\MW\View\Iface $view View object
199
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
200
	 */
201
	public function data( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
202
	{
203
		$manager = \Aimeos\MShop::create( $this->getContext(), 'price/property/type' );
204
205
		$search = $manager->filter( true )->slice( 0, 10000 );
206
		$search->setConditions( $search->compare( '==', 'price.property.type.domain', 'price' ) );
207
		$search->setSortations( [$search->sort( '+', 'price.property.type.position' )] );
208
209
		$view->propertyTypes = $manager->search( $search );
210
211
		return $view;
212
	}
213
214
215
	/**
216
	 * Returns the list of sub-client names configured for the client.
217
	 *
218
	 * @return array List of JQAdm client names
219
	 */
220
	protected function getSubClientNames() : array
221
	{
222
		/** admin/jqadm/attribute/price/property/subparts
223
		 * List of JQAdm sub-clients rendered within the attribute price property section
224
		 *
225
		 * The output of the frontend is composed of the code generated by the JQAdm
226
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
227
		 * that are responsible for rendering certain sub-parts of the output. The
228
		 * sub-clients can contain JQAdm clients themselves and therefore a
229
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
230
		 * the output that is placed inside the container of its parent.
231
		 *
232
		 * At first, always the JQAdm code generated by the parent is printed, then
233
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
234
		 * determines the order of the output of these sub-clients inside the parent
235
		 * container. If the configured list of clients is
236
		 *
237
		 *  array( "subclient1", "subclient2" )
238
		 *
239
		 * you can easily change the order of the output by reordering the subparts:
240
		 *
241
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
242
		 *
243
		 * You can also remove one or more parts if they shouldn't be rendered:
244
		 *
245
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
246
		 *
247
		 * As the clients only generates structural JQAdm, the layout defined via CSS
248
		 * should support adding, removing or reordering content by a fluid like
249
		 * design.
250
		 *
251
		 * @param array List of sub-client names
252
		 * @since 2019.07
253
		 * @category Developer
254
		 */
255
		return $this->getContext()->getConfig()->get( 'admin/jqadm/attribute/price/property/subparts', [] );
256
	}
257
258
259
	/**
260
	 * Creates new and updates existing items using the data array
261
	 *
262
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object without referenced domain items
263
	 * @param array $data Data array
264
	 * @return \Aimeos\MShop\Attribute\Item\Iface Modified attribute item
265
	 */
266
	protected function fromArray( \Aimeos\MShop\Attribute\Item\Iface $item, array $data ) : \Aimeos\MShop\Attribute\Item\Iface
267
	{
268
		$propManager = \Aimeos\MShop::create( $this->getContext(), 'price/property' );
269
		$index = 0;
270
271
		foreach( $item->getRefItems( 'price', null, null, false ) as $refItem )
272
		{
273
			$propItems = $refItem->getPropertyItems( null, false );
274
275
			foreach( (array) $this->getValue( $data, $index . '/property', [] ) as $entry )
276
			{
277
				if( isset( $propItems[$entry['price.property.id']] ) )
278
				{
279
					$propItem = $propItems[$entry['price.property.id']];
280
					unset( $propItems[$entry['price.property.id']] );
281
				}
282
				else
283
				{
284
					$propItem = $propManager->create();
285
				}
286
287
				$propItem->fromArray( $entry, true );
288
				$refItem->addPropertyItem( $propItem );
289
			}
290
291
			$refItem->deletePropertyItems( $propItems->toArray() );
292
			$index++;
293
		}
294
295
		return $item;
296
	}
297
298
299
	/**
300
	 * Constructs the data array for the view from the given item
301
	 *
302
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object including referenced domain items
303
	 * @param array $data Associative list of price data
304
	 * @param bool $copy True if items should be copied, false if not
305
	 * @return string[] Multi-dimensional associative list of item data
306
	 */
307
	protected function toArray( \Aimeos\MShop\Attribute\Item\Iface $item, array $data, bool $copy = false ) : array
308
	{
309
		$idx = 0;
310
		$siteId = $this->getContext()->getLocale()->getSiteId();
311
312
		foreach( $item->getRefItems( 'price', null, null, false ) as $priceItem )
313
		{
314
			$data[$idx]['property'] = [];
315
316
			foreach( $priceItem->getPropertyItems( null, false ) as $propItem )
317
			{
318
				$list = $propItem->toArray( true );
319
320
				if( $copy === true )
321
				{
322
					$list['price.property.siteid'] = $siteId;
323
					$list['price.property.id'] = '';
324
				}
325
326
				$data[$idx]['property'][] = $list;
327
			}
328
329
			$idx++;
330
		}
331
332
		return $data;
333
	}
334
335
336
	/**
337
	 * Returns the rendered template including the view data
338
	 *
339
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
340
	 * @return string HTML output
341
	 */
342
	protected function render( \Aimeos\MW\View\Iface $view ) : string
343
	{
344
		/** admin/jqadm/attribute/price/property/template-item
345
		 * Relative path to the HTML body template of the price subpart for attributes.
346
		 *
347
		 * The template file contains the HTML code and processing instructions
348
		 * to generate the result shown in the body of the frontend. The
349
		 * configuration string is the path to the template file relative
350
		 * to the templates directory (usually in admin/jqadm/templates).
351
		 *
352
		 * You can overwrite the template file configuration in extensions and
353
		 * provide alternative templates. These alternative templates should be
354
		 * named like the default one but with the string "default" replaced by
355
		 * an unique name. You may use the name of your project for this. If
356
		 * you've implemented an alternative client class as well, "default"
357
		 * should be replaced by the name of the new class.
358
		 *
359
		 * @param string Relative path to the template creating the HTML code
360
		 * @since 2019.07
361
		 * @category Developer
362
		 */
363
		$tplconf = 'admin/jqadm/attribute/price/property/template-item';
364
		$default = 'attribute/item-price-property-standard';
365
366
		return $view->render( $view->config( $tplconf, $default ) );
367
	}
368
}
369