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

Standard::toArray()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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