Passed
Push — master ( 733ade...62c20a )
by Aimeos
03:19
created

Standard::export()   A

Complexity

Conditions 4
Paths 32

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 32
nop 0
dl 0
loc 31
rs 9.7
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/base' );
113
			$view->item = $manager->load( $id );
114
115
			$view->itemData = $this->toArray( $view->item, true );
116
			$view->itemBody = parent::copy();
117
		}
118
		catch( \Exception $e )
119
		{
120
			$this->report( $e, 'copy' );
121
		}
122
123
		return $this->render( $view );
124
	}
125
126
127
	/**
128
	 * Creates a new resource
129
	 *
130
	 * @return string|null HTML output
131
	 */
132
	public function create() : ?string
133
	{
134
		$view = $this->object()->data( $this->view() );
135
136
		try
137
		{
138
			$data = $view->param( 'item', [] );
139
140
			if( !isset( $view->item ) ) {
141
				$view->item = \Aimeos\MShop::create( $this->context(), 'order/base' )->create();
142
			}
143
144
			$data['order.siteid'] = $view->item->getSiteId();
145
146
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
147
			$view->itemBody = parent::create();
148
		}
149
		catch( \Exception $e )
150
		{
151
			$this->report( $e, 'create' );
152
		}
153
154
		return $this->render( $view );
155
	}
156
157
158
	/**
159
	 * Exports a resource
160
	 *
161
	 * @return string Admin output to display
162
	 */
163
	public function export() : ?string
164
	{
165
		$view = $this->view();
166
		$context = $this->context();
167
168
		try
169
		{
170
			$params = $this->storeFilter( $view->param(), 'order' );
171
			$msg = ['sitecode' => $context->locale()->getSiteItem()->getCode()];
172
173
			if( isset( $params['filter'] ) ) {
174
				$msg['filter'] = $this->getCriteriaConditions( (array) $params['filter'] );
175
			}
176
177
			if( isset( $params['sort'] ) ) {
178
				$msg['sort'] = (array) $params['sort'];
179
			}
180
181
			$queue = $view->param( 'queue', 'order-export' );
182
			$mq = $context->queue( 'mq-admin', $queue );
183
			$mq->add( json_encode( $msg ) );
184
185
			$msg = $context->translate( 'admin', 'Your export will be available in a few minutes for download' );
186
			$view->info = $view->get( 'info', [] ) + ['order-item' => $msg];
187
		}
188
		catch( \Exception $e )
189
		{
190
			$this->report( $e, 'export' );
191
		}
192
193
		return $this->search();
194
	}
195
196
197
	/**
198
	 * Returns a single resource
199
	 *
200
	 * @return string|null HTML output
201
	 */
202
	public function get() : ?string
203
	{
204
		$view = $this->object()->data( $this->view() );
205
206
		try
207
		{
208
			if( ( $id = $view->param( 'id' ) ) === null )
209
			{
210
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
211
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
212
			}
213
214
			$manager = \Aimeos\MShop::create( $this->context(), 'order/base' );
215
			$refs = ['order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service'];
216
217
			$view->item = $manager->get( $id, $refs );
218
			$view->itemData = $this->toArray( $view->item );
219
			$view->itemBody = parent::get();
220
		}
221
		catch( \Exception $e )
222
		{
223
			$this->report( $e, 'get' );
224
		}
225
226
		return $this->render( $view );
227
	}
228
229
230
	/**
231
	 * Saves the data
232
	 *
233
	 * @return string|null HTML output
234
	 */
235
	public function save() : ?string
236
	{
237
		$view = $this->view();
238
239
		$manager = \Aimeos\MShop::create( $this->context(), 'order/base' );
240
		$manager->begin();
241
242
		try
243
		{
244
			$item = $this->fromArray( $view->param( 'item', [] ) );
245
			$view->item = $item->getId() ? $item : $manager->store( clone $item );
246
			$view->itemBody = parent::save();
247
248
			$manager->store( clone $view->item );
249
			$manager->commit();
250
251
			return $this->redirect( 'order', $view->param( 'next' ), $view->item->getId(), 'save' );
252
		}
253
		catch( \Exception $e )
254
		{
255
			$manager->rollback();
256
			$this->report( $e, 'save' );
257
		}
258
259
		return $this->create();
260
	}
261
262
263
	/**
264
	 * Returns a list of resource according to the conditions
265
	 *
266
	 * @return string|null HTML output
267
	 */
268
	public function search() : ?string
