Passed
Push — master ( b1a653...24fc04 )
by Aimeos
03:29
created

Standard   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 554
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 131
dl 0
loc 554
rs 9.84
c 1
b 0
f 0
wmc 32

15 Methods

Rating   Name   Duplication   Size   Complexity  
A data() 0 5 1
A toArray() 0 15 2
A fromArray() 0 20 5
A render() 0 25 1
A create() 0 23 3
A save() 0 26 3
A getSubClient() 0 76 1
A copy() 0 24 3
A delete() 0 43 4
A batch() 0 3 1
A getTypeItems() 0 8 1
A getDomains() 0 14 1
A getSubClientNames() 0 36 1
A search() 0 50 2
A get() 0 24 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product;
12
13
sprintf( 'goods' ); // for translation
14
sprintf( 'product' ); // for translation
15
16
17
/**
18
 * Default implementation of product 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/product/name
28
	 * Class name of the used account favorite client 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\Product\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Product\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/product/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
	 * @category Developer
59
	 */
60
61
62
	/**
63
	 * Adds the required data used in the template
64
	 *
65
	 * @param \Aimeos\Base\View\Iface $view View object
66
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
67
	 */
68
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
69
	{
70
		$view->itemSubparts = $this->getSubClientNames();
71
		$view->itemTypes = $this->getTypeItems();
72
		return $view;
73
	}
74
75
76
	/**
77
	 * Batch update of a resource
78
	 *
79
	 * @return string|null Output to display
80
	 */
81
	public function batch() : ?string
