Passed
Push — master ( efd741...3cd384 )
by Aimeos
05:30
created

Standard::fromArray()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 6
nop 1
dl 0
loc 20
rs 9.2222
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-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Catalog;
12
13
sprintf( 'goods' ); // for translation
14
sprintf( 'catalog' ); // for translation
15
16
17
/**
18
 * Default implementation of catalog JQAdm client.
19
 *
20
 * @package Admin
21
 * @subpackage JQAdm
22
 */
23
class Standard
24
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
25
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
26
{
27
	/** admin/jqadm/catalog/name
28
	 * Class name of the used catalog panel implementation
29
	 *
30
	 * Each default admin client can be replace by an alternative imlementation.
31
	 * To use this implementation, you have to set the last part of the class
32
	 * name as configuration value so the client factory knows which class it
33
	 * has to instantiate.
34
	 *
35
	 * For example, if the name of the default class is
36
	 *
37
	 *  \Aimeos\Admin\JQAdm\Catalog\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Catalog\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/catalog/name = Myfavorite
46
	 *
47
	 * The value is the last part of your own class name and it's case sensitive,
48
	 * so take care that the configuration value is exactly named like the last
49
	 * part of the class name.
50
	 *
51
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
52
	 * characters are possible! You should always start the last part of the class
53
	 * name with an upper case character and continue only with lower case characters
54
	 * or numbers. Avoid chamel case names like "MyFavorite"!
55
	 *
56
	 * @param string Last part of the class name
57
	 * @since 2016.01
58
	 */
59
60
61
	/**
62
	 * Adds the required data used in the template
63
	 *
64
	 * @param \Aimeos\Base\View\Iface $view View object
65
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
66
	 */
67
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
68
	{
69
		$view->itemSubparts = $this->getSubClientNames();
70
		return $view;
71
	}
72
73
74
	/**
75
	 * Copies a resource
76
	 *
77
	 * @return string|null HTML output
78
	 */
79
	public function copy() : ?string
80
	{
81
		$view = $this->object()->data( $this->view() );
82
83
		try
84
		{
85
			if( ( $id = $view->param( 'id' ) ) === null )
86
			{
87
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
88
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
89
			}
90
91
			$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
92
			$view->item = $manager->get( $id, $this->getDomains() );
93
94
			$view->itemData = $this->toArray( $view->item, true );
95
			$view->itemBody = parent::copy();
96
		}
97
		catch( \Exception $e )
98
		{
99
			$this->report( $e, 'copy' );
100
		}
101
102
		return $this->render( $view );
103
	}
104
105
106
	/**
107
	 * Creates a new resource
108
	 *
109
	 * @return string|null HTML output
110
	 */
111
	public function create() : ?string
112
	{
113
		$view = $this->object()->data( $this->view() );
114
115
		try
116
		{
117
			$data = $view->param( 'item', [] );
118
119
			if( !isset( $view->item ) ) {
120
				$view->item = \Aimeos\MShop::create( $this->context(), 'catalog' )->create();
121
			}
122
123
			$data['catalog.siteid'] = $view->item->getSiteId();
124
125
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
126
			$view->itemBody = parent::create();
127
		}
128
		catch( \Exception $e )
129
		{
130
			$this->report( $e, 'create' );
131
		}
132
133
		return $this->render( $view );
134
	}
135
136
137
	/**
138
	 * Deletes a resource
139
	 *
140
	 * @return string|null HTML output
141
	 */
142
	public function delete() : ?string
143
	{
144
		$tags = ['catalog'];
145
		$view = $this->view();
146
		$context = $this->context();
147
148
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
149
		$manager->begin();
150
151
		try
152
		{
153
			if( ( $ids = $view->param( 'id' ) ) === null )
154
			{
155
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
156
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
157
			}
158
159
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
160
			$search->setConditions( $search->compare( '==', 'catalog.id', $ids ) );
161
			$items = $manager->search( $search, $this->getDomains() );
162
163
			foreach( $items as $item )
164
			{
165
				$tags[] = 'catalog-' . $item->getId();
166
				$view->item = $item;
167
				parent::delete();
168
			}
169
170
			$manager->delete( $items );
171
			$manager->commit();
172
173
			$context->cache()->deleteByTags( $tags );
174
175
			return $this->redirect( 'catalog', 'search', null, 'delete' );
176
		}
177
		catch( \Exception $e )
178
		{
179
			$manager->rollback();
180
			$this->report( $e, 'delete' );
181
		}
182
183
		return $this->search();
184
	}
185
186
187
	/**
188
	 * Returns a single resource
189
	 *
190
	 * @return string|null HTML output
191
	 */
192
	public function get() : ?string
193
	{
194
		$view = $this->object()->data( $this->view() );
195
196
		try
197
		{
198
			if( ( $id = $view->param( 'id' ) ) === null )
199
			{
200
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
201
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
202
			}
203
204
			$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
205
206
			$view->item = $manager->get( $id, $this->getDomains() );
207
			$view->itemData = $this->toArray( $view->item );
208
			$view->itemBody = parent::get();
209
		}
210
		catch( \Exception $e )
211
		{
212
			$this->report( $e, 'get' );
213
		}
214
215
		return $this->render( $view );
216
	}
217
218
219
	/**
220
	 * Saves the data
221
	 *
222
	 * @return string|null HTML output
223
	 */
224
	public function save() : ?string
225
	{
226
		$view = $this->view();
227
		$context = $this->context();
228
229
		$manager = \Aimeos\MShop::create( $context, 'catalog' );
230
		$manager->begin();
231
232
		try
233
		{
234
			$item = $this->fromArray( $view->param( 'item', [] ) );
235
			$view->item = $item->getId() ? $item : $manager->save( $item );
236
			$view->itemBody = parent::save();
237
238
			$manager->save( clone $view->item );
239
			$manager->commit();
240
241
			$context->cache()->deleteByTags( ['catalog', 'catalog-' . $view->item->getId()] );
242
243
			return $this->redirect( 'catalog', 'get', $view->item->getId(), 'save' );
0 ignored issues
show
Bug introduced by
It seems like $view->item->getId() can also be of type Aimeos\Map; however, parameter $id of Aimeos\Admin\JQAdm\Base::redirect() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

243
			return $this->redirect( 'catalog', 'get', /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
244
		}
245
		catch( \Exception $e )
246
		{
247
			$manager->rollback();
248
			$this->report( $e, 'save' );
249
		}
250
251
		return $this->create();
252
	}
253
254
255
	/**
256
	 * Returns the catalog root node
257
	 *
258
	 * @return string|null HTML output
259
	 */
260
	public function search() : ?string
261
	{
262
		return $this->render( $this->view() );
263
	}
264
265
266
	/**
267
	 * Returns the sub-client given by its name.
268
	 *
269
	 * @param string $type Name of the client type
270
	 * @param string|null $name Name of the sub-client (Default if null)
271
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
272
	 */
273
	public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface
274
	{
275
		/** admin/jqadm/catalog/decorators/excludes
276
		 * Excludes decorators added by the "common" option from the catalog JQAdm client
277
		 *
278
		 * Decorators extend the functionality of a class by adding new aspects
279
		 * (e.g. log what is currently done), executing the methods of the underlying
280
		 * class only in certain conditions (e.g. only for logged in users) or
281
		 * modify what is returned to the caller.
282
		 *
283
		 * This option allows you to remove a decorator added via
284
		 * "client/jqadm/common/decorators/default" before they are wrapped
285
		 * around the JQAdm client.
286
		 *
287
		 *  admin/jqadm/catalog/decorators/excludes = array( 'decorator1' )
288
		 *
289
		 * This would remove the decorator named "decorator1" from the list of
290
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
291
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
292
		 *
293
		 * @param array List of decorator names
294
		 * @since 2016.01
295
		 * @see admin/jqadm/common/decorators/default
296
		 * @see admin/jqadm/catalog/decorators/global
297
		 * @see admin/jqadm/catalog/decorators/local
298
		 */
299
300
		/** admin/jqadm/catalog/decorators/global
301
		 * Adds a list of globally available decorators only to the catalog JQAdm client
302
		 *
303
		 * Decorators extend the functionality of a class by adding new aspects
304
		 * (e.g. log what is currently done), executing the methods of the underlying
305
		 * class only in certain conditions (e.g. only for logged in users) or
306
		 * modify what is returned to the caller.
307
		 *
308
		 * This option allows you to wrap global decorators
309
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
310
		 *
311
		 *  admin/jqadm/catalog/decorators/global = array( 'decorator1' )
312
		 *
313
		 * This would add the decorator named "decorator1" defined by
314
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
315
		 *
316
		 * @param array List of decorator names
317
		 * @since 2016.01
318
		 * @see admin/jqadm/common/decorators/default
319
		 * @see admin/jqadm/catalog/decorators/excludes
320
		 * @see admin/jqadm/catalog/decorators/local
321
		 */
322
323
		/** admin/jqadm/catalog/decorators/local
324
		 * Adds a list of local decorators only to the catalog JQAdm client
325
		 *
326
		 * Decorators extend the functionality of a class by adding new aspects
327
		 * (e.g. log what is currently done), executing the methods of the underlying
328
		 * class only in certain conditions (e.g. only for logged in users) or
329
		 * modify what is returned to the caller.
330
		 *
331
		 * This option allows you to wrap local decorators
332
		 * ("\Aimeos\Admin\JQAdm\Catalog\Decorator\*") around the JQAdm client.
333
		 *
334
		 *  admin/jqadm/catalog/decorators/local = array( 'decorator2' )
335
		 *
336
		 * This would add the decorator named "decorator2" defined by
337
		 * "\Aimeos\Admin\JQAdm\Catalog\Decorator\Decorator2" only to the JQAdm client.
338
		 *
339
		 * @param array List of decorator names
340
		 * @since 2016.01
341
		 * @see admin/jqadm/common/decorators/default
342
		 * @see admin/jqadm/catalog/decorators/excludes
343
		 * @see admin/jqadm/catalog/decorators/global
344
		 */
345
		return $this->createSubClient( 'catalog/' . $type, $name );
346
	}
347
348
349
	/**
350
	 * Returns the domain names whose items should be fetched too
351
	 *
352
	 * @return string[] List of domain names
353
	 */
354
	protected function getDomains() : array
355
	{
356
		/** admin/jqadm/catalog/domains
357
		 * List of domain items that should be fetched along with the catalog
358
		 *
359
		 * If you need to display additional content, you can configure your own
360
		 * list of domains (attribute, media, price, catalog, text, etc. are
361
		 * domains) whose items are fetched from the storage.
362
		 *
363
		 * @param array List of domain names
364
		 * @since 2016.01
365
		 */
366
		return $this->context()->config()->get( 'admin/jqadm/catalog/domains', [] );
367
	}
368
369
370
	/**
371
	 * Returns the list of sub-client names configured for the client.
372
	 *
373
	 * @return array List of JQAdm client names
374
	 */
375
	protected function getSubClientNames() : array
376
	{
377
		/** admin/jqadm/catalog/subparts
378
		 * List of JQAdm sub-clients rendered within the catalog section
379
		 *
380
		 * The output of the frontend is composed of the code generated by the JQAdm
381
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
382
		 * that are responsible for rendering certain sub-parts of the output. The
383
		 * sub-clients can contain JQAdm clients themselves and therefore a
384
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
385
		 * the output that is placed inside the container of its parent.
386
		 *
387
		 * At first, always the JQAdm code generated by the parent is printed, then
388
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
389
		 * determines the order of the output of these sub-clients inside the parent
390
		 * container. If the configured list of clients is
391
		 *
392
		 *  array( "subclient1", "subclient2" )
393
		 *
394
		 * you can easily change the order of the output by reordering the subparts:
395
		 *
396
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
397
		 *
398
		 * You can also remove one or more parts if they shouldn't be rendered:
399
		 *
400
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
401
		 *
402
		 * As the clients only generates structural JQAdm, the layout defined via CSS
403
		 * should support adding, removing or reordering content by a fluid like
404
		 * design.
405
		 *
406
		 * @param array List of sub-client names
407
		 * @since 2016.01
408
		 */
409
		return $this->context()->config()->get( 'admin/jqadm/catalog/subparts', [] );
410
	}
411
412
413
	/**
414
	 * Creates new and updates existing items using the data array
415
	 *
416
	 * @param array $data Data array
417
	 * @return \Aimeos\MShop\Catalog\Item\Iface New catalog item object
418
	 */
419
	protected function fromArray( array $data ) : \Aimeos\MShop\Catalog\Item\Iface
420
	{
421
		$manager = \Aimeos\MShop::create( $this->context(), 'catalog' );
422
423
		if( isset( $data['catalog.id'] ) && $data['catalog.id'] != '' ) {
424
			$item = $manager->get( $data['catalog.id'], $this->getDomains() );
425
		} else {
426
			$item = $manager->create();
427
		}
428
429
		$item->fromArray( $data, true )->setConfig( [] );
430
431
		foreach( (array) $this->val( $data, 'config', [] ) as $cfg )
432
		{
433
			if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) {
434
				$item->setConfigValue( $key, json_decode( $val, true ) ?? $val );
435
			}
436
		}
437
438
		return $item;
439
	}
440
441
442
	/**
443
	 * Constructs the data array for the view from the given item
444
	 *
445
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object
446
	 * @return string[] Multi-dimensional associative list of item data
447
	 */
448
	protected function toArray( \Aimeos\MShop\Catalog\Item\Iface $item, bool $copy = false ) : array
449
	{
450
		$data = $item->toArray( true );
451
		$data['config'] = $this->flatten( $item->getConfig() );
452
453
		if( $copy === true )
454
		{
455
			$data['catalog.id'] = '';
456
			$data['catalog.url'] = '';
457
			$data['catalog.siteid'] = $item->getSiteId();
458
			$data['catalog.code'] = $data['catalog.code'] . '_' . substr( md5( microtime( true ) ), -5 );
459
		}
460
461
		return $data;
462
	}
463
464
465
	/**
466
	 * Returns the rendered template including the view data
467
	 *
468
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
469
	 * @return string HTML output
470
	 */
471
	protected function render( \Aimeos\Base\View\Iface $view ) : string
472
	{
473
		/** admin/jqadm/catalog/template-item
474
		 * Relative path to the HTML body template for the catalog item.
475
		 *
476
		 * The template file contains the HTML code and processing instructions
477
		 * to generate the result shown in the body of the frontend. The
478
		 * configuration string is the path to the template file relative
479
		 * to the templates directory (usually in templates/admin/jqadm).
480
		 *
481
		 * You can overwrite the template file configuration in extensions and
482
		 * provide alternative templates. These alternative templates should be
483
		 * named like the default one but with the string "default" replaced by
484
		 * an unique name. You may use the name of your project for this. If
485
		 * you've implemented an alternative client class as well, "default"
486
		 * should be replaced by the name of the new class.
487
		 *
488
		 * @param string Relative path to the template creating the HTML code
489
		 * @since 2016.04
490
		 */
491
		$tplconf = 'admin/jqadm/catalog/template-item';
492
		$default = 'catalog/item';
493
494
		return $view->render( $view->config( $tplconf, $default ) );
495
	}
496
}
497