Passed
Push — master ( 8c5804...4577b9 )
by Aimeos
03:39
created

Standard::fromArray()   B

Complexity

Conditions 11
Paths 13

Size

Total Lines 47
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 26
nc 13
nop 2
dl 0
loc 47
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2023
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Media;
12
13
sprintf( 'media' ); // for translation
14
15
16
/**
17
 * Default implementation of product media 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/media/name
27
	 * Name of the media subpart used by the JQAdm product implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Media\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 2017.07
34
	 */
35
36
37
	/**
38
	 * Adds the required data used in the product template
39
	 *
40
	 * @param \Aimeos\Base\View\Iface $view View object
41
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
42
	 */
43
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
44
	{
45
		$context = $this->context();
46
47
		$typeManager = \Aimeos\MShop::create( $context, 'media/type' );
48
		$listTypeManager = \Aimeos\MShop::create( $context, 'product/lists/type' );
49
50
		$search = $typeManager->filter( true )->slice( 0, 10000 );
51
		$search->add( 'media.type.domain', '==', 'product' )->order( 'media.type.code' );
52
53
		$listSearch = $listTypeManager->filter( true )->slice( 0, 10000 );
54
		$listSearch->add( 'product.lists.type.domain', '==', 'media' )->order( 'product.lists.type.code' );
55
56
		$view->mediaListTypes = $listTypeManager->search( $listSearch );
57
		$view->mediaTypes = $typeManager->search( $search );
58
59
		return $view;
60
	}
61
62
63
	/**
64
	 * Copies a resource
65
	 *
66
	 * @return string|null HTML output
67
	 */
68
	public function copy() : ?string
69
	{
70
		$view = $this->object()->data( $this->view() );
71
		$view->mediaData = $this->toArray( $view->item, true );
72
		$view->mediaBody = parent::copy();
73
74
		return $this->render( $view );
75
	}
76
77
78
	/**
79
	 * Creates a new resource
80
	 *
81
	 * @return string|null HTML output
82
	 */
83
	public function create() : ?string
84
	{
85
		$view = $this->object()->data( $this->view() );
86
		$siteid = $this->context()->locale()->getSiteId();
87
88
		$itemData = $this->toArray( $view->item );
89
		$data = array_replace_recursive( $itemData, $view->param( 'media', [] ) );
90
91
		foreach( $data as $idx => $entry )
92
		{
93
			$data[$idx]['media.siteid'] = $siteid;
94
			$data[$idx]['media.url'] = $entry['media.url'] ?? null;
95
			$data[$idx]['media.preview'] = $entry['media.preview'] ?? null;
96
			$data[$idx]['product.lists.siteid'] = $siteid;
97
		}
98
99
		$view->mediaData = $data;
100
		$view->mediaBody = parent::create();
101
102
		return $this->render( $view );
103
	}
104
105
106
	/**
107
	 * Deletes a resource
108
	 *
109
	 * @return string|null HTML output
110
	 */
111
	public function delete() : ?string
112
	{
113
		parent::delete();
114
115
		$item = $this->view()->item;
116
		$this->deleteMediaItems( $item, $item->getListItems( 'media', null, null, false ) );
117
118
		return null;
119
	}
120
121
122
	/**
123
	 * Returns a single resource
124
	 *
125
	 * @return string|null HTML output
126
	 */
127
	public function get() : ?string
128
	{
129
		$view = $this->object()->data( $this->view() );
130
		$view->mediaData = $this->toArray( $view->item );
131
		$view->mediaBody = parent::get();
132
133
		return $this->render( $view );
134
	}
135
136
137
	/**
138
	 * Saves the data
139
	 *
140
	 * @return string|null HTML output
141
	 */
142
	public function save() : ?string
143
	{
144
		$view = $this->view();
145
146
		$view->item = $this->fromArray( $view->item, $view->param( 'media', [] ) );
147
		$view->mediaBody = parent::save();
148
149
		return null;
150
	}
151
152
153
	/**
154
	 * Returns the sub-client given by its name.
155
	 *
156
	 * @param string $type Name of the client type
157
	 * @param string|null $name Name of the sub-client (Default if null)
158
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
159
	 */
