Completed
Push — master ( 75f1fb...9dfb2a )
by Aimeos
08:01
created

Standard::saveItem()   D

Complexity

Conditions 9
Paths 65

Size

Total Lines 179
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 47
nc 65
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-2016
7
 * @package MShop
8
 * @subpackage Order
9
 */
10
11
12
namespace Aimeos\MShop\Order\Manager;
13
14
15
/**
16
 * Default order manager implementation.
17
 *
18
 * @package MShop
19
 * @subpackage Order
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Manager\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MShop\Order\Manager\Iface
24
{
25
	private $searchConfig = array(
26
		'order.id'=> array(
27
			'code'=>'order.id',
28
			'internalcode'=>'mord."id"',
29
			'label'=>'Order invoice ID',
30
			'type'=> 'integer',
31
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_INT,
32
		),
33
		'order.siteid'=> array(
34
			'code'=>'order.siteid',
35
			'internalcode'=>'mord."siteid"',
36
			'label'=>'Order invoice site ID',
37
			'type'=> 'integer',
38
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_INT,
39
			'public' => false,
40
		),
41
		'order.baseid'=> array(
42
			'code'=>'order.baseid',
43
			'internalcode'=>'mord."baseid"',
44
			'label'=>'Order base ID',
45
			'type'=> 'integer',
46
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_INT,
47
			'public' => false,
48
		),
49
		'order.type'=> array(
50
			'code'=>'order.type',
51
			'internalcode'=>'mord."type"',
52
			'label'=>'Order type',
53
			'type'=> 'string',
54
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
55
		),
56
		'order.datepayment'=> array(
57
			'code'=>'order.datepayment',
58
			'internalcode'=>'mord."datepayment"',
59
			'label'=>'Order purchase date',
60
			'type'=> 'datetime',
61
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
62
		),
63
		'order.datedelivery'=> array(
64
			'code'=>'order.datedelivery',
65
			'internalcode'=>'mord."datedelivery"',
66
			'label'=>'Order delivery date',
67
			'type'=> 'datetime',
68
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
69
		),
70
		'order.statusdelivery'=> array(
71
			'code'=>'order.statusdelivery',
72
			'internalcode'=>'mord."statusdelivery"',
73
			'label'=>'Order delivery status',
74
			'type'=> 'integer',
75
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_INT,
76
		),
77
		'order.statuspayment'=> array(
78
			'code'=>'order.statuspayment',
79
			'internalcode'=>'mord."statuspayment"',
80
			'label'=>'Order payment status',
81
			'type'=> 'integer',
82
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_INT,
83
		),
84
		'order.relatedid'=> array(
85
			'code'=>'order.relatedid',
86
			'internalcode'=>'mord."relatedid"',
87
			'label'=>'Order related order ID',
88
			'type'=> 'integer',
89
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_INT,
90
		),
91
		'order.cdate'=> array(
92
			'code'=>'order.cdate',
93
			'internalcode'=>'mord."cdate"',
94
			'label'=>'Order creation date',
95
			'type'=> 'string',
96
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
97
		),
98
		'order.cmonth'=> array(
99
			'code'=>'order.cmonth',
100
			'internalcode'=>'mord."cmonth"',
101
			'label'=>'Order creation month',
102
			'type'=> 'string',
103
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
104
		),
105
		'order.cweek'=> array(
106
			'code'=>'order.cweek',
107
			'internalcode'=>'mord."cweek"',
108
			'label'=>'Order creation week',
109
			'type'=> 'string',
110
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
111
		),
112
		'order.chour'=> array(
113
			'code'=>'order.chour',
114
			'internalcode'=>'mord."chour"',
115
			'label'=>'Order creation hour',
116
			'type'=> 'string',
117
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
118
		),
119
		'order.ctime'=> array(
120
			'code'=>'order.ctime',
121
			'internalcode'=>'mord."ctime"',
122
			'label'=>'Order creation date/time',
123
			'type'=> 'datetime',
124
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
125
		),
126
		'order.mtime'=> array(
127
			'code'=>'order.mtime',
128
			'internalcode'=>'mord."mtime"',
129
			'label'=>'Order modification date',
130
			'type'=> 'datetime',
131
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
132
		),
133
		'order.editor'=> array(
134
			'code'=>'order.editor',
135
			'internalcode'=>'mord."editor"',
136
			'label'=>'Order editor',
137
			'type'=> 'string',
138
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
139
		),
140
		'order.containsStatus' => array(
141
			'code'=>'order.containsStatus()',
142
			'internalcode'=>'( SELECT COUNT(mordst_cs."parentid")
143
				FROM "mshop_order_status" AS mordst_cs
144
				WHERE mord."id" = mordst_cs."parentid" AND :site
145
				AND mordst_cs."type" = $1 AND mordst_cs."value" IN ( $2 ) )',
146
			'label'=>'Number of order status items, parameter(<type>,<value>)',
147
			'type'=> 'integer',
148
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
149
			'public' => false,
150
		),
151
	);
152
153
154
	/**
155
	 * Creates the manager that will use the given context object.
156
	 *
157
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects
158
	 */
