Passed
Push — master ( 98a0a0...f25a73 )
by Aimeos
18:48
created

Standard::fromArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 31
rs 9.6333
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Characteristic\Attribute;
12
13
14
/**
15
 * Default implementation of product attribute JQAdm client.
16
 *
17
 * @package Admin
18
 * @subpackage JQAdm
19
 */
20
class Standard
21
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
22
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
23
{
24
	/** admin/jqadm/product/characteristic/attribute/name
25
	 * Name of the characteristic/attribute subpart used by the JQAdm product implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Characteristic\Attribute\Myname".
28
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
29
	 *
30
	 * @param string Last part of the JQAdm class name
31
	 * @since 2016.04
32
	 */
33
34
35
	/**
36
	 * Adds the required data used in the template
37
	 *
38
	 * @param \Aimeos\Base\View\Iface $view View object
39
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
40
	 */
41
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
42
	{
43
		/** admin/jqadm/product/characteristic/attribute/types
44
		 * List of attribute types shown in the JQAdm product characteristic panel
45
		 *
46
		 * Limits the attribute types shown in the JQAdm product characteristic panel
47
		 * to the ones listed here. The order of the types in the list defines
48
		 * the order of the attribute types in the JQAdm product characteristic panel.
49
		 *
50
		 * @param array List of attribute types
51
		 * @since 2025.04
52
		 */
53
		$types = $this->context()->config()->get( 'admin/jqadm/product/characteristic/attribute/types', [] );
54
55
		$manager = \Aimeos\MShop::create( $this->context(), 'product/lists/type' );
56
		$filter = $manager->filter()->add( 'product.lists.type.code', '==', $types )->slice( 0, 10000 );
57
		$map = $manager->search( $filter )->col( null, 'product.lists.type.code' );
58
59
		$view->attributeTypes = $map->order( $types );
60
		return $view;
61
	}
62
63
64
	/**
65
	 * Copies a resource
66
	 *
67
	 * @return string|null HTML output
68
	 */
69
	public function copy() : ?string
70
	{
71
		$view = $this->object()->data( $this->view() );
72
		$view->attributeData = $this->toArray( $view->item, true );
73
		$view->attributeBody = parent::copy();
74
75
		return $this->render( $view );
76
	}
77
78
79
	/**
80
	 * Creates a new resource
81
	 *
82
	 * @return string|null HTML output
83
	 */
84
	public function create() : ?string
85
	{
86
		$view = $this->object()->data( $this->view() );
87
		$siteid = $this->context()->locale()->getSiteId();
88
89
		$itemData = $this->toArray( $view->item );
90
		$data = array_replace_recursive( $itemData, $view->param( 'characteristic/attribute', [] ) );
91
92
		foreach( $data as $key => $entry ) {
93
			$data[$key]['product.lists.siteid'] = $siteid;
94
		}
95
96
		$view->attributeData = $data;
97
		$view->attributeBody = parent::create();
98
99
		return $this->render( $view );
100
	}
101
102
103
	/**
104
	 * Returns a single resource
105
	 *
106
	 * @return string|null HTML output
107
	 */
108
	public function get() : ?string
109
	{
110
		$view = $this->object()->data( $this->view() );
111
		$view->attributeData = $this->toArray( $view->item );
112
		$view->attributeBody = parent::get();
113
114
		return $this->render( $view );
115
	}
116
117
118
	/**
119
	 * Saves the data
120
	 *
121
	 * @return string|null HTML output
122
	 */
123
	public function save() : ?string
124
	{
125
		$view = $this->view();
126
127
		$this->fromArray( $view->item, $view->param( 'characteristic/attribute', [] ) );
128
		$view->attributeBody = parent::save();
129
130
		return null;
131
	}
132
133
134
	/**
135
	 * Returns the sub-client given by its name.
136
	 *
137
	 * @param string $type Name of the client type
138
	 * @param string|null $name Name of the sub-client (Default if null)
139
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
140
	 */
141
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
142
	{
143
		/** admin/jqadm/product/characteristic/attribute/decorators/excludes
144
		 * Excludes decorators added by the "common" option from the product JQAdm client
145
		 *
146
		 * Decorators extend the functionality of a class by adding new aspects
147
		 * (e.g. log what is currently done), executing the methods of the underlying
148
		 * class only in certain conditions (e.g. only for logged in users) or
149
		 * modify what is returned to the caller.
150
		 *
151
		 * This option allows you to remove a decorator added via
152
		 * "admin/jqadm/common/decorators/default" before they are wrapped
153
		 * around the JQAdm client.
154
		 *
155
		 *  admin/jqadm/product/characteristic/attribute/decorators/excludes = array( 'decorator1' )
156
		 *
157
		 * This would remove the decorator named "decorator1" from the list of
158
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
159
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
160
		 *
161
		 * @param array List of decorator names
162
		 * @since 2016.01
163
		 * @see admin/jqadm/common/decorators/default
164
		 * @see admin/jqadm/product/characteristic/attribute/decorators/global
165
		 * @see admin/jqadm/product/characteristic/attribute/decorators/local
166
		 */
167
168
		/** admin/jqadm/product/characteristic/attribute/decorators/global
169
		 * Adds a list of globally available decorators only to the product JQAdm client
170
		 *
171
		 * Decorators extend the functionality of a class by adding new aspects
172
		 * (e.g. log what is currently done), executing the methods of the underlying
173
		 * class only in certain conditions (e.g. only for logged in users) or
174
		 * modify what is returned to the caller.
175
		 *
176
		 * This option allows you to wrap global decorators
177
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
178
		 *
179
		 *  admin/jqadm/product/characteristic/attribute/decorators/global = array( 'decorator1' )
180
		 *
181
		 * This would add the decorator named "decorator1" defined by
182
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
183
		 *
184
		 * @param array List of decorator names
185
		 * @since 2016.01
186
		 * @see admin/jqadm/common/decorators/default
187
		 * @see admin/jqadm/product/characteristic/attribute/decorators/excludes
188
		 * @see admin/jqadm/product/characteristic/attribute/decorators/local
189
		 */
190
191
		/** admin/jqadm/product/characteristic/attribute/decorators/local
192
		 * Adds a list of local decorators only to the product JQAdm client
193
		 *
194
		 * Decorators extend the functionality of a class by adding new aspects
195
		 * (e.g. log what is currently done), executing the methods of the underlying
196
		 * class only in certain conditions (e.g. only for logged in users) or
197
		 * modify what is returned to the caller.
198
		 *
199
		 * This option allows you to wrap local decorators
200
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
201
		 *
202
		 *  admin/jqadm/product/characteristic/attribute/decorators/local = array( 'decorator2' )
203
		 *
204
		 * This would add the decorator named "decorator2" defined by
205
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
206
		 *
207
		 * @param array List of decorator names
208
		 * @since 2016.01
209
		 * @see admin/jqadm/common/decorators/default
210
		 * @see admin/jqadm/product/characteristic/attribute/decorators/excludes
211
		 * @see admin/jqadm/product/characteristic/attribute/decorators/global
212
		 */
213
		return $this->createSubClient( 'product/characteristic/attribute/' . $type, $name );
214
	}
215
216
217
	/**
218
	 * Returns the list of sub-client names configured for the client.
219
	 *
220
	 * @return array List of JQAdm client names
221
	 */
222
	protected function getSubClientNames() : array
223
	{
224
		/** admin/jqadm/product/characteristic/attribute/subparts
225
		 * List of JQAdm sub-clients rendered within the product attribute section
226
		 *
227
		 * The output of the frontend is composed of the code generated by the JQAdm
228
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
229
		 * that are responsible for rendering certain sub-parts of the output. The
230
		 * sub-clients can contain JQAdm clients themselves and therefore a
231
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
232
		 * the output that is placed inside the container of its parent.
233
		 *
234
		 * At first, always the JQAdm code generated by the parent is printed, then
235
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
236
		 * determines the order of the output of these sub-clients inside the parent
237
		 * container. If the configured list of clients is
238
		 *
239
		 *  array( "subclient1", "subclient2" )
240
		 *
241
		 * you can easily change the order of the output by reordering the subparts:
242
		 *
243
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
244
		 *
245
		 * You can also remove one or more parts if they shouldn't be rendered:
246
		 *
247
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
248
		 *
249
		 * As the clients only generates structural JQAdm, the layout defined via CSS
250
		 * should support adding, removing or reordering content by a fluid like
251
		 * design.
252
		 *
253
		 * @param array List of sub-client names
254
		 * @since 2016.01
255
		 */
256
		return $this->context()->config()->get( 'admin/jqadm/product/characteristic/attribute/subparts', [] );
257
	}
258
259
260
	/**
261
	 * Creates new and updates existing items using the data array
262
	 *
263
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
264
	 * @param array $data Data array
265
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
266
	 */
267
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
268
	{
269
		$idx = 0;
270
		$context = $this->context();
271
		$listItems = $item->getListItems( 'attribute', null, null, false )->remove(
272
			$item->getListItems( 'attribute', ['config', 'custom'], ['interval', 'price'], false )->keys()
273
		);
274
275
		$manager = \Aimeos\MShop::create( $context, 'attribute' );
276
		$filter = $manager->filter()
277
			->add( 'attribute.id', '==', array_column( $data, 'attribute.id' ) )
278
			->slice( 0, count( $data ) );
279
		$refItems = $manager->search( $filter );
280
281
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
282
283
		foreach( $data as $entry )
284
		{
285
			$id = $this->val( $entry, 'product.lists.id' );
286
			$refid = $this->val( $entry, 'attribute.id' );
287
288
			$config = array_column( (array) $this->val( $entry, 'config', [] ), 'val', 'key' );
289
			$config = array_filter( array_map( fn( $val ) => trim( json_decode( $val, true ) ?? $val ?? '' ), $config ) );
290
291
			$listItem = $listItems->pull( $id ) ?: $manager->createListItem();
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

291
			$listItem = $listItems->pull( $id ) ?: $manager->/** @scrutinizer ignore-call */ createListItem();

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...
292
			$listItem->fromArray( $entry, true )->setId( $id )->setRefId( $refid )->setPosition( $idx++ )->setConfigFlat( $config );
293
294
			$item->addListItem( 'attribute', $listItem, $refItems->get( $refid ) );
295
		}
296
297
		return $item->deleteListItems( $listItems );
298
	}
299
300
301
	/**
302
	 * Constructs the data array for the view from the given item
303
	 *
304
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
305
	 * @param bool $copy True if items should be copied, false if not
306
	 * @return string[] Multi-dimensional associative list of item data
307
	 */
308
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
309
	{
310
		$data = [];
311
		$siteId = $this->context()->locale()->getSiteId();
312
313
		foreach( $item->getListItems( 'attribute', null, null, false ) as $listItem )
314
		{
315
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
316
				continue;
317
			}
318
319
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
320
321
			if( $copy === true )
322
			{
323
				$list['product.lists.siteid'] = $siteId;
324
				$list['product.lists.id'] = '';
325
			}
326
327
			$list['config'] = [];
328
329
			foreach( $listItem->getConfig() as $key => $value ) {
330
				$list['config'][] = ['key' => $key, 'val' => $value];
331
			}
332
333
			$data[] = $list;
334
		}
335
336
		return $data;
337
	}
