Standard   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 446
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 111
dl 0
loc 446
rs 9.92
c 0
b 0
f 0
wmc 31

13 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 36 5
A render() 0 24 1
A save() 0 8 1
B fromArray() 0 48 9
A getSubClientNames() 0 35 1
A deleteMediaItems() 0 22 4
A create() 0 20 2
A delete() 0 8 1
A data() 0 14 1
A get() 0 7 1
A getSubClient() 0 73 1
A copy() 0 7 1
A addMediaAttributes() 0 15 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
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 )->order( 'media.type.code' )->slice( 0, 10000 );
51
		$listSearch = $listTypeManager->filter( true )->order( 'product.lists.type.code' )->slice( 0, 10000 );
52
53
		$view->mediaListTypes = $listTypeManager->search( $listSearch );
54
		$view->mediaTypes = $typeManager->search( $search );
55
56
		return $view;
57
	}
58
59
60
	/**
61
	 * Copies a resource
62
	 *
63
	 * @return string|null HTML output
64
	 */
65
	public function copy() : ?string
66
	{
67
		$view = $this->object()->data( $this->view() );
68
		$view->mediaData = $this->toArray( $view->item, true );
69
		$view->mediaBody = parent::copy();
70
71
		return $this->render( $view );
72
	}
73
74
75
	/**
76
	 * Creates a new resource
77
	 *
78
	 * @return string|null HTML output
79
	 */
80
	public function create() : ?string
81
	{
82
		$view = $this->object()->data( $this->view() );
83
		$siteid = $this->context()->locale()->getSiteId();
84
85
		$itemData = $this->toArray( $view->item );
86
		$data = array_replace_recursive( $itemData, $view->param( 'media', [] ) );
87
88
		foreach( $data as $idx => $entry )
89
		{
90
			$data[$idx]['media.siteid'] = $siteid;
91
			$data[$idx]['media.url'] = $entry['media.url'] ?? null;
92
			$data[$idx]['media.preview'] = $entry['media.preview'] ?? null;
93
			$data[$idx]['product.lists.siteid'] = $siteid;
94
		}
95
96
		$view->mediaData = $data;
97
		$view->mediaBody = parent::create();
98
99
		return $this->render( $view );
100
	}
101
102
103
	/**
104
	 * Deletes a resource
105
	 *
106
	 * @return string|null HTML output
107
	 */
108
	public function delete() : ?string
109
	{
110
		parent::delete();
111
112
		$item = $this->view()->item;
113
		$this->deleteMediaItems( $item, $item->getListItems( 'media', null, null, false ) );
114
115
		return null;
116
	}
117
118
119
	/**
120
	 * Returns a single resource
121
	 *
122
	 * @return string|null HTML output
123
	 */
124
	public function get() : ?string
125
	{
126
		$view = $this->object()->data( $this->view() );
127
		$view->mediaData = $this->toArray( $view->item );
128
		$view->mediaBody = parent::get();
129
130
		return $this->render( $view );
131
	}
132
133
134
	/**
135
	 * Saves the data
136
	 *
137
	 * @return string|null HTML output
138
	 */
139
	public function save() : ?string
140
	{
141
		$view = $this->view();
142
143
		$view->item = $this->fromArray( $view->item, $view->param( 'media', [] ) );
144
		$view->mediaBody = parent::save();
145
146
		return null;
147
	}
148
149
150
	/**
151
	 * Returns the sub-client given by its name.
152
	 *
153
	 * @param string $type Name of the client type
154
	 * @param string|null $name Name of the sub-client (Default if null)
155
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
156
	 */
157
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
158
	{
159
		/** admin/jqadm/product/media/decorators/excludes
160
		 * Excludes decorators added by the "common" option from 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 remove a decorator added via
168
		 * "admin/jqadm/common/decorators/default" before they are wrapped
169
		 * around the JQAdm client.
170
		 *
171
		 *  admin/jqadm/product/media/decorators/excludes = array( 'decorator1' )
172
		 *
173
		 * This would remove the decorator named "decorator1" from the list of
174
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
175
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
176
		 *
177
		 * @param array List of decorator names
178
		 * @since 2017.07
179
		 * @see admin/jqadm/common/decorators/default
180
		 * @see admin/jqadm/product/media/decorators/global
181
		 * @see admin/jqadm/product/media/decorators/local
182
		 */
183
184
		/** admin/jqadm/product/media/decorators/global
185
		 * Adds a list of globally available decorators only to the product JQAdm client
186
		 *
187
		 * Decorators extend the functionality of a class by adding new aspects
188
		 * (e.g. log what is currently done), executing the methods of the underlying
189
		 * class only in certain conditions (e.g. only for logged in users) or
190
		 * modify what is returned to the caller.
191
		 *
192
		 * This option allows you to wrap global decorators
193
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
194
		 *
195
		 *  admin/jqadm/product/media/decorators/global = array( 'decorator1' )
196
		 *
197
		 * This would add the decorator named "decorator1" defined by
198
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
199
		 *
200
		 * @param array List of decorator names
201
		 * @since 2017.07
202
		 * @see admin/jqadm/common/decorators/default
203
		 * @see admin/jqadm/product/media/decorators/excludes
204
		 * @see admin/jqadm/product/media/decorators/local
205
		 */
206
207
		/** admin/jqadm/product/media/decorators/local
208
		 * Adds a list of local decorators only to the product JQAdm client
209
		 *
210
		 * Decorators extend the functionality of a class by adding new aspects
211
		 * (e.g. log what is currently done), executing the methods of the underlying
212
		 * class only in certain conditions (e.g. only for logged in users) or
213
		 * modify what is returned to the caller.
214
		 *
215
		 * This option allows you to wrap local decorators
216
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
217
		 *
218
		 *  admin/jqadm/product/media/decorators/local = array( 'decorator2' )
219
		 *
220
		 * This would add the decorator named "decorator2" defined by
221
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
222
		 *
223
		 * @param array List of decorator names
224
		 * @since 2017.07
225
		 * @see admin/jqadm/common/decorators/default
226
		 * @see admin/jqadm/product/media/decorators/excludes
227
		 * @see admin/jqadm/product/media/decorators/global
228
		 */
229
		return $this->createSubClient( 'product/media/' . $type, $name );
230
	}
