Passed
Push — master ( 0ca7f5...355141 )
by Aimeos
07:40 queued 03:13
created

Standard::getOrderBaseItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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