269
	{
270
		$view = $this->view();
271
272
		try
273
		{
274
			$total = 0;
275
			$context = $this->context();
276
			$manager = \Aimeos\MShop::create( $context, 'order' );
277
			$params = $this->storeFilter( $view->param(), 'order' );
278
279
			$search = $manager->filter( false, true );
280
			$search->setSortations( [$search->sort( '-', 'order.id' )] );
281
			$search = $this->initCriteria( $search, $params );
282
283
			$view->items = $manager->search( $search, [], $total );
284
			$view->baseItems = $this->getOrderBaseItems( $view->items );
285
			$view->filterAttributes = $manager->getSearchAttributes( true );
286
			$view->filterOperators = $search->getOperators();
287
			$view->itemBody = parent::search();
288
			$view->total = $total;
289
		}
290
		catch( \Exception $e )
291
		{
292
			$this->report( $e, 'search' );
293
		}
294
295
		/** admin/jqadm/order/template-list
296
		 * Relative path to the HTML body template for the order list.
297
		 *
298
		 * The template file contains the HTML code and processing instructions
299
		 * to generate the result shown in the body of the frontend. The
300
		 * configuration string is the path to the template file relative
301
		 * to the templates directory (usually in admin/jqadm/templates).
302
		 *
303
		 * You can overwrite the template file configuration in extensions and
304
		 * provide alternative templates. These alternative templates should be
305
		 * named like the default one but with the string "default" replaced by
306
		 * an unique name. You may use the name of your project for this. If
307
		 * you've implemented an alternative client class as well, "default"
308
		 * should be replaced by the name of the new class.
309
		 *
310
		 * @param string Relative path to the template creating the HTML code
311
		 * @since 2016.04
312
		 * @category Developer
313
		 */
314
		$tplconf = 'admin/jqadm/order/template-list';
315
		$default = 'order/list';
316
317
		return $view->render( $view->config( $tplconf, $default ) );
318
	}
319
320
321
	/**
322
	 * Returns the sub-client given by its name.
323
	 *
324
	 * @param string $type Name of the client type
325
	 * @param string|null $name Name of the sub-client (Default if null)
326
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
327
	 */
328
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
329
	{
330
		/** admin/jqadm/order/decorators/excludes
331
		 * Excludes decorators added by the "common" option from the order JQAdm client
332
		 *
333
		 * Decorators extend the functionality of a class by adding new aspects
334
		 * (e.g. log what is currently done), executing the methods of the underlying
335
		 * class only in certain conditions (e.g. only for logged in users) or
336
		 * modify what is returned to the caller.
337
		 *
338
		 * This option allows you to remove a decorator added via
339
		 * "client/jqadm/common/decorators/default" before they are wrapped
340
		 * around the JQAdm client.
341
		 *
342
		 *  admin/jqadm/order/decorators/excludes = array( 'decorator1' )
343
		 *
344
		 * This would remove the decorator named "decorator1" from the list of
345
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
346
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
347
		 *
348
		 * @param array List of decorator names
349
		 * @since 2016.01
350
		 * @category Developer
351
		 * @see admin/jqadm/common/decorators/default
352
		 * @see admin/jqadm/order/decorators/global
353
		 * @see admin/jqadm/order/decorators/local
354
		 */
355
356
		/** admin/jqadm/order/decorators/global
357
		 * Adds a list of globally available decorators only to the order JQAdm client
358
		 *
359
		 * Decorators extend the functionality of a class by adding new aspects
360
		 * (e.g. log what is currently done), executing the methods of the underlying
361
		 * class only in certain conditions (e.g. only for logged in users) or
362
		 * modify what is returned to the caller.
363
		 *
364
		 * This option allows you to wrap global decorators
365
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
366
		 *
367
		 *  admin/jqadm/order/decorators/global = array( 'decorator1' )
368
		 *
369
		 * This would add the decorator named "decorator1" defined by
370
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
371
		 *
372
		 * @param array List of decorator names
373
		 * @since 2016.01
374
		 * @category Developer
375
		 * @see admin/jqadm/common/decorators/default
376
		 * @see admin/jqadm/order/decorators/excludes
377
		 * @see admin/jqadm/order/decorators/local
378
		 */
379
380
		/** admin/jqadm/order/decorators/local
381
		 * Adds a list of local decorators only to the order JQAdm client
382
		 *
383
		 * Decorators extend the functionality of a class by adding new aspects
384
		 * (e.g. log what is currently done), executing the methods of the underlying
385
		 * class only in certain conditions (e.g. only for logged in users) or
386
		 * modify what is returned to the caller.
387
		 *
388
		 * This option allows you to wrap local decorators
389
		 * ("\Aimeos\Admin\JQAdm\Order\Decorator\*") around the JQAdm client.
390
		 *
391
		 *  admin/jqadm/order/decorators/local = array( 'decorator2' )
392
		 *
393
		 * This would add the decorator named "decorator2" defined by
394
		 * "\Aimeos\Admin\JQAdm\Order\Decorator\Decorator2" only to the JQAdm client.
395
		 *
396
		 * @param array List of decorator names
397
		 * @since 2016.01
398
		 * @category Developer
399
		 * @see admin/jqadm/common/decorators/default
400
		 * @see admin/jqadm/order/decorators/excludes
401
		 * @see admin/jqadm/order/decorators/global
402
		 */
403
		return $this->createSubClient( 'order/' . $type, $name );
404
	}
