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

340
			if( ( $listItem = $item->getListItem( 'media', $listType, /** @scrutinizer ignore-type */ $entry['media.id'] ?? null, false ) ) === null ) {
Loading history...
341
				$listItem = $listManager->create();
342
			}
343
344
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
345
				$refItem = $mediaManager->create();
346
			}
347
348
			$refItem->fromArray( $entry, true )->setDomain( 'catalog' );
349
			$file = $this->getValue( $files, 'media/' . $idx . '/file' );
350
			$preview = $this->getValue( $files, 'media/' . $idx . '/preview' );
351
352
			if( $refItem->getId() === null && $refItem->getUrl() !== '' ) {
353
				$refItem = $cntl->copy( $refItem );
354
			}
355
356
			if( $file && $file->getError() !== UPLOAD_ERR_NO_FILE )
357
			{
358
				$refItem = $cntl->add( $refItem, $file );
359
360
				if( $preview && $preview->getError() !== UPLOAD_ERR_NO_FILE ) {
361
					$refItem = $cntl->addPreview( $refItem, $preview );
362
				}
363
			}
364
365
			$listItem->fromArray( $entry, true )->setPosition( $idx )->setConfig( [] );
366
367
			foreach( (array) $this->getValue( $entry, 'config', [] ) as $cfg )
368
			{
369
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' ) {
370
					$listItem->setConfigValue( $key, trim( $cfg['val'] ?? '' ) );
371
				}
372
			}
373
374
			$item->addListItem( 'media', $listItem, $refItem );
375
			unset( $listItems[$listItem->getId()] );
376
		}
377
378
		return $this->deleteMediaItems( $item, $listItems->toArray() );
379
	}
380
381
382
	/**
383
	 * Constructs the data array for the view from the given item
384
	 *
385
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object including referenced domain items
386
	 * @param bool $copy True if items should be copied, false if not
387
	 * @return string[] Multi-dimensional associative list of item data
388
	 */
389
	protected function toArray( \Aimeos\MShop\Catalog\Item\Iface $item, bool $copy = false ) : array
390
	{
391
		$data = [];
392
		$siteId = $this->getContext()->getLocale()->getSiteId();
393
394
		foreach( $item->getListItems( 'media', null, null, false ) as $listItem )
395
		{
396
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
397
				continue;
398
			}
399
400
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
401
402
			if( $copy === true )
403
			{
404
				$list['catalog.lists.siteid'] = $siteId;
405
				$list['catalog.lists.id'] = '';
406
				$list['media.siteid'] = $siteId;
407
				$list['media.id'] = null;
408
			}
409
410
			$list['catalog.lists.datestart'] = str_replace( ' ', 'T', $list['catalog.lists.datestart'] );
411
			$list['catalog.lists.dateend'] = str_replace( ' ', 'T', $list['catalog.lists.dateend'] );
412
			$list['config'] = [];
413
414
			foreach( $listItem->getConfig() as $key => $value ) {
415
				$list['config'][] = ['key' => $key, 'val' => $value];
416
			}
417
418
			$data[] = $list;
419
		}
420
421
		return $data;
422
	}
423
424
425
	/**
426
	 * Returns the rendered template including the view data
427
	 *
428
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
429
	 * @return string HTML output
430
	 */
431
	protected function render( \Aimeos\MW\View\Iface $view ) : string
432
	{
433
		/** admin/jqadm/catalog/media/template-item
434
		 * Relative path to the HTML body template of the media subpart for catalogs.
435
		 *
436
		 * The template file contains the HTML code and processing instructions
437
		 * to generate the result shown in the body of the frontend. The
438
		 * configuration string is the path to the template file relative
439
		 * to the templates directory (usually in admin/jqadm/templates).
440
		 *
441
		 * You can overwrite the template file configuration in extensions and
442
		 * provide alternative templates. These alternative templates should be
443
		 * named like the default one but with the string "default" replaced by
444
		 * an unique name. You may use the name of your project for this. If
445
		 * you've implemented an alternative client class as well, "default"
446
		 * should be replaced by the name of the new class.
447
		 *
448
		 * @param string Relative path to the template creating the HTML code
449
		 * @since 2017.07
450
		 * @category Developer
451
		 */
452
		$tplconf = 'admin/jqadm/catalog/media/template-item';
453
		$default = 'catalog/item-media-standard';
454
455
		return $view->render( $view->config( $tplconf, $default ) );
456
	}
457
}
458