Passed
Push — master ( 0dde8b...fa860a )
by Aimeos
04:21
created

Standard::__construct()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 40
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 4
nop 1
dl 0
loc 40
rs 8.4444
c 0
b 0
f 0
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-2018
7
 * @package MShop
8
 * @subpackage Customer
9
 */
10
11
12
namespace Aimeos\MShop\Customer\Manager;
13
14
15
/**
16
 * Default implementation of the customer class.
17
 *
18
 * @package MShop
19
 * @subpackage Customer
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Customer\Manager\Base
23
	implements \Aimeos\MShop\Customer\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
24
{
25
	use \Aimeos\MShop\Common\Manager\AddressRef\Traits;
26
	use \Aimeos\MShop\Common\Manager\PropertyRef\Traits;
27
28
29
	private $searchConfig = array(
30
		// no siteid
31
		'customer.id' => array(
32
			'label' => 'ID',
33
			'code' => 'customer.id',
34
			'internalcode' => 'mcus."id"',
35
			'type' => 'integer',
36
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
37
			'public' => false,
38
		),
39
		'customer.code' => array(
40
			'label' => 'Username',
41
			'code' => 'customer.code',
42
			'internalcode' => 'mcus."code"',
43
			'type' => 'string',
44
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
45
		),
46
		'customer.label' => array(
47
			'label' => 'Label',
48
			'code' => 'customer.label',
49
			'internalcode' => 'mcus."label"',
50
			'type' => 'string',
51
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
52
		),
53
		'customer.salutation' => array(
54
			'label' => 'Salutation',
55
			'code' => 'customer.salutation',
56
			'internalcode' => 'mcus."salutation"',
57
			'type' => 'string',
58
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
59
		),
60
		'customer.company' => array(
61
			'label' => 'Company',
62
			'code' => 'customer.company',
63
			'internalcode' => 'mcus."company"',
64
			'type' => 'string',
65
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
66
		),
67
		'customer.vatid' => array(
68
			'label' => 'Vat ID',
69
			'code' => 'customer.vatid',
70
			'internalcode' => 'mcus."vatid"',
71
			'type' => 'string',
72
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
73
		),
74
		'customer.title' => array(
75
			'label' => 'Title',
76
			'code' => 'customer.title',
77
			'internalcode' => 'mcus."title"',
78
			'type' => 'string',
79
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
80
		),
81
		'customer.firstname' => array(
82
			'label' => 'Firstname',
83
			'code' => 'customer.firstname',
84
			'internalcode' => 'mcus."firstname"',
85
			'type' => 'string',
86
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
87
		),
88
		'customer.lastname' => array(
89
			'label' => 'Lastname',
90
			'code' => 'customer.lastname',
91
			'internalcode' => 'mcus."lastname"',
92
			'type' => 'string',
93
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
94
		),
95
		'customer.address1' => array(
96
			'label' => 'Address part one',
97
			'code' => 'customer.address1',
98
			'internalcode' => 'mcus."address1"',
99
			'type' => 'string',
100
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
101
		),
102
		'customer.address2' => array(
103
			'label' => 'Address part two',
104
			'code' => 'customer.address2',
105
			'internalcode' => 'mcus."address2"',
106
			'type' => 'string',
107
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
108
		),
109
		'customer.address3' => array(
110
			'label' => 'Address part three',
111
			'code' => 'customer.address3',
112
			'internalcode' => 'mcus."address3"',
113
			'type' => 'string',
114
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
115
		),
116
		'customer.postal' => array(
117
			'label' => 'Postal',
118
			'code' => 'customer.postal',
119
			'internalcode' => 'mcus."postal"',
120
			'type' => 'string',
121
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
122
		),
123
		'customer.city' => array(
124
			'label' => 'City',
125
			'code' => 'customer.city',
126
			'internalcode' => 'mcus."city"',
127
			'type' => 'string',
128
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
129
		),
130
		'customer.state' => array(
131
			'label' => 'State',
132
			'code' => 'customer.state',
133
			'internalcode' => 'mcus."state"',
134
			'type' => 'string',
135
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
136
		),
137
		'customer.languageid' => array(
138
			'label' => 'Language',
139
			'code' => 'customer.languageid',
140
			'internalcode' => 'mcus."langid"',
141
			'type' => 'string',
142
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
143
		),
144
		'customer.countryid' => array(
145
			'label' => 'Country',
146
			'code' => 'customer.countryid',
147
			'internalcode' => 'mcus."countryid"',
148
			'type' => 'string',
149
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
150
		),
151
		'customer.telephone' => array(
152
			'label' => 'Telephone',
153
			'code' => 'customer.telephone',
154
			'internalcode' => 'mcus."telephone"',
155
			'type' => 'string',
156
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
157
		),
158
		'customer.email' => array(
159
			'label' => 'E-mail',
160
			'code' => 'customer.email',
161
			'internalcode' => 'mcus."email"',
162
			'type' => 'string',
163
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
164
		),
165
		'customer.telefax' => array(
166
			'label' => 'Facsimile',
167
			'code' => 'customer.telefax',
168
			'internalcode' => 'mcus."telefax"',
169
			'type' => 'string',
170
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
171
		),
172
		'customer.website' => array(
173
			'label' => 'Web site',
174
			'code' => 'customer.website',
175
			'internalcode' => 'mcus."website"',
176
			'type' => 'string',
177
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
178
		),
179
		'customer.longitude' => array(
180
			'label' => 'Longitude',
181
			'code' => 'customer.longitude',
182
			'internalcode' => 'mcus."longitude"',
183
			'type' => 'string',
184
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
185
			'public' => false,
186
		),
187
		'customer.latitude' => array(
188
			'label' => 'Latitude',
189
			'code' => 'customer.latitude',
190
			'internalcode' => 'mcus."latitude"',
191
			'type' => 'string',
192
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
193
			'public' => false,
194
		),
195
		'customer.birthday' => array(
196
			'label' => 'Birthday',
197
			'code' => 'customer.birthday',
198
			'internalcode' => 'mcus."birthday"',
199
			'type' => 'string',
200
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
201
		),
202
		'customer.status' => array(
203
			'label' => 'Status',
204
			'code' => 'customer.status',
205
			'internalcode' => 'mcus."status"',
206
			'type' => 'integer',
207
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
208
		),
209
		'customer.dateverified' => array(
210
			'label' => 'Verification date',
211
			'code' => 'customer.dateverified',
212
			'internalcode' => 'mcus."vdate"',
213
			'type' => 'date',
214
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
215
			'public' => false,
216
		),
217
		'customer.password' => array(
218
			'label' => 'Password',
219
			'code' => 'customer.password',
220
			'internalcode' => 'mcus."password"',
221
			'type' => 'string',
222
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
223
			'public' => false,
224
		),
225
		'customer.ctime' => array(
226
			'label' => 'Create date/time',
227
			'code' => 'customer.ctime',
228
			'internalcode' => 'mcus."ctime"',
229
			'type' => 'datetime',
230
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
231
			'public' => false,
232
		),
233
		'customer.mtime' => array(
234
			'label' => 'Modify date/time',
235
			'code' => 'customer.mtime',
236
			'internalcode' => 'mcus."mtime"',
237
			'type' => 'datetime',
238
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
239
			'public' => false,
240
		),
241
		'customer.editor' => array(
242
			'label' => 'Editor',
243
			'code' => 'customer.editor',
244
			'internalcode' => 'mcus."editor"',
245
			'type' => 'string',
246
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
247
			'public' => false,
248
		),
249
		'customer:has' => array(
250
			'code' => 'customer:has()',
251
			'internalcode' => '(
252
				SELECT mcusli_has."id" FROM mshop_customer_list AS mcusli_has
253
				WHERE mcus."id" = mcusli_has."parentid" AND :site AND mcusli_has."domain" = $1 :type :refid
254
				LIMIT 1
255
			)',
256
			'label' => 'Customer has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
257
			'type' => 'null',
258
			'internaltype' => 'null',
259
			'public' => false,
260
		),
261
		'customer:prop' => array(
262
			'code' => 'customer:prop()',
263
			'internalcode' => '(
264
				SELECT mcuspr_prop."id" FROM mshop_customer_property AS mcuspr_prop
265
				WHERE mcus."id" = mcuspr_prop."parentid" AND :site AND mcuspr_prop."type" = $1 :langid :value
266
				LIMIT 1
267
			)',
268
			'label' => 'Customer has property item, parameter(<property type>[,<language code>[,<property value>]])',
269
			'type' => 'null',
270
			'internaltype' => 'null',
271
			'public' => false,
272
		),
273
	);
274
275
276
	/**
277
	 * Initializes the object.
278
	 *
279
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
280
	 */