231
232
233
	/**
234
	 * Adds the product variant attributes to the media item
235
	 * Then, the images will only be shown if the customer selected the product variant
236
	 *
237
	 * @param \Aimeos\MShop\Media\Item\Iface $mediaItem Media item, maybe with referenced attribute items
238
	 * @param iterable $attrListItems Product list items referencing variant attributes
239
	 * @return \Aimeos\MShop\Media\Item\Iface Modified media item
240
	 */
241
	protected function addMediaAttributes( \Aimeos\MShop\Media\Item\Iface $mediaItem, iterable $attrListItems ) : \Aimeos\MShop\Media\Item\Iface
242
	{
243
		$manager = \Aimeos\MShop::create( $this->context(), 'media' );
244
		$listItems = $mediaItem->getListItems( 'attribute', 'variant', null, false );
245
246
		foreach( $attrListItems as $listItem )
247
		{
248
			if( ( $litem = $mediaItem->getListItem( 'attribute', 'variant', $listItem->getRefId(), false ) ) === null ) {
249
				$mediaItem->addListItem( 'attribute', $manager->createListItem()->setType( 'variant' )->setRefId( $listItem->getRefId() ) );
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

249
				$mediaItem->addListItem( 'attribute', $manager->/** @scrutinizer ignore-call */ createListItem()->setType( 'variant' )->setRefId( $listItem->getRefId() ) );

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...
250
			} else {
251
				unset( $listItems[$litem->getId()] );
252
			}
253
		}
254
255
		return $mediaItem->deleteListItems( $listItems );
256
	}
257
258
259
	/**
260
	 * Removes the media reference and the media item if not shared
261
	 *
262
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item including media reference
263
	 * @param iterable $listItems Media list items to be removed
264
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
265
	 */
266
	protected function deleteMediaItems( \Aimeos\MShop\Product\Item\Iface $item, iterable $listItems ) : \Aimeos\MShop\Product\Item\Iface
267
	{
268
		$context = $this->context();
269
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
270
		$manager = \Aimeos\MShop::create( $context, 'product' );
271
		$search = $manager->filter();
272
273
		foreach( $listItems as $listItem )
274
		{
275
			$func = $search->make( 'product:has', ['media', $listItem->getType(), $listItem->getRefId()] );
276
			$search->setConditions( $search->compare( '!=', $func, null ) );
277
			$items = $manager->search( $search );
278
			$refItem = null;
279
280
			if( count( $items ) === 1 && ( $refItem = $listItem->getRefItem() ) !== null ) {
281
				$mediaManager->delete( $refItem );
282
			}
283
284
			$item->deleteListItem( 'media', $listItem, $refItem );
285
		}
286
287
		return $item;
288
	}
289
290
291
	/**
292
	 * Returns the list of sub-client names configured for the client.
293
	 *
294
	 * @return array List of JQAdm client names
295
	 */
296
	protected function getSubClientNames() : array
297
	{
298
		/** admin/jqadm/product/media/subparts
299
		 * List of JQAdm sub-clients rendered within the product media section
300
		 *
301
		 * The output of the frontend is composed of the code generated by the JQAdm
302
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
303
		 * that are responsible for rendering certain sub-parts of the output. The
304
		 * sub-clients can contain JQAdm clients themselves and therefore a
305
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
306
		 * the output that is placed inside the container of its parent.
307
		 *
308
		 * At first, always the JQAdm code generated by the parent is printed, then
309
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
310
		 * determines the order of the output of these sub-clients inside the parent
311
		 * container. If the configured list of clients is
312
		 *
313
		 *  array( "subclient1", "subclient2" )
314
		 *
315
		 * you can easily change the order of the output by reordering the subparts:
316
		 *
317
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
318
		 *
319
		 * You can also remove one or more parts if they shouldn't be rendered:
320
		 *
321
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
322
		 *
323
		 * As the clients only generates structural JQAdm, the layout defined via CSS
324
		 * should support adding, removing or reordering content by a fluid like
325
		 * design.
326
		 *
327
		 * @param array List of sub-client names
328
		 * @since 2017.07
329
		 */
330
		return $this->context()->config()->get( 'admin/jqadm/product/media/subparts', [] );
331
	}
