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

Standard::getOrderBaseItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
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), 2017-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Order;
12
13
sprintf( 'sales' ); // for translation
14
sprintf( 'order' ); // for translation
15
16
17
/**
18
 * Default implementation of order 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/order/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\Order\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Order\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/order/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 2016.01
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
		$codes = [];
71
72
		foreach( $this->context()->config()->get( 'common/countries', [] ) as $code ) {
73
			$codes[$code] = $view->translate( 'country', $code );
74
		}
75
76
		asort( $codes );
77
78
		$view->itemSubparts = $this->getSubClientNames();
79
		$view->countries = $codes;
80
		return $view;
81
	}
82
83
84
	/**
85
	 * Batch update of a resource
86
	 *
87
	 * @return string|null Output to display
88
	 */
89
	public function batch() : ?string
90
	{
91
		return $this->batchBase( 'order' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('order') 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...
92
	}
93
94
95
	/**
96
	 * Copies a resource
97
	 *
98
	 * @return string|null HTML output
99
	 */
100
	public function copy() : ?string
101
	{
102
		$view = $this->object()->data( $this->view() );
103
104
		try
105
		{
106
			if( ( $id = $view->param( 'id' ) ) === null )
107
			{
108
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
109
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
110
			}
111
112
			$manager = \Aimeos\MShop::create( $this->context(), 'order' );
113
			$domains = ['order/address', 'order/coupon', 'order/product', 'order/service'];
114
115
			$view->item = $manager->get( $id, $domains );
116
			$view->itemData = $this->toArray( $view->item, true );
117
			$view->itemBody = parent::copy();
118
		}
119
		catch( \Exception $e )
120
		{
121
			$this->report( $e, 'copy' );
122
		}
123
124
		return $this->render( $view );
125
	}
126
127
128
	/**
129
	 * Creates a new resource
130
	 *
131
	 * @return string|null HTML output
132
	 */
133
	public function create() : ?string
134
	{
135
		$view = $this->object()->data( $this->view() );
136
137
		try
138
		{
139
			$data = $view->param( 'item', [] );
140
141
			if( !isset( $view->item ) ) {
142
				$view->item = \Aimeos\MShop::create( $this->context(), 'order' )->create();
143
			}
144
145
			$data['order.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
	 * Exports a resource
161
	 *
162
	 * @return string Admin output to display
163
	 */
164
	public function export() : ?string
165
	{
166
		$view = $this->view();
167
		$context = $this->context();
168
169
		try
170
		{
171
			$params = $this->storeFilter( $view->param(), 'order' );
172
			$msg = ['sitecode' => $context->locale()->getSiteItem()->getCode()];
173
174
			if( isset( $params['filter'] ) ) {
175
				$msg['filter'] = $this->getCriteriaConditions( (array) $params['filter'] );
176
			}
177
178
			if( isset( $params['sort'] ) ) {
179
				$msg['sort'] = (array) $params['sort'];
180
			}
181
182
			$queue = $view->param( 'queue', 'order-export' );
183
			$mq = $context->queue( 'mq-admin', $queue );
184
			$mq->add( json_encode( $msg ) );
185
186
			$msg = $context->translate( 'admin', 'Your export will be available in a few minutes for download' );
187
			$view->info = $view->get( 'info', [] ) + ['order-item' => $msg];
188
		}
189
		catch( \Exception $e )
190
		{
191
			$this->report( $e, 'export' );
192
		}
193
194
		return $this->search();
195
	}
196
197
198
	/**
199
	 * Returns a single resource
200
	 *
201
	 * @return string|null HTML output
202
	 */
203
	public function get() : ?string
204
	{
205
		$view = $this->object()->data( $this->view() );
206
207
		try
208
		{
209
			if( ( $id = $view->param( 'id' ) ) === null )
210
			{
211
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
212
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
213
			}
214
215
			$manager = \Aimeos\MShop::create( $this->context(), 'order' );
216
			$refs = ['order/address', 'order/coupon', 'order/product', 'order/service'];
217
218
			$view->item = $manager->get( $id, $refs );
219
			$view->itemData = $this->toArray( $view->item );
220
			$view->itemBody = parent::get();
221
		}
222
		catch( \Exception $e )
223
		{
224
			$this->report( $e, 'get' );
225
		}
226
227
		return $this->render( $view );
228
	}
229
230
231
	/**
232
	 * Saves the data
233
	 *
234
	 * @return string|null HTML output
235
	 */
236
	public function save() : ?string
237
	{
238
		$view = $this->view();
239
240
		$manager = \Aimeos\MShop::create( $this->context(), 'order' );
241
		$manager->begin();
242
243
		try
244
		{
245
			$item = $this->fromArray( $view->param( 'item', [] ) );
246
			$view->item = $item->getId() ? $item : $manager->save( clone $item );
247
			$view->itemBody = parent::save();
248
249
			$manager->save( clone $view->item );
250
			$manager->commit();
251
252
			return $this->redirect( 'order', $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

252
			return $this->redirect( 'order', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
253
		}
254
		catch( \Exception $e )
255
		{
256
			$manager->rollback();
257
			$this->report( $e, 'save' );
258
		}
259
260
		return $this->create();
261
	}
262
263
264
	/**
265
	 * Returns a list of resource according to the conditions
266
	 *
267
	 * @return string|null HTML output
268
	 */
269
	public function search() : ?string
270
	{
271
		$view = $this->view();
272
273
		try
274
		{
275
			$total = 0;
276
			$context = $this->context();
277
			$manager = \Aimeos\MShop::create( $context, 'order' );
278
			$params = $this->storeFilter( $view->param(), 'order' );
279
280
			$search = $manager->filter( false, true )->order( '-order.id' );
281
			$search = $this->initCriteria( $search, $params );
282
283
			$domains = ['order/address', 'order/coupon', 'order/product', 'order/service'];
284
285
			$view->items = $manager->search( $search, $domains, $total );
286
			$view->filterAttributes = $manager->getSearchAttributes( true );
287
			$view->filterOperators = $search->getOperators();
288
			$view->itemBody = parent::search();
289
			$view->total = $total;
290
		}
291
		catch( \Exception $e )
292
		{
293
			$this->report( $e, 'search' );
294
		}
295
296
		/** admin/jqadm/order/template-list
297
		 * Relative path to the HTML body template for the order list.
298
		 *
299
		 * The template file contains the HTML code and processing instructions
300
		 * to generate the result shown in the body of the frontend. The
301
		 * configuration string is the path to the template file relative
302
		 * to the templates directory (usually in templates/admin/jqadm).
303
		 *
304
		 * You can overwrite the template file configuration in extensions and
305
		 * provide alternative templates. These alternative templates should be
306
		 * named like the default one but with the string "default" replaced by
307
		 * an unique name. You may use the name of your project for this. If
308
		 * you've implemented an alternative client class as well, "default"
309
		 * should be replaced by the name of the new class.
310
		 *
311
		 * @param string Relative path to the template creating the HTML code
312
		 * @since 2016.04
313
		 * @category Developer
314
		 */
315
		$tplconf = 'admin/jqadm/order/template-list';
316
		$default = 'order/list';
317
318
		return $view->render( $view->config( $tplconf, $default ) );
319
	}
320
321
322
	/**
323
	 * Returns the sub-client given by its name.
324
	 *
325
	 * @param string $type Name of the client type
326
	 * @param string|null $name Name of the sub-client (Default if null)
327
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
328
	 */
329
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
330
	{
331
		/** admin/jqadm/order/decorators/excludes
332
		 * Excludes decorators added by the "common" option from the order 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 remove a decorator added via
340
		 * "client/jqadm/common/decorators/default" before they are wrapped
341
		 * around the JQAdm client.
342
		 *
343
		 *  admin/jqadm/order/decorators/excludes = array( 'decorator1' )
344
		 *
345
		 * This would remove the decorator named "decorator1" from the list of
346
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
347
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
348
		 *
349
		 * @param array List of decorator names
350
		 * @since 2016.01
351
		 * @category Developer
352
		 * @see admin/jqadm/common/decorators/default
353
		 * @see admin/jqadm/order/decorators/global
354
		 * @see admin/jqadm/order/decorators/local
355
		 */
356
357
		/** admin/jqadm/order/decorators/global
358
		 * Adds a list of globally available decorators only to the order JQAdm client
359
		 *
360
		 * Decorators extend the functionality of a class by adding new aspects
361
		 * (e.g. log what is currently done), executing the methods of the underlying
362
		 * class only in certain conditions (e.g. only for logged in users) or
363
		 * modify what is returned to the caller.
364
		 *
365
		 * This option allows you to wrap global decorators
366
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
367
		 *
368
		 *  admin/jqadm/order/decorators/global = array( 'decorator1' )
369
		 *
370
		 * This would add the decorator named "decorator1" defined by
371
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
372
		 *
373
		 * @param array List of decorator names
374
		 * @since 2016.01
375
		 * @category Developer
376
		 * @see admin/jqadm/common/decorators/default
377
		 * @see admin/jqadm/order/decorators/excludes
378
		 * @see admin/jqadm/order/decorators/local
379
		 */
380
381
		/** admin/jqadm/order/decorators/local
382
		 * Adds a list of local decorators only to the order JQAdm client
383
		 *
384
		 * Decorators extend the functionality of a class by adding new aspects
385
		 * (e.g. log what is currently done), executing the methods of the underlying
386
		 * class only in certain conditions (e.g. only for logged in users) or
387
		 * modify what is returned to the caller.
388
		 *
389
		 * This option allows you to wrap local decorators
390
		 * ("\Aimeos\Admin\JQAdm\Order\Decorator\*") around the JQAdm client.
391
		 *
392
		 *  admin/jqadm/order/decorators/local = array( 'decorator2' )
393
		 *
394
		 * This would add the decorator named "decorator2" defined by
395
		 * "\Aimeos\Admin\JQAdm\Order\Decorator\Decorator2" only to the JQAdm client.
396
		 *
397
		 * @param array List of decorator names
398
		 * @since 2016.01
399
		 * @category Developer
400
		 * @see admin/jqadm/common/decorators/default
401
		 * @see admin/jqadm/order/decorators/excludes
402
		 * @see admin/jqadm/order/decorators/global
403
		 */
404
		return $this->createSubClient( 'order/' . $type, $name );
405
	}
406
407
408
	/**
409
	 * Returns the list of sub-client names configured for the client.
410
	 *
411
	 * @return array List of JQAdm client names
412
	 */
413
	protected function getSubClientNames() : array
414
	{
415
		/** admin/jqadm/order/subparts
416
		 * List of JQAdm sub-clients rendered within the order section
417
		 *
418
		 * The output of the frontend is composed of the code generated by the JQAdm
419
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
420
		 * that are responsible for rendering certain sub-parts of the output. The
421
		 * sub-clients can contain JQAdm clients themselves and therefore a
422
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
423
		 * the output that is placed inside the container of its parent.
424
		 *
425
		 * At first, always the JQAdm code generated by the parent is printed, then
426
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
427
		 * determines the order of the output of these sub-clients inside the parent
428
		 * container. If the configured list of clients is
429
		 *
430
		 *  array( "subclient1", "subclient2" )
431
		 *
432
		 * you can easily change the order of the output by reordering the subparts:
433
		 *
434
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
435
		 *
436
		 * You can also remove one or more parts if they shouldn't be rendered:
437
		 *
438
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
439
		 *
440
		 * As the clients only generates structural JQAdm, the layout defined via CSS
441
		 * should support adding, removing or reordering content by a fluid like
442
		 * design.
443
		 *
444
		 * @param array List of sub-client names
445
		 * @since 2016.01
446
		 * @category Developer
447
		 */
448
		return $this->context()->config()->get( 'admin/jqadm/order/subparts', [] );
449
	}
450
451
452
	/**
453
	 * Creates new and updates existing items using the data array
454
	 *
455
	 * @param array $data Data array
456
	 * @return \Aimeos\MShop\Order\Item\Iface New order item object
457
	 */
458
	protected function fromArray( array $data ) : \Aimeos\MShop\Order\Item\Iface
459
	{
460
		$manager = \Aimeos\MShop::create( $this->context(), 'order' );
461
		$attrManager = \Aimeos\MShop::create( $this->context(), 'order/service/attribute' );
462
		$domains = ['order/address', 'order/product', 'order/service'];
463
464
		if( isset( $data['order.id'] ) ) {
465
			$basket = $manager->get( $data['order.id'], $domains )->off();
466
		} else {
467
			$basket = $manager->create()->off();
468
		}
469
470
		$basket->fromArray( $data, true );
471
		$allowed = array_flip( [
472
			'order.product.statusdelivery',
473
			'order.product.statuspayment',
474
			'order.product.qtyopen',
475
			'order.product.timeframe',
476
			'order.product.notes',
477
		] );
478
479
		foreach( $basket->getProducts() as $pos => $product )
480
		{
481
			$list = array_intersect_key( $data['product'][$pos], $allowed );
482
			$product->fromArray( $list );
483
		}
484
485
		foreach( $basket->getAddresses() as $type => $addresses )
486
		{
487
			foreach( $addresses as $pos => $address )
488
			{
489
				if( isset( $data['address'][$type][$pos] ) ) {
490
					$list = (array) $data['address'][$type][$pos];
491
					$basket->addAddress( $address->fromArray( $list, true ), $type, $pos );
492
				} else {
493
					$basket->deleteAddress( $type, $pos );
494
				}
495
			}
496
		}
497
498
		foreach( $basket->getServices() as $type => $services )
499
		{
500
			foreach( $services as $index => $service )
501
			{
502
				$list = [];
503
				$attrItems = $service->getAttributeItems();
504
505
				if( isset( $data['service'][$type][$service->getServiceId()] ) )
506
				{
507
					foreach( (array) $data['service'][$type][$service->getServiceId()] as $key => $pair )
508
					{
509
						foreach( $pair as $pos => $value ) {
510
							$list[$pos][$key] = $value;
511
						}
512
					}
513
514
					foreach( $list as $array )
515
					{
516
						if( isset( $attrItems[$array['order.service.attribute.id']] ) )
517
						{
518
							$attrItem = $attrItems[$array['order.service.attribute.id']];
519
							unset( $attrItems[$array['order.service.attribute.id']] );
520
						}
521
						else
522
						{
523
							$attrItem = $attrManager->create();
524
						}
525
526
						$attrItem->fromArray( $array, true );
527
						$attrItem->setParentId( $service->getId() );
528
529
						$item = $attrManager->save( $attrItem );
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
530
					}
531
				}
532
533
				$attrManager->delete( $attrItems->toArray() );
534
			}
535
		}
536
537
		return $basket;
538
	}
539
540
541
	/**
542
	 * Constructs the data array for the view from the given item
543
	 *
544
	 * @param \Aimeos\MShop\Order\Item\Iface $item Order item object
545
	 * @return string[] Multi-dimensional associative list of item data
546
	 */
547
	protected function toArray( \Aimeos\MShop\Order\Item\Iface $item, bool $copy = false ) : array
548
	{
549
		$siteId = $this->context()->locale()->getSiteId();
550
		$data = $item->toArray( true );
551
552
		if( $item->getCustomerId() != '' )
553
		{
554
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
555
556
			try {
557
				$data += $manager->get( $item->getCustomerId() )->toArray();
558
			} catch( \Exception $e ) {};
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
559
		}
560
561
562
		if( $copy === true )
563
		{
564
			$data['order.siteid'] = $siteId;
565
			$data['order.id'] = '';
566
		}
567
568
		foreach( $item->getAddresses() as $type => $addresses )
569
		{
570
			foreach( $addresses as $pos => $addrItem )
571
			{
572
				$list = $addrItem->toArray( true );
573
574
				foreach( $list as $key => $value ) {
575
					$data['address'][$type][$pos][$key] = $value;
576
				}
577
578
				if( $copy === true )
579
				{
580
					$data['address'][$type][$pos]['order.address.siteid'] = $siteId;
581
					$data['address'][$type][$pos]['order.address.id'] = '';
582
				}
583
			}
584
		}
585
586
		if( $copy !== true )
587
		{
588
			foreach( $item->getServices() as $type => $services )
589
			{
590
				foreach( $services as $serviceItem )
591
				{
592
					$serviceId = $serviceItem->getServiceId();
593
594
					foreach( $serviceItem->getAttributeItems() as $attrItem )
595
					{
596
						foreach( $attrItem->toArray( true ) as $key => $value ) {
597
							$data['service'][$type][$serviceId][$key][] = $value;
598
						}
599
					}
600
				}
601
			}
602
		}
603
604
		foreach( $item->getProducts() as $pos => $productItem ) {
605
			$data['product'][$pos] = $productItem->toArray();
606
		}
607
608
		return $data;
609
	}
610
611
612
	/**
613
	 * Returns the rendered template including the view data
614
	 *
615
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
616
	 * @return string HTML output
617
	 */
618
	protected function render( \Aimeos\Base\View\Iface $view ) : string
619
	{
620
		/** admin/jqadm/order/template-item
621
		 * Relative path to the HTML body template for the order item.
622
		 *
623
		 * The template file contains the HTML code and processing instructions
624
		 * to generate the result shown in the body of the frontend. The
625
		 * configuration string is the path to the template file relative
626
		 * to the templates directory (usually in templates/admin/jqadm).
627
		 *
628
		 * You can overwrite the template file configuration in extensions and
629
		 * provide alternative templates. These alternative templates should be
630
		 * named like the default one but with the string "default" replaced by
631
		 * an unique name. You may use the name of your project for this. If
632
		 * you've implemented an alternative client class as well, "default"
633
		 * should be replaced by the name of the new class.
634
		 *
635
		 * @param string Relative path to the template creating the HTML code
636
		 * @since 2016.04
637
		 * @category Developer
638
		 */
639
		$tplconf = 'admin/jqadm/order/template-item';
640
		$default = 'order/item';
641
642
		return $view->render( $view->config( $tplconf, $default ) );
643
	}
644
}
645