405
406
407
	/**
408
	 * Returns the base order items (baskets) for the given order items (invoices)
409
	 *
410
	 * @param \Aimeos\Map $orderItems List of order items implementing \Aimeos\MShop\Order\Item\Iface
411
	 * @return \Aimeos\Map List of order base items implementing \Aimeos\MShop\Order\Item\Base\Iface
412
	 */
413
	protected function getOrderBaseItems( \Aimeos\Map $orderItems ) : \Aimeos\Map
414
	{
415
		$baseIds = $orderItems->getBaseId()->toArray();
416
		$manager = \Aimeos\MShop::create( $this->context(), 'order/base' );
417
418
		$search = $manager->filter( false, true )->slice( 0, count( $baseIds ) );
419
		$search->setConditions( $search->compare( '==', 'order.base.id', $baseIds ) );
420
421
		$domains = ['order/base/address', 'order/base/coupon', 'order/base/product', 'order/base/service'];
422
		return $manager->search( $search, $domains );
423
	}
424
425
426
	/**
427
	 * Returns the list of sub-client names configured for the client.
428
	 *
429
	 * @return array List of JQAdm client names
430
	 */
431
	protected function getSubClientNames() : array
432
	{
433
		/** admin/jqadm/order/subparts
434
		 * List of JQAdm sub-clients rendered within the order section
435
		 *
436
		 * The output of the frontend is composed of the code generated by the JQAdm
437
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
438
		 * that are responsible for rendering certain sub-parts of the output. The
439
		 * sub-clients can contain JQAdm clients themselves and therefore a
440
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
441
		 * the output that is placed inside the container of its parent.
442
		 *
443
		 * At first, always the JQAdm code generated by the parent is printed, then
444
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
445
		 * determines the order of the output of these sub-clients inside the parent
446
		 * container. If the configured list of clients is
447
		 *
448
		 *  array( "subclient1", "subclient2" )
449
		 *
450
		 * you can easily change the order of the output by reordering the subparts:
451
		 *
452
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
453
		 *
454
		 * You can also remove one or more parts if they shouldn't be rendered:
455
		 *
456
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
457
		 *
458
		 * As the clients only generates structural JQAdm, the layout defined via CSS
459
		 * should support adding, removing or reordering content by a fluid like
460
		 * design.
461
		 *
462
		 * @param array List of sub-client names
463
		 * @since 2016.01
464
		 * @category Developer
465
		 */
466
		return $this->context()->config()->get( 'admin/jqadm/order/subparts', [] );
467
	}
468
469
470
	/**
471
	 * Creates new and updates existing items using the data array
472
	 *
473
	 * @param array $data Data array
474
	 * @return \Aimeos\MShop\Order\Item\Base\Iface New order item object
475
	 */
476
	protected function fromArray( array $data ) : \Aimeos\MShop\Order\Item\Base\Iface
477
	{
478
		$manager = \Aimeos\MShop::create( $this->context(), 'order/base' );
479
		$attrManager = \Aimeos\MShop::create( $this->context(), 'order/base/service/attribute' );
480
		$domains = ['order/base/address', 'order/base/product', 'order/base/service'];
481
482
		if( isset( $data['order.base.id'] ) ) {
483
			$basket = $manager->get( $data['order.base.id'], $domains )->off();
484
		} else {
485
			$basket = $manager->create()->off();
486
		}
487
488
		$basket->fromArray( $data, true );
489
		$allowed = array_flip( [
490
			'order.base.product.statusdelivery',
491
			'order.base.product.statuspayment',
492
			'order.base.product.qtyopen',
493
			'order.base.product.timeframe',
494
			'order.base.product.notes',
495
		] );
496
497
		foreach( $basket->getProducts() as $pos => $product )
498
		{
499
			$list = array_intersect_key( $data['product'][$pos], $allowed );
500
			$product->fromArray( $list );
501
		}
502
503
		foreach( $basket->getAddresses() as $type => $addresses )
504
		{
505
			foreach( $addresses as $pos => $address )
506
			{
507
				if( isset( $data['address'][$type][$pos] ) ) {
508
					$list = (array) $data['address'][$type][$pos];
509
					$basket->addAddress( $address->fromArray( $list, true ), $type, $pos );
510
				} else {
511
					$basket->deleteAddress( $type, $pos );
512
				}
513
			}
514
		}
515
516
		foreach( $basket->getServices() as $type => $services )
517
		{
518
			foreach( $services as $index => $service )
519
			{
520
				$list = [];
521
				$attrItems = $service->getAttributeItems();
522
523
				if( isset( $data['service'][$type][$service->getServiceId()] ) )
524
				{
525
					foreach( (array) $data['service'][$type][$service->getServiceId()] as $key => $pair )
526
					{
527
						foreach( $pair as $pos => $value ) {
528
							$list[$pos][$key] = $value;
529
						}
530
					}
531
532
					foreach( $list as $array )
533
					{
534
						if( isset( $attrItems[$array['order.base.service.attribute.id']] ) )
535
						{
536
							$attrItem = $attrItems[$array['order.base.service.attribute.id']];
537
							unset( $attrItems[$array['order.base.service.attribute.id']] );
538
						}
539
						else
540
						{
541
							$attrItem = $attrManager->create();
542
						}
543
544
						$attrItem->fromArray( $array, true );
545
						$attrItem->setParentId( $service->getId() );
546
547
						$item = $attrManager->save( $attrItem );
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
548
					}
549
				}
550
551
				$attrManager->delete( $attrItems->toArray() );
552
			}
553
		}
554
555
		return $basket;
556
	}