160
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
161
	{
162
		/** admin/jqadm/product/media/decorators/excludes
163
		 * Excludes decorators added by the "common" option from 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 option allows you to remove a decorator added via
171
		 * "admin/jqadm/common/decorators/default" before they are wrapped
172
		 * around the JQAdm client.
173
		 *
174
		 *  admin/jqadm/product/media/decorators/excludes = array( 'decorator1' )
175
		 *
176
		 * This would remove the decorator named "decorator1" from the list of
177
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
178
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
179
		 *
180
		 * @param array List of decorator names
181
		 * @since 2017.07
182
		 * @see admin/jqadm/common/decorators/default
183
		 * @see admin/jqadm/product/media/decorators/global
184
		 * @see admin/jqadm/product/media/decorators/local
185
		 */
186
187
		/** admin/jqadm/product/media/decorators/global
188
		 * Adds a list of globally available decorators only to the product JQAdm client
189
		 *
190
		 * Decorators extend the functionality of a class by adding new aspects
191
		 * (e.g. log what is currently done), executing the methods of the underlying
192
		 * class only in certain conditions (e.g. only for logged in users) or
193
		 * modify what is returned to the caller.
194
		 *
195
		 * This option allows you to wrap global decorators
196
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
197
		 *
198
		 *  admin/jqadm/product/media/decorators/global = array( 'decorator1' )
199
		 *
200
		 * This would add the decorator named "decorator1" defined by
201
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
202
		 *
203
		 * @param array List of decorator names
204
		 * @since 2017.07
205
		 * @see admin/jqadm/common/decorators/default
206
		 * @see admin/jqadm/product/media/decorators/excludes
207
		 * @see admin/jqadm/product/media/decorators/local
208
		 */
209
210
		/** admin/jqadm/product/media/decorators/local
211
		 * Adds a list of local decorators only to the product JQAdm client
212
		 *
213
		 * Decorators extend the functionality of a class by adding new aspects
214
		 * (e.g. log what is currently done), executing the methods of the underlying
215
		 * class only in certain conditions (e.g. only for logged in users) or
216
		 * modify what is returned to the caller.
217
		 *
218
		 * This option allows you to wrap local decorators
219
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
220
		 *
221
		 *  admin/jqadm/product/media/decorators/local = array( 'decorator2' )
222
		 *
223
		 * This would add the decorator named "decorator2" defined by
224
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
225
		 *
226
		 * @param array List of decorator names
227
		 * @since 2017.07
228
		 * @see admin/jqadm/common/decorators/default
229
		 * @see admin/jqadm/product/media/decorators/excludes
230
		 * @see admin/jqadm/product/media/decorators/global
231
		 */
232
		return $this->createSubClient( 'product/media/' . $type, $name );
233
	}
234
235
236
	/**
237
	 * Adds the product variant attributes to the media item
238
	 * Then, the images will only be shown if the customer selected the product variant
239
	 *
240
	 * @param \Aimeos\MShop\Media\Item\Iface $mediaItem Media item, maybe with referenced attribute items
241
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $attrListItems Product list items referencing variant attributes
242
	 * @return \Aimeos\MShop\Media\Item\Iface Modified media item
243
	 */
244
	protected function addMediaAttributes( \Aimeos\MShop\Media\Item\Iface $mediaItem, array $attrListItems ) : \Aimeos\MShop\Media\Item\Iface
245
	{
246
		$listManager = \Aimeos\MShop::create( $this->context(), 'media/lists' );
247
		$listItems = $mediaItem->getListItems( 'attribute', 'variant', null, false );
248
249
		foreach( $attrListItems as $listItem )
250
		{
251
			if( ( $litem = $mediaItem->getListItem( 'attribute', 'variant', $listItem->getRefId(), false ) ) === null ) {
252
				$mediaItem->addListItem( 'attribute', $listManager->create()->setType( 'variant' )->setRefId( $listItem->getRefId() ) );
253
			} else {
254
				unset( $listItems[$litem->getId()] );
255
			}
256
		}
257
258
		return $mediaItem->deleteListItems( $listItems->toArray() );
259
	}