332
333
334
	/**
335
	 * Creates new and updates existing items using the data array
336
	 *
337
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
338
	 * @param array $data Data array
339
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
340
	 */
341
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
342
	{
343
		$context = $this->context();
344
345
		$manager = \Aimeos\MShop::create( $context, 'product' );
346
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
347
348
		$listItems = $item->getListItems( 'media', null, null, false );
349
		$files = (array) $this->view()->request()->getUploadedFiles();
350
351
		foreach( $data as $idx => $entry )
352
		{
353
			// disallow overwriting for security reasons
354
			unset( $entry['media.url'], $entry['media.preview'], $entry['media.previews'] );
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();
360
			$refItem = $listItem->getRefItem() ?: $mediaManager->create();
361
362
			$refItem->fromArray( $entry, true )->setDomain( 'product' );
363
364
			$preview = $this->val( $files, 'media/' . $idx . '/preview' );
365
			$file = $this->val( $files, 'media/' . $idx . '/file' );
366
367
			if( $refItem->getId() === null && $refItem->getUrl() !== '' ) {
368
				$refItem = $mediaManager->copy( $refItem );
369
			}
370
371
			$refItem = $mediaManager->upload( $refItem, $file, $preview );
372
			$listItem->fromArray( $entry, true )->setPosition( $idx )->setConfig( [] );
373
374
			foreach( (array) $this->val( $entry, 'config', [] ) as $cfg )
375
			{
376
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) {
377
					$listItem->setConfigValue( $key, json_decode( $val, true ) ?? $val );
378
				}
379
			}
380
381
			$attrListItems = $item->getListItems( 'attribute', 'variant', null, false );
382
			$refItem = $this->addMediaAttributes( $refItem, $attrListItems );
383
			$item->addListItem( 'media', $listItem, $refItem );
384
385
			unset( $listItems[$listItem->getId()] );
386
		}
387
388
		return $this->deleteMediaItems( $item, $listItems );
389
	}
390
391
392
	/**
393
	 * Constructs the data array for the view from the given item
394
	 *
395
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
396
	 * @param bool $copy True if items should be copied, false if not
397
	 * @return string[] Multi-dimensional associative list of item data
398
	 */
399
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
400
	{
401
		$data = [];
402
		$siteId = $this->context()->locale()->getSiteId();
403
404
		foreach( $item->getListItems( 'media', null, null, false ) as $listItem )
405
		{
406
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
407
				continue;
408
			}
409
410
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
411
412
			if( $copy === true )
413
			{
414
				$list['product.lists.siteid'] = $siteId;
415
				$list['product.lists.id'] = '';
416
				$list['media.siteid'] = $siteId;
417
				$list['media.id'] = null;
418
			}
419
420
			$list['media.previews'] = $this->view()->imageset( $refItem->getPreviews(), $refItem->getFileSystem() );
421
			$list['media.preview'] = $this->view()->content( $refItem->getPreview(), $refItem->getFileSystem() );
422
423
			$list['product.lists.datestart'] = str_replace( ' ', 'T', $list['product.lists.datestart'] ?? '' );
424
			$list['product.lists.dateend'] = str_replace( ' ', 'T', $list['product.lists.dateend'] ?? '' );
425
			$list['config'] = [];
426
427
			foreach( $listItem->getConfig() as $key => $value ) {
428
				$list['config'][] = ['key' => $key, 'val' => $value];
429
			}
430
431
			$data[] = $list;
432
		}
433
434
		return $data;
435
	}
436
437
438
	/**
439
	 * Returns the rendered template including the view data
440
	 *
441
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
442
	 * @return string HTML output
443
	 */
444
	protected function render( \Aimeos\Base\View\Iface $view ) : string
445
	{
446
		/** admin/jqadm/product/media/template-item
447
		 * Relative path to the HTML body template of the media subpart for products.
448
		 *
449
		 * The template file contains the HTML code and processing instructions
450
		 * to generate the result shown in the body of the frontend. The
451
		 * configuration string is the path to the template file relative
452
		 * to the templates directory (usually in templates/admin/jqadm).
453
		 *
454
		 * You can overwrite the template file configuration in extensions and
455
		 * provide alternative templates. These alternative templates should be
456
		 * named like the default one but with the string "default" replaced by
457
		 * an unique name. You may use the name of your project for this. If
458
		 * you've implemented an alternative client class as well, "default"
459
		 * should be replaced by the name of the new class.
460
		 *
461
		 * @param string Relative path to the template creating the HTML code
462
		 * @since 2017.07
463
		 */
464
		$tplconf = 'admin/jqadm/product/media/template-item';
465
		$default = 'product/item-media';
466
467
		return $view->render( $view->config( $tplconf, $default ) );
468
	}
469
}
470