557
558
559
	/**
560
	 * Constructs the data array for the view from the given item
561
	 *
562
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $item Order base item object
563
	 * @return string[] Multi-dimensional associative list of item data
564
	 */
565
	protected function toArray( \Aimeos\MShop\Order\Item\Base\Iface $item, bool $copy = false ) : array
566
	{
567
		$siteId = $this->context()->locale()->getSiteId();
568
		$data = $item->toArray( true );
569
570
		if( $item->getCustomerId() != '' )
571
		{
572
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
573
574
			try {
575
				$data += $manager->get( $item->getCustomerId() )->toArray();
576
			} catch( \Exception $e ) {};
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
577
		}
578
579
580
		if( $copy === true )
581
		{
582
			$data['order.base.siteid'] = $siteId;
583
			$data['order.base.id'] = '';
584
		}
585
586
		foreach( $item->getAddresses() as $type => $addresses )
587
		{
588
			foreach( $addresses as $pos => $addrItem )
589
			{
590
				$list = $addrItem->toArray( true );
591
592
				foreach( $list as $key => $value ) {
593
					$data['address'][$type][$pos][$key] = $value;
594
				}
595
596
				if( $copy === true )
597
				{
598
					$data['address'][$type][$pos]['order.base.address.siteid'] = $siteId;
599
					$data['address'][$type][$pos]['order.base.address.id'] = '';
600
				}
601
			}
602
		}
603
604
		if( $copy !== true )
605
		{
606
			foreach( $item->getServices() as $type => $services )
607
			{
608
				foreach( $services as $serviceItem )
609
				{
610
					$serviceId = $serviceItem->getServiceId();
611
612
					foreach( $serviceItem->getAttributeItems() as $attrItem )
613
					{
614
						foreach( $attrItem->toArray( true ) as $key => $value ) {
615
							$data['service'][$type][$serviceId][$key][] = $value;
616
						}
617
					}
618
				}
619
			}
620
		}
621
622
		foreach( $item->getProducts() as $pos => $productItem ) {
623
			$data['product'][$pos] = $productItem->toArray();
624
		}
625
626
		return $data;
627
	}
628
629
630
	/**
631
	 * Returns the rendered template including the view data
632
	 *
633
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
634
	 * @return string HTML output
635
	 */
636
	protected function render( \Aimeos\Base\View\Iface $view ) : string
637
	{
638
		/** admin/jqadm/order/template-item
639
		 * Relative path to the HTML body template for the order item.
640
		 *
641
		 * The template file contains the HTML code and processing instructions
642
		 * to generate the result shown in the body of the frontend. The
643
		 * configuration string is the path to the template file relative
644
		 * to the templates directory (usually in admin/jqadm/templates).
645
		 *
646
		 * You can overwrite the template file configuration in extensions and
647
		 * provide alternative templates. These alternative templates should be
648
		 * named like the default one but with the string "default" replaced by
649
		 * an unique name. You may use the name of your project for this. If
650
		 * you've implemented an alternative client class as well, "default"
651
		 * should be replaced by the name of the new class.
652
		 *
653
		 * @param string Relative path to the template creating the HTML code
654
		 * @since 2016.04
655
		 * @category Developer
656
		 */
657
		$tplconf = 'admin/jqadm/order/template-item';
658
		$default = 'order/item';
659
660
		return $view->render( $view->config( $tplconf, $default ) );
661
	}
662
}
663