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

Standard::search()   A

Complexity

Conditions 2
Paths 8

Size

Total Lines 45
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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

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