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

Standard::delete()   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-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Service\Media;
12
13
sprintf( 'media' ); // for translation
14
15
16
/**
17
 * Default implementation of service 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/service/media/name
27
	 * Name of the media subpart used by the JQAdm service implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Service\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 service 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, 'service/lists/type' );
50
51
		$search = $typeManager->createSearch( true )->setSlice( 0, 10000 );
52
		$search->setConditions( $search->compare( '==', 'media.type.domain', 'service' ) );
53
		$search->setSortations( [$search->sort( '+', 'media.type.position' )] );
54
55
		$listSearch = $listTypeManager->createSearch( true )->setSlice( 0, 10000 );
56
		$listSearch->setConditions( $listSearch->compare( '==', 'service.lists.type.domain', 'media' ) );
57
		$listSearch->setSortations( [$listSearch->sort( '+', 'service.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
		$view->mediaData = $this->toArray( $view->item, true );
75
		$view->mediaBody = '';
76
77
		foreach( $this->getSubClients() as $client ) {
78
			$view->mediaBody .= $client->copy();
79
		}
80
81
		return $this->render( $view );
82
	}
83
84
85
	/**
86
	 * Creates a new resource
87
	 *
88
	 * @return string|null HTML output
89
	 */
90
	public function create() : ?string
91
	{
92
		$view = $this->getObject()->addData( $this->getView() );
93
		$siteid = $this->getContext()->getLocale()->getSiteId();
94
95
		$itemData = $this->toArray( $view->item );
96
		$data = array_replace_recursive( $itemData, $view->param( 'media', [] ) );
97
98
		foreach( $data as $idx => $entry )
99
		{
100
			$data[$idx]['media.siteid'] = $siteid;
101
			$data[$idx]['media.url'] = $entry['media.url'] ?? null;
102
			$data[$idx]['media.preview'] = $entry['media.preview'] ?? null;
103
			$data[$idx]['service.lists.siteid'] = $siteid;
104
		}
105
106
		$view->mediaData = $data;
107
		$view->mediaBody = '';
108
109
		foreach( $this->getSubClients() as $client ) {
110
			$view->mediaBody .= $client->create();
111
		}
112
113
		return $this->render( $view );
114
	}
115
116
117
	/**
118
	 * Deletes a resource
119
	 *
120
	 * @return string|null HTML output
121
	 */
122
	public function delete() : ?string
123
	{
124
		parent::delete();
125
126
		$item = $this->getView()->item;
127
		$this->deleteMediaItems( $item, $item->getListItems( 'media', null, null, false )->toArray() );
128
129
		return null;
130
	}
131
132
133
	/**
134
	 * Returns a single resource
135
	 *
136
	 * @return string|null HTML output
137
	 */
138
	public function get() : ?string
139
	{
140
		$view = $this->getObject()->addData( $this->getView() );
141
		$view->mediaData = $this->toArray( $view->item );
142
		$view->mediaBody = '';
143
144
		foreach( $this->getSubClients() as $client ) {
145
			$view->mediaBody .= $client->get();
146
		}
147
148
		return $this->render( $view );
149
	}
150
151
152
	/**
153
	 * Saves the data
154
	 *
155
	 * @return string|null HTML output
156
	 */
157
	public function save() : ?string
158
	{
159
		$view = $this->getView();
160
161
		$view->item = $this->fromArray( $view->item, $view->param( 'media', [] ) );
162
		$view->mediaBody = '';
163
164
		foreach( $this->getSubClients() as $client ) {
165
			$view->mediaBody .= $client->save();
166
		}
167
168
		return null;
169
	}
170
171
172
	/**
173
	 * Returns the sub-client given by its name.
174
	 *
175
	 * @param string $type Name of the client type
176
	 * @param string|null $name Name of the sub-client (Default if null)
177
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
178
	 */
