Standard   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 549
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 135
dl 0
loc 549
rs 9.6
c 0
b 0
f 0
wmc 35

15 Methods

Rating   Name   Duplication   Size   Complexity  
A copy() 0 25 3
A data() 0 11 1
A create() 0 23 3
A batch() 0 3 1
A delete() 0 36 4
A getSubClientNames() 0 35 1
A fromArray() 0 23 6
A get() 0 25 3
A render() 0 24 1
A toArray() 0 22 3
A getSubClient() 0 73 1
A getConfigAttributes() 0 8 2
A save() 0 25 3
A search() 0 47 2
A getTypeItems() 0 6 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Rule;
12
13
sprintf( 'marketing' ); // for translation
14
sprintf( 'rule' ); // for translation
15
16
17
/**
18
 * Default implementation of rule 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/rule/name
28
	 * Class name of the used rule 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\Rule\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Rule\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/rule/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 2021.04
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->itemDecorators = $this->getClassNames( 'MShop/Rule/Provider/Catalog/Decorator' );
70
		$view->itemProviders = [
71
			'catalog' => $this->getClassNames( 'MShop/Rule/Provider/Catalog' )
72
		];
73
74
		$view->itemSubparts = $this->getSubClientNames();
75
		$view->itemTypes = $this->getTypeItems();
76
77
		return $view;
78
	}
79
80
81
	/**
82
	 * Batch update of a resource
83
	 *
84
	 * @return string|null Output to display
85
	 */
86
	public function batch() : ?string
87
	{
88
		return $this->batchBase( 'rule' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('rule') 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...
89
	}
90
91
92
	/**
93
	 * Copies a resource
94
	 *
95
	 * @return string|null HTML output
96
	 */
97
	public function copy() : ?string
98
	{
99
		$view = $this->object()->data( $this->view() );
100
101
		try
102
		{
103
			if( ( $id = $view->param( 'id' ) ) === null )
104
			{
105
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
106
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
107
			}
108
109
			$manager = \Aimeos\MShop::create( $this->context(), 'rule' );
110
111
			$view->item = $manager->get( $id );
112
			$view->itemData = $this->toArray( $view->item, true );
113
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
114
			$view->itemBody = parent::copy();
115
		}
116
		catch( \Exception $e )
117
		{
118
			$this->report( $e, 'copy' );
119
		}
120
121
		return $this->render( $view );
122
	}
123
124
125
	/**
126
	 * Creates a new resource
127
	 *
128
	 * @return string|null HTML output
129
	 */
130
	public function create() : ?string
131
	{
132
		$view = $this->object()->data( $this->view() );
133
134
		try
135
		{
136
			$data = $view->param( 'item', [] );
137
138
			if( !isset( $view->item ) ) {
139
				$view->item = \Aimeos\MShop::create( $this->context(), 'rule' )->create();
140
			}
141
142
			$data['rule.siteid'] = $view->item->getSiteId();
143
144
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
145
			$view->itemBody = parent::create();
146
		}
147
		catch( \Exception $e )
148
		{
149
			$this->report( $e, 'create' );
150
		}
151
152
		return $this->render( $view );
153
	}
154
155
156
	/**
157
	 * Deletes a resource
158
	 *
159
	 * @return string|null HTML output
160
	 */
161
	public function delete() : ?string
162
	{
163
		$view = $this->view();
164
165
		$manager = \Aimeos\MShop::create( $this->context(), 'rule' );
166
		$manager->begin();
167
168
		try
169
		{
170
			if( ( $ids = $view->param( 'id' ) ) === null )
171
			{
172
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
173
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
174
			}
175
176
			$search = $manager->filter()->add( 'rule.id', '==', $ids )->slice( 0, count( (array) $ids ) );
177
			$items = $manager->search( $search );
178
179
			foreach( $items as $item )
180
			{
181
				$view->item = $item;
182
				parent::delete();
183
			}
184
185
			$manager->delete( $items );
186
			$manager->commit();
187
188
			return $this->redirect( 'rule', 'search', null, 'delete' );
189
		}
190
		catch( \Exception $e )
191
		{
192
			$manager->rollback();
193
			$this->report( $e, 'delete' );
194
		}
195
196
		return $this->search();
197
	}
198
199
200
	/**
201
	 * Returns a single resource
202
	 *
203
	 * @return string|null HTML output
204
	 */
205
	public function get() : ?string
206
	{
207
		$view = $this->object()->data( $this->view() );
208
209
		try
210
		{
211
			if( ( $id = $view->param( 'id' ) ) === null )
212
			{
213
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
214
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
215
			}
216
217
			$manager = \Aimeos\MShop::create( $this->context(), 'rule' );
218
219
			$view->item = $manager->get( $id );
220
			$view->itemData = $this->toArray( $view->item );
221
			$view->itemAttributes = $this->getConfigAttributes( $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
242
		$manager = \Aimeos\MShop::create( $this->context(), 'rule' );
243
		$manager->begin();
244
245
		try
246
		{
247
			$item = $this->fromArray( $view->param( 'item', [] ) );
248
			$view->item = $item->getId() ? $item : $manager->save( $item );
249
			$view->itemBody = parent::save();
250
251
			$manager->save( clone $view->item );
252
			$manager->commit();
253
254
			return $this->redirect( 'rule', $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

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