159
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
160
	{
161
		parent::__construct( $context );
162
		$this->setResourceName( 'db-order' );
163
164
165
		$sites = $context->getLocale()->getSiteSubTree();
166
		$this->replaceSiteMarker( $this->searchConfig['order.containsStatus'], 'mordst_cs."siteid"', $sites, ':site' );
167
	}
168
169
170
	/**
171
	 * Counts the number items that are available for the values of the given key.
172
	 *
173
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria
174
	 * @param string $key Search key to aggregate items for
175
	 * @return array List of the search keys as key and the number of counted items as value
176
	 */
177
	public function aggregate( \Aimeos\MW\Criteria\Iface $search, $key )
178
	{
179
		/** mshop/order/manager/standard/aggregate/mysql
180
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
181
		 *
182
		 * @see mshop/order/manager/standard/aggregate/ansi
183
		 */
184
185
		/** mshop/order/manager/standard/aggregate/ansi
186
		 * Counts the number of records grouped by the values in the key column and matched by the given criteria
187
		 *
188
		 * Groups all records by the values in the key column and counts their
189
		 * occurence. The matched records can be limited by the given criteria
190
		 * from the order database. The records must be from one of the sites
191
		 * that are configured via the context item. If the current site is part
192
		 * of a tree of sites, the statement can count all records from the
193
		 * current site and the complete sub-tree of sites.
194
		 *
195
		 * As the records can normally be limited by criteria from sub-managers,
196
		 * their tables must be joined in the SQL context. This is done by
197
		 * using the "internaldeps" property from the definition of the ID
198
		 * column of the sub-managers. These internal dependencies specify
199
		 * the JOIN between the tables and the used columns for joining. The
200
		 * ":joins" placeholder is then replaced by the JOIN strings from
201
		 * the sub-managers.
202
		 *
203
		 * To limit the records matched, conditions can be added to the given
204
		 * criteria object. It can contain comparisons like column names that
205
		 * must match specific values which can be combined by AND, OR or NOT
206
		 * operators. The resulting string of SQL conditions replaces the
207
		 * ":cond" placeholder before the statement is sent to the database
208
		 * server.
209
		 *
210
		 * This statement doesn't return any records. Instead, it returns pairs
211
		 * of the different values found in the key column together with the
212
		 * number of records that have been found for that key values.
213
		 *
214
		 * The SQL statement should conform to the ANSI standard to be
215
		 * compatible with most relational database systems. This also
216
		 * includes using double quotes for table and column names.
217
		 *
218
		 * @param string SQL statement for aggregating order items
219
		 * @since 2014.09
220
		 * @category Developer
221
		 * @see mshop/order/manager/standard/insert/ansi
222
		 * @see mshop/order/manager/standard/update/ansi
223
		 * @see mshop/order/manager/standard/newid/ansi
224
		 * @see mshop/order/manager/standard/delete/ansi
225
		 * @see mshop/order/manager/standard/search/ansi
226
		 * @see mshop/order/manager/standard/count/ansi
227
		 */
228
		$cfgkey = 'mshop/order/manager/standard/aggregate';
229
		return $this->aggregateBase( $search, $key, $cfgkey, array( 'order' ) );
230
	}
