Passed
Push — master ( e56adc...f6e7c5 )
by Aimeos
07:09 queued 03:09
created

Standard::fromArray()   C

Complexity

Conditions 12
Paths 49

Size

Total Lines 61
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 33
nc 49
nop 2
dl 0
loc 61
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    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-2021
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
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Adds the required data used in the product template
40
	 *
41
	 * @param \Aimeos\MW\View\Iface $view View object
42
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
43
	 */
44
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
45
	{
46
		$context = $this->getContext();
47
48
		$typeManager = \Aimeos\MShop::create( $context, 'media/type' );
49
		$listTypeManager = \Aimeos\MShop::create( $context, 'product/lists/type' );
50
51
		$search = $typeManager->filter( true )->slice( 0, 10000 );
52
		$search->setConditions( $search->compare( '==', 'media.type.domain', 'product' ) );
53
		$search->setSortations( [$search->sort( '+', 'media.type.position' )] );
54
55
		$listSearch = $listTypeManager->filter( true )->slice( 0, 10000 );
56
		$listSearch->setConditions( $listSearch->compare( '==', 'product.lists.type.domain', 'media' ) );
57
		$listSearch->setSortations( [$listSearch->sort( '+', 'product.lists.type.position' )] );
58
59
		$view->mediaListTypes = $listTypeManager->search( $listSearch );
60
		$view->mediaTypes = $typeManager->search( $search );
61
62
		return $view;
63
	}
64
65
66
	/**
67
	 * Copies a resource
68
	 *
69
	 * @return string|null HTML output
70
	 */
71
	public function copy() : ?string
72
	{
73
		$view = $this->getObject()->addData( $this->getView() );
74
		$view->mediaData = $this->toArray( $view->item, true );
75
		$view->mediaBody = parent::copy();
76
77
		return $this->render( $view );
78
	}
79
80
81
	/**
82
	 * Creates a new resource
83
	 *
84
	 * @return string|null HTML output
85
	 */
86
	public function create() : ?string
87
	{
88
		$view = $this->getObject()->addData( $this->getView() );
89
		$siteid = $this->getContext()->getLocale()->getSiteId();
90
91
		$itemData = $this->toArray( $view->item );
92
		$data = array_replace_recursive( $itemData, $view->param( 'media', [] ) );
93
94
		foreach( $data as $idx => $entry )
95
		{
96
			$data[$idx]['media.siteid'] = $siteid;
97
			$data[$idx]['media.url'] = $entry['media.url'] ?? null;
98
			$data[$idx]['media.preview'] = $entry['media.preview'] ?? null;
99
			$data[$idx]['product.lists.siteid'] = $siteid;
100
		}
101
102
		$view->mediaData = $data;
103
		$view->mediaBody = parent::create();
104
105
		return $this->render( $view );
106
	}
107
108
109
	/**
110
	 * Deletes a resource
111
	 *
112
	 * @return string|null HTML output
113
	 */
114
	public function delete() : ?string
115
	{
116
		parent::delete();
117
118
		$item = $this->getView()->item;
119
		$this->deleteMediaItems( $item, $item->getListItems( 'media', null, null, false ) );
120
121
		return null;
122
	}
123
124
125
	/**
126
	 * Returns a single resource
127
	 *
128
	 * @return string|null HTML output
129
	 */
130
	public function get() : ?string
131
	{
132
		$view = $this->getObject()->addData( $this->getView() );
133
		$view->mediaData = $this->toArray( $view->item );
134
		$view->mediaBody = parent::get();
135
136
		return $this->render( $view );
137
	}
138
139
140
	/**
141
	 * Saves the data
142
	 *
143
	 * @return string|null HTML output
144
	 */
145
	public function save() : ?string
146
	{
147
		$view = $this->getView();
148
149
		$view->item = $this->fromArray( $view->item, $view->param( 'media', [] ) );
150
		$view->mediaBody = parent::save();
151
152
		return null;
153
	}
