Completed
Push — master ( 626216...e78c82 )
by Aimeos
03:45
created

Standard::getSubClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 76
rs 10
c 0
b 0
f 0

How to fix   Long Method   

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), 2017-2020
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
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Copies a resource
40
	 *
41
	 * @return string|null HTML output
42
	 */
43
	public function copy() : ?string
44
	{
45
		$view = $this->getObject()->addData( $this->getView() );
46
		$view->mediaData = $this->toArray( $view->item, true );
47
		$view->mediaBody = '';
48
49
		foreach( $this->getSubClients() as $client ) {
50
			$view->mediaBody .= $client->copy();
51
		}
52
53
		return $this->render( $view );
54
	}
55
56
57
	/**
58
	 * Creates a new resource
59
	 *
60
	 * @return string|null HTML output
61
	 */
62
	public function create() : ?string
63
	{
64
		$view = $this->getObject()->addData( $this->getView() );
65
		$siteid = $this->getContext()->getLocale()->getSiteId();
66
67
		$itemData = $this->toArray( $view->item );
68
		$data = array_replace_recursive( $itemData, $view->param( 'media', [] ) );
69
70
		foreach( $data as $idx => $entry )
71
		{
72
			$data[$idx]['media.siteid'] = $siteid;
73
			$data[$idx]['media.url'] = $entry['media.url'] ?? null;
74
			$data[$idx]['media.preview'] = $entry['media.preview'] ?? null;
75
			$data[$idx]['catalog.lists.siteid'] = $siteid;
76
		}
77
78
		$view->mediaData = $data;
79
		$view->mediaBody = '';
80
81
		foreach( $this->getSubClients() as $client ) {
82
			$view->mediaBody .= $client->create();
83
		}
84
85
		return $this->render( $view );
86
	}
87
88
89
	/**
90
	 * Deletes a resource
91
	 *
92
	 * @return string|null HTML output
93
	 */
94
	public function delete() : ?string
95
	{
96
		parent::delete();
97
98
		$item = $this->getView()->item;
99
		$this->deleteMediaItems( $item, $item->getListItems( 'media', null, null, false )->toArray() );
100
101
		return null;
102
	}
103
104
105
	/**
106
	 * Returns a single resource
107
	 *
108
	 * @return string|null HTML output
109
	 */
110
	public function get() : ?string
111
	{
112
		$view = $this->getObject()->addData( $this->getView() );
113
		$view->mediaData = $this->toArray( $view->item );
114
		$view->mediaBody = '';
115
116
		foreach( $this->getSubClients() as $client ) {
117
			$view->mediaBody .= $client->get();
118
		}
119
120
		return $this->render( $view );
121
	}
122
123
124
	/**
125
	 * Saves the data
126
	 *
127
	 * @return string|null HTML output
128
	 */
129
	public function save() : ?string
130
	{
131
		$view = $this->getView();
132
133
		$view->item = $this->fromArray( $view->item, $view->param( 'media', [] ) );
134
		$view->mediaBody = '';
135
136
		foreach( $this->getSubClients() as $client ) {
137
			$view->mediaBody .= $client->save();
138
		}
139
140
		return null;
141
	}
142
143
144
	/**
145
	 * Returns the sub-client given by its name.
146
	 *
147
	 * @param string $type Name of the client type
148
	 * @param string|null $name Name of the sub-client (Default if null)
149
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
150
	 */
