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

Standard::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 0
dl 0
loc 20
rs 9.9
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), 2017-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Attribute\Price;
12
13
sprintf( 'price' ); // 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/name
27
	 * Name of the price subpart used by the JQAdm attribute implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Attribute\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
	 * @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, true );
47
		$view->priceBody = '';
48
49
		foreach( $this->getSubClients() as $client ) {
50
			$view->priceBody .= $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->param( 'price', [] );
67
68
		foreach( $data as $idx => $entry )
69
		{
70
			$data[$idx]['attribute.lists.siteid'] = $siteid;
71
			$data[$idx]['price.siteid'] = $siteid;
72
		}
73
74
		$view->priceData = $data;
75
		$view->priceBody = '';
76
77
		foreach( $this->getSubClients() as $client ) {
78
			$view->priceBody .= $client->create();
79
		}
80
81
		return $this->render( $view );
82
	}
83
84
85
	/**
86
	 * Deletes a resource
87
	 *
88
	 * @return string|null HTML output
89
	 */
90
	public function delete() : ?string
91
	{
92
		parent::delete();
93
94
		$item = $this->getView()->item;
95
		$item->deleteListItems( $item->getListItems( 'price', null, null, false )->toArray(), true );
96
97
		return null;
98
	}
99
100
101
	/**
102
	 * Returns a single resource
103
	 *
104
	 * @return string|null HTML output
105
	 */
106
	public function get() : ?string
107
	{
108
		$view = $this->getObject()->addData( $this->getView() );
109
		$view->priceData = $this->toArray( $view->item );
110
		$view->priceBody = '';
111
112
		foreach( $this->getSubClients() as $client ) {
113
			$view->priceBody .= $client->get();
114
		}
115
116
		return $this->render( $view );
117
	}
118
119
120
	/**
121
	 * Saves the data
122
	 *
123
	 * @return string|null HTML output
124
	 */
125
	public function save() : ?string
126
	{
127
		$view = $this->getView();
128
129
		$view->item = $this->fromArray( $view->item, $view->param( 'price', [] ) );
130
		$view->priceBody = '';
131
132
		foreach( $this->getSubClients() as $client ) {
133
			$view->priceBody .= $client->save();
134
		}
135
136
		return null;
137
	}
138
139
140
	/**
141
	 * Returns the sub-client given by its name.
142
	 *
143
	 * @param string $type Name of the client type
144
	 * @param string|null $name Name of the sub-client (Default if null)
145
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
146
	 */
147
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
148
	{
149
		/** admin/jqadm/attribute/price/decorators/excludes
150
		 * Excludes decorators added by the "common" option from the attribute JQAdm client
151
		 *
152
		 * Decorators extend the functionality of a class by adding new aspects
153
		 * (e.g. log what is currently done), executing the methods of the underlying
154
		 * class only in certain conditions (e.g. only for logged in users) or
155
		 * modify what is returned to the caller.
156
		 *
157
		 * This option allows you to remove a decorator added via
158
		 * "admin/jqadm/common/decorators/default" before they are wrapped
159
		 * around the JQAdm client.
160
		 *
161
		 *  admin/jqadm/attribute/price/decorators/excludes = array( 'decorator1' )
162
		 *
163
		 * This would remove the decorator named "decorator1" from the list of
164
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
165
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
166
		 *
167
		 * @param array List of decorator names
168
		 * @since 2016.01
169
		 * @category Developer
170
		 * @see admin/jqadm/common/decorators/default
171
		 * @see admin/jqadm/attribute/price/decorators/global
172
		 * @see admin/jqadm/attribute/price/decorators/local
173
		 */
174
175
		/** admin/jqadm/attribute/price/decorators/global
176
		 * Adds a list of globally available decorators only to the attribute JQAdm client
177
		 *
178
		 * Decorators extend the functionality of a class by adding new aspects
179
		 * (e.g. log what is currently done), executing the methods of the underlying
180
		 * class only in certain conditions (e.g. only for logged in users) or
181
		 * modify what is returned to the caller.
182
		 *
183
		 * This option allows you to wrap global decorators
184
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
185
		 *
186
		 *  admin/jqadm/attribute/price/decorators/global = array( 'decorator1' )
187
		 *
188
		 * This would add the decorator named "decorator1" defined by
189
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
190
		 *
191
		 * @param array List of decorator names
192
		 * @since 2016.01
193
		 * @category Developer
194
		 * @see admin/jqadm/common/decorators/default
195
		 * @see admin/jqadm/attribute/price/decorators/excludes
196
		 * @see admin/jqadm/attribute/price/decorators/local
197
		 */
198
199
		/** admin/jqadm/attribute/price/decorators/local
200
		 * Adds a list of local decorators only to the attribute JQAdm client
201
		 *
202
		 * Decorators extend the functionality of a class by adding new aspects
203
		 * (e.g. log what is currently done), executing the methods of the underlying
204
		 * class only in certain conditions (e.g. only for logged in users) or
205
		 * modify what is returned to the caller.
206
		 *
207
		 * This option allows you to wrap local decorators
208
		 * ("\Aimeos\Admin\JQAdm\Attribute\Decorator\*") around the JQAdm client.
209
		 *
210
		 *  admin/jqadm/attribute/price/decorators/local = array( 'decorator2' )
211
		 *
212
		 * This would add the decorator named "decorator2" defined by
213
		 * "\Aimeos\Admin\JQAdm\Attribute\Decorator\Decorator2" only to the JQAdm client.
214
		 *
215
		 * @param array List of decorator names
216
		 * @since 2016.01
217
		 * @category Developer
218
		 * @see admin/jqadm/common/decorators/default
219
		 * @see admin/jqadm/attribute/price/decorators/excludes
220
		 * @see admin/jqadm/attribute/price/decorators/global
221
		 */
222
		return $this->createSubClient( 'attribute/price/' . $type, $name );
223
	}