231
232
233
	/**
234
	 * Removes old entries from the storage.
235
	 *
236
	 * @param integer[] $siteids List of IDs for sites whose entries should be deleted
237
	 */
238
	public function cleanup( array $siteids )
239
	{
240
		$path = 'mshop/order/manager/submanagers';
241
		foreach( $this->getContext()->getConfig()->get( $path, array( 'status', 'base' ) ) as $domain ) {
242
			$this->getSubManager( $domain )->cleanup( $siteids );
243
		}
244
245
		$this->cleanupBase( $siteids, 'mshop/order/manager/standard/delete' );
246
	}
247
248
249
	/**
250
	 * Returns a new and empty invoice.
251
	 *
252
	 * @return \Aimeos\MShop\Order\Item\Iface Invoice without assigned values or items
253
	 */
254
	public function createItem()
255
	{
256
		$values = array( 'order.siteid'=> $this->getContext()->getLocale()->getSiteId() );
257
		return $this->createItemBase( $values );
258
	}
259
260
261
	/**
262
	 * Creates a one-time order in the storage from the given invoice object.
263
	 *
264
	 * @param \Aimeos\MShop\Common\Item\Iface $item Order item with necessary values
265
	 * @param boolean $fetch True if the new ID should be returned in the item
266
	 */
