Standard::fromArray()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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

333
			$listItem = $item->getListItem( 'media', $type, $id, false ) ?: $manager->/** @scrutinizer ignore-call */ createListItem();

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