154
155
156
	/**
157
	 * Returns the sub-client given by its name.
158
	 *
159
	 * @param string $type Name of the client type
160
	 * @param string|null $name Name of the sub-client (Default if null)
161
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
162
	 */
163
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
164
	{
165
		/** admin/jqadm/product/media/decorators/excludes
166
		 * Excludes decorators added by the "common" option from 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 option allows you to remove a decorator added via
174
		 * "admin/jqadm/common/decorators/default" before they are wrapped
175
		 * around the JQAdm client.
176
		 *
177
		 *  admin/jqadm/product/media/decorators/excludes = array( 'decorator1' )
178
		 *
179
		 * This would remove the decorator named "decorator1" from the list of
180
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
181
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
182
		 *
183
		 * @param array List of decorator names
184
		 * @since 2017.07
185
		 * @category Developer
186
		 * @see admin/jqadm/common/decorators/default
187
		 * @see admin/jqadm/product/media/decorators/global
188
		 * @see admin/jqadm/product/media/decorators/local
189
		 */
190
191
		/** admin/jqadm/product/media/decorators/global
192
		 * Adds a list of globally available 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 global decorators
200
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
201
		 *
202
		 *  admin/jqadm/product/media/decorators/global = array( 'decorator1' )
203
		 *
204
		 * This would add the decorator named "decorator1" defined by
205
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
206
		 *
207
		 * @param array List of decorator names
208
		 * @since 2017.07
209
		 * @category Developer
210
		 * @see admin/jqadm/common/decorators/default
211
		 * @see admin/jqadm/product/media/decorators/excludes
212
		 * @see admin/jqadm/product/media/decorators/local
213
		 */
214
215
		/** admin/jqadm/product/media/decorators/local
216
		 * Adds a list of local decorators only to the product JQAdm client
217
		 *
218
		 * Decorators extend the functionality of a class by adding new aspects
219
		 * (e.g. log what is currently done), executing the methods of the underlying
220
		 * class only in certain conditions (e.g. only for logged in users) or
221
		 * modify what is returned to the caller.
222
		 *
223
		 * This option allows you to wrap local decorators
224
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
225
		 *
226
		 *  admin/jqadm/product/media/decorators/local = array( 'decorator2' )
227
		 *
228
		 * This would add the decorator named "decorator2" defined by
229
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
230
		 *
231
		 * @param array List of decorator names
232
		 * @since 2017.07
233
		 * @category Developer
234
		 * @see admin/jqadm/common/decorators/default
235
		 * @see admin/jqadm/product/media/decorators/excludes
236
		 * @see admin/jqadm/product/media/decorators/global
237
		 */
238
		return $this->createSubClient( 'product/media/' . $type, $name );
239
	}
240
241
242
	/**
243
	 * Adds the product variant attributes to the media item
244
	 * Then, the images will only be shown if the customer selected the product variant
245
	 *
246
	 * @param \Aimeos\MShop\Media\Item\Iface $mediaItem Media item, maybe with referenced attribute items
247
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $attrListItems Product list items referencing variant attributes
248
	 * @return \Aimeos\MShop\Media\Item\Iface Modified media item
249
	 */
250
	protected function addMediaAttributes( \Aimeos\MShop\Media\Item\Iface $mediaItem, array $attrListItems ) : \Aimeos\MShop\Media\Item\Iface
251
	{
252
		$listManager = \Aimeos\MShop::create( $this->getContext(), 'media/lists' );
253
		$listItems = $mediaItem->getListItems( 'attribute', 'variant', null, false );
254
255
		foreach( $attrListItems as $listItem )
256
		{
257
			if( ( $litem = $mediaItem->getListItem( 'attribute', 'variant', $listItem->getRefId(), false ) ) !== null )
258
			{
259
				unset( $listItems[$litem->getId()] );
260
				continue;
261
			}
262
263
			$litem = $listManager->create()->setType( 'variant' )->setRefId( $listItem->getRefId() );
264
			$mediaItem->addListItem( 'attribute', $litem );
265
		}
266
267
		return $mediaItem->deleteListItems( $listItems->toArray() );
268
	}
