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

Standard::data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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