267
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
268
	{
269
		$iface = '\\Aimeos\\MShop\\Order\\Item\\Iface';
270
		if( !( $item instanceof $iface ) ) {
271
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Object is not of required type "%1$s"', $iface ) );
272
		}
273
274
		if( $item->getBaseId() === null ) {
275
			throw new \Aimeos\MShop\Order\Exception( 'Required order base ID is missing' );
276
		}
277
278
		if( !$item->isModified() ) { return; }
279
280
		$context = $this->getContext();
281
282
		$dbm = $context->getDatabaseManager();
283
		$dbname = $this->getResourceName();
284
		$conn = $dbm->acquire( $dbname );
285
286
		try
287
		{
288
			$id = $item->getId();
289
			$date = date( 'Y-m-d H:i:s' );
290
291
			if( $id === null )
292
			{
293
				/** mshop/order/manager/standard/insert/mysql
294
				 * Inserts a new order record into the database table
295
				 *
296
				 * @see mshop/order/manager/standard/insert/ansi
297
				 */
298
299
				/** mshop/order/manager/standard/insert/ansi
300
				 * Inserts a new order record into the database table
301
				 *
302
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
303
				 * the database and the newly created ID retrieved afterwards
304
				 * using the "newid" SQL statement.
305
				 *
306
				 * The SQL statement must be a string suitable for being used as
307
				 * prepared statement. It must include question marks for binding
308
				 * the values from the order item to the statement before they are
309
				 * sent to the database server. The number of question marks must
310
				 * be the same as the number of columns listed in the INSERT
311
				 * statement. The order of the columns must correspond to the
312
				 * order in the saveItems() method, so the correct values are
313
				 * bound to the columns.
314
				 *
315
				 * The SQL statement should conform to the ANSI standard to be
316
				 * compatible with most relational database systems. This also
317
				 * includes using double quotes for table and column names.
318
				 *
319
				 * @param string SQL statement for inserting records
320
				 * @since 2014.03
321
				 * @category Developer
322
				 * @see mshop/order/manager/standard/update/ansi
323
				 * @see mshop/order/manager/standard/newid/ansi
324
				 * @see mshop/order/manager/standard/delete/ansi
325
				 * @see mshop/order/manager/standard/search/ansi
326
				 * @see mshop/order/manager/standard/count/ansi
327
				 */
328
				$path = 'mshop/order/manager/standard/insert';
329
			}
330
			else
331
			{
332
				/** mshop/order/manager/standard/update/mysql
333
				 * Updates an existing order record in the database
334
				 *
335
				 * @see mshop/order/manager/standard/update/ansi
336
				 */
337
338
				/** mshop/order/manager/standard/update/ansi
339
				 * Updates an existing order record in the database
340
				 *
341
				 * Items which already have an ID (i.e. the ID is not NULL) will
342
				 * be updated in the database.
343
				 *
344
				 * The SQL statement must be a string suitable for being used as
345
				 * prepared statement. It must include question marks for binding
346
				 * the values from the order item to the statement before they are
347
				 * sent to the database server. The order of the columns must
348
				 * correspond to the order in the saveItems() method, so the
349
				 * correct values are bound to the columns.
350
				 *
351
				 * The SQL statement should conform to the ANSI standard to be
352
				 * compatible with most relational database systems. This also
353
				 * includes using double quotes for table and column names.
354
				 *
355
				 * @param string SQL statement for updating records
356
				 * @since 2014.03
357
				 * @category Developer
358
				 * @see mshop/order/manager/standard/insert/ansi
359
				 * @see mshop/order/manager/standard/newid/ansi
360
				 * @see mshop/order/manager/standard/delete/ansi
361
				 * @see mshop/order/manager/standard/search/ansi
362
				 * @see mshop/order/manager/standard/count/ansi
363
				 */
364
				$path = 'mshop/order/manager/standard/update';
365
			}
366
367
			$stmt = $this->getCachedStatement( $conn, $path );
368
369
			$stmt->bind( 1, $item->getBaseId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
370
			$stmt->bind( 2, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
371
			$stmt->bind( 3, $item->getType() );
372
			$stmt->bind( 4, $item->getDatePayment() );
373
			$stmt->bind( 5, $item->getDateDelivery() );
374
			$stmt->bind( 6, $item->getDeliveryStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
375
			$stmt->bind( 7, $item->getPaymentStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
376
			$stmt->bind( 8, $item->getRelatedId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
377
			$stmt->bind( 9, $date ); // mtime
378
			$stmt->bind( 10, $context->getEditor() );
379
380
			if( $id !== null ) {
381
				$stmt->bind( 11, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
382
				$item->setId( $id ); // is not modified anymore
383
			} else {
384
				$stmt->bind( 11, $date ); // ctime
385
				$stmt->bind( 12, date( 'Y-m-d' ) ); // cdate
386
				$stmt->bind( 13, date( 'Y-m' ) ); // cmonth
387
				$stmt->bind( 14, date( 'Y-W' ) ); // cweek
388
				$stmt->bind( 15, date( 'H' ) ); // chour
389
			}
390
391
			$stmt->execute()->finish();
392
393
			if( $id === null && $fetch === true )
394
			{
395
				/** mshop/order/manager/standard/newid/mysql
396
				 * Retrieves the ID generated by the database when inserting a new record
397
				 *
398
				 * @see mshop/order/manager/standard/newid/ansi
399
				 */
400
401
				/** mshop/order/manager/standard/newid/ansi
402
				 * Retrieves the ID generated by the database when inserting a new record
403
				 *
404
				 * As soon as a new record is inserted into the database table,
405
				 * the database server generates a new and unique identifier for
406
				 * that record. This ID can be used for retrieving, updating and
407
				 * deleting that specific record from the table again.
408
				 *
409
				 * For MySQL:
410
				 *  SELECT LAST_INSERT_ID()
411
				 * For PostgreSQL:
412
				 *  SELECT currval('seq_mord_id')
413
				 * For SQL Server:
414
				 *  SELECT SCOPE_IDENTITY()
415
				 * For Oracle:
416
				 *  SELECT "seq_mord_id".CURRVAL FROM DUAL
417
				 *
418
				 * There's no way to retrive the new ID by a SQL statements that
419
				 * fits for most database servers as they implement their own
420
				 * specific way.
421
				 *
422
				 * @param string SQL statement for retrieving the last inserted record ID
423
				 * @since 2014.03
424
				 * @category Developer
425
				 * @see mshop/order/manager/standard/insert/ansi
426
				 * @see mshop/order/manager/standard/update/ansi
427
				 * @see mshop/order/manager/standard/delete/ansi
428
				 * @see mshop/order/manager/standard/search/ansi
429
				 * @see mshop/order/manager/standard/count/ansi
430
				 */
431
				$path = 'mshop/order/manager/standard/newid';
432
				$item->setId( $this->newId( $conn, $path ) );
433
			}
434
435
			$dbm->release( $conn, $dbname );
436
		}
437
		catch( \Exception $e )
438
		{
439
			$dbm->release( $conn, $dbname );
440
			throw $e;
441
		}
442
443
444
		$this->addStatus( $item );
445
	}
446
447
448
	/**
449
	 * Returns an order invoice item built from database values.
450
	 *
451
	 * @param integer $id Unique id of the order invoice
452
	 * @param string[] $ref List of domains to fetch list items and referenced items for
453
	 * @param boolean $default Add default criteria
454
	 * @return \Aimeos\MShop\Order\Item\Iface Returns order invoice item of the given id
455
	 * @throws \Aimeos\MShop\Order\Exception If item couldn't be found
456
	 */
457
	public function getItem( $id, array $ref = [], $default = false )
458
	{
459
		return $this->getItemBase( 'order.id', $id, $ref, $default );
460
	}
461
462
463
	/**
464
	 * Removes multiple items specified by ids in the array.
465
	 *
466
	 * @param array $ids List of IDs
467
	 */
468
	public function deleteItems( array $ids )
469
	{
470
		/** mshop/order/manager/standard/delete/mysql
471
		 * Deletes the items matched by the given IDs from the database
472
		 *
473
		 * @see mshop/order/manager/standard/delete/ansi
474
		 */
475
476
		/** mshop/order/manager/standard/delete/ansi
477
		 * Deletes the items matched by the given IDs from the database
478
		 *
479
		 * Removes the records specified by the given IDs from the order database.
480
		 * The records must be from the site that is configured via the
481
		 * context item.
482
		 *
483
		 * The ":cond" placeholder is replaced by the name of the ID column and
484
		 * the given ID or list of IDs while the site ID is bound to the question
485
		 * mark.
486
		 *
487
		 * The SQL statement should conform to the ANSI standard to be
488
		 * compatible with most relational database systems. This also
489
		 * includes using double quotes for table and column names.
490
		 *
491
		 * @param string SQL statement for deleting items
492
		 * @since 2014.03
493
		 * @category Developer
494
		 * @see mshop/order/manager/standard/insert/ansi
495
		 * @see mshop/order/manager/standard/update/ansi
496
		 * @see mshop/order/manager/standard/newid/ansi
497
		 * @see mshop/order/manager/standard/search/ansi
498
		 * @see mshop/order/manager/standard/count/ansi
499
		 */
500
		$path = 'mshop/order/manager/standard/delete';
501
		$this->deleteItemsBase( $ids, $path );
502
	}
503
504
505
	/**
506
	 * Returns the available manager types
507
	 *
508
	 * @param boolean $withsub Return also the resource type of sub-managers if true
509
	 * @return array Type of the manager and submanagers, subtypes are separated by slashes
510
	 */
511
	public function getResourceType( $withsub = true )
512
	{
513
		$path = 'mshop/order/manager/submanagers';
514
515
		return $this->getResourceTypeBase( 'order', $path, array( 'base', 'status' ), $withsub );
516
	}
517
518
519
	/**
520
	 * Returns the attributes that can be used for searching.
521
	 *
522
	 * @param boolean $withsub Return also attributes of sub-managers if true
523
	 * @return array List of attribute items implementing \Aimeos\MW\Criteria\Attribute\Iface
524
	 */
525
	public function getSearchAttributes( $withsub = true )
526
	{
527
		/** mshop/order/manager/submanagers
528
		 * List of manager names that can be instantiated by the order manager
529
		 *
530
		 * Managers provide a generic interface to the underlying storage.
531
		 * Each manager has or can have sub-managers caring about particular
532
		 * aspects. Each of these sub-managers can be instantiated by its
533
		 * parent manager using the getSubManager() method.
534
		 *
535
		 * The search keys from sub-managers can be normally used in the
536
		 * manager as well. It allows you to search for items of the manager
537
		 * using the search keys of the sub-managers to further limit the
538
		 * retrieved list of items.
539
		 *
540
		 * @param array List of sub-manager names
541
		 * @since 2014.03
542
		 * @category Developer
543
		 */
544
		$path = 'mshop/order/manager/submanagers';
545
		$default = array( 'base', 'status' );
546
547
		return $this->getSearchAttributesBase( $this->searchConfig, $path, $default, $withsub );
548
	}
549
550
551
	/**
552
	 * Searches for orders based on the given criteria.
553
	 *
554
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
555
	 * @param string[] $ref List of domains to fetch list items and referenced items for
556
	 * @param integer|null &$total Number of items that are available in total
557
	 * @return array List of items implementing \Aimeos\MShop\Order\Item\Iface
558
	 */
559
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
560
	{
561
		$context = $this->getContext();
562
563
		$dbm = $context->getDatabaseManager();
564
		$dbname = $this->getResourceName();
565
		$conn = $dbm->acquire( $dbname );
566
567
		$items = [];
568
569
		try
570
		{
571
			$required = array( 'order' );
572
			$sitelevel = \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE;
573
574
			/** mshop/order/manager/standard/search/mysql
575
			 * Retrieves the records matched by the given criteria in the database
576
			 *
577
			 * @see mshop/order/manager/standard/search/ansi
578
			 */
579
580
			/** mshop/order/manager/standard/search/ansi
581
			 * Retrieves the records matched by the given criteria in the database
582
			 *
583
			 * Fetches the records matched by the given criteria from the order
584
			 * database. The records must be from one of the sites that are
585
			 * configured via the context item. If the current site is part of
586
			 * a tree of sites, the SELECT statement can retrieve all records
587
			 * from the current site and the complete sub-tree of sites.
588
			 *
589
			 * As the records can normally be limited by criteria from sub-managers,
590
			 * their tables must be joined in the SQL context. This is done by
591
			 * using the "internaldeps" property from the definition of the ID
592
			 * column of the sub-managers. These internal dependencies specify
593
			 * the JOIN between the tables and the used columns for joining. The
594
			 * ":joins" placeholder is then replaced by the JOIN strings from
595
			 * the sub-managers.
596
			 *
597
			 * To limit the records matched, conditions can be added to the given
598
			 * criteria object. It can contain comparisons like column names that
599
			 * must match specific values which can be combined by AND, OR or NOT
600
			 * operators. The resulting string of SQL conditions replaces the
601
			 * ":cond" placeholder before the statement is sent to the database
602
			 * server.
603
			 *
604
			 * If the records that are retrieved should be ordered by one or more
605
			 * columns, the generated string of column / sort direction pairs
606
			 * replaces the ":order" placeholder. In case no ordering is required,
607
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
608
			 * markers is removed to speed up retrieving the records. Columns of
609
			 * sub-managers can also be used for ordering the result set but then
610
			 * no index can be used.
611
			 *
612
			 * The number of returned records can be limited and can start at any
613
			 * number between the begining and the end of the result set. For that
614
			 * the ":size" and ":start" placeholders are replaced by the
615
			 * corresponding values from the criteria object. The default values
616
			 * are 0 for the start and 100 for the size value.
617
			 *
618
			 * The SQL statement should conform to the ANSI standard to be
619
			 * compatible with most relational database systems. This also
620
			 * includes using double quotes for table and column names.
621
			 *
622
			 * @param string SQL statement for searching items
623
			 * @since 2014.03
624
			 * @category Developer
625
			 * @see mshop/order/manager/standard/insert/ansi
626
			 * @see mshop/order/manager/standard/update/ansi
627
			 * @see mshop/order/manager/standard/newid/ansi
628
			 * @see mshop/order/manager/standard/delete/ansi
629
			 * @see mshop/order/manager/standard/count/ansi
630
			 */
631
			$cfgPathSearch = 'mshop/order/manager/standard/search';
632
633
			/** mshop/order/manager/standard/count/mysql
634
			 * Counts the number of records matched by the given criteria in the database
635
			 *
636
			 * @see mshop/order/manager/standard/count/ansi
637
			 */
638
639
			/** mshop/order/manager/standard/count/ansi
640
			 * Counts the number of records matched by the given criteria in the database
641
			 *
642
			 * Counts all records matched by the given criteria from the order
643
			 * database. The records must be from one of the sites that are
644
			 * configured via the context item. If the current site is part of
645
			 * a tree of sites, the statement can count all records from the
646
			 * current site and the complete sub-tree of sites.
647
			 *
648
			 * As the records can normally be limited by criteria from sub-managers,
649
			 * their tables must be joined in the SQL context. This is done by
650
			 * using the "internaldeps" property from the definition of the ID
651
			 * column of the sub-managers. These internal dependencies specify
652
			 * the JOIN between the tables and the used columns for joining. The
653
			 * ":joins" placeholder is then replaced by the JOIN strings from
654
			 * the sub-managers.
655
			 *
656
			 * To limit the records matched, conditions can be added to the given
657
			 * criteria object. It can contain comparisons like column names that
658
			 * must match specific values which can be combined by AND, OR or NOT
659
			 * operators. The resulting string of SQL conditions replaces the
660
			 * ":cond" placeholder before the statement is sent to the database
661
			 * server.
662
			 *
663
			 * Both, the strings for ":joins" and for ":cond" are the same as for
664
			 * the "search" SQL statement.
665
			 *
666
			 * Contrary to the "search" statement, it doesn't return any records
667
			 * but instead the number of records that have been found. As counting
668
			 * thousands of records can be a long running task, the maximum number
669
			 * of counted records is limited for performance reasons.
670
			 *
671
			 * The SQL statement should conform to the ANSI standard to be
672
			 * compatible with most relational database systems. This also
673
			 * includes using double quotes for table and column names.
674
			 *
675
			 * @param string SQL statement for counting items
676
			 * @since 2014.03
677
			 * @category Developer
678
			 * @see mshop/order/manager/standard/insert/ansi
679
			 * @see mshop/order/manager/standard/update/ansi
680
			 * @see mshop/order/manager/standard/newid/ansi
681
			 * @see mshop/order/manager/standard/delete/ansi
682
			 * @see mshop/order/manager/standard/search/ansi
683
			 */
684
			$cfgPathCount = 'mshop/order/manager/standard/count';
685
686
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount,
687
				$required, $total, $sitelevel );
688
689
			try
690
			{
691
				while( ( $row = $results->fetch() ) !== false ) {
692
					$items[$row['order.id']] = $this->createItemBase( $row );
693
				}
694
			}
695
			catch( \Exception $e )
696
			{
697
				$results->finish();
698
				throw $e;
699
			}
700
701
			$dbm->release( $conn, $dbname );
702
		}
703
		catch( \Exception $e )
704
		{
705
			$dbm->release( $conn, $dbname );
706
			throw $e;
707
		}
708
709
		return $items;
710
	}
711
712
713
	/**
714
	 * Returns a new manager for order extensions
715
	 *
716
	 * @param string $manager Name of the sub manager type in lower case
717
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
718
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g base, etc.
719
	 */
720
	public function getSubManager( $manager, $name = null )
721
	{
722
		return $this->getSubManagerBase( 'order', $manager, $name );
723
	}
724
725
726
	/**
727
	 * Adds the new payment and delivery values to the order status log.
728
	 *
729
	 * @param \Aimeos\MShop\Order\Item\Iface $item Order item object
730
	 */
731
	protected function addStatus( \Aimeos\MShop\Order\Item\Iface $item )
732
	{
733
		$statusManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'order/status' );
734
735
		$statusItem = $statusManager->createItem();
736
		$statusItem->setParentId( $item->getId() );
737
738
		if( $item->getPaymentStatus() != $item->oldPaymentStatus )
739
		{
740
			$statusItem->setId( null );
741
			$statusItem->setType( \Aimeos\MShop\Order\Item\Status\Base::STATUS_PAYMENT );
742
			$statusItem->setValue( $item->getPaymentStatus() );
743
744
			$statusManager->saveItem( $statusItem, false );
745
		}
746
747
		if( $item->getDeliveryStatus() != $item->oldDeliveryStatus )
748
		{
749
			$statusItem->setId( null );
750
			$statusItem->setType( \Aimeos\MShop\Order\Item\Status\Base::STATUS_DELIVERY );
751
			$statusItem->setValue( $item->getDeliveryStatus() );
752
753
			$statusManager->saveItem( $statusItem, false );
754
		}
755
	}
756
757
758
	/**
759
	 * Creates a new order item.
760
	 *
761
	 * @param array $values List of attributes for order item
762
	 * @return \Aimeos\MShop\Order\Item\Iface New order item
763
	 */
764
	protected function createItemBase( array $values = [] )
765
	{
766
		return new \Aimeos\MShop\Order\Item\Standard( $values );
767
	}
768
}
769