224
225
226
	/**
227
	 * Returns the list of sub-client names configured for the client.
228
	 *
229
	 * @return array List of JQAdm client names
230
	 */
231
	protected function getSubClientNames() : array
232
	{
233
		/** admin/jqadm/attribute/price/standard/subparts
234
		 * List of JQAdm sub-clients rendered within the attribute price section
235
		 *
236
		 * The output of the frontend is composed of the code generated by the JQAdm
237
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
238
		 * that are responsible for rendering certain sub-parts of the output. The
239
		 * sub-clients can contain JQAdm clients themselves and therefore a
240
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
241
		 * the output that is placed inside the container of its parent.
242
		 *
243
		 * At first, always the JQAdm code generated by the parent is printed, then
244
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
245
		 * determines the order of the output of these sub-clients inside the parent
246
		 * container. If the configured list of clients is
247
		 *
248
		 *  array( "subclient1", "subclient2" )
249
		 *
250
		 * you can easily change the order of the output by reordering the subparts:
251
		 *
252
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
253
		 *
254
		 * You can also remove one or more parts if they shouldn't be rendered:
255
		 *
256
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
257
		 *
258
		 * As the clients only generates structural JQAdm, the layout defined via CSS
259
		 * should support adding, removing or reordering content by a fluid like
260
		 * design.
261
		 *
262
		 * @param array List of sub-client names
263
		 * @since 2016.01
264
		 * @category Developer
265
		 */
266
		return $this->getContext()->getConfig()->get( 'admin/jqadm/attribute/price/standard/subparts', [] );
267
	}
268
269
270
	/**
271
	 * Adds the required data used in the price template
272
	 *
273
	 * @param \Aimeos\MW\View\Iface $view View object
274
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
275
	 */
276
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
277
	{
278
		$context = $this->getContext();
279
280
		$priceTypeManager = \Aimeos\MShop::create( $context, 'price/type' );
281
		$listTypeManager = \Aimeos\MShop::create( $context, 'attribute/lists/type' );
282
		$currencyManager = \Aimeos\MShop::create( $context, 'locale/currency' );
283
284
		$search = $priceTypeManager->createSearch( true )->setSlice( 0, 10000 );
285
		$search->setConditions( $search->compare( '==', 'price.type.domain', 'attribute' ) );
286
		$search->setSortations( [$search->sort( '+', 'price.type.position' )] );
287
288
		$listSearch = $listTypeManager->createSearch( true )->setSlice( 0, 10000 );
289
		$listSearch->setConditions( $listSearch->compare( '==', 'attribute.lists.type.domain', 'price' ) );
290
		$listSearch->setSortations( [$listSearch->sort( '+', 'attribute.lists.type.position' )] );
291
292
		$curSearch = $currencyManager->createSearch( true )->setSlice( 0, 10000 );
293
294
		$view->priceTypes = $priceTypeManager->searchItems( $search );
295
		$view->priceListTypes = $listTypeManager->searchItems( $listSearch );
296
		$view->priceCurrencies = $currencyManager->searchItems( $curSearch );
297
298
		if( $view->priceCurrencies->isEmpty() ) {
299
			throw new \Aimeos\Admin\JQAdm\Exception( 'No currencies available. Please enable at least one currency' );
300
		}
301
302
		return $view;
303
	}
