Standard   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 346
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 346
rs 10
c 0
b 0
f 0
wmc 21

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubClient() 0 73 1
A create() 0 14 2
A get() 0 7 1
A save() 0 8 1
A copy() 0 7 1
A render() 0 24 1
A toArray() 0 39 5
A getSubClientNames() 0 35 1
A getIntervalItems() 0 9 1
B fromArray() 0 31 7
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2025
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
	 */
35
36
37
	/**
38
	 * Copies a resource
39
	 *
40
	 * @return string|null HTML output
41
	 */
42
	public function copy() : ?string
43
	{
44
		$view = $this->object()->data( $this->view() );
45
		$view->subscriptionData = $this->toArray( $view->item, true );
46
		$view->subscriptionBody = parent::copy();
47
48
		return $this->render( $view );
49
	}
50
51
52
	/**
53
	 * Creates a new resource
54
	 *
55
	 * @return string|null HTML output
56
	 */
57
	public function create() : ?string
58
	{
59
		$view = $this->object()->data( $this->view() );
60
		$siteid = $this->context()->locale()->getSiteId();
61
		$data = $view->param( 'subscription', [] );
62
63
		foreach( $view->value( $data, 'product.lists.id', [] ) as $idx => $value ) {
64
			$data['product.lists.siteid'][$idx] = $siteid;
65
		}
66
67
		$view->subscriptionData = $data;
68
		$view->subscriptionBody = parent::create();
69
70
		return $this->render( $view );
71
	}
72
73
74
	/**
75
	 * Returns a single resource
76
	 *
77
	 * @return string|null HTML output
78
	 */
79
	public function get() : ?string
80
	{
81
		$view = $this->object()->data( $this->view() );
82
		$view->subscriptionData = $this->toArray( $view->item );
83
		$view->subscriptionBody = parent::get();
84
85
		return $this->render( $view );
86
	}
87
88
89
	/**
90
	 * Saves the data
91
	 *
92
	 * @return string|null HTML output
93
	 */
94
	public function save() : ?string
95
	{
96
		$view = $this->view();
97
98
		$this->fromArray( $view->item, $view->param( 'subscription', [] ) );
99
		$view->subscriptionBody = parent::save();
100
101
		return null;
102
	}
103
104
105
	/**
106
	 * Returns the sub-client given by its name.
107
	 *
108
	 * @param string $type Name of the client type
109
	 * @param string|null $name Name of the sub-client (Default if null)
110
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
111
	 */
112
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
113
	{
114
		/** admin/jqadm/product/subscription/decorators/excludes
115
		 * Excludes decorators added by the "common" subscription from the product JQAdm client
116
		 *
117
		 * Decorators extend the functionality of a class by adding new aspects
118
		 * (e.g. log what is currently done), executing the methods of the underlying
119
		 * class only in certain conditions (e.g. only for logged in users) or
120
		 * modify what is returned to the caller.
121
		 *
122
		 * This subscription allows you to remove a decorator added via
123
		 * "admin/jqadm/common/decorators/default" before they are wrapped
124
		 * around the JQAdm client.
125
		 *
126
		 *  admin/jqadm/product/subscription/decorators/excludes = array( 'decorator1' )
127
		 *
128
		 * This would remove the decorator named "decorator1" from the list of
129
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
130
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
131
		 *
132
		 * @param array List of decorator names
133
		 * @since 2018.04
134
		 * @see admin/jqadm/common/decorators/default
135
		 * @see admin/jqadm/product/subscription/decorators/global
136
		 * @see admin/jqadm/product/subscription/decorators/local
137
		 */
138
139
		/** admin/jqadm/product/subscription/decorators/global
140
		 * Adds a list of globally available decorators only to the product JQAdm client
141
		 *
142
		 * Decorators extend the functionality of a class by adding new aspects
143
		 * (e.g. log what is currently done), executing the methods of the underlying
144
		 * class only in certain conditions (e.g. only for logged in users) or
145
		 * modify what is returned to the caller.
146
		 *
147
		 * This subscription allows you to wrap global decorators
148
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
149
		 *
150
		 *  admin/jqadm/product/subscription/decorators/global = array( 'decorator1' )
151
		 *
152
		 * This would add the decorator named "decorator1" defined by
153
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
154
		 *
155
		 * @param array List of decorator names
156
		 * @since 2018.04
157
		 * @see admin/jqadm/common/decorators/default
158
		 * @see admin/jqadm/product/subscription/decorators/excludes
159
		 * @see admin/jqadm/product/subscription/decorators/local
160
		 */
161
162
		/** admin/jqadm/product/subscription/decorators/local
163
		 * Adds a list of local decorators only to the product JQAdm client
164
		 *
165
		 * Decorators extend the functionality of a class by adding new aspects
166
		 * (e.g. log what is currently done), executing the methods of the underlying
167
		 * class only in certain conditions (e.g. only for logged in users) or
168
		 * modify what is returned to the caller.
169
		 *
170
		 * This subscription allows you to wrap local decorators
171
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
172
		 *
173
		 *  admin/jqadm/product/subscription/decorators/local = array( 'decorator2' )
174
		 *
175
		 * This would add the decorator named "decorator2" defined by
176
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
177
		 *
178
		 * @param array List of decorator names
179
		 * @since 2018.04
180
		 * @see admin/jqadm/common/decorators/default
181
		 * @see admin/jqadm/product/subscription/decorators/excludes
182
		 * @see admin/jqadm/product/subscription/decorators/global
183
		 */
184
		return $this->createSubClient( 'product/subscription/' . $type, $name );
185
	}
