Passed
Push — master ( 419227...7de7ad )
by Aimeos
04:34
created

Standard   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 372
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 81
c 2
b 0
f 0
dl 0
loc 372
rs 10
wmc 25

10 Methods

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