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

Standard::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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\Attribute\Media;
12
13
sprintf( 'media' ); // for translation
14
15
16
/**
17
 * Default implementation of attribute 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/attribute/media/name
27
	 * Name of the media subpart used by the JQAdm attribute implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Attribute\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 attribute 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, 'attribute/lists/type' );
50
51
		$search = $typeManager->filter( true )->slice( 0, 10000 );
52
		$search->setConditions( $search->compare( '==', 'media.type.domain', 'attribute' ) );
53
		$search->setSortations( [$search->sort( '+', 'media.type.position' )] );
54
55
		$listSearch = $listTypeManager->filter( true )->slice( 0, 10000 );
56
		$listSearch->setConditions( $listSearch->compare( '==', 'attribute.lists.type.domain', 'media' ) );
57
		$listSearch->setSortations( [$listSearch->sort( '+', 'attribute.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
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->getObject()->data( $this->view() );
90
		$siteid = $this->getContext()->getLocale()->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]['attribute.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->getObject()->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/attribute/media/decorators/excludes
168
		 * Excludes decorators added by the "common" option from the attribute 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/attribute/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 2017.07
187
		 * @category Developer
188
		 * @see admin/jqadm/common/decorators/default
189
		 * @see admin/jqadm/attribute/media/decorators/global
190
		 * @see admin/jqadm/attribute/media/decorators/local
191
		 */
192
193
		/** admin/jqadm/attribute/media/decorators/global
194
		 * Adds a list of globally available decorators only to the attribute 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/attribute/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 2017.07
211
		 * @category Developer
212
		 * @see admin/jqadm/common/decorators/default
213
		 * @see admin/jqadm/attribute/media/decorators/excludes
214
		 * @see admin/jqadm/attribute/media/decorators/local
215
		 */
216
217
		/** admin/jqadm/attribute/media/decorators/local
218
		 * Adds a list of local decorators only to the attribute 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\Attribute\Decorator\*") around the JQAdm client.
227
		 *
228
		 *  admin/jqadm/attribute/media/decorators/local = array( 'decorator2' )
229
		 *
230
		 * This would add the decorator named "decorator2" defined by
231
		 * "\Aimeos\Admin\JQAdm\Attribute\Decorator\Decorator2" only to the JQAdm client.
232
		 *
233
		 * @param array List of decorator names
234
		 * @since 2017.07
235
		 * @category Developer
236
		 * @see admin/jqadm/common/decorators/default
237
		 * @see admin/jqadm/attribute/media/decorators/excludes
238
		 * @see admin/jqadm/attribute/media/decorators/global
239
		 */
240
		return $this->createSubClient( 'attribute/media/' . $type, $name );
241
	}
242
243
244
	/**
245
	 * Removes the media reference and the media item if not shared
246
	 *
247
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item including media reference
248
	 * @param array $listItems Media list items to be removed
249
	 * @return \Aimeos\MShop\Attribute\Item\Iface Modified attribute item
250
	 */
251
	protected function deleteMediaItems( \Aimeos\MShop\Attribute\Item\Iface $item, array $listItems ) : \Aimeos\MShop\Attribute\Item\Iface
252
	{
253
		$context = $this->getContext();
254
		$cntl = \Aimeos\Controller\Common\Media\Factory::create( $context );
255
		$manager = \Aimeos\MShop::create( $context, 'attribute' );
256
		$search = $manager->filter();
257
258
		foreach( $listItems as $listItem )
259
		{
260
			$func = $search->make( 'attribute: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
				$cntl->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/attribute/media/subparts
284
		 * List of JQAdm sub-clients rendered within the attribute 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 2017.07
314
		 * @category Developer
315
		 */
316
		return $this->getContext()->getConfig()->get( 'admin/jqadm/attribute/media/subparts', [] );
317
	}
318
319
320
	/**
321
	 * Creates new and updates existing items using the data array
322
	 *
323
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object without referenced domain items
324
	 * @param array $data Data array
325
	 * @return \Aimeos\MShop\Attribute\Item\Iface Modified attribute item
326
	 */
327
	protected function fromArray( \Aimeos\MShop\Attribute\Item\Iface $item, array $data ) : \Aimeos\MShop\Attribute\Item\Iface
328
	{
329
		$context = $this->getContext();
330
331
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
332
		$listManager = \Aimeos\MShop::create( $context, 'attribute/lists' );
333
		$cntl = \Aimeos\Controller\Common\Media\Factory::create( $context );
334
335
		$listItems = $item->getListItems( 'media', null, null, false );
336
		$files = (array) $this->view()->request()->getUploadedFiles();
337
338
		foreach( $data as $idx => $entry )
339
		{
340
			$listType = $entry['attribute.lists.type'] ?? 'default';
341
342
			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

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