260
261
262
	/**
263
	 * Removes the media reference and the media item if not shared
264
	 *
265
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item including media reference
266
	 * @param \Aimeos\Map $listItems Media list items to be removed
267
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
268
	 */
269
	protected function deleteMediaItems( \Aimeos\MShop\Product\Item\Iface $item, \Aimeos\Map $listItems ) : \Aimeos\MShop\Product\Item\Iface
270
	{
271
		$context = $this->context();
272
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
0 ignored issues
show
Unused Code introduced by
The assignment to $mediaManager is dead and can be removed.
Loading history...
273
		$manager = \Aimeos\MShop::create( $context, 'product' );
274
		$search = $manager->filter();
275
276
		foreach( $listItems as $listItem )
277
		{
278
			$func = $search->make( 'product:has', ['media', $listItem->getType(), $listItem->getRefId()] );
279
			$search->setConditions( $search->compare( '!=', $func, null ) );
280
			$items = $manager->search( $search );
281
			$refItem = null;
282
283
			if( count( $items ) === 1 && ( $refItem = $listItem->getRefItem() ) !== null ) {
284
				$cntl->delete( $refItem );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cntl seems to be never defined.
Loading history...
285
			}
286
287
			$item->deleteListItem( 'media', $listItem, $refItem );
288
		}
289
290
		return $item;
291
	}
292
293
294
	/**
295
	 * Returns the list of sub-client names configured for the client.
296
	 *
297
	 * @return array List of JQAdm client names
298
	 */
299
	protected function getSubClientNames() : array
300
	{
301
		/** admin/jqadm/product/media/subparts
302
		 * List of JQAdm sub-clients rendered within the product media section
303
		 *
304
		 * The output of the frontend is composed of the code generated by the JQAdm
305
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
306
		 * that are responsible for rendering certain sub-parts of the output. The
307
		 * sub-clients can contain JQAdm clients themselves and therefore a
308
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
309
		 * the output that is placed inside the container of its parent.
310
		 *
311
		 * At first, always the JQAdm code generated by the parent is printed, then
312
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
313
		 * determines the order of the output of these sub-clients inside the parent
314
		 * container. If the configured list of clients is
315
		 *
316
		 *  array( "subclient1", "subclient2" )
317
		 *
318
		 * you can easily change the order of the output by reordering the subparts:
319
		 *
320
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
321
		 *
322
		 * You can also remove one or more parts if they shouldn't be rendered:
323
		 *
324
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
325
		 *
326
		 * As the clients only generates structural JQAdm, the layout defined via CSS
327
		 * should support adding, removing or reordering content by a fluid like
328
		 * design.
329
		 *
330
		 * @param array List of sub-client names
331
		 * @since 2017.07
332
		 */
333
		return $this->context()->config()->get( 'admin/jqadm/product/media/subparts', [] );
334
	}
335
336
337
	/**
338
	 * Creates new and updates existing items using the data array
339
	 *
340
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
341
	 * @param array $data Data array
342
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
343
	 */
