Passed
Push — master ( 8c5804...4577b9 )
by Aimeos
03:39
created

Standard::save()   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-2023
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 )->toArray() );
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 )->slice( 0, 10000 );
224
		$search->add( 'media.type.domain', '==', 'catalog' )->order( 'media.type.code' );
225
226
		$listSearch = $listTypeManager->filter( true )->slice( 0, 10000 );
227
		$listSearch->add( 'catalog.lists.type.domain', '==', 'media' )->order( 'catalog.lists.type.code' );
228
229
		$view->mediaListTypes = $listTypeManager->search( $listSearch );
230
		$view->mediaTypes = $typeManager->search( $search );
231
232
		return $view;
233
	}
234
235
236
	/**
237
	 * Removes the media reference and the media item if not shared
238
	 *
239
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item including media reference
240
	 * @param array $listItems Media list items to be removed
241
	 * @return \Aimeos\MShop\Catalog\Item\Iface Modified catalog item
242
	 */
243
	protected function deleteMediaItems( \Aimeos\MShop\Catalog\Item\Iface $item, array $listItems )
244
	{
245
		$context = $this->context();
246
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
247
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
248
		$search = $manager->filter();
249
250
		foreach( $listItems as $listItem )
251
		{
252
			$func = $search->make( 'catalog:has', ['media', $listItem->getType(), $listItem->getRefId()] );
253
			$search->setConditions( $search->compare( '!=', $func, null ) );
254
			$items = $manager->search( $search );
255
			$refItem = null;
256
257
			if( count( $items ) === 1 && ( $refItem = $listItem->getRefItem() ) !== null ) {
258
				$mediaManager->delete( $refItem );
259
			}
260
261
			$item->deleteListItem( 'media', $listItem, $refItem );
262
		}
263
264
		return $item;
265
	}
266
267
268
	/**
269
	 * Returns the list of sub-client names configured for the client.
270
	 *
271
	 * @return array List of JQAdm client names
272
	 */
273
	protected function getSubClientNames() : array
274
	{
275
		/** admin/jqadm/catalog/media/subparts
276
		 * List of JQAdm sub-clients rendered within the catalog media section
277
		 *
278
		 * The output of the frontend is composed of the code generated by the JQAdm
279
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
280
		 * that are responsible for rendering certain sub-parts of the output. The
281
		 * sub-clients can contain JQAdm clients themselves and therefore a
282
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
283
		 * the output that is placed inside the container of its parent.
284
		 *
285
		 * At first, always the JQAdm code generated by the parent is printed, then
286
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
287
		 * determines the order of the output of these sub-clients inside the parent
288
		 * container. If the configured list of clients is
289
		 *
290
		 *  array( "subclient1", "subclient2" )
291
		 *
292
		 * you can easily change the order of the output by reordering the subparts:
293
		 *
294
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
295
		 *
296
		 * You can also remove one or more parts if they shouldn't be rendered:
297
		 *
298
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
299
		 *
300
		 * As the clients only generates structural JQAdm, the layout defined via CSS
301
		 * should support adding, removing or reordering content by a fluid like
302
		 * design.
303
		 *
304
		 * @param array List of sub-client names
305
		 * @since 2017.07
306
		 */
307
		return $this->context()->config()->get( 'admin/jqadm/catalog/media/subparts', [] );
308
	}
309
310
311
	/**
312
	 * Creates new and updates existing items using the data array
313
	 *
314
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object without referenced domain items
315
	 * @param array $data Data array
316
	 * @return \Aimeos\MShop\Catalog\Item\Iface Modified catalog item
317
	 */
318
	protected function fromArray( \Aimeos\MShop\Catalog\Item\Iface $item, array $data ) : \Aimeos\MShop\Catalog\Item\Iface
319
	{
320
		$context = $this->context();
321
322
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
323
		$listManager = \Aimeos\MShop::create( $context, 'catalog/lists' );
324
325
		$listItems = $item->getListItems( 'media', null, null, false );
326
		$files = (array) $this->view()->request()->getUploadedFiles();
327
328
		foreach( $data as $idx => $entry )
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 ) ?: $listManager->create();
334
			$refItem = $listItem->getRefItem() ?: $mediaManager->create();
