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

Standard::addViewData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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