338
339
340
	/**
341
	 * Returns the rendered template including the view data
342
	 *
343
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
344
	 * @return string HTML output
345
	 */
346
	protected function render( \Aimeos\Base\View\Iface $view ) : string
347
	{
348
		/** admin/jqadm/product/characteristic/attribute/template-item
349
		 * Relative path to the HTML body template of the attribute characteristic subpart for products.
350
		 *
351
		 * The template file contains the HTML code and processing instructions
352
		 * to generate the result shown in the body of the frontend. The
353
		 * configuration string is the path to the template file relative
354
		 * to the templates directory (usually in templates/admin/jqadm).
355
		 *
356
		 * You can overwrite the template file configuration in extensions and
357
		 * provide alternative templates. These alternative templates should be
358
		 * named like the default one but with the string "default" replaced by
359
		 * an unique name. You may use the name of your project for this. If
360
		 * you've implemented an alternative client class as well, "default"
361
		 * should be replaced by the name of the new class.
362
		 *
363
		 * @param string Relative path to the template creating the HTML code
364
		 * @since 2016.04
365
		 */
366
		$tplconf = 'admin/jqadm/product/characteristic/attribute/template-item';
367
		$default = 'product/item-characteristic-attribute';
368
369
		return $view->render( $view->config( $tplconf, $default ) );
370
	}
371
}
372