Passed
Push — master ( 85cd01...c58492 )
by Aimeos
03:18
created

Standard::data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
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 data( \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()->data( $this->view() );
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()->data( $this->view() );
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->view()->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()->data( $this->view() );
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->view();
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' );
0 ignored issues
show
Unused Code introduced by
The assignment to $listManager is dead and can be removed.
Loading history...
253
		$listItems = $mediaItem->getListItems( 'attribute', 'variant', null, false );
254
255
		foreach( $attrListItems as $listItem )
256
		{
257
			if( ( $litem = $mediaItem->getListItem( 'attribute', $listItem->getType(), $listItem->getRefId(), false ) ) === null ) {
0 ignored issues
show
Bug introduced by
It seems like $listItem->getType() can also be of type null; however, parameter $listtype of Aimeos\MShop\Common\Item...ef\Iface::getListItem() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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