Completed
Push — master ( a13539...bd1b6d )
by Aimeos
04:27
created

Standard::delete()   A

Complexity

Conditions 5
Paths 25

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 25
nop 0
dl 0
loc 40
rs 9.2568
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Coupon;
12
13
sprintf( 'coupon' ); // for translation
14
15
16
/**
17
 * Default implementation of coupon JQAdm client.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
class Standard
23
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
24
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
25
{
26
	/**
27
	 * Copies a resource
28
	 *
29
	 * @return string|null HTML output
30
	 */
31
	public function copy() : ?string
32
	{
33
		$view = $this->getView();
34
		$context = $this->getContext();
35
36
		try
37
		{
38
			if( ( $id = $view->param( 'id' ) ) === null ) {
39
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
40
			}
41
42
			$manager = \Aimeos\MShop::create( $context, 'coupon' );
43
44
			$view->item = $manager->getItem( $id );
45
			$view->itemData = $this->toArray( $view->item, true );
46
			$view->itemSubparts = $this->getSubClientNames();
47
			$view->itemProviders = $this->getProviderNames();
48
			$view->itemDecorators = $this->getDecoratorNames();
49
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
50
			$view->itemBody = '';
51
52
			foreach( $this->getSubClients() as $idx => $client )
53
			{
54
				$view->tabindex = ++$idx + 1;
55
				$view->itemBody .= $client->copy();
56
			}
57
		}
58
		catch( \Exception $e )
59
		{
60
			$this->report( $e, 'copy' );
61
		}
62
63
		return $this->render( $view );
64
	}
65
66
67
	/**
68
	 * Creates a new resource
69
	 *
70
	 * @return string|null HTML output
71
	 */
72
	public function create() : ?string
73
	{
74
		$view = $this->getView();
75
		$context = $this->getContext();
76
77
		try
78
		{
79
			$data = $view->param( 'item', [] );
80
81
			if( !isset( $view->item ) ) {
82
				$view->item = \Aimeos\MShop::create( $context, 'coupon' )->createItem();
83
			}
84
85
			$data['coupon.siteid'] = $view->item->getSiteId();
86
87
			$view->itemSubparts = $this->getSubClientNames();
88
			$view->itemDecorators = $this->getDecoratorNames();
89
			$view->itemProviders = $this->getProviderNames();
90
			$view->itemData = $data;
91
			$view->itemBody = '';
92
93
			foreach( $this->getSubClients() as $idx => $client )
94
			{
95
				$view->tabindex = ++$idx + 1;
96
				$view->itemBody .= $client->create();
97
			}
98
		}
99
		catch( \Exception $e )
100
		{
101
			$this->report( $e, 'create' );
102
		}
103
104
		return $this->render( $view );
105
	}
106
107
108
	/**
109
	 * Deletes a resource
110
	 *
111
	 * @return string|null HTML output
112
	 */
113
	public function delete() : ?string
114
	{
115
		$view = $this->getView();
116
		$context = $this->getContext();
117
118
		$manager = \Aimeos\MShop::create( $context, 'coupon' );
119
		$manager->begin();
120
121
		try
122
		{
123
			if( ( $ids = $view->param( 'id' ) ) === null ) {
124
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
125
			}
126
127
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
128
			$search->setConditions( $search->compare( '==', 'coupon.id', $ids ) );
129
			$items = $manager->searchItems( $search );
130
131
			foreach( $items as $item )
132
			{
133
				$view->item = $item;
134
135
				foreach( $this->getSubClients() as $client ) {
136
					$client->delete();
137
				}
138
			}
139
140
			$manager->deleteItems( $items->toArray() );
141
			$manager->commit();
142
143
			$this->nextAction( $view, 'search', 'coupon', null, 'delete' );
144
			return null;
145
		}
146
		catch( \Exception $e )
147
		{
148
			$manager->rollback();
149
			$this->report( $e, 'save' );
150
		}
151
152
		return $this->search();
153
	}
154
155
156
	/**
157
	 * Returns a single resource
158
	 *
159
	 * @return string|null HTML output
160
	 */
161
	public function get() : ?string
162
	{
163
		$view = $this->getView();
164
		$context = $this->getContext();
165
166
		try
167
		{
168
			if( ( $id = $view->param( 'id' ) ) === null ) {
169
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
170
			}
171
172
			$manager = \Aimeos\MShop::create( $context, 'coupon' );
173
174
			$view->item = $manager->getItem( $id );
175
			$view->itemData = $this->toArray( $view->item );
176
			$view->itemSubparts = $this->getSubClientNames();
177
			$view->itemDecorators = $this->getDecoratorNames();
178
			$view->itemProviders = $this->getProviderNames();
179
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
180
			$view->itemBody = '';
181
182
			foreach( $this->getSubClients() as $idx => $client )
183
			{
184
				$view->tabindex = ++$idx + 1;
185
				$view->itemBody .= $client->get();
186
			}
187
		}
188
		catch( \Exception $e )
189
		{
190
			$this->report( $e, 'get' );
191
		}
192
193
		return $this->render( $view );
194
	}
195
196
197
	/**
198
	 * Saves the data
199
	 *
200
	 * @return string|null HTML output
201
	 */
202
	public function save() : ?string
203
	{
204
		$view = $this->getView();
205
		$context = $this->getContext();
206
207
		$manager = \Aimeos\MShop::create( $context, 'coupon' );
208
		$manager->begin();
209
210
		try
211
		{
212
			$item = $this->fromArray( $view->param( 'item', [] ) );
213
			$view->item = $item->getId() ? $item : $manager->saveItem( $item );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

213
			$view->item = $item->getId() ? $item : $manager->/** @scrutinizer ignore-call */ saveItem( $item );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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