281
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
282
	{
283
		parent::__construct( $context );
284
285
		$locale = $context->getLocale();
286
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
287
		$level = $context->getConfig()->get( 'mshop/customer/manager/sitemode', $level );
288
289
		$siteIds = [$locale->getSiteId()];
290
291
		if( $level & \Aimeos\MShop\Locale\Manager\Base::SITE_PATH ) {
292
			$siteIds = array_merge( $siteIds, $locale->getSitePath() );
293
		}
294
295
		if( $level & \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) {
296
			$siteIds = array_merge( $siteIds, $locale->getSiteSubTree() );
297
		}
298
299
		$this->replaceSiteMarker( $this->searchConfig['customer:has'], 'mcusli_has."siteid"', $siteIds, ':site' );
300
		$this->replaceSiteMarker( $this->searchConfig['customer:prop'], 'mcuspr_prop."siteid"', $siteIds, ':site' );
301
302
303
		$this->searchConfig['customer:has']['function'] = function( &$source, array $params ) {
304
305
			$source = str_replace( ':type', isset( $params[1] ) ? 'AND mcusli_has."type" = $2' : '', $source );
306
			$source = str_replace( ':refid', isset( $params[2] ) ? 'AND mcusli_has."refid" = $3' : '', $source );
307
308
			return $params;
309
		};
310
311
312
		$this->searchConfig['customer:prop']['function'] = function( &$source, array $params ) {
313
314
			$lang = 'AND mcuspr_prop."langid"';
315
			$lang = isset( $params[1] ) ? ( $params[1] !== 'null' ? $lang . ' = $2' : $lang . ' IS NULL' ) : '';
316
317
			$source = str_replace( ':langid', $lang, $source );
318
			$source = str_replace( ':value', isset( $params[2] ) ? 'AND mcuspr_prop."value" = $3' : '', $source );
319
320
			return $params;
321
		};
322
	}