179
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
180
	{
181
		/** admin/jqadm/service/media/decorators/excludes
182
		 * Excludes decorators added by the "common" option from the service JQAdm client
183
		 *
184
		 * Decorators extend the functionality of a class by adding new aspects
185
		 * (e.g. log what is currently done), executing the methods of the underlying
186
		 * class only in certain conditions (e.g. only for logged in users) or
187
		 * modify what is returned to the caller.
188
		 *
189
		 * This option allows you to remove a decorator added via
190
		 * "admin/jqadm/common/decorators/default" before they are wrapped
191
		 * around the JQAdm client.
192
		 *
193
		 *  admin/jqadm/service/media/decorators/excludes = array( 'decorator1' )
194
		 *
195
		 * This would remove the decorator named "decorator1" from the list of
196
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
197
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
198
		 *
199
		 * @param array List of decorator names
200
		 * @since 2017.07
201
		 * @category Developer
202
		 * @see admin/jqadm/common/decorators/default
203
		 * @see admin/jqadm/service/media/decorators/global
204
		 * @see admin/jqadm/service/media/decorators/local
205
		 */
206
207
		/** admin/jqadm/service/media/decorators/global
208
		 * Adds a list of globally available decorators only to the service JQAdm client
209
		 *
210
		 * Decorators extend the functionality of a class by adding new aspects
211
		 * (e.g. log what is currently done), executing the methods of the underlying
212
		 * class only in certain conditions (e.g. only for logged in users) or
213
		 * modify what is returned to the caller.
214
		 *
215
		 * This option allows you to wrap global decorators
216
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
217
		 *
218
		 *  admin/jqadm/service/media/decorators/global = array( 'decorator1' )
219
		 *
220
		 * This would add the decorator named "decorator1" defined by
221
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
222
		 *
223
		 * @param array List of decorator names
224
		 * @since 2017.07
225
		 * @category Developer
226
		 * @see admin/jqadm/common/decorators/default
227
		 * @see admin/jqadm/service/media/decorators/excludes
228
		 * @see admin/jqadm/service/media/decorators/local
229
		 */
230
231
		/** admin/jqadm/service/media/decorators/local
232
		 * Adds a list of local decorators only to the service JQAdm client
233
		 *
234
		 * Decorators extend the functionality of a class by adding new aspects
235
		 * (e.g. log what is currently done), executing the methods of the underlying
236
		 * class only in certain conditions (e.g. only for logged in users) or
237
		 * modify what is returned to the caller.
238
		 *
239
		 * This option allows you to wrap local decorators
240
		 * ("\Aimeos\Admin\JQAdm\Service\Decorator\*") around the JQAdm client.
241
		 *
242
		 *  admin/jqadm/service/media/decorators/local = array( 'decorator2' )
243
		 *
244
		 * This would add the decorator named "decorator2" defined by
245
		 * "\Aimeos\Admin\JQAdm\Service\Decorator\Decorator2" only to the JQAdm client.
246
		 *
247
		 * @param array List of decorator names
248
		 * @since 2017.07
249
		 * @category Developer
250
		 * @see admin/jqadm/common/decorators/default
251
		 * @see admin/jqadm/service/media/decorators/excludes
252
		 * @see admin/jqadm/service/media/decorators/global
253
		 */
254
		return $this->createSubClient( 'service/media/' . $type, $name );
255
	}
256
257
258
	/**
259
	 * Removes the media reference and the media item if not shared
260
	 *
261
	 * @param \Aimeos\MShop\Service\Item\Iface $item Service item including media reference
262
	 * @param array $listItems Media list items to be removed
263
	 * @return \Aimeos\MShop\Service\Item\Iface Modified service item
264
	 */
265
	protected function deleteMediaItems( \Aimeos\MShop\Service\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, 'service' );
270
		$search = $manager->createSearch();
271
272
		foreach( $listItems as $listItem )
273
		{
274
			$func = $search->createFunction( 'service: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/service/media/standard/subparts
298
		 * List of JQAdm sub-clients rendered within the service 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/service/media/standard/subparts', [] );
331
	}
332
333
334
	/**
335
	 * Creates new and updates existing items using the data array
336
	 *
337
	 * @param \Aimeos\MShop\Service\Item\Iface $item Service item object without referenced domain items
338
	 * @param array $data Data array
339
	 * @return \Aimeos\MShop\Service\Item\Iface Modified service item
340
	 */
341
	protected function fromArray( \Aimeos\MShop\Service\Item\Iface $item, array $data ) : \Aimeos\MShop\Service\Item\Iface
342
	{
343
		$context = $this->getContext();
344
345
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
346
		$listManager = \Aimeos\MShop::create( $context, 'service/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['service.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\Service\Item\Iface $item Service 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\Service\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['service.lists.siteid'] = $siteId;
416
				$list['service.lists.id'] = '';
417
				$list['media.siteid'] = $siteId;
418
				$list['media.id'] = null;
419
			}
420
421
			$list['service.lists.datestart'] = str_replace( ' ', 'T', $list['service.lists.datestart'] );
422
			$list['service.lists.dateend'] = str_replace( ' ', 'T', $list['service.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/service/media/template-item
445
		 * Relative path to the HTML body template of the media subpart for services.
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/service/media/template-item';
464
		$default = 'service/item-media-standard';
465
466
		return $view->render( $view->config( $tplconf, $default ) );
467
	}
468
}
469