151
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
152
	{
153
		/** admin/jqadm/catalog/media/decorators/excludes
154
		 * Excludes decorators added by the "common" option from the catalog JQAdm client
155
		 *
156
		 * Decorators extend the functionality of a class by adding new aspects
157
		 * (e.g. log what is currently done), executing the methods of the underlying
158
		 * class only in certain conditions (e.g. only for logged in users) or
159
		 * modify what is returned to the caller.
160
		 *
161
		 * This option allows you to remove a decorator added via
162
		 * "admin/jqadm/common/decorators/default" before they are wrapped
163
		 * around the JQAdm client.
164
		 *
165
		 *  admin/jqadm/catalog/media/decorators/excludes = array( 'decorator1' )
166
		 *
167
		 * This would remove the decorator named "decorator1" from the list of
168
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
169
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
170
		 *
171
		 * @param array List of decorator names
172
		 * @since 2017.07
173
		 * @category Developer
174
		 * @see admin/jqadm/common/decorators/default
175
		 * @see admin/jqadm/catalog/media/decorators/global
176
		 * @see admin/jqadm/catalog/media/decorators/local
177
		 */
178
179
		/** admin/jqadm/catalog/media/decorators/global
180
		 * Adds a list of globally available decorators only to the catalog JQAdm client
181
		 *
182
		 * Decorators extend the functionality of a class by adding new aspects
183
		 * (e.g. log what is currently done), executing the methods of the underlying
184
		 * class only in certain conditions (e.g. only for logged in users) or
185
		 * modify what is returned to the caller.
186
		 *
187
		 * This option allows you to wrap global decorators
188
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
189
		 *
190
		 *  admin/jqadm/catalog/media/decorators/global = array( 'decorator1' )
191
		 *
192
		 * This would add the decorator named "decorator1" defined by
193
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
194
		 *
195
		 * @param array List of decorator names
196
		 * @since 2017.07
197
		 * @category Developer
198
		 * @see admin/jqadm/common/decorators/default
199
		 * @see admin/jqadm/catalog/media/decorators/excludes
200
		 * @see admin/jqadm/catalog/media/decorators/local
201
		 */
202
203
		/** admin/jqadm/catalog/media/decorators/local
204
		 * Adds a list of local decorators only to the catalog JQAdm client
205
		 *
206
		 * Decorators extend the functionality of a class by adding new aspects
207
		 * (e.g. log what is currently done), executing the methods of the underlying
208
		 * class only in certain conditions (e.g. only for logged in users) or
209
		 * modify what is returned to the caller.
210
		 *
211
		 * This option allows you to wrap local decorators
212
		 * ("\Aimeos\Admin\JQAdm\Catalog\Decorator\*") around the JQAdm client.
213
		 *
214
		 *  admin/jqadm/catalog/media/decorators/local = array( 'decorator2' )
215
		 *
216
		 * This would add the decorator named "decorator2" defined by
217
		 * "\Aimeos\Admin\JQAdm\Catalog\Decorator\Decorator2" only to the JQAdm client.
218
		 *
219
		 * @param array List of decorator names
220
		 * @since 2017.07
221
		 * @category Developer
222
		 * @see admin/jqadm/common/decorators/default
223
		 * @see admin/jqadm/catalog/media/decorators/excludes
224
		 * @see admin/jqadm/catalog/media/decorators/global
225
		 */
226
		return $this->createSubClient( 'catalog/media/' . $type, $name );
227
	}
228
229
230
	/**
231
	 * Adds the required data used in the catalog template
232
	 *
233
	 * @param \Aimeos\MW\View\Iface $view View object
234
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
235
	 */
236
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
237
	{
238
		$context = $this->getContext();
239
240
		$typeManager = \Aimeos\MShop::create( $context, 'media/type' );
241
		$listTypeManager = \Aimeos\MShop::create( $context, 'catalog/lists/type' );
242
243
		$search = $typeManager->createSearch( true )->setSlice( 0, 10000 );
244
		$search->setConditions( $search->compare( '==', 'media.type.domain', 'catalog' ) );
245
		$search->setSortations( [$search->sort( '+', 'media.type.position' )] );
246
247
		$listSearch = $listTypeManager->createSearch( true )->setSlice( 0, 10000 );
248
		$listSearch->setConditions( $listSearch->compare( '==', 'catalog.lists.type.domain', 'media' ) );
249
		$listSearch->setSortations( [$listSearch->sort( '+', 'catalog.lists.type.position' )] );
250
251
		$view->mediaListTypes = $listTypeManager->searchItems( $listSearch );
252
		$view->mediaTypes = $typeManager->searchItems( $search );
253
254
		return $view;
255
	}
