Passed
Push — master ( a5f59f...10fa5b )
by Aimeos
03:58
created

Standard::toArray()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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