Passed
Push — master ( e9f33e...6a445e )
by Aimeos
03:39
created

Standard::fromArray()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
nc 4
nop 1
dl 0
loc 32
c 0
b 0
f 0
cc 8
rs 8.4444
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2022
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
	 * @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
		$ds = DIRECTORY_SEPARATOR;
71
72
		$view->itemDecorators = $this->getClassNames( 'MShop' . $ds . 'Rule' . $ds . 'Provider' . $ds . 'Catalog' . $ds . 'Decorator' );
73
		$view->itemProviders = [
74
			'catalog' => $this->getClassNames( 'MShop' . $ds . 'Rule' . $ds . 'Provider' . $ds . 'Catalog' )
75
		];
76
77
		$view->itemSubparts = $this->getSubClientNames();
78
		$view->itemTypes = $this->getTypeItems();
79
80
		return $view;
81
	}
82
83
84
	/**
85
	 * Batch update of a resource
86
	 *
87
	 * @return string|null Output to display
88
	 */
89
	public function batch() : ?string
90
	{
91
		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...
92
	}
93
94
95
	/**
96
	 * Copies a resource
97
	 *
98
	 * @return string|null HTML output
99
	 */
100
	public function copy() : ?string
101
	{
102
		$view = $this->object()->data( $this->view() );
103
104
		try
105
		{
106
			if( ( $id = $view->param( 'id' ) ) === null )
107
			{
108
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
109
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
110
			}
111
112
			$manager = \Aimeos\MShop::create( $this->context(), 'rule' );
113
114
			$view->item = $manager->get( $id );
115
			$view->itemData = $this->toArray( $view->item, true );
116
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
117
			$view->itemBody = parent::copy();
118
		}
119
		catch( \Exception $e )
120
		{
121
			$this->report( $e, 'copy' );
122
		}
123
124
		return $this->render( $view );
125
	}
126
127
128
	/**
129
	 * Creates a new resource
130
	 *
131
	 * @return string|null HTML output
132
	 */
133
	public function create() : ?string
134
	{
135
		$view = $this->object()->data( $this->view() );
136
137
		try
138
		{
139
			$data = $view->param( 'item', [] );
140
141
			if( !isset( $view->item ) ) {
142
				$view->item = \Aimeos\MShop::create( $this->context(), 'rule' )->create();
143
			}
144
145
			$data['rule.siteid'] = $view->item->getSiteId();
146
147
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
148
			$view->itemBody = parent::create();
149
		}
150
		catch( \Exception $e )
151
		{
152
			$this->report( $e, 'create' );
153
		}
154
155
		return $this->render( $view );
156
	}
157
158
159
	/**
160
	 * Deletes a resource
161
	 *
162
	 * @return string|null HTML output
163
	 */
164
	public function delete() : ?string
165
	{
166
		$view = $this->view();
167
168
		$manager = \Aimeos\MShop::create( $this->context(), 'rule' );
169
		$manager->begin();
170
171
		try
172
		{
173
			if( ( $ids = $view->param( 'id' ) ) === null )
174
			{
175
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
176
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
177
			}
178
179
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
180
			$search->setConditions( $search->compare( '==', 'rule.id', $ids ) );
181
			$items = $manager->search( $search );
182
183
			foreach( $items as $item )
184
			{
185
				$view->item = $item;
186
				parent::delete();
187
			}
188
189
			$manager->delete( $items->toArray() );
190
			$manager->commit();
191
192
			return $this->redirect( 'rule', 'search', null, 'delete' );
193
		}
194
		catch( \Exception $e )
195
		{
196
			$manager->rollback();
197
			$this->report( $e, 'delete' );
198
		}
199
200
		return $this->search();
201
	}
202
203
204
	/**
205
	 * Returns a single resource
206
	 *
207
	 * @return string|null HTML output
208
	 */
209
	public function get() : ?string
210
	{
211
		$view = $this->object()->data( $this->view() );
212
213
		try
214
		{
215
			if( ( $id = $view->param( 'id' ) ) === null )
216
			{
217
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
218
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
219
			}
220
221
			$manager = \Aimeos\MShop::create( $this->context(), 'rule' );
222
223
			$view->item = $manager->get( $id );
224
			$view->itemData = $this->toArray( $view->item );
225
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
226
			$view->itemBody = parent::get();
227
		}
228
		catch( \Exception $e )
229
		{
230
			$this->report( $e, 'get' );
231
		}
232
233
		return $this->render( $view );
234
	}
235
236
237
	/**
238
	 * Saves the data
239
	 *
240
	 * @return string|null HTML output
241
	 */
242
	public function save() : ?string
243
	{
244
		$view = $this->view();
245
246
		$manager = \Aimeos\MShop::create( $this->context(), 'rule' );
247
		$manager->begin();
248
249
		try
250
		{
251
			$item = $this->fromArray( $view->param( 'item', [] ) );
252
			$view->item = $item->getId() ? $item : $manager->save( $item );
253
			$view->itemBody = parent::save();
254
255
			$manager->save( clone $view->item );
256
			$manager->commit();
257
258
			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

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