Passed
Push — master ( 8739c4...264741 )
by Aimeos
20:06 queued 17:24
created

Standard::fromArray()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 27
c 0
b 0
f 0
nc 10
nop 2
dl 0
loc 47
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

342
			$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...
343
			$refItem = $listItem->getRefItem() ?: $mediaManager->create();
344
345
			$refItem->fromArray( $entry, true )->setDomain( 'cms' );
0 ignored issues
show
Bug introduced by
The method setDomain() does not exist on Aimeos\MShop\Common\Item\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Product\Item\Iface or Aimeos\MAdmin\Log\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Group\Item\Iface or Aimeos\MShop\Rule\Item\Iface or Aimeos\MShop\Locale\Item\Site\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Order\Item\Product\Iface or Aimeos\MAdmin\Cache\Item\Iface or Aimeos\MShop\Common\Item\ListsRef\Iface or Aimeos\MShop\Stock\Item\Iface or Aimeos\MShop\Order\Item\Product\Attribute\Iface or Aimeos\MShop\Order\Item\Service\Iface or Aimeos\MShop\Order\Item\Iface or Aimeos\MShop\Subscription\Item\Iface or Aimeos\MShop\Coupon\Item\Iface or Aimeos\MShop\Order\Item\Service\Attribute\Iface or Aimeos\MAdmin\Job\Item\Iface or Aimeos\MShop\Order\Item\Status\Iface or Aimeos\MShop\Locale\Item\Iface or Aimeos\MShop\Common\Item\PropertyRef\Iface or Aimeos\MShop\Order\Item\Basket\Iface or Aimeos\MShop\Order\Item\Service\Transaction\Iface or Aimeos\MShop\Locale\Item\Language\Iface or Aimeos\MShop\Cms\Item\Iface or Aimeos\MShop\Common\Item\Address\Iface or Aimeos\MShop\Common\Item\Property\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Common\Item\AddressRef\Iface or Aimeos\MShop\Plugin\Item\Iface or Aimeos\MShop\Coupon\Item\Code\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Order\Item\Coupon\Iface or Aimeos\MShop\Locale\Item\Currency\Iface or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Cms\Item\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Product\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Customer\Item\Address\Iface or Aimeos\MShop\Order\Item\Address\Iface or Aimeos\MShop\Supplier\Item\Address\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Supplier\Item\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

345
			$refItem->fromArray( $entry, true )->/** @scrutinizer ignore-call */ setDomain( 'cms' );
Loading history...
346
			$file = $this->val( $files, 'media/' . $idx . '/file' );
347
			$preview = $this->val( $files, 'media/' . $idx . '/preview' );
