Standard   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 537
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 134
dl 0
loc 537
rs 9.68
c 0
b 0
f 0
wmc 34

14 Methods

Rating   Name   Duplication   Size   Complexity  
A batch() 0 3 1
A create() 0 33 5
A copy() 0 25 3
A data() 0 4 1
A export() 0 30 4
A fromArray() 0 13 3
A delete() 0 36 4
A save() 0 25 3
A getSubClient() 0 73 1
A getSubClientNames() 0 35 1
A search() 0 46 2
A get() 0 25 3
A render() 0 24 1
A toArray() 0 12 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Subscription;
12
13
sprintf( 'sales' ); // for translation
14
sprintf( 'subscription' ); // for translation
15
16
17
/**
18
 * Default implementation of subscription 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/subscription/name
28
	 * Class name of the used subscription 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\Subscription\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Subscription\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/subscription/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 2018.04
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
		$view->itemSubparts = $this->getSubClientNames();
70
		return $view;
71
	}
72
73
74
	/**
75
	 * Batch update of a resource
76
	 *
77
	 * @return string|null Output to display
78
	 */
79
	public function batch() : ?string
80
	{
81
		return $this->batchBase( 'subscription' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('subscription') targeting Aimeos\Admin\JQAdm\Base::batchBase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
82
	}
83
84
85
	/**
86
	 * Copies a resource
87
	 *
88
	 * @return string|null HTML output
89
	 */
90
	public function copy() : ?string
91
	{
92
		$view = $this->object()->data( $this->view() );
93
		$context = $this->context();
94
95
		try
96
		{
97
			if( ( $id = $view->param( 'id' ) ) === null )
98
			{
99
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
100
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
101
			}
102
103
			$manager = \Aimeos\MShop::create( $context, 'subscription' );
104
105
			$view->item = $manager->get( $id, ['order', 'order/address', 'order/product'] );
106
			$view->itemData = $this->toArray( $view->item, true );
107
			$view->itemBody = parent::copy();
108
		}
109
		catch( \Exception $e )
110
		{
111
			$this->report( $e, 'copy' );
112
		}
113
114
		return $this->render( $view );
115
	}
116
117
118
	/**
119
	 * Creates a new resource
120
	 *
121
	 * @return string|null HTML output
122
	 */
123
	public function create() : ?string
124
	{
125
		$view = $this->object()->data( $this->view() );
126
		$context = $this->context();
127
128
		try
129
		{
130
			$data = $view->param( 'item', [] );
131
132
			if( !isset( $view->item ) ) {
133
				$view->item = \Aimeos\MShop::create( $context, 'subscription' )->create();
134
			}
135
136
			$manager = \Aimeos\MShop::create( $context, 'order' );
137
			$orderId = ( $view->item->getOrderId() ?: $view->param( 'item/subscription.orderid' ) );
138
139
			if( $orderId ) {
140
				$view->itemOrder = $manager->get( $orderId, ['order/address', 'order/product'] );
141
			} else {
142
				$view->itemOrder = $manager->create();
143
			}
144
145
			$data['subscription.siteid'] = $view->item->getSiteId();
146
147
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
148
			$view->itemBody = parent::create();
149
		}
150
		catch( \Exception $e )
151
		{
152
			$this->report( $e, 'create' );
153
		}
154
155
		return $this->render( $view );
156
	}
157
158
159
	/**
160
	 * Deletes a resource
161
	 *
162
	 * @return string|null HTML output
163
	 */
164
	public function delete() : ?string
165
	{
166
		$view = $this->view();
167
168
		$manager = \Aimeos\MShop::create( $this->context(), 'subscription' );
169
		$manager->begin();
170
171
		try
172
		{
173
			if( ( $ids = $view->param( 'id' ) ) === null )
174
			{
175
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
176
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
177
			}
178
179
			$search = $manager->filter()->add( 'subscription.id', '==', $ids )->slice( 0, count( (array) $ids ) );
180
			$items = $manager->search( $search );
181
182
			foreach( $items as $item )
183
			{
184
				$view->item = $item;
185
				parent::delete();
186
			}
187
188
			$manager->delete( $items );
189
			$manager->commit();
190
191
			return $this->redirect( 'subscription', 'search', null, 'delete' );
192
		}
193
		catch( \Exception $e )
194
		{
195
			$manager->rollback();
196
			$this->report( $e, 'delete' );
197
		}
198
199
		return $this->search();
200
	}
201
202
203
	/**
204
	 * Exports a resource
205
	 *
206
	 * @return string Admin output to display
207
	 */
208
	public function export() : ?string
209
	{
210
		$view = $this->view();
211
		$context = $this->context();
212
213
		try
214
		{
215
			$params = $this->storeFilter( $view->param(), 'subscription' );
216
			$msg = ['sitecode' => $context->locale()->getSiteItem()->getCode()];
217
218
			if( isset( $params['filter'] ) ) {
219
				$msg['filter'] = $this->getCriteriaConditions( (array) $params['filter'] );
220
			}
221
222
			if( isset( $params['sort'] ) ) {
223
				$msg['sort'] = (array) $params['sort'];
224
			}
225
226
			$mq = $context->queue( 'mq-admin', 'subscription-export' );
227
			$mq->add( json_encode( $msg ) );
228
229
			$msg = $context->translate( 'admin', 'Your export will be available in a few minutes for download' );
230
			$view->info = $view->get( 'info', [] ) + ['subscription-item' => $msg];
231
		}
232
		catch( \Exception $e )
233
		{
234
			$this->report( $e, 'export' );
235
		}
236
237
		return $this->search();
238
	}
239
240
241
	/**
242
	 * Returns a single resource
243
	 *
244
	 * @return string|null HTML output
245
	 */
246
	public function get() : ?string
247
	{
248
		$view = $this->object()->data( $this->view() );
249
		$context = $this->context();
250
251
		try
252
		{
253
			if( ( $id = $view->param( 'id' ) ) === null )
254
			{
255
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
256
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
257
			}
258
259
			$manager = \Aimeos\MShop::create( $context, 'subscription' );
260
261
			$view->item = $manager->get( $id, ['order', 'order/address', 'order/product'] );
262
			$view->itemData = $this->toArray( $view->item );
263
			$view->itemBody = parent::get();
264
		}
265
		catch( \Exception $e )
266
		{
267
			$this->report( $e, 'get' );
268
		}
269
270
		return $this->render( $view );
271
	}
272
273
274
	/**
275
	 * Saves the data
276
	 *
277
	 * @return string|null HTML output
278
	 */
279
	public function save() : ?string
280
	{
281
		$view = $this->view();
282
283
		$manager = \Aimeos\MShop::create( $this->context(), 'subscription' );
284
		$manager->begin();
285
286
		try
287
		{
288
			$item = $this->fromArray( $view->param( 'item', [] ) );
289
			$view->item = $item->getId() ? $item : $manager->save( $item );
290
			$view->itemBody = parent::save();
291
292
			$manager->save( clone $view->item );
293
			$manager->commit();
294
295
			return $this->redirect( 'subscription', $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

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