323
324
325
	/**
326
	 * Removes old entries from the storage.
327
	 *
328
	 * @param string[] $siteids List of IDs for sites whose entries should be deleted
329
	 * @return \Aimeos\MShop\Customer\Manager\Iface Manager object for chaining method calls
330
	 */
331
	public function cleanup( array $siteids )
332
	{
333
		$path = 'mshop/customer/manager/submanagers';
334
		$default = ['address', 'group', 'lists', 'property'];
335
336
		foreach( $this->getContext()->getConfig()->get( $path, $default ) as $domain ) {
337
			$this->getObject()->getSubManager( $domain )->cleanup( $siteids );
338
		}
339
340
		return $this->cleanupBase( $siteids, 'mshop/customer/manager/standard/delete' );
341
	}
342
343
344
	/**
345
	 * Creates a new empty item instance
346
	 *
347
	 * @param array $values Values the item should be initialized with
348
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item object
349
	 */
350
	public function createItem( array $values = [] )
351
	{
352
		$values['customer.siteid'] = $this->getContext()->getLocale()->getSiteId();
353
		return $this->createItemBase( $values );
354
	}
355
356
357
	/**
358
	 * Returns the available manager types
359
	 *
360
	 * @param boolean $withsub Return also the resource type of sub-managers if true
361
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
362
	 */
363
	public function getResourceType( $withsub = true )
364
	{
365
		$path = 'mshop/customer/manager/submanagers';
366
		$default = ['address', 'group', 'lists', 'property'];
367
368
		return $this->getResourceTypeBase( 'customer', $path, $default, $withsub );
369
	}
