Standard::get()   A
last analyzed

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

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