186
187
188
	/**
189
	 * Returns the available attribute items of type "interval"
190
	 *
191
	 * @return \Aimeos\Map List of attribute IDs as keys and items implementing \Aimeos\MShop\Attribute\Item\Iface
192
	 */
193
	protected function getIntervalItems() : \Aimeos\Map
194
	{
195
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute' );
196
197
		$search = $manager->filter()
198
			->add( ['attribute.type' => 'interval', 'attribute.domain' => 'product'] )
199
			->order( 'attribute.code' );
200
201
		return $manager->search( $search );
202
	}
203
204
205
	/**
206
	 * Returns the list of sub-client names configured for the client.
207
	 *
208
	 * @return array List of JQAdm client names
209
	 */
210
	protected function getSubClientNames() : array
211
	{
212
		/** admin/jqadm/product/subscription/subparts
213
		 * List of JQAdm sub-clients rendered within the product subscription section
214
		 *
215
		 * The output of the frontend is composed of the code generated by the JQAdm
216
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
217
		 * that are responsible for rendering certain sub-parts of the output. The
218
		 * sub-clients can contain JQAdm clients themselves and therefore a
219
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
220
		 * the output that is placed inside the container of its parent.
221
		 *
222
		 * At first, always the JQAdm code generated by the parent is printed, then
223
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
224
		 * determines the order of the output of these sub-clients inside the parent
225
		 * container. If the configured list of clients is
226
		 *
227
		 *  array( "subclient1", "subclient2" )
228
		 *
229
		 * you can easily change the order of the output by reordering the subparts:
230
		 *
231
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
232
		 *
233
		 * You can also remove one or more parts if they shouldn't be rendered:
234
		 *
235
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
236
		 *
237
		 * As the clients only generates structural JQAdm, the layout defined via CSS
238
		 * should support adding, removing or reordering content by a fluid like
239
		 * design.
240
		 *
241
		 * @param array List of sub-client names
242
		 * @since 2018.04
243
		 */
244
		return $this->context()->config()->get( 'admin/jqadm/product/subscription/subparts', [] );
245
	}
246
247
248
	/**
249
	 * Creates new and updates existing items using the data array
250
	 *
251
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
252
	 * @param array $data Data array
253
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
254
	 */