370
371
372
	/**
373
	 * Returns the attributes that can be used for searching.
374
	 *
375
	 * @param boolean $withsub Return also attributes of sub-managers if true
376
	 * @return \Aimeos\MW\Criteria\Attribute\Iface List of search attribute items
377
	 */
378
	public function getSearchAttributes( $withsub = true )
379
	{
380
		/** mshop/customer/manager/submanagers
381
		 * List of manager names that can be instantiated by the customer manager
382
		 *
383
		 * Managers provide a generic interface to the underlying storage.
384
		 * Each manager has or can have sub-managers caring about particular
385
		 * aspects. Each of these sub-managers can be instantiated by its
386
		 * parent manager using the getSubManager() method.
387
		 *
388
		 * The search keys from sub-managers can be normally used in the
389
		 * manager as well. It allows you to search for items of the manager
390
		 * using the search keys of the sub-managers to further limit the
391
		 * retrieved list of items.
392
		 *
393
		 * @param array List of sub-manager names
394
		 * @since 2014.03
395
		 * @category Developer
396
		 */
397
		$path = 'mshop/customer/manager/submanagers';
398
399
		return $this->getSearchAttributesBase( $this->searchConfig, $path, ['address'], $withsub );
400
	}
401
402
403
	/**
404
	 * Removes multiple items specified by ids in the array.
405
	 *
406
	 * @param string[] $ids List of IDs
407
	 * @return \Aimeos\MShop\Customer\Manager\Iface Manager object for chaining method calls
408
	 */
409
	public function deleteItems( array $ids )
410
	{
411
		/** mshop/customer/manager/standard/delete/mysql
412
		 * Deletes the items matched by the given IDs from the database
413
		 *
414
		 * @see mshop/customer/manager/standard/delete/ansi
415
		 */
416
417
		/** mshop/customer/manager/standard/delete/ansi
418
		 * Deletes the items matched by the given IDs from the database
419
		 *
420
		 * Removes the records specified by the given IDs from the customer database.
421
		 * The records must be from the site that is configured via the
422
		 * context item.
423
		 *
424
		 * The ":cond" placeholder is replaced by the name of the ID column and
425
		 * the given ID or list of IDs while the site ID is bound to the question
426
		 * mark.
427
		 *
428
		 * The SQL statement should conform to the ANSI standard to be
429
		 * compatible with most relational database systems. This also
430
		 * includes using double quotes for table and column names.
431
		 *
432
		 * @param string SQL statement for deleting items
433
		 * @since 2014.03
434
		 * @category Developer
435
		 * @see mshop/customer/manager/standard/insert/ansi
436
		 * @see mshop/customer/manager/standard/update/ansi
437
		 * @see mshop/customer/manager/standard/newid/ansi
438
		 * @see mshop/customer/manager/standard/search/ansi
439
		 * @see mshop/customer/manager/standard/count/ansi
440
		 */
441
		$path = 'mshop/customer/manager/standard/delete';
442
443
		return $this->deleteItemsBase( $ids, $path );
444
	}
445
446
447
	/**
448
	 * Saves a customer item object.
449
	 *
450
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
451
	 * @param boolean $fetch True if the new ID should be returned in the item
452
	 * @return \Aimeos\MShop\Customer\Item\Iface $item Updated item including the generated ID
453
	 */