304
305
306
	/**
307
	 * Creates new and updates existing items using the data array
308
	 *
309
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object without referenced domain items
310
	 * @param array $data Data array
311
	 * @return \Aimeos\MShop\Attribute\Item\Iface Modified attribute item
312
	 */
313
	protected function fromArray( \Aimeos\MShop\Attribute\Item\Iface $item, array $data ) : \Aimeos\MShop\Attribute\Item\Iface
314
	{
315
		$context = $this->getContext();
316
317
		$priceManager = \Aimeos\MShop::create( $context, 'price' );
318
		$listManager = \Aimeos\MShop::create( $context, 'attribute/lists' );
319
320
		$listItems = $item->getListItems( 'price', null, null, false );
321
322
323
		foreach( $data as $idx => $entry )
324
		{
325
			if( ( $listItem = $item->getListItem( 'price', $entry['attribute.lists.type'], $entry['price.id'], false ) ) === null ) {
326
				$listItem = $listManager->createItem();
327
			}
328
329
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
330
				$refItem = $priceManager->createItem();
331
			}
332
333
			$refItem->fromArray( $entry, true );
334
			$conf = [];
335
336
			foreach( (array) $this->getValue( $entry, 'config', [] ) as $cfg )
337
			{
338
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' ) {
339
					$conf[$key] = trim( $cfg['val'] ?? '' );
340
				}
341
			}
342
343
			$listItem->fromArray( $entry, true );
344
			$listItem->setPosition( $idx );
345
			$listItem->setConfig( $conf );
346
347
			$item->addListItem( 'price', $listItem, $refItem );
348
349
			unset( $listItems[$listItem->getId()] );
350
		}
351
352
		return $item->deleteListItems( $listItems->toArray(), true );
353
	}
354
355
356
	/**
357
	 * Constructs the data array for the view from the given item
358
	 *
359
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object including referenced domain items
360
	 * @param bool $copy True if items should be copied, false if not
361
	 * @return string[] Multi-dimensional associative list of item data
362
	 */
363
	protected function toArray( \Aimeos\MShop\Attribute\Item\Iface $item, bool $copy = false ) : array
364
	{
365
		$data = [];
366
		$siteId = $this->getContext()->getLocale()->getSiteId();
367
368
		foreach( $item->getListItems( 'price', null, null, false ) as $listItem )
369
		{
370
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
371
				continue;
372
			}
373
374
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
375
376
			if( $copy === true )
377
			{
378
				$list['attribute.lists.siteid'] = $siteId;
379
				$list['attribute.lists.id'] = '';
380
				$list['price.siteid'] = $siteId;
381
				$list['price.id'] = null;
382
			}
383
384
			$list['attribute.lists.datestart'] = str_replace( ' ', 'T', $list['attribute.lists.datestart'] );
385
			$list['attribute.lists.dateend'] = str_replace( ' ', 'T', $list['attribute.lists.dateend'] );
386
			$list['config'] = [];
387
388
			foreach( $listItem->getConfig() as $key => $value ) {
389
				$list['config'][] = ['key' => $key, 'val' => $value];
390
			}
391
392
			if( empty( $refItem->getTaxRates() ) ) {
393
				$list['price.taxrates'] = ['' => ''];
394
			}
395
396
			$data[] = $list;
397
		}
398
399
		return $data;
400
	}
401
402
403
	/**
404
	 * Returns the rendered template including the view data
405
	 *
406
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
407
	 * @return string|null HTML output
408
	 */
409
	protected function render( \Aimeos\MW\View\Iface $view ) : string
410
	{
411
		/** admin/jqadm/attribute/price/template-item
412
		 * Relative path to the HTML body template of the price subpart for attributes.
413
		 *
414
		 * The template file contains the HTML code and processing instructions
415
		 * to generate the result shown in the body of the frontend. The
416
		 * configuration string is the path to the template file relative
417
		 * to the templates directory (usually in admin/jqadm/templates).
418
		 *
419
		 * You can overwrite the template file configuration in extensions and
420
		 * provide alternative templates. These alternative templates should be
421
		 * named like the default one but with the string "default" replaced by
422
		 * an unique name. You may use the name of your project for this. If
423
		 * you've implemented an alternative client class as well, "default"
424
		 * should be replaced by the name of the new class.
425
		 *
426
		 * @param string Relative path to the template creating the HTML code
427
		 * @since 2016.04
428
		 * @category Developer
429
		 */
430
		$tplconf = 'admin/jqadm/attribute/price/template-item';
431
		$default = 'attribute/item-price-standard';
432
433
		return $view->render( $view->config( $tplconf, $default ) );
434
	}
435
}
436