256
257
258
	/**
259
	 * Removes the media reference and the media item if not shared
260
	 *
261
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item including media reference
262
	 * @param array $listItems Media list items to be removed
263
	 * @return \Aimeos\MShop\Catalog\Item\Iface Modified catalog item
264
	 */
265
	protected function deleteMediaItems( \Aimeos\MShop\Catalog\Item\Iface $item, array $listItems )
266
	{
267
		$context = $this->getContext();
268
		$cntl = \Aimeos\Controller\Common\Media\Factory::create( $context );
269
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
270
		$search = $manager->createSearch();
271
272
		foreach( $listItems as $listItem )
273
		{
274
			$func = $search->createFunction( 'catalog:has', ['media', $listItem->getType(), $listItem->getRefId()] );
275
			$search->setConditions( $search->compare( '!=', $func, null ) );
276
			$items = $manager->searchItems( $search );
277
			$refItem = null;
278
279
			if( count( $items ) === 1 && ( $refItem = $listItem->getRefItem() ) !== null ) {
280
				$cntl->delete( $refItem );
281
			}
282
283
			$item->deleteListItem( 'media', $listItem, $refItem );
284
		}
285
286
		return $item;
287
	}
288
289
290
	/**
291
	 * Returns the list of sub-client names configured for the client.
292
	 *
293
	 * @return array List of JQAdm client names
294
	 */
295
	protected function getSubClientNames() : array
296
	{
297
		/** admin/jqadm/catalog/media/standard/subparts
298
		 * List of JQAdm sub-clients rendered within the catalog media section
299
		 *
300
		 * The output of the frontend is composed of the code generated by the JQAdm
301
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
302
		 * that are responsible for rendering certain sub-parts of the output. The
303
		 * sub-clients can contain JQAdm clients themselves and therefore a
304
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
305
		 * the output that is placed inside the container of its parent.
306
		 *
307
		 * At first, always the JQAdm code generated by the parent is printed, then
308
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
309
		 * determines the order of the output of these sub-clients inside the parent
310
		 * container. If the configured list of clients is
311
		 *
312
		 *  array( "subclient1", "subclient2" )
313
		 *
314
		 * you can easily change the order of the output by reordering the subparts:
315
		 *
316
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
317
		 *
318
		 * You can also remove one or more parts if they shouldn't be rendered:
319
		 *
320
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
321
		 *
322
		 * As the clients only generates structural JQAdm, the layout defined via CSS
323
		 * should support adding, removing or reordering content by a fluid like
324
		 * design.
325
		 *
326
		 * @param array List of sub-client names
327
		 * @since 2017.07
328
		 * @category Developer
329
		 */
330
		return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/media/standard/subparts', [] );
331
	}
332
333
334
	/**
335
	 * Creates new and updates existing items using the data array
336
	 *
337
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object without referenced domain items
338
	 * @param array $data Data array
339
	 * @return \Aimeos\MShop\Catalog\Item\Iface Modified catalog item
340
	 */
341
	protected function fromArray( \Aimeos\MShop\Catalog\Item\Iface $item, array $data ) : \Aimeos\MShop\Catalog\Item\Iface