454
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
455
	{
456
		self::checkClass( \Aimeos\MShop\Customer\Item\Iface::class, $item );
457
458
		$item = $this->addGroups( $item );
459
460
		if( !$item->isModified() )
461
		{
462
			$item = $this->savePropertyItems( $item, 'customer', $fetch );
463
			$item = $this->saveAddressItems( $item, 'customer', $fetch );
464
			return $this->saveListItems( $item, 'customer', $fetch );
465
		}
466
467
		$context = $this->getContext();
468
469
		$dbm = $context->getDatabaseManager();
470
		$dbname = $this->getResourceName();
471
		$conn = $dbm->acquire( $dbname );
472
473
		try
474
		{
475
			$id = $item->getId();
476
			$date = date( 'Y-m-d H:i:s' );
477
			$billingAddress = $item->getPaymentAddress();
478
479
			if( $id === null )
480
			{
481
				/** mshop/customer/manager/standard/insert/mysql
482
				 * Inserts a new customer record into the database table
483
				 *
484
				 * @see mshop/customer/manager/standard/insert/ansi
485
				 */
486
487
				/** mshop/customer/manager/standard/insert/ansi
488
				 * Inserts a new customer record into the database table
489
				 *
490
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
491
				 * the database and the newly created ID retrieved afterwards
492
				 * using the "newid" SQL statement.
493
				 *
494
				 * The SQL statement must be a string suitable for being used as
495
				 * prepared statement. It must include question marks for binding
496
				 * the values from the customer item to the statement before they are
497
				 * sent to the database server. The number of question marks must
498
				 * be the same as the number of columns listed in the INSERT
499
				 * statement. The order of the columns must correspond to the
500
				 * order in the saveItems() method, so the correct values are
501
				 * bound to the columns.
502
				 *
503
				 * The SQL statement should conform to the ANSI standard to be
504
				 * compatible with most relational database systems. This also
505
				 * includes using double quotes for table and column names.
506
				 *
507
				 * @param string SQL statement for inserting records
508
				 * @since 2014.03
509
				 * @category Developer
510
				 * @see mshop/customer/manager/standard/update/ansi
511
				 * @see mshop/customer/manager/standard/newid/ansi
512
				 * @see mshop/customer/manager/standard/delete/ansi
513
				 * @see mshop/customer/manager/standard/search/ansi
514
				 * @see mshop/customer/manager/standard/count/ansi
515
				 */
516
				$path = 'mshop/customer/manager/standard/insert';
517
			}
518
			else
519
			{
520
				/** mshop/customer/manager/standard/update/mysql
521
				 * Updates an existing customer record in the database
522
				 *
523
				 * @see mshop/customer/manager/standard/update/ansi
524
				 */
525
526
				/** mshop/customer/manager/standard/update/ansi
527
				 * Updates an existing customer record in the database
528
				 *
529
				 * Items which already have an ID (i.e. the ID is not NULL) will
530
				 * be updated in the database.
531
				 *
532
				 * The SQL statement must be a string suitable for being used as
533
				 * prepared statement. It must include question marks for binding
534
				 * the values from the customer item to the statement before they are
535
				 * sent to the database server. The order of the columns must
536
				 * correspond to the order in the saveItems() method, so the
537
				 * correct values are bound to the columns.
538
				 *
539
				 * The SQL statement should conform to the ANSI standard to be
540
				 * compatible with most relational database systems. This also
541
				 * includes using double quotes for table and column names.
542
				 *
543
				 * @param string SQL statement for updating records
544
				 * @since 2014.03
545
				 * @category Developer
546
				 * @see mshop/customer/manager/standard/insert/ansi
547
				 * @see mshop/customer/manager/standard/newid/ansi
548
				 * @see mshop/customer/manager/standard/delete/ansi
549
				 * @see mshop/customer/manager/standard/search/ansi
550
				 * @see mshop/customer/manager/standard/count/ansi
551
				 */
552
				$path = 'mshop/customer/manager/standard/update';
553
			}
554
555
			$stmt = $this->getCachedStatement( $conn, $path );
556
557
			$stmt->bind( 1, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
558
			$stmt->bind( 2, $item->getLabel() );
559
			$stmt->bind( 3, $item->getCode() );
560
			$stmt->bind( 4, $billingAddress->getCompany() );
561
			$stmt->bind( 5, $billingAddress->getVatID() );
562
			$stmt->bind( 6, $billingAddress->getSalutation() );
563
			$stmt->bind( 7, $billingAddress->getTitle() );
564
			$stmt->bind( 8, $billingAddress->getFirstname() );
565
			$stmt->bind( 9, $billingAddress->getLastname() );
566
			$stmt->bind( 10, $billingAddress->getAddress1() );
567
			$stmt->bind( 11, $billingAddress->getAddress2() );
568
			$stmt->bind( 12, $billingAddress->getAddress3() );
569
			$stmt->bind( 13, $billingAddress->getPostal() );
570
			$stmt->bind( 14, $billingAddress->getCity() );
571
			$stmt->bind( 15, $billingAddress->getState() );
572
			$stmt->bind( 16, $billingAddress->getCountryId() );
573
			$stmt->bind( 17, $billingAddress->getLanguageId() );
574
			$stmt->bind( 18, $billingAddress->getTelephone() );
575
			$stmt->bind( 19, $billingAddress->getEmail() );
576
			$stmt->bind( 20, $billingAddress->getTelefax() );
577
			$stmt->bind( 21, $billingAddress->getWebsite() );
578
			$stmt->bind( 22, $billingAddress->getLongitude() );
579
			$stmt->bind( 23, $billingAddress->getLatitude() );
580
			$stmt->bind( 24, $item->getBirthday() );
581
			$stmt->bind( 25, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
582
			$stmt->bind( 26, $item->getDateVerified() );
583
			$stmt->bind( 27, $item->getPassword() );
584
			$stmt->bind( 28, $date ); // Modification time
585
			$stmt->bind( 29, $context->getEditor() );
586
587
			if( $id !== null ) {
588
				$stmt->bind( 30, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
589
				$billingAddress->setId( $id ); // enforce ID to be present
590
				$item->setId( $id );
591
			} else {
592
				$stmt->bind( 30, $date ); // Creation time
593
			}
594
595
			$stmt->execute()->finish();
596
597
			if( $id === null )
598
			{
599
				/** mshop/customer/manager/standard/newid/mysql
600
				 * Retrieves the ID generated by the database when inserting a new record
601
				 *
602
				 * @see mshop/customer/manager/standard/newid/ansi
603
				 */
604
605
				/** mshop/customer/manager/standard/newid/ansi
606
				 * Retrieves the ID generated by the database when inserting a new record
607
				 *
608
				 * As soon as a new record is inserted into the database table,
609
				 * the database server generates a new and unique identifier for
610
				 * that record. This ID can be used for retrieving, updating and
611
				 * deleting that specific record from the table again.
612
				 *
613
				 * For MySQL:
614
				 *  SELECT LAST_INSERT_ID()
615
				 * For PostgreSQL:
616
				 *  SELECT currval('seq_mcus_id')
617
				 * For SQL Server:
618
				 *  SELECT SCOPE_IDENTITY()
619
				 * For Oracle:
620
				 *  SELECT "seq_mcus_id".CURRVAL FROM DUAL
621
				 *
622
				 * There's no way to retrive the new ID by a SQL statements that
623
				 * fits for most database servers as they implement their own
624
				 * specific way.
625
				 *
626
				 * @param string SQL statement for retrieving the last inserted record ID
627
				 * @since 2014.03
628
				 * @category Developer
629
				 * @see mshop/customer/manager/standard/insert/ansi
630
				 * @see mshop/customer/manager/standard/update/ansi
631
				 * @see mshop/customer/manager/standard/delete/ansi
632
				 * @see mshop/customer/manager/standard/search/ansi
633
				 * @see mshop/customer/manager/standard/count/ansi
634
				 */
635
				$path = 'mshop/customer/manager/standard/newid';
636
				$item->setId( $this->newId( $conn, $path ) );
637
			}
638
639
			$dbm->release( $conn, $dbname );
640
		}
641
		catch( \Exception $e )
642
		{
643
			$dbm->release( $conn, $dbname );
644
			throw $e;
645
		}
646
647
		$item = $this->savePropertyItems( $item, 'customer', $fetch );
648
		$item = $this->saveAddressItems( $item, 'customer', $fetch );
649
		return $this->saveListItems( $item, 'customer', $fetch );
650
	}
651
652
653
	/**
654
	 * Returns the item objects matched by the given search criteria.
655
	 *
656
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
657
	 * @param string[] $ref List of domains to fetch list items and referenced items for
658
	 * @param integer|null &$total Number of items that are available in total
659
	 * @return array Associative list of IDs as keys and items implementing \Aimeos\MShop\Customer\Item\Iface as values
660
	 */
661
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
662
	{
663
		$map = [];
664
		$context = $this->getContext();
665
666
		$dbm = $context->getDatabaseManager();
667
		$dbname = $this->getResourceName();
668
		$conn = $dbm->acquire( $dbname );
669
670
		try
671
		{
672
			$required = array( 'customer' );
673
674
			/** mshop/customer/manager/sitemode
675
			 * Mode how items from levels below or above in the site tree are handled
676
			 *
677
			 * By default, only items from the current site are fetched from the
678
			 * storage. If the ai-sites extension is installed, you can create a
679
			 * tree of sites. Then, this setting allows you to define for the
680
			 * whole customer domain if items from parent sites are inherited,
681
			 * sites from child sites are aggregated or both.
682
			 *
683
			 * Available constants for the site mode are:
684
			 * * 0 = only items from the current site
685
			 * * 1 = inherit items from parent sites
686
			 * * 2 = aggregate items from child sites
687
			 * * 3 = inherit and aggregate items at the same time
688
			 *
689
			 * You also need to set the mode in the locale manager
690
			 * (mshop/locale/manager/standard/sitelevel) to one of the constants.
691
			 * If you set it to the same value, it will work as described but you
692
			 * can also use different modes. For example, if inheritance and
693
			 * aggregation is configured the locale manager but only inheritance
694
			 * in the domain manager because aggregating items makes no sense in
695
			 * this domain, then items wil be only inherited. Thus, you have full
696
			 * control over inheritance and aggregation in each domain.
697
			 *
698
			 * @param integer Constant from Aimeos\MShop\Locale\Manager\Base class
699
			 * @category Developer
700
			 * @since 2018.01
701
			 * @see mshop/locale/manager/standard/sitelevel
702
			 */
703
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
704
			$level = $context->getConfig()->get( 'mshop/customer/manager/sitemode', $level );
705
706
			/** mshop/customer/manager/standard/search/mysql
707
			 * Retrieves the records matched by the given criteria in the database
708
			 *
709
			 * @see mshop/customer/manager/standard/search/ansi
710
			 */
711
712
			/** mshop/customer/manager/standard/search/ansi
713
			 * Retrieves the records matched by the given criteria in the database
714
			 *
715
			 * Fetches the records matched by the given criteria from the customer
716
			 * database. The records must be from one of the sites that are
717
			 * configured via the context item. If the current site is part of
718
			 * a tree of sites, the SELECT statement can retrieve all records
719
			 * from the current site and the complete sub-tree of sites.
720
			 *
721
			 * As the records can normally be limited by criteria from sub-managers,
722
			 * their tables must be joined in the SQL context. This is done by
723
			 * using the "internaldeps" property from the definition of the ID
724
			 * column of the sub-managers. These internal dependencies specify
725
			 * the JOIN between the tables and the used columns for joining. The
726
			 * ":joins" placeholder is then replaced by the JOIN strings from
727
			 * the sub-managers.
728
			 *
729
			 * To limit the records matched, conditions can be added to the given
730
			 * criteria object. It can contain comparisons like column names that
731
			 * must match specific values which can be combined by AND, OR or NOT
732
			 * operators. The resulting string of SQL conditions replaces the
733
			 * ":cond" placeholder before the statement is sent to the database
734
			 * server.
735
			 *
736
			 * If the records that are retrieved should be ordered by one or more
737
			 * columns, the generated string of column / sort direction pairs
738
			 * replaces the ":order" placeholder. In case no ordering is required,
739
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
740
			 * markers is removed to speed up retrieving the records. Columns of
741
			 * sub-managers can also be used for ordering the result set but then
742
			 * no index can be used.
743
			 *
744
			 * The number of returned records can be limited and can start at any
745
			 * number between the begining and the end of the result set. For that
746
			 * the ":size" and ":start" placeholders are replaced by the
747
			 * corresponding values from the criteria object. The default values
748
			 * are 0 for the start and 100 for the size value.
749
			 *
750
			 * The SQL statement should conform to the ANSI standard to be
751
			 * compatible with most relational database systems. This also
752
			 * includes using double quotes for table and column names.
753
			 *
754
			 * @param string SQL statement for searching items
755
			 * @since 2014.03
756
			 * @category Developer
757
			 * @see mshop/customer/manager/standard/insert/ansi
758
			 * @see mshop/customer/manager/standard/update/ansi
759
			 * @see mshop/customer/manager/standard/newid/ansi
760
			 * @see mshop/customer/manager/standard/delete/ansi
761
			 * @see mshop/customer/manager/standard/count/ansi
762
			 */
763
			$cfgPathSearch = 'mshop/customer/manager/standard/search';
764
765
			/** mshop/customer/manager/standard/count/mysql
766
			 * Counts the number of records matched by the given criteria in the database
767
			 *
768
			 * @see mshop/customer/manager/standard/count/ansi
769
			 */
770
771
			/** mshop/customer/manager/standard/count/ansi
772
			 * Counts the number of records matched by the given criteria in the database
773
			 *
774
			 * Counts all records matched by the given criteria from the customer
775
			 * database. The records must be from one of the sites that are
776
			 * configured via the context item. If the current site is part of
777
			 * a tree of sites, the statement can count all records from the
778
			 * current site and the complete sub-tree of sites.
779
			 *
780
			 * As the records can normally be limited by criteria from sub-managers,
781
			 * their tables must be joined in the SQL context. This is done by
782
			 * using the "internaldeps" property from the definition of the ID
783
			 * column of the sub-managers. These internal dependencies specify
784
			 * the JOIN between the tables and the used columns for joining. The
785
			 * ":joins" placeholder is then replaced by the JOIN strings from
786
			 * the sub-managers.
787
			 *
788
			 * To limit the records matched, conditions can be added to the given
789
			 * criteria object. It can contain comparisons like column names that
790
			 * must match specific values which can be combined by AND, OR or NOT
791
			 * operators. The resulting string of SQL conditions replaces the
792
			 * ":cond" placeholder before the statement is sent to the database
793
			 * server.
794
			 *
795
			 * Both, the strings for ":joins" and for ":cond" are the same as for
796
			 * the "search" SQL statement.
797
			 *
798
			 * Contrary to the "search" statement, it doesn't return any records
799
			 * but instead the number of records that have been found. As counting
800
			 * thousands of records can be a long running task, the maximum number
801
			 * of counted records is limited for performance reasons.
802
			 *
803
			 * The SQL statement should conform to the ANSI standard to be
804
			 * compatible with most relational database systems. This also
805
			 * includes using double quotes for table and column names.
806
			 *
807
			 * @param string SQL statement for counting items
808
			 * @since 2014.03
809
			 * @category Developer
810
			 * @see mshop/customer/manager/standard/insert/ansi
811
			 * @see mshop/customer/manager/standard/update/ansi
812
			 * @see mshop/customer/manager/standard/newid/ansi
813
			 * @see mshop/customer/manager/standard/delete/ansi
814
			 * @see mshop/customer/manager/standard/search/ansi
815
			 */
816
			$cfgPathCount = 'mshop/customer/manager/standard/count';
817
818
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
819
			while( ( $row = $results->fetch() ) !== false ) {
820
				$map[$row['customer.id']] = $row;
821
			}
822
823
			$dbm->release( $conn, $dbname );
824
		}
825
		catch( \Exception $e )
826
		{
827
			$dbm->release( $conn, $dbname );
828
			throw $e;
829
		}
830
831
		$addrItems = [];
832
		if( in_array( 'customer/address', $ref, true ) ) {
833
			$addrItems = $this->getAddressItems( array_keys( $map ), 'customer' );
834
		}
835
836
		$propItems = [];
837
		if( in_array( 'customer/property', $ref, true ) ) {
838
			$propItems = $this->getPropertyItems( array_keys( $map ), 'customer' );
839
		}
840
841
		return $this->buildItems( $map, $ref, 'customer', $addrItems, $propItems );
842
	}
843
844
845
	/**
846
	 * Returns a new manager for customer extensions.
847
	 *
848
	 * @param string $manager Name of the sub manager type in lower case
849
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
850
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g stock, tags, locations, etc.
851
	 */
852
	public function getSubManager( $manager, $name = null )
853
	{
854
		return $this->getSubManagerBase( 'customer', $manager, $name );
855
	}
856
}
857