Completed
Push — master ( c0744c...d846fe )
by Aimeos
20:51
created

Standard::saveItem()   D

Complexity

Conditions 9
Paths 68

Size

Total Lines 179
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 48
nc 68
nop 2
dl 0
loc 179
rs 4.8196
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 * @package MShop
8
 * @subpackage Order
9
 */
10
11
12
namespace Aimeos\MShop\Order\Manager\Base;
13
14
15
/**
16
 * Default implementation for order base manager.
17
 *
18
 * @package MShop
19
 * @subpackage Order
20
 */
21
class Standard extends Base
22
{
23
	private $searchConfig = array(
24
		'order.base.id' => array(
25
			'code' => 'order.base.id',
26
			'internalcode' => 'mordba."id"',
27
			'internaldeps' => array( 'LEFT JOIN "mshop_order_base" AS mordba ON ( mord."baseid" = mordba."id" )' ),
28
			'label' => 'Order ID',
29
			'type' => 'integer',
30
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
31
			'public' => false,
32
		),
33
		'order.base.siteid' => array(
34
			'code' => 'order.base.siteid',
35
			'internalcode' => 'mordba."siteid"',
36
			'label' => 'Order site ID',
37
			'type' => 'integer',
38
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
39
			'public' => false,
40
		),
41
		'order.base.sitecode' => array(
42
			'code' => 'order.base.sitecode',
43
			'internalcode' => 'mordba."sitecode"',
44
			'label' => 'Order site code',
45
			'type' => 'string',
46
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
47
			'public' => false,
48
		),
49
		'order.base.customerid' => array(
50
			'code' => 'order.base.customerid',
51
			'internalcode' => 'mordba."customerid"',
52
			'label' => 'Order customer ID',
53
			'type' => 'string',
54
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
55
		),
56
		'order.base.languageid' => array(
57
			'code' => 'order.base.languageid',
58
			'internalcode' => 'mordba."langid"',
59
			'label' => 'Order language code',
60
			'type' => 'string',
61
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
62
		),
63
		'order.base.currencyid' => array(
64
			'code' => 'order.base.currencyid',
65
			'internalcode' => 'mordba."currencyid"',
66
			'label' => 'Order currencyid code',
67
			'type' => 'string',
68
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
69
		),
70
		'order.base.price' => array(
71
			'code' => 'order.base.price',
72
			'internalcode' => 'mordba."price"',
73
			'label' => 'Order price amount',
74
			'type' => 'string',
75
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
76
		),
77
		'order.base.costs' => array(
78
			'code' => 'order.base.costs',
79
			'internalcode' => 'mordba."costs"',
80
			'label' => 'Order shipping amount',
81
			'type' => 'string',
82
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
83
		),
84
		'order.base.rebate' => array(
85
			'code' => 'order.base.rebate',
86
			'internalcode' => 'mordba."rebate"',
87
			'label' => 'Order rebate amount',
88
			'type' => 'string',
89
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
90
		),
91
		'order.base.taxvalue' => array(
92
			'code' => 'order.base.taxvalue',
93
			'internalcode' => 'mordba."tax"',
94
			'label' => 'Order tax amount',
95
			'type' => 'string',
96
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
97
		),
98
		'order.base.taxflag' => array(
99
			'code' => 'order.base.taxflag',
100
			'internalcode' => 'mordba."taxflag"',
101
			'label' => 'Order tax flag (0=net, 1=gross)',
102
			'type' => 'string',
103
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
104
		),
105
		'order.base.comment' => array(
106
			'code' => 'order.base.comment',
107
			'internalcode' => 'mordba."comment"',
108
			'label' => 'Order comment',
109
			'type' => 'string',
110
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
111
		),
112
		'order.base.status' => array(
113
			'code' => 'order.base.status',
114
			'internalcode' => 'mordba."status"',
115
			'label' => 'Order subscription status',
116
			'type' => 'integer',
117
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
118
		),
119
		'order.base.ctime' => array(
120
			'code' => 'order.base.ctime',
121
			'internalcode' => 'mordba."ctime"',
122
			'label' => 'Order create date/time',
123
			'type' => 'datetime',
124
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
125
			'public' => false,
126
		),
127
		'order.base.mtime' => array(
128
			'code' => 'order.base.mtime',
129
			'internalcode' => 'mordba."mtime"',
130
			'label' => 'Order modify date/time',
131
			'type' => 'datetime',
132
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
133
			'public' => false,
134
		),
135
		'order.base.editor' => array(
136
			'code' => 'order.base.editor',
137
			'internalcode' => 'mordba."editor"',
138
			'label' => 'Order editor',
139
			'type' => 'string',
140
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
141
			'public' => false,
142
		),
143
	);
144
145
146
	/**
147
	 * Initializes the object.
148
	 *
149
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
150
	 */
151
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
152
	{
153
		parent::__construct( $context );
154
		$this->setResourceName( 'db-order' );
155
	}
156
157
158
	/**
159
	 * Counts the number items that are available for the values of the given key.
160
	 *
161
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria
162
	 * @param string $key Search key to aggregate items for
163
	 * @return array List of the search keys as key and the number of counted items as value
164
	 * @todo 2018.01 Add optional parameters to interface
165
	 */
166
	public function aggregate( \Aimeos\MW\Criteria\Iface $search, $key, $value = null, $type = null )
167
	{
168
		/** mshop/order/manager/base/standard/aggregate/mysql
169
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
170
		 *
171
		 * @see mshop/order/manager/base/standard/aggregate/ansi
172
		 */
173
174
		/** mshop/order/manager/base/standard/aggregate/ansi
175
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
176
		 *
177
		 * Groups all records by the values in the key column and counts their
178
		 * occurence. The matched records can be limited by the given criteria
179
		 * from the order database. The records must be from one of the sites
180
		 * that are configured via the context item. If the current site is part
181
		 * of a tree of sites, the statement can count all records from the
182
		 * current site and the complete sub-tree of sites.
183
		 *
184
		 * As the records can normally be limited by criteria from sub-managers,
185
		 * their tables must be joined in the SQL context. This is done by
186
		 * using the "internaldeps" property from the definition of the ID
187
		 * column of the sub-managers. These internal dependencies specify
188
		 * the JOIN between the tables and the used columns for joining. The
189
		 * ":joins" placeholder is then replaced by the JOIN strings from
190
		 * the sub-managers.
191
		 *
192
		 * To limit the records matched, conditions can be added to the given
193
		 * criteria object. It can contain comparisons like column names that
194
		 * must match specific values which can be combined by AND, OR or NOT
195
		 * operators. The resulting string of SQL conditions replaces the
196
		 * ":cond" placeholder before the statement is sent to the database
197
		 * server.
198
		 *
199
		 * This statement doesn't return any records. Instead, it returns pairs
200
		 * of the different values found in the key column together with the
201
		 * number of records that have been found for that key values.
202
		 *
203
		 * The SQL statement should conform to the ANSI standard to be
204
		 * compatible with most relational database systems. This also
205
		 * includes using double quotes for table and column names.
206
		 *
207
		 * @param string SQL statement for aggregating order items
208
		 * @since 2014.09
209
		 * @category Developer
210
		 * @see mshop/order/manager/base/standard/insert/ansi
211
		 * @see mshop/order/manager/base/standard/update/ansi
212
		 * @see mshop/order/manager/base/standard/newid/ansi
213
		 * @see mshop/order/manager/base/standard/delete/ansi
214
		 * @see mshop/order/manager/base/standard/search/ansi
215
		 * @see mshop/order/manager/base/standard/count/ansi
216
		 */
217
218
		/** mshop/order/manager/base/standard/aggregateavg/mysql
219
		 * Computes the average of all values grouped by the key column and matched by the given criteria
220
		 *
221
		 * @param string SQL statement for aggregating the order base items and computing the average value
222
		 * @since 2017.10
223
		 * @category Developer
224
		 * @see mshop/order/manager/base/standard/aggregateavg/ansi
225
		 * @see mshop/order/manager/base/standard/aggregate/mysql
226
		 */
227
228
		/** mshop/order/manager/base/standard/aggregateavg/ansi
229
		 * Computes the average of all values grouped by the key column and matched by the given criteria
230
		 *
231
		 * @param string SQL statement for aggregating the order base items and computing the average value
232
		 * @since 2017.10
233
		 * @category Developer
234
		 * @see mshop/order/manager/base/standard/aggregate/ansi
235
		 */
236
237
		/** mshop/order/manager/base/standard/aggregatesum/mysql
238
		 * Computes the sum of all values grouped by the key column and matched by the given criteria
239
		 *
240
		 * @param string SQL statement for aggregating the order base items and computing the sum
241
		 * @since 2017.10
242
		 * @category Developer
243
		 * @see mshop/order/manager/base/standard/aggregatesum/ansi
244
		 * @see mshop/order/manager/base/standard/aggregate/mysql
245
		 */
246
247
		/** mshop/order/manager/base/standard/aggregatesum/ansi
248
		 * Computes the sum of all values grouped by the key column and matched by the given criteria
249
		 *
250
		 * @param string SQL statement for aggregating the order base items and computing the sum
251
		 * @since 2017.10
252
		 * @category Developer
253
		 * @see mshop/order/manager/base/standard/aggregate/ansi
254
		 */
255
256
		$cfgkey = 'mshop/order/manager/base/standard/aggregate' . $type;
257
		return $this->aggregateBase( $search, $key, $cfgkey, array( 'order.base' ), $value );
258
	}
259
260
261
	/**
262
	 * Removes old entries from the storage.
263
	 *
264
	 * @param integer[] $siteids List of IDs for sites whose entries should be deleted
265
	 */
266
	public function cleanup( array $siteids )
267
	{
268
		$path = 'mshop/order/manager/base/submanagers';
269
		$default = array( 'address', 'coupon', 'product', 'service' );
270
271
		foreach( $this->getContext()->getConfig()->get( $path, $default ) as $domain ) {
272
			$this->getObject()->getSubManager( $domain )->cleanup( $siteids );
273
		}
274
275
		$this->cleanupBase( $siteids, 'mshop/order/manager/base/standard/delete' );
276
	}
277
278
279
	/**
280
	 * Returns a new and empty order base item (shopping basket).
281
	 *
282
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base object
283
	 */
284
	public function createItem()
285
	{
286
		$context = $this->getContext();
287
		$priceManager = \Aimeos\MShop\Factory::createManager( $context, 'price' );
288
		$values = array( 'order.base.siteid' => $context->getLocale()->getSiteId() );
289
290
		$base = $this->createItemBase( $priceManager->createItem(), clone $context->getLocale(), $values );
291
292
		\Aimeos\MShop\Factory::createManager( $context, 'plugin' )->register( $base, 'order' );
293
294
		return $base;
295
	}
296
297
298
	/**
299
	 * Creates a search object
300
	 *
301
	 * @param boolean $default Add default criteria
302
	 * @return \Aimeos\MW\Criteria\Iface
303
	 */
304
	public function createSearch( $default = false )
305
	{
306
		$search = parent::createSearch( $default );
307
308
		if( $default !== false )
309
		{
310
			$userId = $this->getContext()->getUserId();
311
			$expr = [
312
				$search->compare( '==', 'order.base.customerid', $userId ),
313
				$search->getConditions(),
314
			];
315
			$search->setConditions( $search->combine( '&&', $expr ) );
316
		}
317
318
		return $search;
319
	}
320
321
322
	/**
323
	 * Removes multiple items specified by ids in the array.
324
	 *
325
	 * @param array $ids List of IDs
326
	 */
327
	public function deleteItems( array $ids )
328
	{
329
		/** mshop/order/manager/base/standard/delete/mysql
330
		 * Deletes the items matched by the given IDs from the database
331
		 *
332
		 * @see mshop/order/manager/base/standard/delete/ansi
333
		 */
334
335
		/** mshop/order/manager/base/standard/delete/ansi
336
		 * Deletes the items matched by the given IDs from the database
337
		 *
338
		 * Removes the records specified by the given IDs from the order database.
339
		 * The records must be from the site that is configured via the
340
		 * context item.
341
		 *
342
		 * The ":cond" placeholder is replaced by the name of the ID column and
343
		 * the given ID or list of IDs while the site ID is bound to the question
344
		 * mark.
345
		 *
346
		 * The SQL statement should conform to the ANSI standard to be
347
		 * compatible with most relational database systems. This also
348
		 * includes using double quotes for table and column names.
349
		 *
350
		 * @param string SQL statement for deleting items
351
		 * @since 2014.03
352
		 * @category Developer
353
		 * @see mshop/order/manager/base/standard/insert/ansi
354
		 * @see mshop/order/manager/base/standard/update/ansi
355
		 * @see mshop/order/manager/base/standard/newid/ansi
356
		 * @see mshop/order/manager/base/standard/search/ansi
357
		 * @see mshop/order/manager/base/standard/count/ansi
358
		 */
359
		$path = 'mshop/order/manager/base/standard/delete';
360
		$this->deleteItemsBase( $ids, $path );
361
	}
362
363
364
	/**
365
	 * Returns the order base item specified by the given ID.
366
	 *
367
	 * @param integer $id Unique id of the order base
368
	 * @param string[] $ref List of domains to fetch list items and referenced items for
369
	 * @param boolean $default Add default criteria
370
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Returns Order base item of the given id
371
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
372
	 */
373
	public function getItem( $id, array $ref = [], $default = false )
374
	{
375
		return $this->getItemBase( 'order.base.id', $id, $ref, $default );
376
	}
377
378
379
	/**
380
	 * Returns the available manager types
381
	 *
382
	 * @param boolean $withsub Return also the resource type of sub-managers if true
383
	 * @return array Type of the manager and submanagers, subtypes are separated by slashes
384
	 */
385
	public function getResourceType( $withsub = true )
386
	{
387
		$path = 'mshop/order/manager/base/submanagers';
388
389
		return $this->getResourceTypeBase( 'order/base', $path, array( 'address', 'coupon', 'product', 'service' ), $withsub );
390
	}
391
392
393
	/**
394
	 * Returns the attributes that can be used for searching.
395
	 *
396
	 * @param boolean $withsub Return also attributes of sub-managers if true
397
	 * @return array List of attribute items implementing \Aimeos\MW\Criteria\Attribute\Iface
398
	 */
399
	public function getSearchAttributes( $withsub = true )
400
	{
401
		/** mshop/order/manager/base/submanagers
402
		 * List of manager names that can be instantiated by the order base manager
403
		 *
404
		 * Managers provide a generic interface to the underlying storage.
405
		 * Each manager has or can have sub-managers caring about particular
406
		 * aspects. Each of these sub-managers can be instantiated by its
407
		 * parent manager using the getSubManager() method.
408
		 *
409
		 * The search keys from sub-managers can be normally used in the
410
		 * manager as well. It allows you to search for items of the manager
411
		 * using the search keys of the sub-managers to further limit the
412
		 * retrieved list of items.
413
		 *
414
		 * @param array List of sub-manager names
415
		 * @since 2014.03
416
		 * @category Developer
417
		 */
418
		$path = 'mshop/order/manager/base/submanagers';
419
		$default = array( 'address', 'coupon', 'product', 'service' );
420
421
		return $this->getSearchAttributesBase( $this->searchConfig, $path, $default, $withsub );
422
	}
423
424
425
	/**
426
	 * Returns a new manager for order base extensions.
427
	 *
428
	 * @param string $manager Name of the sub manager type in lower case
429
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
430
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g address, coupon, product, service, etc.
431
	 */
432
	public function getSubManager( $manager, $name = null )
433
	{
434
		/** mshop/order/manager/base/name
435
		 * Class name of the used order base manager implementation
436
		 *
437
		 * Each default order base manager can be replaced by an alternative imlementation.
438
		 * To use this implementation, you have to set the last part of the class
439
		 * name as configuration value so the manager factory knows which class it
440
		 * has to instantiate.
441
		 *
442
		 * For example, if the name of the default class is
443
		 *
444
		 *  \Aimeos\MShop\Order\Manager\Base\Standard
445
		 *
446
		 * and you want to replace it with your own version named
447
		 *
448
		 *  \Aimeos\MShop\Order\Manager\Base\Mybase
449
		 *
450
		 * then you have to set the this configuration option:
451
		 *
452
		 *  mshop/order/manager/base/name = Mybase
453
		 *
454
		 * The value is the last part of your own class name and it's case sensitive,
455
		 * so take care that the configuration value is exactly named like the last
456
		 * part of the class name.
457
		 *
458
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
459
		 * characters are possible! You should always start the last part of the class
460
		 * name with an upper case character and continue only with lower case characters
461
		 * or numbers. Avoid chamel case names like "MyBase"!
462
		 *
463
		 * @param string Last part of the class name
464
		 * @since 2014.03
465
		 * @category Developer
466
		 */
467
468
		/** mshop/order/manager/base/decorators/excludes
469
		 * Excludes decorators added by the "common" option from the order base manager
470
		 *
471
		 * Decorators extend the functionality of a class by adding new aspects
472
		 * (e.g. log what is currently done), executing the methods of the underlying
473
		 * class only in certain conditions (e.g. only for logged in users) or
474
		 * modify what is returned to the caller.
475
		 *
476
		 * This option allows you to remove a decorator added via
477
		 * "mshop/common/manager/decorators/default" before they are wrapped
478
		 * around the order base manager.
479
		 *
480
		 *  mshop/order/manager/base/decorators/excludes = array( 'decorator1' )
481
		 *
482
		 * This would remove the decorator named "decorator1" from the list of
483
		 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
484
		 * "mshop/common/manager/decorators/default" for the order base manager.
485
		 *
486
		 * @param array List of decorator names
487
		 * @since 2014.03
488
		 * @category Developer
489
		 * @see mshop/common/manager/decorators/default
490
		 * @see mshop/order/manager/base/decorators/global
491
		 * @see mshop/order/manager/base/decorators/local
492
		 */
493
494
		/** mshop/order/manager/base/decorators/global
495
		 * Adds a list of globally available decorators only to the order base manager
496
		 *
497
		 * Decorators extend the functionality of a class by adding new aspects
498
		 * (e.g. log what is currently done), executing the methods of the underlying
499
		 * class only in certain conditions (e.g. only for logged in users) or
500
		 * modify what is returned to the caller.
501
		 *
502
		 * This option allows you to wrap global decorators
503
		 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the order base manager.
504
		 *
505
		 *  mshop/order/manager/base/decorators/global = array( 'decorator1' )
506
		 *
507
		 * This would add the decorator named "decorator1" defined by
508
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the order controller.
509
		 *
510
		 * @param array List of decorator names
511
		 * @since 2014.03
512
		 * @category Developer
513
		 * @see mshop/common/manager/decorators/default
514
		 * @see mshop/order/manager/base/decorators/excludes
515
		 * @see mshop/order/manager/base/decorators/local
516
		 */
517
518
		/** mshop/order/manager/base/decorators/local
519
		 * Adds a list of local decorators only to the order base manager
520
		 *
521
		 * Decorators extend the functionality of a class by adding new aspects
522
		 * (e.g. log what is currently done), executing the methods of the underlying
523
		 * class only in certain conditions (e.g. only for logged in users) or
524
		 * modify what is returned to the caller.
525
		 *
526
		 * This option allows you to wrap local decorators
527
		 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the order base manager.
528
		 *
529
		 *  mshop/order/manager/base/decorators/local = array( 'decorator2' )
530
		 *
531
		 * This would add the decorator named "decorator2" defined by
532
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator2" only to the order
533
		 * controller.
534
		 *
535
		 * @param array List of decorator names
536
		 * @since 2014.03
537
		 * @category Developer
538
		 * @see mshop/common/manager/decorators/default
539
		 * @see mshop/order/manager/base/decorators/excludes
540
		 * @see mshop/order/manager/base/decorators/global
541
		 */
542
543
		return $this->getSubManagerBase( 'order', 'base/' . $manager, $name );
544
	}
545
546
547
	/**
548
	 * Adds or updates an order base item in the storage.
549
	 *
550
	 * @param \Aimeos\MShop\Common\Item\Iface $item Order base object (sub-items are not saved)
551
	 * @param boolean $fetch True if the new ID should be returned in the item
552
	 * @return \Aimeos\MShop\Common\Item\Iface $item Updated item including the generated ID
553
	 */
554
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
555
	{
556
		$iface = '\\Aimeos\\MShop\\Order\\Item\\Base\\Iface';
557
		if( !( $item instanceof $iface ) ) {
558
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Object is not of required type "%1$s"', $iface ) );
559
		}
560
561
		if( !$item->isModified() && !$item->getLocale()->isModified() ) {
562
			return $item;
563
		}
564
565
		$context = $this->getContext();
566
567
		$dbm = $context->getDatabaseManager();
568
		$dbname = $this->getResourceName();
569
		$conn = $dbm->acquire( $dbname );
570
571
		try
572
		{
573
			$id = $item->getId();
574
			$date = date( 'Y-m-d H:i:s' );
575
576
			if( $id === null )
577
			{
578
				/** mshop/order/manager/base/standard/insert/mysql
579
				 * Inserts a new order record into the database table
580
				 *
581
				 * @see mshop/order/manager/base/standard/insert/ansi
582
				 */
583
584
				/** mshop/order/manager/base/standard/insert/ansi
585
				 * Inserts a new order record into the database table
586
				 *
587
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
588
				 * the database and the newly created ID retrieved afterwards
589
				 * using the "newid" SQL statement.
590
				 *
591
				 * The SQL statement must be a string suitable for being used as
592
				 * prepared statement. It must include question marks for binding
593
				 * the values from the order item to the statement before they are
594
				 * sent to the database server. The number of question marks must
595
				 * be the same as the number of columns listed in the INSERT
596
				 * statement. The order of the columns must correspond to the
597
				 * order in the saveItems() method, so the correct values are
598
				 * bound to the columns.
599
				 *
600
				 * The SQL statement should conform to the ANSI standard to be
601
				 * compatible with most relational database systems. This also
602
				 * includes using double quotes for table and column names.
603
				 *
604
				 * @param string SQL statement for inserting records
605
				 * @since 2014.03
606
				 * @category Developer
607
				 * @see mshop/order/manager/base/standard/update/ansi
608
				 * @see mshop/order/manager/base/standard/newid/ansi
609
				 * @see mshop/order/manager/base/standard/delete/ansi
610
				 * @see mshop/order/manager/base/standard/search/ansi
611
				 * @see mshop/order/manager/base/standard/count/ansi
612
				 */
613
				$path = 'mshop/order/manager/base/standard/insert';
614
			}
615
			else
616
			{
617
				/** mshop/order/manager/base/standard/update/mysql
618
				 * Updates an existing order record in the database
619
				 *
620
				 * @see mshop/order/manager/base/standard/update/ansi
621
				 */
622
623
				/** mshop/order/manager/base/standard/update/ansi
624
				 * Updates an existing order record in the database
625
				 *
626
				 * Items which already have an ID (i.e. the ID is not NULL) will
627
				 * be updated in the database.
628
				 *
629
				 * The SQL statement must be a string suitable for being used as
630
				 * prepared statement. It must include question marks for binding
631
				 * the values from the order item to the statement before they are
632
				 * sent to the database server. The order of the columns must
633
				 * correspond to the order in the saveItems() method, so the
634
				 * correct values are bound to the columns.
635
				 *
636
				 * The SQL statement should conform to the ANSI standard to be
637
				 * compatible with most relational database systems. This also
638
				 * includes using double quotes for table and column names.
639
				 *
640
				 * @param string SQL statement for updating records
641
				 * @since 2014.03
642
				 * @category Developer
643
				 * @see mshop/order/manager/base/standard/insert/ansi
644
				 * @see mshop/order/manager/base/standard/newid/ansi
645
				 * @see mshop/order/manager/base/standard/delete/ansi
646
				 * @see mshop/order/manager/base/standard/search/ansi
647
				 * @see mshop/order/manager/base/standard/count/ansi
648
				 */
649
				$path = 'mshop/order/manager/base/standard/update';
650
			}
651
652
			$priceItem = $item->getPrice();
653
			$localeItem = $context->getLocale();
654
655
			$stmt = $this->getCachedStatement( $conn, $path );
656
657
			$stmt->bind( 1, $item->getCustomerId() );
658
			$stmt->bind( 2, $localeItem->getSite()->getCode() );
659
			$stmt->bind( 3, $item->getLocale()->getLanguageId() );
660
			$stmt->bind( 4, $priceItem->getCurrencyId() );
661
			$stmt->bind( 5, $priceItem->getValue() );
662
			$stmt->bind( 6, $priceItem->getCosts() );
663
			$stmt->bind( 7, $priceItem->getRebate() );
664
			$stmt->bind( 8, $priceItem->getTaxValue() );
665
			$stmt->bind( 9, $priceItem->getTaxFlag(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
666
			$stmt->bind( 10, $item->getComment() );
667
			$stmt->bind( 11, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
668
			$stmt->bind( 12, $date ); // mtime
669
			$stmt->bind( 13, $context->getEditor() );
670
			$stmt->bind( 14, $localeItem->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
671
672
			if( $id !== null ) {
673
				$stmt->bind( 15, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
674
				$item->setId( $id );
675
			} else {
676
				$stmt->bind( 15, $date ); // ctime
677
			}
678
679
			$stmt->execute()->finish();
680
681
			if( $id === null && $fetch === true )
682
			{
683
				/** mshop/order/manager/base/standard/newid/mysql
684
				 * Retrieves the ID generated by the database when inserting a new record
685
				 *
686
				 * @see mshop/order/manager/base/standard/newid/ansi
687
				 */
688
689
				/** mshop/order/manager/base/standard/newid/ansi
690
				 * Retrieves the ID generated by the database when inserting a new record
691
				 *
692
				 * As soon as a new record is inserted into the database table,
693
				 * the database server generates a new and unique identifier for
694
				 * that record. This ID can be used for retrieving, updating and
695
				 * deleting that specific record from the table again.
696
				 *
697
				 * For MySQL:
698
				 *  SELECT LAST_INSERT_ID()
699
				 * For PostgreSQL:
700
				 *  SELECT currval('seq_mord_id')
701
				 * For SQL Server:
702
				 *  SELECT SCOPE_IDENTITY()
703
				 * For Oracle:
704
				 *  SELECT "seq_mord_id".CURRVAL FROM DUAL
705
				 *
706
				 * There's no way to retrive the new ID by a SQL statements that
707
				 * fits for most database servers as they implement their own
708
				 * specific way.
709
				 *
710
				 * @param string SQL statement for retrieving the last inserted record ID
711
				 * @since 2014.03
712
				 * @category Developer
713
				 * @see mshop/order/manager/base/standard/insert/ansi
714
				 * @see mshop/order/manager/base/standard/update/ansi
715
				 * @see mshop/order/manager/base/standard/delete/ansi
716
				 * @see mshop/order/manager/base/standard/search/ansi
717
				 * @see mshop/order/manager/base/standard/count/ansi
718
				 */
719
				$path = 'mshop/order/manager/base/standard/newid';
720
				$item->setId( $this->newId( $conn, $path ) );
721
			}
722
723
			$dbm->release( $conn, $dbname );
724
		}
725
		catch( \Exception $e )
726
		{
727
			$dbm->release( $conn, $dbname );
728
			throw $e;
729
		}
730
731
		return $item;
732
	}
733
734
735
	/**
736
	 * Search for orders based on the given criteria.
737
	 *
738
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
739
	 * @param string[] $ref List of domains to fetch list items and referenced items for, e.g.
740
	 *	"order/base/address", "order/base/coupon", "order/base/product", "order/base/service"
741
	 * @param integer|null &$total Number of items that are available in total
742
	 * @return array List of items implementing \Aimeos\MShop\Order\Item\Base\Iface
743
	 */
744
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
745
	{
746
		$map = [];
747
748
		$context = $this->getContext();
749
		$priceManager = \Aimeos\MShop\Factory::createManager( $context, 'price' );
750
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
751
752
		$dbm = $context->getDatabaseManager();
753
		$dbname = $this->getResourceName();
754
		$conn = $dbm->acquire( $dbname );
755
756
		try
757
		{
758
			$required = array( 'order.base' );
759
760
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE;
761
			$level = $context->getConfig()->get( 'mshop/order/manager/sitemode', $level );
762
763
			/** mshop/order/manager/base/standard/search/mysql
764
			 * Retrieves the records matched by the given criteria in the database
765
			 *
766
			 * @see mshop/order/manager/base/standard/search/ansi
767
			 */
768
769
			/** mshop/order/manager/base/standard/search/ansi
770
			 * Retrieves the records matched by the given criteria in the database
771
			 *
772
			 * Fetches the records matched by the given criteria from the order
773
			 * database. The records must be from one of the sites that are
774
			 * configured via the context item. If the current site is part of
775
			 * a tree of sites, the SELECT statement can retrieve all records
776
			 * from the current site and the complete sub-tree of sites.
777
			 *
778
			 * As the records can normally be limited by criteria from sub-managers,
779
			 * their tables must be joined in the SQL context. This is done by
780
			 * using the "internaldeps" property from the definition of the ID
781
			 * column of the sub-managers. These internal dependencies specify
782
			 * the JOIN between the tables and the used columns for joining. The
783
			 * ":joins" placeholder is then replaced by the JOIN strings from
784
			 * the sub-managers.
785
			 *
786
			 * To limit the records matched, conditions can be added to the given
787
			 * criteria object. It can contain comparisons like column names that
788
			 * must match specific values which can be combined by AND, OR or NOT
789
			 * operators. The resulting string of SQL conditions replaces the
790
			 * ":cond" placeholder before the statement is sent to the database
791
			 * server.
792
			 *
793
			 * If the records that are retrieved should be ordered by one or more
794
			 * columns, the generated string of column / sort direction pairs
795
			 * replaces the ":order" placeholder. In case no ordering is required,
796
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
797
			 * markers is removed to speed up retrieving the records. Columns of
798
			 * sub-managers can also be used for ordering the result set but then
799
			 * no index can be used.
800
			 *
801
			 * The number of returned records can be limited and can start at any
802
			 * number between the begining and the end of the result set. For that
803
			 * the ":size" and ":start" placeholders are replaced by the
804
			 * corresponding values from the criteria object. The default values
805
			 * are 0 for the start and 100 for the size value.
806
			 *
807
			 * The SQL statement should conform to the ANSI standard to be
808
			 * compatible with most relational database systems. This also
809
			 * includes using double quotes for table and column names.
810
			 *
811
			 * @param string SQL statement for searching items
812
			 * @since 2014.03
813
			 * @category Developer
814
			 * @see mshop/order/manager/base/standard/insert/ansi
815
			 * @see mshop/order/manager/base/standard/update/ansi
816
			 * @see mshop/order/manager/base/standard/newid/ansi
817
			 * @see mshop/order/manager/base/standard/delete/ansi
818
			 * @see mshop/order/manager/base/standard/count/ansi
819
			 */
820
			$cfgPathSearch = 'mshop/order/manager/base/standard/search';
821
822
			/** mshop/order/manager/base/standard/count/mysql
823
			 * Counts the number of records matched by the given criteria in the database
824
			 *
825
			 * @see mshop/order/manager/base/standard/count/ansi
826
			 */
827
828
			/** mshop/order/manager/base/standard/count/ansi
829
			 * Counts the number of records matched by the given criteria in the database
830
			 *
831
			 * Counts all records matched by the given criteria from the order
832
			 * database. The records must be from one of the sites that are
833
			 * configured via the context item. If the current site is part of
834
			 * a tree of sites, the statement can count all records from the
835
			 * current site and the complete sub-tree of sites.
836
			 *
837
			 * As the records can normally be limited by criteria from sub-managers,
838
			 * their tables must be joined in the SQL context. This is done by
839
			 * using the "internaldeps" property from the definition of the ID
840
			 * column of the sub-managers. These internal dependencies specify
841
			 * the JOIN between the tables and the used columns for joining. The
842
			 * ":joins" placeholder is then replaced by the JOIN strings from
843
			 * the sub-managers.
844
			 *
845
			 * To limit the records matched, conditions can be added to the given
846
			 * criteria object. It can contain comparisons like column names that
847
			 * must match specific values which can be combined by AND, OR or NOT
848
			 * operators. The resulting string of SQL conditions replaces the
849
			 * ":cond" placeholder before the statement is sent to the database
850
			 * server.
851
			 *
852
			 * Both, the strings for ":joins" and for ":cond" are the same as for
853
			 * the "search" SQL statement.
854
			 *
855
			 * Contrary to the "search" statement, it doesn't return any records
856
			 * but instead the number of records that have been found. As counting
857
			 * thousands of records can be a long running task, the maximum number
858
			 * of counted records is limited for performance reasons.
859
			 *
860
			 * The SQL statement should conform to the ANSI standard to be
861
			 * compatible with most relational database systems. This also
862
			 * includes using double quotes for table and column names.
863
			 *
864
			 * @param string SQL statement for counting items
865
			 * @since 2014.03
866
			 * @category Developer
867
			 * @see mshop/order/manager/base/standard/insert/ansi
868
			 * @see mshop/order/manager/base/standard/update/ansi
869
			 * @see mshop/order/manager/base/standard/newid/ansi
870
			 * @see mshop/order/manager/base/standard/delete/ansi
871
			 * @see mshop/order/manager/base/standard/search/ansi
872
			 */
873
			$cfgPathCount = 'mshop/order/manager/base/standard/count';
874
875
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount,
876
				$required, $total, $level );
877
878
			while( ( $row = $results->fetch() ) !== false )
879
			{
880
				$price = $priceManager->createItem();
881
				$price->setCurrencyId( $row['order.base.currencyid'] );
882
				$price->setValue( $row['order.base.price'] );
883
				$price->setCosts( $row['order.base.costs'] );
884
				$price->setRebate( $row['order.base.rebate'] );
885
				$price->setTaxValue( $row['order.base.taxvalue'] );
886
				$price->setTaxFlag( $row['order.base.taxflag'] );
887
888
				// you may need the site object! take care!
889
				$localeItem = $localeManager->createItem();
890
				$localeItem->setLanguageId( $row['order.base.languageid'] );
891
				$localeItem->setCurrencyId( $row['order.base.currencyid'] );
892
				$localeItem->setSiteId( $row['order.base.siteid'] );
893
894
				$map[$row['order.base.id']] = [$price, $localeItem, $row];
895
			}
896
897
			$dbm->release( $conn, $dbname );
898
		}
899
		catch( \Exception $e )
900
		{
901
			$dbm->release( $conn, $dbname );
902
			throw $e;
903
		}
904
905
		return $this->buildItems( $map, $ref );
906
	}
907
908
909
	/**
910
	 * Creates a new basket containing the items from the order excluding the coupons.
911
	 * If the last parameter is ture, the items will be marked as new and
912
	 * modified so an additional order is stored when the basket is saved.
913
	 *
914
	 * @param integer $id Base ID of the order to load
915
	 * @param integer $parts Bitmap of the basket parts that should be loaded
916
	 * @param boolean $fresh Create a new basket by copying the existing one and remove IDs
917
	 * @param boolean $default True to use default criteria, false for no limitation
918
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Basket including all items
919
	 */
920
	public function load( $id, $parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL, $fresh = false, $default = false )
921
	{
922
		$search = $this->getObject()->createSearch( $default );
923
		$expr = [
924
			$search->compare( '==', 'order.base.id', $id ),
925
			$search->getConditions(),
926
		];
927
		$search->setConditions( $search->combine( '&&', $expr ) );
928
929
		$context = $this->getContext();
930
		$dbm = $context->getDatabaseManager();
931
		$dbname = $this->getResourceName();
932
		$conn = $dbm->acquire( $dbname );
933
934
		try
935
		{
936
			$sitelevel = \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE;
937
			$cfgPathSearch = 'mshop/order/manager/base/standard/search';
938
			$cfgPathCount = 'mshop/order/manager/base/standard/count';
939
			$required = array( 'order.base' );
940
			$total = null;
941
942
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $sitelevel );
943
944
			if( ( $row = $results->fetch() ) === false ) {
945
				throw new \Aimeos\MShop\Order\Exception( sprintf( 'Order base item with order ID "%1$s" not found', $id ) );
946
			}
947
			$results->finish();
948
949
			$dbm->release( $conn, $dbname );
950
		}
951
		catch( \Exception $e )
952
		{
953
			$dbm->release( $conn, $dbname );
954
			throw $e;
955
		}
956
957
		$priceManager = \Aimeos\MShop\Factory::createManager( $context, 'price' );
958
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
959
960
		$price = $priceManager->createItem();
961
		$price->setCurrencyId( $row['order.base.currencyid'] );
962
		$price->setValue( $row['order.base.price'] );
963
		$price->setCosts( $row['order.base.costs'] );
964
		$price->setRebate( $row['order.base.rebate'] );
965
		$price->setTaxFlag( $row['order.base.taxflag'] );
966
		$price->setTaxValue( $row['order.base.taxvalue'] );
967
968
		// you may need the site object! take care!
969
		$localeItem = $localeManager->createItem();
970
		$localeItem->setLanguageId( $row['order.base.languageid'] );
971
		$localeItem->setCurrencyId( $row['order.base.currencyid'] );
972
		$localeItem->setSiteId( $row['order.base.siteid'] );
973
974
		if( $fresh === false ) {
975
			$basket = $this->loadItems( $id, $price, $localeItem, $row, $parts );
976
		} else {
977
			$basket = $this->loadFresh( $id, $price, $localeItem, $row, $parts );
978
		}
979
980
		$pluginManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'plugin' );
981
		$pluginManager->register( $basket, 'order' );
982
983
		return $basket;
984
	}
985
986
987
	/**
988
	 * Saves the complete basket to the storage including the items attached.
989
	 *
990
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object containing all information
991
	 * @param integer $parts Bitmap of the basket parts that should be stored
992
	 */
993
	public function store( \Aimeos\MShop\Order\Item\Base\Iface $basket, $parts = \Aimeos\MShop\Order\Item\Base\Base::PARTS_ALL )
994
	{
995
		$basket = $this->getObject()->saveItem( $basket );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $basket is correct as $this->getObject()->saveItem($basket) (which targets Aimeos\MShop\Common\Manager\Iface::saveItem()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
996
997
		if( $parts & \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT
998
			|| $parts & \Aimeos\MShop\Order\Item\Base\Base::PARTS_COUPON
999
		) {
1000
			$this->storeProducts( $basket );
0 ignored issues
show
Documentation introduced by
$basket is of type null, but the function expects a object<Aimeos\MShop\Order\Item\Base\Iface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1001
		}
1002
1003
		if( $parts & \Aimeos\MShop\Order\Item\Base\Base::PARTS_COUPON ) {
1004
			$this->storeCoupons( $basket );
0 ignored issues
show
Documentation introduced by
$basket is of type null, but the function expects a object<Aimeos\MShop\Order\Item\Base\Iface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1005
		}
1006
1007
		if( $parts & \Aimeos\MShop\Order\Item\Base\Base::PARTS_ADDRESS ) {
1008
			$this->storeAddresses( $basket );
0 ignored issues
show
Documentation introduced by
$basket is of type null, but the function expects a object<Aimeos\MShop\Order\Item\Base\Iface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1009
		}
1010
1011
		if( $parts & \Aimeos\MShop\Order\Item\Base\Base::PARTS_SERVICE ) {
1012
			$this->storeServices( $basket );
0 ignored issues
show
Documentation introduced by
$basket is of type null, but the function expects a object<Aimeos\MShop\Order\Item\Base\Iface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1013
		}
1014
	}
1015
}
1016