342
	{
343
		$context = $this->getContext();
344
345
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
346
		$listManager = \Aimeos\MShop::create( $context, 'catalog/lists' );
347
		$cntl = \Aimeos\Controller\Common\Media\Factory::create( $context );
348
349
		$listItems = $item->getListItems( 'media', null, null, false );
350
		$files = (array) $this->getView()->request()->getUploadedFiles();
351
352
		foreach( $data as $idx => $entry )
353
		{
354
			if( ( $listItem = $item->getListItem( 'media', $entry['catalog.lists.type'], $entry['media.id'], false ) ) === null ) {
355
				$listItem = $listManager->createItem();
356
			}
357
358
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
359
				$refItem = $mediaManager->createItem();
360
			}
361
362
			$refItem->fromArray( $entry, true );
363
			$file = $this->getValue( $files, 'media/' . $idx . '/file' );
364
365
			if( $file && $file->getError() !== UPLOAD_ERR_NO_FILE ) {
366
				$refItem = $cntl->add( $refItem, $file );
367
			} elseif( $refItem->getId() === null && $refItem->getUrl() !== '' ) {
368
				$refItem = $cntl->copy( $refItem );
369
			}
370
371
			$conf = [];
372
373
			foreach( (array) $this->getValue( $entry, 'config', [] ) as $cfg )
374
			{
375
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' ) {
376
					$conf[$key] = trim( $cfg['val'] ?? '' );
377
				}
378
			}
379
380
			$listItem->fromArray( $entry, true );
381
			$listItem->setPosition( $idx );
382
			$listItem->setConfig( $conf );
383
384
			$item->addListItem( 'media', $listItem, $refItem );
385
386
			unset( $listItems[$listItem->getId()] );
387
		}
388
389
		return $this->deleteMediaItems( $item, $listItems->toArray() );
390
	}
391
392
393
	/**
394
	 * Constructs the data array for the view from the given item
395
	 *
396
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object including referenced domain items
397
	 * @param bool $copy True if items should be copied, false if not
398
	 * @return string[] Multi-dimensional associative list of item data
399
	 */
400
	protected function toArray( \Aimeos\MShop\Catalog\Item\Iface $item, bool $copy = false ) : array
401
	{
402
		$data = [];
403
		$siteId = $this->getContext()->getLocale()->getSiteId();
404
405
		foreach( $item->getListItems( 'media', null, null, false ) as $listItem )
406
		{
407
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
408
				continue;
409
			}
410
411
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
412
413
			if( $copy === true )
414
			{
415
				$list['catalog.lists.siteid'] = $siteId;
416
				$list['catalog.lists.id'] = '';
417
				$list['media.siteid'] = $siteId;
418
				$list['media.id'] = null;
419
			}
420
421
			$list['catalog.lists.datestart'] = str_replace( ' ', 'T', $list['catalog.lists.datestart'] );
422
			$list['catalog.lists.dateend'] = str_replace( ' ', 'T', $list['catalog.lists.dateend'] );
423
			$list['config'] = [];
424
425
			foreach( $listItem->getConfig() as $key => $value ) {
426
				$list['config'][] = ['key' => $key, 'val' => $value];
427
			}
428
429
			$data[] = $list;
430
		}
431
432
		return $data;
433
	}
434
435
436
	/**
437
	 * Returns the rendered template including the view data
438
	 *
439
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
440
	 * @return string|null HTML output
441
	 */
442
	protected function render( \Aimeos\MW\View\Iface $view ) : string
443
	{
444
		/** admin/jqadm/catalog/media/template-item
445
		 * Relative path to the HTML body template of the media subpart for catalogs.
446
		 *
447
		 * The template file contains the HTML code and processing instructions
448
		 * to generate the result shown in the body of the frontend. The
449
		 * configuration string is the path to the template file relative
450
		 * to the templates directory (usually in admin/jqadm/templates).
451
		 *
452
		 * You can overwrite the template file configuration in extensions and
453
		 * provide alternative templates. These alternative templates should be
454
		 * named like the default one but with the string "default" replaced by
455
		 * an unique name. You may use the name of your project for this. If
456
		 * you've implemented an alternative client class as well, "default"
457
		 * should be replaced by the name of the new class.
458
		 *
459
		 * @param string Relative path to the template creating the HTML code
460
		 * @since 2017.07
461
		 * @category Developer
462
		 */
463
		$tplconf = 'admin/jqadm/catalog/media/template-item';
464
		$default = 'catalog/item-media-standard';
465
466
		return $view->render( $view->config( $tplconf, $default ) );
467
	}
468
}
469