Passed
Push — master ( 85cd01...c58492 )
by Aimeos
03:18
created

Standard::fromArray()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
nc 4
nop 1
dl 0
loc 32
rs 8.4444
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
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
	/**
28
	 * Adds the required data used in the template
29
	 *
30
	 * @param \Aimeos\MW\View\Iface $view View object
31
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
32
	 */
33
	public function data( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
34
	{
35
		$ds = DIRECTORY_SEPARATOR;
36
37
		$view->itemDecorators = $this->getClassNames( 'MShop' . $ds . 'Rule' . $ds . 'Provider' . $ds . 'Catalog' . $ds . 'Decorator' );
38
		$view->itemProviders = [
39
			'catalog' => $this->getClassNames( 'MShop' . $ds . 'Rule' . $ds . 'Provider' . $ds . 'Catalog' )
40
		];
41
42
		$view->itemSubparts = $this->getSubClientNames();
43
		$view->itemTypes = $this->getTypeItems();
44
45
		return $view;
46
	}
47
48
49
	/**
50
	 * Copies a resource
51
	 *
52
	 * @return string|null HTML output
53
	 */
54
	public function copy() : ?string
55
	{
56
		$view = $this->getObject()->data( $this->view() );
57
58
		try
59
		{
60
			if( ( $id = $view->param( 'id' ) ) === null )
61
			{
62
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
63
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
64
			}
65
66
			$manager = \Aimeos\MShop::create( $this->getContext(), 'rule' );
67
68
			$view->item = $manager->get( $id );
69
			$view->itemData = $this->toArray( $view->item, true );
70
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
71
			$view->itemBody = parent::copy();
72
		}
73
		catch( \Exception $e )
74
		{
75
			$this->report( $e, 'copy' );
76
		}
77
78
		return $this->render( $view );
79
	}
80
81
82
	/**
83
	 * Creates a new resource
84
	 *
85
	 * @return string|null HTML output
86
	 */
87
	public function create() : ?string
88
	{
89
		$view = $this->getObject()->data( $this->view() );
90
91
		try
92
		{
93
			$data = $view->param( 'item', [] );
94
95
			if( !isset( $view->item ) ) {
96
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'rule' )->create();
97
			}
98
99
			$data['rule.siteid'] = $view->item->getSiteId();
100
101
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
102
			$view->itemBody = parent::create();
103
		}
104
		catch( \Exception $e )
105
		{
106
			$this->report( $e, 'create' );
107
		}
108
109
		return $this->render( $view );
110
	}
111
112
113
	/**
114
	 * Deletes a resource
115
	 *
116
	 * @return string|null HTML output
117
	 */
118
	public function delete() : ?string
119
	{
120
		$view = $this->view();
121
122
		$manager = \Aimeos\MShop::create( $this->getContext(), 'rule' );
123
		$manager->begin();
124
125
		try
126
		{
127
			if( ( $ids = $view->param( 'id' ) ) === null )
128
			{
129
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
130
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
131
			}
132
133
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
134
			$search->setConditions( $search->compare( '==', 'rule.id', $ids ) );
135
			$items = $manager->search( $search );
136
137
			foreach( $items as $item )
138
			{
139
				$view->item = $item;
140
				parent::delete();
141
			}
142
143
			$manager->delete( $items->toArray() );
144
			$manager->commit();
145
146
			return $this->redirect( 'rule', 'search', null, 'delete' );
147
		}
148
		catch( \Exception $e )
149
		{
150
			$manager->rollback();
151
			$this->report( $e, 'delete' );
152
		}
153
154
		return $this->search();
155
	}
156
157
158
	/**
159
	 * Returns a single resource
160
	 *
161
	 * @return string|null HTML output
162
	 */
163
	public function get() : ?string
164
	{
165
		$view = $this->getObject()->data( $this->view() );
166
167
		try
168
		{
169
			if( ( $id = $view->param( 'id' ) ) === null )
170
			{
171
				$msg = $this->getContext()->translate( 'admin', 'Required parameter "%1$s" is missing' );
172
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
173
			}
174
175
			$manager = \Aimeos\MShop::create( $this->getContext(), 'rule' );
176
177
			$view->item = $manager->get( $id );
178
			$view->itemData = $this->toArray( $view->item );
179
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
180
			$view->itemBody = parent::get();
181
		}
182
		catch( \Exception $e )
183
		{
184
			$this->report( $e, 'get' );
185
		}
186
187
		return $this->render( $view );
188
	}
189
190
191
	/**
192
	 * Saves the data
193
	 *
194
	 * @return string|null HTML output
195
	 */
196
	public function save() : ?string
197
	{
198
		$view = $this->view();
199
200
		$manager = \Aimeos\MShop::create( $this->getContext(), 'rule' );
201
		$manager->begin();
202
203
		try
204
		{
205
			$item = $this->fromArray( $view->param( 'item', [] ) );
206
			$view->item = $item->getId() ? $item : $manager->save( $item );
207
			$view->itemBody = parent::save();
208
209
			$manager->save( clone $view->item );
210
			$manager->commit();
211
212
			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

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