344
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
345
	{
346
		$context = $this->context();
347
348
		$manager = \Aimeos\MShop::create( $context, 'product' );
349
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
350
351
		$listItems = $item->getListItems( 'media', null, null, false );
352
		$files = (array) $this->view()->request()->getUploadedFiles();
353
354
		foreach( $data as $idx => $entry )
355
		{
356
			$id = $this->val( $entry, 'media.id', '' );
357
			$type = $this->val( $entry, 'product.lists.type', 'default' );
358
359
			$listItem = $item->getListItem( 'media', $type, $id, false ) ?: $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

359
			$listItem = $item->getListItem( 'media', $type, $id, false ) ?: $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...
360
			$refItem = $listItem->getRefItem() ?: $mediaManager->create();
361
362
			$refItem->fromArray( $entry, true )->setDomain( 'product' );
363
			$file = $this->val( $files, 'media/' . $idx . '/file' );
364
			$preview = $this->val( $files, 'media/' . $idx . '/preview' );
0 ignored issues
show
Unused Code introduced by
The assignment to $preview is dead and can be removed.
Loading history...
365
366
			if( $refItem->getId() === null && $refItem->getUrl() !== '' ) {
367
				$refItem = $mediaManager->copy( $refItem );
368
			}
369
370
			if( $file && $file->getError() !== UPLOAD_ERR_NO_FILE ) {
371
				$refItem = $mediaManager->upload( $refItem, $file );
372
			}
373
374
			$listItem->fromArray( $entry, true )->setPosition( $idx )->setConfig( [] );
375
376
			foreach( (array) $this->val( $entry, 'config', [] ) as $cfg )
377
			{
378
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) {
379
					$listItem->setConfigValue( $key, json_decode( $val, true ) ?? $val );
380
				}
381
			}
382
383
			$attrListItems = $item->getListItems( 'attribute', 'variant', null, false )->toArray();
384
			$refItem = $this->addMediaAttributes( $refItem, $attrListItems );
385
			$item->addListItem( 'media', $listItem, $refItem );
386
387
			unset( $listItems[$listItem->getId()] );
388
		}
389
390
		return $this->deleteMediaItems( $item, $listItems );
391
	}
392
393
394
	/**
395
	 * Constructs the data array for the view from the given item
396
	 *
397
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
398
	 * @param bool $copy True if items should be copied, false if not
399
	 * @return string[] Multi-dimensional associative list of item data
400
	 */
401
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
402
	{
403
		$data = [];
404
		$siteId = $this->context()->locale()->getSiteId();
405
406
		foreach( $item->getListItems( 'media', null, null, false ) as $listItem )
407
		{
408
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
409
				continue;
410
			}
411
412
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
413
414
			if( $copy === true )
415
			{
416
				$list['product.lists.siteid'] = $siteId;
417
				$list['product.lists.id'] = '';
418
				$list['media.siteid'] = $siteId;
419
				$list['media.id'] = null;
420
			}
421
422
			$list['media.previews'] = $this->view()->imageset( $refItem->getPreviews(), $refItem->getFileSystem() );
423
			$list['media.preview'] = $this->view()->content( $refItem->getPreview(), $refItem->getFileSystem() );
424
425
			$list['product.lists.datestart'] = str_replace( ' ', 'T', $list['product.lists.datestart'] ?? '' );
426
			$list['product.lists.dateend'] = str_replace( ' ', 'T', $list['product.lists.dateend'] ?? '' );
427
			$list['config'] = [];
428
429
			foreach( $listItem->getConfig() as $key => $value ) {
430
				$list['config'][] = ['key' => $key, 'val' => $value];
431
			}
432
433
			$data[] = $list;
434
		}
435
436
		return $data;
437
	}
438
439
440
	/**
441
	 * Returns the rendered template including the view data
442
	 *
443
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
444
	 * @return string HTML output
445
	 */
446
	protected function render( \Aimeos\Base\View\Iface $view ) : string
447
	{
448
		/** admin/jqadm/product/media/template-item
449
		 * Relative path to the HTML body template of the media subpart for products.
450
		 *
451
		 * The template file contains the HTML code and processing instructions
452
		 * to generate the result shown in the body of the frontend. The
453
		 * configuration string is the path to the template file relative
454
		 * to the templates directory (usually in templates/admin/jqadm).
455
		 *
456
		 * You can overwrite the template file configuration in extensions and
457
		 * provide alternative templates. These alternative templates should be
458
		 * named like the default one but with the string "default" replaced by
459
		 * an unique name. You may use the name of your project for this. If
460
		 * you've implemented an alternative client class as well, "default"
461
		 * should be replaced by the name of the new class.
462
		 *
463
		 * @param string Relative path to the template creating the HTML code
464
		 * @since 2017.07
465
		 */
466
		$tplconf = 'admin/jqadm/product/media/template-item';
467
		$default = 'product/item-media';
468
469
		return $view->render( $view->config( $tplconf, $default ) );
470
	}
471
}
472