335
336
			$refItem->fromArray( $entry, true )->setDomain( 'catalog' );
337
			$file = $this->val( $files, 'media/' . $idx . '/file' );
338
			$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...
339
340
			if( $refItem->getId() === null && $refItem->getUrl() !== '' ) {
341
				$refItem = $mediaManager->copy( $refItem );
342
			}
343
344
			if( $file && $file->getError() !== UPLOAD_ERR_NO_FILE ) {
345
				$refItem = $mediaManager->upload( $refItem, $file );
346
			}
347
348
			$listItem->fromArray( $entry, true )->setPosition( $idx )->setConfig( [] );
349
350
			foreach( (array) $this->val( $entry, 'config', [] ) as $cfg )
351
			{
352
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) {
353
					$listItem->setConfigValue( $key, json_decode( $val, true ) ?? $val );
354
				}
355
			}
356
357
			$item->addListItem( 'media', $listItem, $refItem );
358
			unset( $listItems[$listItem->getId()] );
359
		}
360
361
		return $this->deleteMediaItems( $item, $listItems->toArray() );
362
	}
363
364
365
	/**
366
	 * Constructs the data array for the view from the given item
367
	 *
368
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object including referenced domain items
369
	 * @param bool $copy True if items should be copied, false if not
370
	 * @return string[] Multi-dimensional associative list of item data
371
	 */
372
	protected function toArray( \Aimeos\MShop\Catalog\Item\Iface $item, bool $copy = false ) : array
373
	{
374
		$data = [];
375
		$siteId = $this->context()->locale()->getSiteId();
376
377
		foreach( $item->getListItems( 'media', null, null, false ) as $listItem )
378
		{
379
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
380
				continue;
381
			}
382
383
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
384
385
			if( $copy === true )
386
			{
387
				$list['catalog.lists.siteid'] = $siteId;
388
				$list['catalog.lists.id'] = '';
389
				$list['media.siteid'] = $siteId;
390
				$list['media.id'] = null;
391
			}
392
393
			$list['media.previews'] = $this->view()->imageset( $refItem->getPreviews(), $refItem->getFileSystem() );
394
			$list['media.preview'] = $this->view()->content( $refItem->getPreview(), $refItem->getFileSystem() );
395
396
			$list['catalog.lists.datestart'] = str_replace( ' ', 'T', $list['catalog.lists.datestart'] ?? '' );
397
			$list['catalog.lists.dateend'] = str_replace( ' ', 'T', $list['catalog.lists.dateend'] ?? '' );
398
			$list['config'] = [];
399
400
			foreach( $listItem->getConfig() as $key => $value ) {
401
				$list['config'][] = ['key' => $key, 'val' => $value];
402
			}
403
404
			$data[] = $list;
405
		}
406
407
		return $data;
408
	}
409
410
411
	/**
412
	 * Returns the rendered template including the view data
413
	 *
414
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
415
	 * @return string HTML output
416
	 */
417
	protected function render( \Aimeos\Base\View\Iface $view ) : string
418
	{
419
		/** admin/jqadm/catalog/media/template-item
420
		 * Relative path to the HTML body template of the media subpart for catalogs.
421
		 *
422
		 * The template file contains the HTML code and processing instructions
423
		 * to generate the result shown in the body of the frontend. The
424
		 * configuration string is the path to the template file relative
425
		 * to the templates directory (usually in templates/admin/jqadm).
426
		 *
427
		 * You can overwrite the template file configuration in extensions and
428
		 * provide alternative templates. These alternative templates should be
429
		 * named like the default one but with the string "default" replaced by
430
		 * an unique name. You may use the name of your project for this. If
431
		 * you've implemented an alternative client class as well, "default"
432
		 * should be replaced by the name of the new class.
433
		 *
434
		 * @param string Relative path to the template creating the HTML code
435
		 * @since 2017.07
436
		 */
437
		$tplconf = 'admin/jqadm/catalog/media/template-item';
438
		$default = 'catalog/item-media';
439
440
		return $view->render( $view->config( $tplconf, $default ) );
441
	}
442
}
443