Standard   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 519
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 129
dl 0
loc 519
rs 9.76
c 0
b 0
f 0
wmc 33

13 Methods

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

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