269
270
271
	/**
272
	 * Removes the media reference and the media item if not shared
273
	 *
274
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item including media reference
275
	 * @param \Aimeos\Map $listItems Media list items to be removed
276
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
277
	 */
278
	protected function deleteMediaItems( \Aimeos\MShop\Product\Item\Iface $item, \Aimeos\Map $listItems ) : \Aimeos\MShop\Product\Item\Iface
279
	{
280
		$context = $this->getContext();
281
		$cntl = \Aimeos\Controller\Common\Media\Factory::create( $context );
282
		$manager = \Aimeos\MShop::create( $context, 'product' );
283
		$search = $manager->filter();
284
285
		foreach( $listItems as $listItem )
286
		{
287
			$func = $search->make( 'product:has', ['media', $listItem->getType(), $listItem->getRefId()] );
288
			$search->setConditions( $search->compare( '!=', $func, null ) );
289
			$items = $manager->search( $search );
290
			$refItem = null;
291
292
			if( count( $items ) === 1 && ( $refItem = $listItem->getRefItem() ) !== null ) {
293
				$cntl->delete( $refItem );
294
			}
295
296
			$item->deleteListItem( 'media', $listItem, $refItem );
297
		}
298
299
		return $item;
300
	}
301
302
303
	/**
304
	 * Returns the list of sub-client names configured for the client.
305
	 *
306
	 * @return array List of JQAdm client names
307
	 */
308
	protected function getSubClientNames() : array
309
	{
310
		/** admin/jqadm/product/media/subparts
311
		 * List of JQAdm sub-clients rendered within the product media section
312
		 *
313
		 * The output of the frontend is composed of the code generated by the JQAdm
314
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
315
		 * that are responsible for rendering certain sub-parts of the output. The
316
		 * sub-clients can contain JQAdm clients themselves and therefore a
317
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
318
		 * the output that is placed inside the container of its parent.
319
		 *
320
		 * At first, always the JQAdm code generated by the parent is printed, then
321
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
322
		 * determines the order of the output of these sub-clients inside the parent
323
		 * container. If the configured list of clients is
324
		 *
325
		 *  array( "subclient1", "subclient2" )
326
		 *
327
		 * you can easily change the order of the output by reordering the subparts:
328
		 *
329
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
330
		 *
331
		 * You can also remove one or more parts if they shouldn't be rendered:
332
		 *
333
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
334
		 *
335
		 * As the clients only generates structural JQAdm, the layout defined via CSS
336
		 * should support adding, removing or reordering content by a fluid like
337
		 * design.
338
		 *
339
		 * @param array List of sub-client names
340
		 * @since 2017.07
341
		 * @category Developer
342
		 */
343
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/media/subparts', [] );
344
	}
345
346
347
	/**
348
	 * Creates new and updates existing items using the data array
349
	 *
350
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
351
	 * @param array $data Data array
352
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
353
	 */
354
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
355
	{
356
		$context = $this->getContext();
357
358
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
359
		$listManager = \Aimeos\MShop::create( $context, 'product/lists' );
360
		$cntl = \Aimeos\Controller\Common\Media\Factory::create( $context );
361
362
		$listItems = $item->getListItems( 'media', null, null, false );
363
		$files = (array) $this->getView()->request()->getUploadedFiles();
364
365
		foreach( $data as $idx => $entry )
366
		{
367
			$listType = $entry['product.lists.type'] ?? 'default';
368
369
			if( ( $listItem = $item->getListItem( 'media', $listType, $entry['media.id'] ?? null, false ) ) === null ) {
370
				$listItem = $listManager->create();
371
			}
372
373
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
374
				$refItem = $mediaManager->create();
375
			}
376
377
			$refItem->fromArray( $entry, true )->setDomain( 'product' );
378
			$file = $this->getValue( $files, 'media/' . $idx . '/file' );
379
			$preview = $this->getValue( $files, 'media/' . $idx . '/preview' );
380
381
			if( $file && $file->getError() !== UPLOAD_ERR_NO_FILE )
382
			{
383
				$refItem = $cntl->add( $refItem, $file );
384
385
				if( $preview &&  $preview->getError() !== UPLOAD_ERR_NO_FILE ) {
386
					$refItem = $cntl->addPreview( $refItem, $preview );
387
				}
388
			}
389
			elseif( $refItem->getId() === null && $refItem->getUrl() !== '' )
390
			{
391
				$refItem = $cntl->copy( $refItem );
392
			}
393
394
			$conf = [];
395
396
			foreach( (array) $this->getValue( $entry, 'config', [] ) as $cfg )
397
			{
398
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' ) {
399
					$conf[$key] = trim( $cfg['val'] ?? '' );
400
				}
401
			}