82
	{
83
		return $this->batchBase( 'attribute' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('attribute') targeting Aimeos\Admin\JQAdm\Base::batchBase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
84
	}
85
86
87
	/**
88
	 * Copies a resource
89
	 *
90
	 * @return string|null HTML output
91
	 */
92
	public function copy() : ?string
93
	{
94
		$view = $this->object()->data( $this->view() );
95
96
		try
97
		{
98
			if( ( $id = $view->param( 'id' ) ) === null )
99
			{
100
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
101
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
102
			}
103
104
			$manager = \Aimeos\MShop::create( $this->context(), 'product' );
105
			$view->item = $manager->get( $id, $this->getDomains() );
106
107
			$view->itemData = $this->toArray( $view->item, true );
108
			$view->itemBody = parent::copy();
109
		}
110
		catch( \Exception $e )
111
		{
112
			$this->report( $e, 'copy' );
113
		}
114
115
		return $this->render( $view );
116
	}
117
118
119
	/**
120
	 * Creates a new resource
121
	 *
122
	 * @return string|null HTML output
123
	 */
124
	public function create() : ?string
125
	{
126
		$view = $this->object()->data( $this->view() );
127
128
		try
129
		{
130
			$data = $view->param( 'item', [] );
131
132
			if( !isset( $view->item ) ) {
133
				$view->item = \Aimeos\MShop::create( $this->context(), 'product' )->create();
134
			}
135
136
			$data['product.siteid'] = $view->item->getSiteId();
137
138
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
139
			$view->itemBody = parent::create();
140
		}
141
		catch( \Exception $e )
142
		{
143
			$this->report( $e, 'create' );
144
		}
145
146
		return $this->render( $view );
147
	}
148
149
150
	/**
151
	 * Deletes a resource
152
	 *
153
	 * @return string|null HTML output
154
	 */
155
	public function delete() : ?string
156
	{
157
		$tags = ['product'];
158
		$view = $this->view();
159
		$context = $this->context();
160
161
		$manager = \Aimeos\MShop::create( $context, 'product' );
162
		$manager->begin();
163
164
		try
165
		{
166
			if( ( $ids = $view->param( 'id' ) ) === null )
167
			{
168
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
169
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
170
			}
171
172
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
173
			$search->setConditions( $search->compare( '==', 'product.id', $ids ) );
174
			$items = $manager->search( $search, $this->getDomains() );
175
176
			foreach( $items as $id => $item )
177
			{
178
				$tags[] = 'product-' . $id;
179
				$view->item = $item;
180
				parent::delete();
181
			}
182
183
			$manager->delete( $items->toArray() );
184
			$manager->commit();
185
186
			\Aimeos\MShop::create( $context, 'index' )->delete( $items->toArray() );
187
			$context->cache()->deleteByTags( $tags );
188
189
			return $this->redirect( 'product', 'search', null, 'delete' );
190
		}
191
		catch( \Exception $e )
192
		{
193
			$manager->rollback();
194
			$this->report( $e, 'delete' );
195
		}
196
197
		return $this->search();
198
	}
199
200
201
	/**
202
	 * Returns a single resource
203
	 *
204
	 * @return string|null HTML output
205
	 */
206
	public function get() : ?string
207
	{
208
		$view = $this->object()->data( $this->view() );
209
210
		try
211
		{
212
			if( ( $id = $view->param( 'id' ) ) === null )
213
			{
214
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
215
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
216
			}
217
218
			$manager = \Aimeos\MShop::create( $this->context(), 'product' );
219
220
			$view->item = $manager->get( $id, $this->getDomains() );
221
			$view->itemData = $this->toArray( $view->item );
222
			$view->itemBody = parent::get();
223
		}
224
		catch( \Exception $e )
225
		{
226
			$this->report( $e, 'get' );
227
		}
228
229
		return $this->render( $view );
230
	}
231
232
233
	/**
234
	 * Saves the data
235
	 *
236
	 * @return string|null HTML output
237
	 */
238
	public function save() : ?string
239
	{
240
		$view = $this->view();
241
		$context = $this->context();
242
243
		$manager = \Aimeos\MShop::create( $context, 'index' );
244
		$manager->begin();
245
246
		try
247
		{
248
			$item = $this->fromArray( $view->param( 'item', [] ) );
249
			$view->item = $item->getId() ? $item : $manager->save( $item );
250
			$view->itemBody = parent::save();
251
252
			$item = $manager->save( clone $view->item );
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
253
			$manager->commit();
254
255
			return $this->redirect( 'product', $view->param( 'next' ), $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

255
			return $this->redirect( 'product', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
256
		}
257
		catch( \Exception $e )
258
		{
259
			$manager->rollback();
260
			$this->report( $e, 'save' );
261
		}
262
263
		return $this->create();
264
	}
265
266
267
	/**
268
	 * Returns a list of resource according to the conditions
269
	 *
270
	 * @return string|null HTML output
271
	 */
272
	public function search() : ?string
273
	{
274
		$view = $this->view();
275
276
		try
277
		{
278
			$total = 0;
279
			$domains = map( $this->getDomains() )->remove( 'product' );
0 ignored issues
show
Bug introduced by
'product' of type string is incompatible with the type iterable expected by parameter $keys of Aimeos\Map::remove(). ( Ignorable by Annotation )

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

279
			$domains = map( $this->getDomains() )->remove( /** @scrutinizer ignore-type */ 'product' );
Loading history...
280
			$params = $this->storeFilter( $view->param(), 'product' );
281
			$manager = \Aimeos\MShop::create( $this->context(), 'product' );
282
283
			$search = $manager->filter();
284
			$search->setSortations( [$search->sort( '+', 'product.id' )] );
285
			$search = $this->initCriteria( $search, $params );
286
287
			$view->items = $manager->search( $search, $domains->toArray(), $total );
288
			$view->filterAttributes = $manager->getSearchAttributes( true );
289
			$view->filterOperators = $search->getOperators();
290
			$view->itemTypes = $this->getTypeItems();
291
			$view->itemBody = parent::search();
292
			$view->total = $total;
293
		}
294
		catch( \Exception $e )
295
		{
296
			$this->report( $e, 'search' );
297
		}
298
299
		/** admin/jqadm/product/template-list
300
		 * Relative path to the HTML body template for the product list.
301
		 *
302
		 * The template file contains the HTML code and processing instructions
303
		 * to generate the result shown in the body of the frontend. The
304
		 * configuration string is the path to the template file relative
305
		 * to the templates directory (usually in admin/jqadm/templates).
306
		 *
307
		 * You can overwrite the template file configuration in extensions and
308
		 * provide alternative templates. These alternative templates should be
309
		 * named like the default one but with the string "default" replaced by
310
		 * an unique name. You may use the name of your project for this. If
311
		 * you've implemented an alternative client class as well, "default"
312
		 * should be replaced by the name of the new class.
313
		 *
314
		 * @param string Relative path to the template creating the HTML code
315
		 * @since 2016.04
316
		 * @category Developer
317
		 */
318
		$tplconf = 'admin/jqadm/product/template-list';
319
		$default = 'product/list';
320
321
		return $view->render( $view->config( $tplconf, $default ) );
322
	}
323
324
325
	/**
326
	 * Returns the sub-client given by its name.
327
	 *
328
	 * @param string $type Name of the client type
329
	 * @param string|null $name Name of the sub-client (Default if null)
330
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
331
	 */
332
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
333
	{
334
		/** admin/jqadm/product/decorators/excludes
335
		 * Excludes decorators added by the "common" option from the product JQAdm client
336
		 *
337
		 * Decorators extend the functionality of a class by adding new aspects
338
		 * (e.g. log what is currently done), executing the methods of the underlying
339
		 * class only in certain conditions (e.g. only for logged in users) or
340
		 * modify what is returned to the caller.
341
		 *
342
		 * This option allows you to remove a decorator added via
343
		 * "client/jqadm/common/decorators/default" before they are wrapped
344
		 * around the JQAdm client.
345
		 *
346
		 *  admin/jqadm/product/decorators/excludes = array( 'decorator1' )
347
		 *
348
		 * This would remove the decorator named "decorator1" from the list of
349
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
350
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
351
		 *
352
		 * @param array List of decorator names
353
		 * @since 2016.01
354
		 * @category Developer
355
		 * @see admin/jqadm/common/decorators/default
356
		 * @see admin/jqadm/product/decorators/global
357
		 * @see admin/jqadm/product/decorators/local
358
		 */
359
360
		/** admin/jqadm/product/decorators/global
361
		 * Adds a list of globally available decorators only to the product JQAdm client
362
		 *
363
		 * Decorators extend the functionality of a class by adding new aspects
364
		 * (e.g. log what is currently done), executing the methods of the underlying
365
		 * class only in certain conditions (e.g. only for logged in users) or
366
		 * modify what is returned to the caller.
367
		 *
368
		 * This option allows you to wrap global decorators
369
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
370
		 *
371
		 *  admin/jqadm/product/decorators/global = array( 'decorator1' )
372
		 *
373
		 * This would add the decorator named "decorator1" defined by
374
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
375
		 *
376
		 * @param array List of decorator names
377
		 * @since 2016.01
378
		 * @category Developer
379
		 * @see admin/jqadm/common/decorators/default
380
		 * @see admin/jqadm/product/decorators/excludes
381
		 * @see admin/jqadm/product/decorators/local
382
		 */
383
384
		/** admin/jqadm/product/decorators/local
385
		 * Adds a list of local decorators only to the product JQAdm client
386
		 *
387
		 * Decorators extend the functionality of a class by adding new aspects
388
		 * (e.g. log what is currently done), executing the methods of the underlying
389
		 * class only in certain conditions (e.g. only for logged in users) or
390
		 * modify what is returned to the caller.
391
		 *
392
		 * This option allows you to wrap local decorators
393
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
394
		 *
395
		 *  admin/jqadm/product/decorators/local = array( 'decorator2' )
396
		 *
397
		 * This would add the decorator named "decorator2" defined by
398
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
399
		 *
400
		 * @param array List of decorator names
401
		 * @since 2016.01
402
		 * @category Developer
403
		 * @see admin/jqadm/common/decorators/default
404
		 * @see admin/jqadm/product/decorators/excludes
405
		 * @see admin/jqadm/product/decorators/global
406
		 */
407
		return $this->createSubClient( 'product/' . $type, $name );
408
	}
409
410
411
	/**
412
	 * Returns the domain names whose items should be fetched too
413
	 *
414
	 * @return string[] List of domain names
415
	 */
416
	protected function getDomains() : array
417
	{
418
		/** admin/jqadm/product/domains
419
		 * List of domain items that should be fetched along with the product
420
		 *
421
		 * If you need to display additional content, you can configure your own
422
		 * list of domains (attribute, media, price, product, text, etc. are
423
		 * domains) whose items are fetched from the storage.
424
		 *
425
		 * @param array List of domain names
426
		 * @since 2016.01
427
		 * @category Developer
428
		 */
429
		return $this->context()->config()->get( 'admin/jqadm/product/domains', [] );
430
	}
431
432
433
	/**
434
	 * Returns the list of sub-client names configured for the client.
435
	 *
436
	 * @return array List of JQAdm client names
437
	 */
438
	protected function getSubClientNames() : array
439
	{
440
		/** admin/jqadm/product/subparts
441
		 * List of JQAdm sub-clients rendered within the product section
442
		 *
443
		 * The output of the frontend is composed of the code generated by the JQAdm
444
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
445
		 * that are responsible for rendering certain sub-parts of the output. The
446
		 * sub-clients can contain JQAdm clients themselves and therefore a
447
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
448
		 * the output that is placed inside the container of its parent.
449
		 *
450
		 * At first, always the JQAdm code generated by the parent is printed, then
451
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
452
		 * determines the order of the output of these sub-clients inside the parent
453
		 * container. If the configured list of clients is
454
		 *
455
		 *  array( "subclient1", "subclient2" )
456
		 *
457
		 * you can easily change the order of the output by reordering the subparts:
458
		 *
459
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
460
		 *
461
		 * You can also remove one or more parts if they shouldn't be rendered:
462
		 *
463
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
464
		 *
465
		 * As the clients only generates structural JQAdm, the layout defined via CSS
466
		 * should support adding, removing or reordering content by a fluid like
467
		 * design.
468
		 *
469
		 * @param array List of sub-client names
470
		 * @since 2016.01
471
		 * @category Developer
472
		 */
473
		return $this->context()->config()->get( 'admin/jqadm/product/subparts', [] );
474
	}
475
476
477
	/**
478
	 * Returns the available product type items
479
	 *
480
	 * @return \Aimeos\Map List of item implementing \Aimeos\MShop\Common\Type\Iface
481
	 */
482
	protected function getTypeItems() : \Aimeos\Map
483
	{
484
		$typeManager = \Aimeos\MShop::create( $this->context(), 'product/type' );
485
486
		$search = $typeManager->filter( true )->slice( 0, 10000 );
487
		$search->setSortations( [$search->sort( '+', 'product.type.position' )] );
488
489
		return $typeManager->search( $search );
490
	}
491
492
493
	/**
494
	 * Creates new and updates existing items using the data array
495
	 *
496
	 * @param array $data Data array
497
	 * @return \Aimeos\MShop\Product\Item\Iface New product item object
498
	 */
499
	protected function fromArray( array $data ) : \Aimeos\MShop\Product\Item\Iface
500
	{
501
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
502
503
		if( isset( $data['product.id'] ) && $data['product.id'] != '' ) {
504
			$item = $manager->get( $data['product.id'], $this->getDomains() );
505
		} else {
506
			$item = $manager->create();
507
		}
508
509
		$item->fromArray( $data, true )->setConfig( [] );
510
511
		foreach( (array) $this->val( $data, 'config', [] ) as $entry )
512
		{
513
			if( ( $key = trim( $entry['key'] ?? '' ) ) !== '' ) {
514
				$item->setConfigValue( $key, trim( $entry['val'] ?? '' ) );
515
			}
516
		}
517
518
		return $item;
519
	}
520
521
522
	/**
523
	 * Constructs the data array for the view from the given item
524
	 *
525
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object
526
	 * @return string[] Multi-dimensional associative list of item data
527
	 */
528
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
529
	{
530
		$data = $item->toArray( true );
531
		$data['config'] = $this->flatten( $item->getConfig() );
532
533
		if( $copy === true )
534
		{
535
			$data['product.code'] = $data['product.code'] . '_' . substr( md5( microtime( true ) ), -5 );
536
			$data['product.siteid'] = $this->context()->locale()->getSiteId();
537
			$data['product.ctime'] = '';
538
			$data['product.url'] = '';
539
			$data['product.id'] = '';
540
		}
541
542
		return $data;
543
	}
544
545
546
	/**
547
	 * Returns the rendered template including the view data
548
	 *
549
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
550
	 * @return string HTML output
551
	 */
552
	protected function render( \Aimeos\Base\View\Iface $view ) : string
553
	{
554
		/** admin/jqadm/product/template-item
555
		 * Relative path to the HTML body template for the product item.
556
		 *
557
		 * The template file contains the HTML code and processing instructions
558
		 * to generate the result shown in the body of the frontend. The
559
		 * configuration string is the path to the template file relative
560
		 * to the templates directory (usually in admin/jqadm/templates).
561
		 *
562
		 * You can overwrite the template file configuration in extensions and
563
		 * provide alternative templates. These alternative templates should be
564
		 * named like the default one but with the string "default" replaced by
565
		 * an unique name. You may use the name of your project for this. If
566
		 * you've implemented an alternative client class as well, "default"
567
		 * should be replaced by the name of the new class.
568
		 *
569
		 * @param string Relative path to the template creating the HTML code
570
		 * @since 2016.04
571
		 * @category Developer
572
		 */
573
		$tplconf = 'admin/jqadm/product/template-item';
574
		$default = 'product/item';
575
576
		return $view->render( $view->config( $tplconf, $default ) );
577
	}
578
}
579