0 ignored issues
show
Unused Code introduced by
The assignment to $preview is dead and can be removed.
Loading history...
348
349
			if( $file && $file->getError() !== UPLOAD_ERR_NO_FILE ) {
350
				$refItem = $mediaManager->upload( $refItem, $file );
0 ignored issues
show
Bug introduced by
The method upload() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Common\Manager\Decorator\Iface or Aimeos\MShop\Order\Manag...rvice\Transaction\Iface or Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Service\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Coupon\Manager\Code\Iface or Aimeos\MShop\Product\Manager\Iface or Aimeos\MShop\Supplier\Manager\Iface or Aimeos\MShop\Price\Manager\Property\Type\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Cms\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Attribute\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Attribute\Manager\Property\Type\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Address\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Product\Attribute\Iface or Aimeos\MShop\Customer\Manager\Property\Type\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Customer\Manager\Iface or Aimeos\MShop\Rule\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Coupon\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Product\Manager\Property\Type\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Group\Manager\Iface or Aimeos\MShop\Catalog\Manager\Iface or Aimeos\MShop\Locale\Manager\Site\Iface or Aimeos\MShop\Order\Manager\Service\Attribute\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Stock\Manager\Type\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MShop\Common\Manager\Type\Iface or Aimeos\MAdmin\Job\Manager\Iface or Aimeos\MShop\Product\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Type\Iface or Aimeos\MShop\Cms\Manager\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Rule\Manager\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Plugin\Manager\Type\Iface or Aimeos\MShop\Stock\Manager\Iface or Aimeos\MShop\Attribute\Manager\Property\Iface or Aimeos\MShop\Subscription\Manager\Iface or Aimeos\MShop\Media\Manager\Property\Type\Iface or Aimeos\MShop\Product\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Language\Iface or Aimeos\MShop\Media\Manager\Property\Iface or Aimeos\MShop\Service\Manager\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MAdmin\Log\Manager\Iface or Aimeos\MShop\Cms\Manager\Lists\Iface or Aimeos\MShop\Locale\Manager\Iface or Aimeos\MAdmin\Cache\Manager\Iface or Aimeos\MShop\Order\Manager\Basket\Iface or Aimeos\MShop\Order\Manager\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Index\Manager\Iface or Aimeos\MShop\Index\Manager\Attribute\Iface or Aimeos\MShop\Index\Manager\Text\Iface or Aimeos\MShop\Index\Manager\Supplier\Iface or Aimeos\MShop\Index\Manager\Catalog\Iface or Aimeos\MShop\Index\Manager\Price\Iface or Aimeos\MShop\Supplier\Manager\Address\Iface or Aimeos\MShop\Customer\Manager\Address\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

350
				/** @scrutinizer ignore-call */ 
351
    $refItem = $mediaManager->upload( $refItem, $file );
Loading history...
351
			} elseif( $refItem->getId() === null && $refItem->getUrl() !== '' ) {
0 ignored issues
show
Bug introduced by
The method getUrl() does not exist on Aimeos\MShop\Common\Item\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MAdmin\Log\Item\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Group\Item\Iface or Aimeos\MShop\Rule\Item\Iface or Aimeos\MShop\Locale\Item\Site\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Order\Item\Product\Iface or Aimeos\MShop\Text\Item\Iface or Aimeos\MAdmin\Cache\Item\Iface or Aimeos\MShop\Common\Item\ListsRef\Iface or Aimeos\MShop\Stock\Item\Iface or Aimeos\MShop\Order\Item\Product\Attribute\Iface or Aimeos\MShop\Order\Item\Service\Iface or Aimeos\MShop\Order\Item\Iface or Aimeos\MShop\Subscription\Item\Iface or Aimeos\MShop\Coupon\Item\Iface or Aimeos\MShop\Order\Item\Service\Attribute\Iface or Aimeos\MShop\Review\Item\Iface or Aimeos\MAdmin\Job\Item\Iface or Aimeos\MShop\Order\Item\Status\Iface or Aimeos\MShop\Common\Item\Lists\Iface or Aimeos\MShop\Price\Item\Iface or Aimeos\MShop\Locale\Item\Iface or Aimeos\MShop\Common\Item\PropertyRef\Iface or Aimeos\MShop\Tag\Item\Iface or Aimeos\MShop\Order\Item\Basket\Iface or Aimeos\MShop\Common\Item\Type\Iface or Aimeos\MShop\Attribute\Item\Iface or Aimeos\MShop\Order\Item\Service\Transaction\Iface or Aimeos\MShop\Locale\Item\Language\Iface or Aimeos\MShop\Common\Item\Address\Iface or Aimeos\MShop\Common\Item\Property\Iface or Aimeos\MShop\Common\Item\AddressRef\Iface or Aimeos\MShop\Plugin\Item\Iface or Aimeos\MShop\Coupon\Item\Code\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Order\Item\Coupon\Iface or Aimeos\MShop\Locale\Item\Currency\Iface or Aimeos\MShop\Service\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Text\Item\Iface or Aimeos\MShop\Price\Item\Iface or Aimeos\MShop\Attribute\Item\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Price\Item\Iface or Aimeos\MShop\Attribute\Item\Iface or Aimeos\MShop\Customer\Item\Address\Iface or Aimeos\MShop\Order\Item\Address\Iface or Aimeos\MShop\Supplier\Item\Address\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Supplier\Item\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

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