255
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
256
	{
257
		$context = $this->context();
258
259
		$attrManager = \Aimeos\MShop::create( $context, 'attribute' );
260
		$manager = \Aimeos\MShop::create( $context, 'product' );
261
262
		$listItems = $item->getListItems( 'attribute', 'config', 'interval', false );
263
264
		foreach( $data as $entry )
265
		{
266
			if( !array_key_exists( 'attribute.id', $entry ) ) {
267
				continue;
268
			}
269
270
			if( $entry['attribute.id'] == '' || ( $listItem = $item->getListItem( 'attribute', 'config', $entry['attribute.id'], false ) ) === null ) {
271
				$listItem = $manager->createListItem()->setType( 'config' );
0 ignored issues
show
Bug introduced by
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
				$listItem = $manager->/** @scrutinizer ignore-call */ createListItem()->setType( 'config' );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
272
			}
273
274
			if( $entry['attribute.id'] == '' || ( $refItem = $listItem->getRefItem() ) === null )
275
			{
276
				$refItem = $attrManager->create()->setType( 'interval' );
277
				$refItem->fromArray( $entry, true );
278
			}
279
280
			unset( $listItems[$listItem->getId()] );
281
282
			$item->addListItem( 'attribute', $listItem, $refItem );
283
		}
284
285
		return $item->deleteListItems( $listItems );
286
	}
287
288
289
	/**
290
	 * Constructs the data array for the view from the given item
291
	 *
292
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
293
	 * @param bool $copy True if items should be copied, false if not
294
	 * @return string[] Multi-dimensional associative list of item data
295
	 */
296
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
297
	{
298
		$data = $map = [];
299
		$siteId = $this->context()->locale()->getSiteId();
300
301
		foreach( $item->getListItems( 'attribute', 'config', 'interval', false ) as $listItem ) {
302
			$map[$listItem->getRefId()] = $listItem;
303
		}
304
305
		foreach( $this->getIntervalItems() as $attrId => $attrItem )
306
		{
307
			$list = $attrItem->toArray( true );
308
309
			if( isset( $map[$attrId] ) && $copy !== true )
310
			{
311
				$list['product.lists.siteid'] = (string) $map[$attrId]->getSiteId();
312
				$list['product.lists.id'] = (string) $map[$attrId]->getId();
313
			}
314
			else
315
			{
316
				$list['product.lists.siteid'] = $siteId;
317
				$list['product.lists.id'] = '';
318
			}
319
320
			$matches = [];
321
			$regex = '/^P(([0-9]+)Y)?(([0-9]+)M)?(([0-9]+)W)?(([0-9]+)D)?(T([0-9]+)H)?$/';
322
323
			preg_match( $regex, $list['attribute.code'], $matches );
324
325
			$list['Y'] = (int) ( $matches[2] ?? 0 );
326
			$list['M'] = (int) ( $matches[4] ?? 0 );
327
			$list['W'] = (int) ( $matches[6] ?? 0 );
328
			$list['D'] = (int) ( $matches[8] ?? 0 );
329
			$list['H'] = (int) ( $matches[10] ?? 0 );
330
331
			$data[] = $list;
332
		}
333
334
		return $data;
335
	}
336
337
338
	/**
339
	 * Returns the rendered template including the view data
340
	 *
341
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
342
	 * @return string HTML output
343
	 */
344
	protected function render( \Aimeos\Base\View\Iface $view ) : string
345
	{
346
		/** admin/jqadm/product/subscription/template-item
347
		 * Relative path to the HTML body template of the subscription subpart for products.
348
		 *
349
		 * The template file contains the HTML code and processing instructions
350
		 * to generate the result shown in the body of the frontend. The
351
		 * configuration string is the path to the template file relative
352
		 * to the templates directory (usually in templates/admin/jqadm).
353
		 *
354
		 * You can overwrite the template file configuration in extensions and
355
		 * provide alternative templates. These alternative templates should be
356
		 * named like the default one but with the string "default" replaced by
357
		 * an unique name. You may use the name of your project for this. If
358
		 * you've implemented an alternative client class as well, "default"
359
		 * should be replaced by the name of the new class.
360
		 *
361
		 * @param string Relative path to the template creating the HTML code
362
		 * @since 2018.04
363
		 */
364
		$tplconf = 'admin/jqadm/product/subscription/template-item';
365
		$default = 'product/item-subscription';
366
367
		return $view->render( $view->config( $tplconf, $default ) );
368
	}
369
}
370