Completed
Push — master ( 626216...e78c82 )
by Aimeos
03:45
created

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