Completed
Push — master ( 626216...e78c82 )
by Aimeos
03:45
created

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