Completed
Push — master ( 626216...e78c82 )
by Aimeos
03:45
created

Standard::get()   A

Complexity

Conditions 4
Paths 12

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 12
nop 0
dl 0
loc 29
rs 9.7666
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
	 * Adds the required data used in the template
28
	 *
29
	 * @param \Aimeos\MW\View\Iface $view View object
30
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
31
	 */
32
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
33
	{
34
		$ds = DIRECTORY_SEPARATOR;
35
		$view->itemDecorators = $this->getClassNames( 'MShop' . $ds . 'Coupon' . $ds . 'Provider' . $ds . 'Decorator' );
36
		$view->itemProviders = $this->getClassNames( 'MShop' . $ds . 'Coupon' . $ds . 'Provider' );
37
		$view->itemSubparts = $this->getSubClientNames();
38
39
		return $view;
40
	}
41
42
43
	/**
44
	 * Copies a resource
45
	 *
46
	 * @return string|null HTML output
47
	 */
48
	public function copy() : ?string
49
	{
50
		$view = $this->getObject()->addData( $this->getView() );
51
52
		try
53
		{
54
			if( ( $id = $view->param( 'id' ) ) === null ) {
55
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
56
			}
57
58
			$manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' );
59
60
			$view->item = $manager->getItem( $id );
61
			$view->itemData = $this->toArray( $view->item, true );
62
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
63
			$view->itemBody = '';
64
65
			foreach( $this->getSubClients() as $idx => $client )
66
			{
67
				$view->tabindex = ++$idx + 1;
68
				$view->itemBody .= $client->copy();
69
			}
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(), 'coupon' )->createItem();
95
			}
96
97
			$data['coupon.siteid'] = $view->item->getSiteId();
98
99
			$view->itemData = $data;
100
			$view->itemBody = '';
101
102
			foreach( $this->getSubClients() as $idx => $client )
103
			{
104
				$view->tabindex = ++$idx + 1;
105
				$view->itemBody .= $client->create();
106
			}
107
		}
108
		catch( \Exception $e )
109
		{
110
			$this->report( $e, 'create' );
111
		}
112
113
		return $this->render( $view );
114
	}
115
116
117
	/**
118
	 * Deletes a resource
119
	 *
120
	 * @return string|null HTML output
121
	 */
122
	public function delete() : ?string
123
	{
124
		$view = $this->getView();
125
126
		$manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' );
127
		$manager->begin();
128
129
		try
130
		{
131
			if( ( $ids = $view->param( 'id' ) ) === null ) {
132
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
133
			}
134
135
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
136
			$search->setConditions( $search->compare( '==', 'coupon.id', $ids ) );
137
			$items = $manager->searchItems( $search );
138
139
			foreach( $items as $item )
140
			{
141
				$view->item = $item;
142
143
				foreach( $this->getSubClients() as $client ) {
144
					$client->delete();
145
				}
146
			}
147
148
			$manager->deleteItems( $items->toArray() );
149
			$manager->commit();
150
151
			$this->nextAction( $view, 'search', 'coupon', null, 'delete' );
152
			return null;
153
		}
154
		catch( \Exception $e )
155
		{
156
			$manager->rollback();
157
			$this->report( $e, 'save' );
158
		}
159
160
		return $this->search();
161
	}
162
163
164
	/**
165
	 * Returns a single resource
166
	 *
167
	 * @return string|null HTML output
168
	 */
169
	public function get() : ?string
170
	{
171
		$view = $this->getObject()->addData( $this->getView() );
172
173
		try
174
		{
175
			if( ( $id = $view->param( 'id' ) ) === null ) {
176
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
177
			}
178
179
			$manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' );
180
181
			$view->item = $manager->getItem( $id );
182
			$view->itemData = $this->toArray( $view->item );
183
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
184
			$view->itemBody = '';
185
186
			foreach( $this->getSubClients() as $idx => $client )
187
			{
188
				$view->tabindex = ++$idx + 1;
189
				$view->itemBody .= $client->get();
190
			}
191
		}
192
		catch( \Exception $e )
193
		{
194
			$this->report( $e, 'get' );
195
		}
196
197
		return $this->render( $view );
198
	}
199
200
201
	/**
202
	 * Saves the data
203
	 *
204
	 * @return string|null HTML output
205
	 */
206
	public function save() : ?string
207
	{
208
		$view = $this->getView();
209
210
		$manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' );
211
		$manager->begin();
212
213
		try
214
		{
215
			$item = $this->fromArray( $view->param( 'item', [] ) );
216
			$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

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