Passed
Push — master ( bcaaf7...28325c )
by Aimeos
03:53
created

Standard::fromArray()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 28
rs 9.2222
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2023
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 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\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()->slice( 0, count( (array) $ids ) );
177
			$search->setConditions( $search->compare( '==', 'rule.id', $ids ) );
178
			$items = $manager->search( $search );
179
180
			foreach( $items as $item )
181
			{
182
				$view->item = $item;
183
				parent::delete();
184
			}
185
186
			$manager->delete( $items->toArray() );
187
			$manager->commit();
188
189
			return $this->redirect( 'rule', '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(), 'rule' );
219
220
			$view->item = $manager->get( $id );
221
			$view->itemData = $this->toArray( $view->item );
222
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
223
			$view->itemBody = parent::get();
224
		}
225
		catch( \Exception $e )
226
		{
227
			$this->report( $e, 'get' );
228
		}
229
230
		return $this->render( $view );
231
	}
232
233
234
	/**
235
	 * Saves the data
236
	 *
237
	 * @return string|null HTML output
238
	 */
239
	public function save() : ?string
240
	{
241
		$view = $this->view();
242
243
		$manager = \Aimeos\MShop::create( $this->context(), 'rule' );
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
			$manager->save( clone $view->item );
253
			$manager->commit();
254
255
			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

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