402
403
			$listItem->fromArray( $entry, true );
404
			$listItem->setPosition( $idx );
405
			$listItem->setConfig( $conf );
406
407
			$attrListItems = $item->getListItems( 'attribute', 'variant', null, false )->toArray();
408
			$refItem = $this->addMediaAttributes( $refItem, $attrListItems );
409
			$item->addListItem( 'media', $listItem, $refItem );
410
411
			unset( $listItems[$listItem->getId()] );
412
		}
413
414
		return $this->deleteMediaItems( $item, $listItems );
415
	}
416
417
418
	/**
419
	 * Constructs the data array for the view from the given item
420
	 *
421
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
422
	 * @param bool $copy True if items should be copied, false if not
423
	 * @return string[] Multi-dimensional associative list of item data
424
	 */
425
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
426
	{
427
		$data = [];
428
		$siteId = $this->getContext()->getLocale()->getSiteId();
429
430
		foreach( $item->getListItems( 'media', null, null, false ) as $listItem )
431
		{
432
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
433
				continue;
434
			}
435
436
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
437
438
			if( $copy === true )
439
			{
440
				$list['product.lists.siteid'] = $siteId;
441
				$list['product.lists.id'] = '';
442
				$list['media.siteid'] = $siteId;
443
				$list['media.id'] = null;
444
			}
445
446
			$list['product.lists.datestart'] = str_replace( ' ', 'T', $list['product.lists.datestart'] );
447
			$list['product.lists.dateend'] = str_replace( ' ', 'T', $list['product.lists.dateend'] );
448
			$list['config'] = [];
449
450
			foreach( $listItem->getConfig() as $key => $value ) {
451
				$list['config'][] = ['key' => $key, 'val' => $value];
452
			}
453
454
			$data[] = $list;
455
		}
456
457
		return $data;
458
	}
459
460
461
	/**
462
	 * Returns the rendered template including the view data
463
	 *
464
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
465
	 * @return string HTML output
466
	 */
467
	protected function render( \Aimeos\MW\View\Iface $view ) : string
468
	{
469
		/** admin/jqadm/product/media/template-item
470
		 * Relative path to the HTML body template of the media subpart for products.
471
		 *
472
		 * The template file contains the HTML code and processing instructions
473
		 * to generate the result shown in the body of the frontend. The
474
		 * configuration string is the path to the template file relative
475
		 * to the templates directory (usually in admin/jqadm/templates).
476
		 *
477
		 * You can overwrite the template file configuration in extensions and
478
		 * provide alternative templates. These alternative templates should be
479
		 * named like the default one but with the string "default" replaced by
480
		 * an unique name. You may use the name of your project for this. If
481
		 * you've implemented an alternative client class as well, "default"
482
		 * should be replaced by the name of the new class.
483
		 *
484
		 * @param string Relative path to the template creating the HTML code
485
		 * @since 2017.07
486
		 * @category Developer
487
		 */
488
		$tplconf = 'admin/jqadm/product/media/template-item';
489
		$default = 'product/item-media-standard';
490
491
		return $view->render( $view->config( $tplconf, $default ) );
492
	}
493
}
494