Passed
Push — master ( a90b51...436fb8 )
by Aimeos
04:00
created

Standard::fromArray()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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

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