Passed
Push — master ( 8c606e...aebd94 )
by Aimeos
04:08
created

Standard::addData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 10
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 addData( \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()->addData( $this->getView() );
57
58
		try
59
		{
60
			if( ( $id = $view->param( 'id' ) ) === null ) {
61
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
62
			}
63
64
			$manager = \Aimeos\MShop::create( $this->getContext(), 'rule' );
65
66
			$view->item = $manager->get( $id );
67
			$view->itemData = $this->toArray( $view->item, true );
68
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
69
			$view->itemBody = parent::copy();
70
		}
71
		catch( \Exception $e )
72
		{
73
			$this->report( $e, 'copy' );
74
		}
75
76
		return $this->render( $view );
77
	}
78
79
80
	/**
81
	 * Creates a new resource
82
	 *
83
	 * @return string|null HTML output
84
	 */
85
	public function create() : ?string
86
	{
87
		$view = $this->getObject()->addData( $this->getView() );
88
89
		try
90
		{
91
			$data = $view->param( 'item', [] );
92
93
			if( !isset( $view->item ) ) {
94
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'rule' )->create();
95
			}
96
97
			$data['rule.siteid'] = $view->item->getSiteId();
98
99
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
100
			$view->itemBody = parent::create();
101
		}
102
		catch( \Exception $e )
103
		{
104
			$this->report( $e, 'create' );
105
		}
106
107
		return $this->render( $view );
108
	}
109
110
111
	/**
112
	 * Deletes a resource
113
	 *
114
	 * @return string|null HTML output
115
	 */
116
	public function delete() : ?string
117
	{
118
		$view = $this->getView();
119
120
		$manager = \Aimeos\MShop::create( $this->getContext(), 'rule' );
121
		$manager->begin();
122
123
		try
124
		{
125
			if( ( $ids = $view->param( 'id' ) ) === null ) {
126
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
127
			}
128
129
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
130
			$search->setConditions( $search->compare( '==', 'rule.id', $ids ) );
131
			$items = $manager->search( $search );
132
133
			foreach( $items as $item )
134
			{
135
				$view->item = $item;
136
				parent::delete();
137
			}
138
139
			$manager->delete( $items->toArray() );
140
			$manager->commit();
141
142
			return $this->redirect( 'rule', 'search', null, 'delete' );
143
		}
144
		catch( \Exception $e )
145
		{
146
			$manager->rollback();
147
			$this->report( $e, 'delete' );
148
		}
149
150
		return $this->search();
151
	}
152
153
154
	/**
155
	 * Returns a single resource
156
	 *
157
	 * @return string|null HTML output
158
	 */
159
	public function get() : ?string
160
	{
161
		$view = $this->getObject()->addData( $this->getView() );
162
163
		try
164
		{
165
			if( ( $id = $view->param( 'id' ) ) === null ) {
166
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
167
			}
168
169
			$manager = \Aimeos\MShop::create( $this->getContext(), 'rule' );
170
171
			$view->item = $manager->get( $id );
172
			$view->itemData = $this->toArray( $view->item );
173
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
174
			$view->itemBody = parent::get();
175
		}
176
		catch( \Exception $e )
177
		{
178
			$this->report( $e, 'get' );
179
		}
180
181
		return $this->render( $view );
182
	}
183
184
185
	/**
186
	 * Saves the data
187
	 *
188
	 * @return string|null HTML output
189
	 */
190
	public function save() : ?string
191
	{
192
		$view = $this->getView();
193
194
		$manager = \Aimeos\MShop::create( $this->getContext(), 'rule' );
195
		$manager->begin();
196
197
		try
198
		{
199
			$item = $this->fromArray( $view->param( 'item', [] ) );
200
			$view->item = $item->getId() ? $item : $manager->save( $item );
201
			$view->itemBody = parent::save();
202
203
			$manager->save( clone $view->item );
204
			$manager->commit();
205
206
			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

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