Passed
Push — master ( 69c572...92beea )
by Aimeos
05:23
created

Standard::__construct()   B

Complexity

Conditions 10
Paths 1

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 22
nc 1
nop 1
dl 0
loc 42
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

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, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
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
	private $searchConfig = array(
26
		'customer.id' => array(
27
			'label' => 'ID',
28
			'code' => 'customer.id',
29
			'internalcode' => 'mcus."id"',
30
			'type' => 'integer',
31
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
32
			'public' => false,
33
		),
34
		'customer.siteid' => array(
35
			'code' => 'customer.siteid',
36
			'internalcode' => 'mcus."siteid"',
37
			'label' => 'Customer site ID',
38
			'type' => 'string',
39
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
40
			'public' => false,
41
		),
42
		'customer.code' => array(
43
			'label' => 'Username',
44
			'code' => 'customer.code',
45
			'internalcode' => 'mcus."code"',
46
			'type' => 'string',
47
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
48
		),
49
		'customer.label' => array(
50
			'label' => 'Label',
51
			'code' => 'customer.label',
52
			'internalcode' => 'mcus."label"',
53
			'type' => 'string',
54
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
55
		),
56
		'customer.salutation' => array(
57
			'label' => 'Salutation',
58
			'code' => 'customer.salutation',
59
			'internalcode' => 'mcus."salutation"',
60
			'type' => 'string',
61
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
62
		),
63
		'customer.company' => array(
64
			'label' => 'Company',
65
			'code' => 'customer.company',
66
			'internalcode' => 'mcus."company"',
67
			'type' => 'string',
68
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
69
		),
70
		'customer.vatid' => array(
71
			'label' => 'Vat ID',
72
			'code' => 'customer.vatid',
73
			'internalcode' => 'mcus."vatid"',
74
			'type' => 'string',
75
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
76
		),
77
		'customer.title' => array(
78
			'label' => 'Title',
79
			'code' => 'customer.title',
80
			'internalcode' => 'mcus."title"',
81
			'type' => 'string',
82
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
83
		),
84
		'customer.firstname' => array(
85
			'label' => 'Firstname',
86
			'code' => 'customer.firstname',
87
			'internalcode' => 'mcus."firstname"',
88
			'type' => 'string',
89
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
90
		),
91
		'customer.lastname' => array(
92
			'label' => 'Lastname',
93
			'code' => 'customer.lastname',
94
			'internalcode' => 'mcus."lastname"',
95
			'type' => 'string',
96
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
97
		),
98
		'customer.address1' => array(
99
			'label' => 'Address part one',
100
			'code' => 'customer.address1',
101
			'internalcode' => 'mcus."address1"',
102
			'type' => 'string',
103
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
104
		),
105
		'customer.address2' => array(
106
			'label' => 'Address part two',
107
			'code' => 'customer.address2',
108
			'internalcode' => 'mcus."address2"',
109
			'type' => 'string',
110
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
111
		),
112
		'customer.address3' => array(
113
			'label' => 'Address part three',
114
			'code' => 'customer.address3',
115
			'internalcode' => 'mcus."address3"',
116
			'type' => 'string',
117
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
118
		),
119
		'customer.postal' => array(
120
			'label' => 'Postal',
121
			'code' => 'customer.postal',
122
			'internalcode' => 'mcus."postal"',
123
			'type' => 'string',
124
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
125
		),
126
		'customer.city' => array(
127
			'label' => 'City',
128
			'code' => 'customer.city',
129
			'internalcode' => 'mcus."city"',
130
			'type' => 'string',
131
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
132
		),
133
		'customer.state' => array(
134
			'label' => 'State',
135
			'code' => 'customer.state',
136
			'internalcode' => 'mcus."state"',
137
			'type' => 'string',
138
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
139
		),
140
		'customer.languageid' => array(
141
			'label' => 'Language',
142
			'code' => 'customer.languageid',
143
			'internalcode' => 'mcus."langid"',
144
			'type' => 'string',
145
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
146
		),
147
		'customer.countryid' => array(
148
			'label' => 'Country',
149
			'code' => 'customer.countryid',
150
			'internalcode' => 'mcus."countryid"',
151
			'type' => 'string',
152
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
153
		),
154
		'customer.telephone' => array(
155
			'label' => 'Telephone',
156
			'code' => 'customer.telephone',
157
			'internalcode' => 'mcus."telephone"',
158
			'type' => 'string',
159
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
160
		),
161
		'customer.email' => array(
162
			'label' => 'E-mail',
163
			'code' => 'customer.email',
164
			'internalcode' => 'mcus."email"',
165
			'type' => 'string',
166
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
167
		),
168
		'customer.telefax' => array(
169
			'label' => 'Facsimile',
170
			'code' => 'customer.telefax',
171
			'internalcode' => 'mcus."telefax"',
172
			'type' => 'string',
173
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
174
		),
175
		'customer.website' => array(
176
			'label' => 'Web site',
177
			'code' => 'customer.website',
178
			'internalcode' => 'mcus."website"',
179
			'type' => 'string',
180
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
181
		),
182
		'customer.longitude' => array(
183
			'label' => 'Longitude',
184
			'code' => 'customer.longitude',
185
			'internalcode' => 'mcus."longitude"',
186
			'type' => 'string',
187
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
188
			'public' => false,
189
		),
190
		'customer.latitude' => array(
191
			'label' => 'Latitude',
192
			'code' => 'customer.latitude',
193
			'internalcode' => 'mcus."latitude"',
194
			'type' => 'string',
195
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
196
			'public' => false,
197
		),
198
		'customer.birthday' => array(
199
			'label' => 'Birthday',
200
			'code' => 'customer.birthday',
201
			'internalcode' => 'mcus."birthday"',
202
			'type' => 'string',
203
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
204
		),
205
		'customer.status' => array(
206
			'label' => 'Status',
207
			'code' => 'customer.status',
208
			'internalcode' => 'mcus."status"',
209
			'type' => 'integer',
210
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
211
		),
212
		'customer.dateverified' => array(
213
			'label' => 'Verification date',
214
			'code' => 'customer.dateverified',
215
			'internalcode' => 'mcus."vdate"',
216
			'type' => 'date',
217
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
218
			'public' => false,
219
		),
220
		'customer.password' => array(
221
			'label' => 'Password',
222
			'code' => 'customer.password',
223
			'internalcode' => 'mcus."password"',
224
			'type' => 'string',
225
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
226
			'public' => false,
227
		),
228
		'customer.ctime' => array(
229
			'label' => 'Create date/time',
230
			'code' => 'customer.ctime',
231
			'internalcode' => 'mcus."ctime"',
232
			'type' => 'datetime',
233
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
234
			'public' => false,
235
		),
236
		'customer.mtime' => array(
237
			'label' => 'Modify date/time',
238
			'code' => 'customer.mtime',
239
			'internalcode' => 'mcus."mtime"',
240
			'type' => 'datetime',
241
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
242
			'public' => false,
243
		),
244
		'customer.editor' => array(
245
			'label' => 'Editor',
246
			'code' => 'customer.editor',
247
			'internalcode' => 'mcus."editor"',
248
			'type' => 'string',
249
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
250
			'public' => false,
251
		),
252
		'customer:has' => array(
253
			'code' => 'customer:has()',
254
			'internalcode' => ':site AND :key AND mcusli."id"',
255
			'internaldeps' => ['LEFT JOIN "mshop_customer_list" AS mcusli ON ( mcusli."parentid" = mcus."id" )'],
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' => ':site AND :key AND mcuspr."id"',
264
			'internaldeps' => ['LEFT JOIN "mshop_customer_property" AS mcuspr ON ( mcuspr."parentid" = mcus."id" )'],
265
			'label' => 'Customer has property item, parameter(<property type>[,<language code>[,<property value>]])',
266
			'type' => 'null',
267
			'internaltype' => 'null',
268
			'public' => false,
269
		),
270
	);
271
272
273
	/**
274
	 * Initializes the object.
275
	 *
276
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
277
	 */
278
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
279
	{
280
		parent::__construct( $context );
281
282
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
283
		$level = $context->config()->get( 'mshop/customer/manager/sitemode', $level );
284
285
286
		$this->searchConfig['customer:has']['function'] = function( &$source, array $params ) use ( $level ) {
287
288
			$keys = [];
289
290
			foreach( (array) ( $params[1] ?? '' ) as $type ) {
291
				foreach( (array) ( $params[2] ?? '' ) as $id ) {
292
					$keys[] = $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id;
293
				}
294
			}
295
296
			$sitestr = $this->getSiteString( 'mcusli."siteid"', $level );
297
			$keystr = $this->toExpression( 'mcusli."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' );
298
			$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
299
300
			return $params;
301
		};
302
303
304
		$this->searchConfig['customer:prop']['function'] = function( &$source, array $params ) use ( $level ) {
305
306
			$keys = [];
307
			$langs = array_key_exists( 1, $params ) ? ( $params[1] ?? 'null' ) : '';
308
309
			foreach( (array) $langs as $lang ) {
310
				foreach( (array) ( $params[2] ?? '' ) as $val ) {
311
					$keys[] = substr( $params[0] . '|' . ( $lang === null ? 'null|' : ( $lang ? $lang . '|' : '' ) ) . $val, 0, 255 );
312
				}
313
			}
314
315
			$sitestr = $this->getSiteString( 'mcuspr."siteid"', $level );
316
			$keystr = $this->toExpression( 'mcuspr."key"', $keys, '=~' );
317
			$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
318
319
			return $params;
320
		};
321
	}
322
323
324
	/**
325
	 * Removes old entries from the storage.
326
	 *
327
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
328
	 * @return \Aimeos\MShop\Customer\Manager\Iface Manager object for chaining method calls
329
	 */
330
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
331
	{
332
		$path = 'mshop/customer/manager/submanagers';
333
		$default = ['address', 'group', 'lists', 'property'];
334
335
		foreach( $this->context()->config()->get( $path, $default ) as $domain ) {
336
			$this->object()->getSubManager( $domain )->clear( $siteids );
337
		}
338
339
		return $this->clearBase( $siteids, 'mshop/customer/manager/delete' );
340
	}
341
342
343
	/**
344
	 * Creates a new empty item instance
345
	 *
346
	 * @param array $values Values the item should be initialized with
347
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item object
348
	 */
349
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
350
	{
351
		$values['customer.siteid'] = $this->context()->locale()->getSiteId();
352
		return $this->createItemBase( $values );
353
	}
354
355
356
	/**
357
	 * Returns the available manager types
358
	 *
359
	 * @param bool $withsub Return also the resource type of sub-managers if true
360
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
361
	 */
362
	public function getResourceType( bool $withsub = true ) : array
363
	{
364
		$path = 'mshop/customer/manager/submanagers';
365
		$default = ['address', 'group', 'lists', 'property'];
366
367
		return $this->getResourceTypeBase( 'customer', $path, $default, $withsub );
368
	}
369
370
371
	/**
372
	 * Returns the attributes that can be used for searching.
373
	 *
374
	 * @param bool $withsub Return also attributes of sub-managers if true
375
	 * @return \Aimeos\MW\Criteria\Attribute\Iface List of search attribute items
376
	 */
377
	public function getSearchAttributes( bool $withsub = true ) : array
378
	{
379
		/** mshop/customer/manager/submanagers
380
		 * List of manager names that can be instantiated by the customer manager
381
		 *
382
		 * Managers provide a generic interface to the underlying storage.
383
		 * Each manager has or can have sub-managers caring about particular
384
		 * aspects. Each of these sub-managers can be instantiated by its
385
		 * parent manager using the getSubManager() method.
386
		 *
387
		 * The search keys from sub-managers can be normally used in the
388
		 * manager as well. It allows you to search for items of the manager
389
		 * using the search keys of the sub-managers to further limit the
390
		 * retrieved list of items.
391
		 *
392
		 * @param array List of sub-manager names
393
		 * @since 2014.03
394
		 * @category Developer
395
		 */
396
		$path = 'mshop/customer/manager/submanagers';
397
398
		return $this->getSearchAttributesBase( $this->searchConfig, $path, ['address'], $withsub );
399
	}
400
401
402
	/**
403
	 * Removes multiple items.
404
	 *
405
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
406
	 * @return \Aimeos\MShop\Customer\Manager\Iface Manager object for chaining method calls
407
	 */
408
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
409
	{
410
		/** mshop/customer/manager/delete/mysql
411
		 * Deletes the items matched by the given IDs from the database
412
		 *
413
		 * @see mshop/customer/manager/delete/ansi
414
		 */
415
416
		/** mshop/customer/manager/delete/ansi
417
		 * Deletes the items matched by the given IDs from the database
418
		 *
419
		 * Removes the records specified by the given IDs from the customer database.
420
		 * The records must be from the site that is configured via the
421
		 * context item.
422
		 *
423
		 * The ":cond" placeholder is replaced by the name of the ID column and
424
		 * the given ID or list of IDs while the site ID is bound to the question
425
		 * mark.
426
		 *
427
		 * The SQL statement should conform to the ANSI standard to be
428
		 * compatible with most relational database systems. This also
429
		 * includes using double quotes for table and column names.
430
		 *
431
		 * @param string SQL statement for deleting items
432
		 * @since 2014.03
433
		 * @category Developer
434
		 * @see mshop/customer/manager/insert/ansi
435
		 * @see mshop/customer/manager/update/ansi
436
		 * @see mshop/customer/manager/newid/ansi
437
		 * @see mshop/customer/manager/search/ansi
438
		 * @see mshop/customer/manager/count/ansi
439
		 */
440
		$path = 'mshop/customer/manager/delete';
441
442
		return $this->deleteItemsBase( $itemIds, $path )->deleteRefItems( $itemIds );
443
	}
444
445
446
	/**
447
	 * Saves a customer item object.
448
	 *
449
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
450
	 * @param bool $fetch True if the new ID should be returned in the item
451
	 * @return \Aimeos\MShop\Customer\Item\Iface $item Updated item including the generated ID
452
	 */
453
	public function saveItem( \Aimeos\MShop\Customer\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Customer\Item\Iface
454
	{
455
		$item = $this->addGroups( $item );
456
457
		if( !$item->isModified() )
458
		{
459
			$item = $this->savePropertyItems( $item, 'customer', $fetch );
460
			$item = $this->saveAddressItems( $item, 'customer', $fetch );
461
			return $this->saveListItems( $item, 'customer', $fetch );
462
		}
463
464
		$context = $this->context();
465
466
		$dbm = $context->db();
467
		$dbname = $this->getResourceName();
468
		$conn = $dbm->acquire( $dbname );
469
470
		try
471
		{
472
			$id = $item->getId();
473
			$date = date( 'Y-m-d H:i:s' );
474
			$billingAddress = $item->getPaymentAddress();
475
			$columns = $this->object()->getSaveAttributes();
476
477
			if( $id === null )
478
			{
479
				/** mshop/customer/manager/insert/mysql
480
				 * Inserts a new customer record into the database table
481
				 *
482
				 * @see mshop/customer/manager/insert/ansi
483
				 */
484
485
				/** mshop/customer/manager/insert/ansi
486
				 * Inserts a new customer record into the database table
487
				 *
488
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
489
				 * the database and the newly created ID retrieved afterwards
490
				 * using the "newid" SQL statement.
491
				 *
492
				 * The SQL statement must be a string suitable for being used as
493
				 * prepared statement. It must include question marks for binding
494
				 * the values from the customer item to the statement before they are
495
				 * sent to the database server. The number of question marks must
496
				 * be the same as the number of columns listed in the INSERT
497
				 * statement. The order of the columns must correspond to the
498
				 * order in the save() method, so the correct values are
499
				 * bound to the columns.
500
				 *
501
				 * The SQL statement should conform to the ANSI standard to be
502
				 * compatible with most relational database systems. This also
503
				 * includes using double quotes for table and column names.
504
				 *
505
				 * @param string SQL statement for inserting records
506
				 * @since 2014.03
507
				 * @category Developer
508
				 * @see mshop/customer/manager/update/ansi
509
				 * @see mshop/customer/manager/newid/ansi
510
				 * @see mshop/customer/manager/delete/ansi
511
				 * @see mshop/customer/manager/search/ansi
512
				 * @see mshop/customer/manager/count/ansi
513
				 */
514
				$path = 'mshop/customer/manager/insert';
515
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) );
0 ignored issues
show
Bug introduced by
It seems like $this->getSqlConfig($path) can also be of type array; however, parameter $sql of Aimeos\MShop\Common\Manager\Base::addSqlColumns() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

515
				$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
516
			}
517
			else
518
			{
519
				/** mshop/customer/manager/update/mysql
520
				 * Updates an existing customer record in the database
521
				 *
522
				 * @see mshop/customer/manager/update/ansi
523
				 */
524
525
				/** mshop/customer/manager/update/ansi
526
				 * Updates an existing customer record in the database
527
				 *
528
				 * Items which already have an ID (i.e. the ID is not NULL) will
529
				 * be updated in the database.
530
				 *
531
				 * The SQL statement must be a string suitable for being used as
532
				 * prepared statement. It must include question marks for binding
533
				 * the values from the customer item to the statement before they are
534
				 * sent to the database server. The order of the columns must
535
				 * correspond to the order in the save() method, so the
536
				 * correct values are bound to the columns.
537
				 *
538
				 * The SQL statement should conform to the ANSI standard to be
539
				 * compatible with most relational database systems. This also
540
				 * includes using double quotes for table and column names.
541
				 *
542
				 * @param string SQL statement for updating records
543
				 * @since 2014.03
544
				 * @category Developer
545
				 * @see mshop/customer/manager/insert/ansi
546
				 * @see mshop/customer/manager/newid/ansi
547
				 * @see mshop/customer/manager/delete/ansi
548
				 * @see mshop/customer/manager/search/ansi
549
				 * @see mshop/customer/manager/count/ansi
550
				 */
551
				$path = 'mshop/customer/manager/update';
552
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
553
			}
554
555
			$idx = 1;
556
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
557
558
			foreach( $columns as $name => $entry ) {
559
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
0 ignored issues
show
Bug introduced by
It seems like $entry->getInternalType() can also be of type string; however, parameter $type of Aimeos\MW\DB\Statement\Iface::bind() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

559
				$stmt->bind( $idx++, $item->get( $name ), /** @scrutinizer ignore-type */ $entry->getInternalType() );
Loading history...
560
			}
561
562
			$stmt->bind( $idx++, $item->getLabel() );
563
			$stmt->bind( $idx++, $item->getCode() );
564
			$stmt->bind( $idx++, $billingAddress->getCompany() );
565
			$stmt->bind( $idx++, $billingAddress->getVatID() );
566
			$stmt->bind( $idx++, $billingAddress->getSalutation() );
567
			$stmt->bind( $idx++, $billingAddress->getTitle() );
568
			$stmt->bind( $idx++, $billingAddress->getFirstname() );
569
			$stmt->bind( $idx++, $billingAddress->getLastname() );
570
			$stmt->bind( $idx++, $billingAddress->getAddress1() );
571
			$stmt->bind( $idx++, $billingAddress->getAddress2() );
572
			$stmt->bind( $idx++, $billingAddress->getAddress3() );
573
			$stmt->bind( $idx++, $billingAddress->getPostal() );
574
			$stmt->bind( $idx++, $billingAddress->getCity() );
575
			$stmt->bind( $idx++, $billingAddress->getState() );
576
			$stmt->bind( $idx++, $billingAddress->getCountryId() );
577
			$stmt->bind( $idx++, $billingAddress->getLanguageId() );
578
			$stmt->bind( $idx++, $billingAddress->getTelephone() );
579
			$stmt->bind( $idx++, $billingAddress->getEmail() );
580
			$stmt->bind( $idx++, $billingAddress->getTelefax() );
581
			$stmt->bind( $idx++, $billingAddress->getWebsite() );
582
			$stmt->bind( $idx++, $billingAddress->getLongitude() );
583
			$stmt->bind( $idx++, $billingAddress->getLatitude() );
584
			$stmt->bind( $idx++, $billingAddress->getBirthday() );
585
			$stmt->bind( $idx++, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
586
			$stmt->bind( $idx++, $item->getDateVerified() );
587
			$stmt->bind( $idx++, $item->getPassword() );
588
			$stmt->bind( $idx++, $date ); // Modification time
589
			$stmt->bind( $idx++, $context->editor() );
590
			$stmt->bind( $idx++, $context->locale()->getSiteId() );
591
592
			if( $id !== null ) {
593
				$stmt->bind( $idx, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
594
				$billingAddress->setId( $id ); // enforce ID to be present
595
			} else {
596
				$stmt->bind( $idx, $date ); // Creation time
597
			}
598
599
			$stmt->execute()->finish();
600
601
			if( $id === null )
602
			{
603
				/** mshop/customer/manager/newid/mysql
604
				 * Retrieves the ID generated by the database when inserting a new record
605
				 *
606
				 * @see mshop/customer/manager/newid/ansi
607
				 */
608
609
				/** mshop/customer/manager/newid/ansi
610
				 * Retrieves the ID generated by the database when inserting a new record
611
				 *
612
				 * As soon as a new record is inserted into the database table,
613
				 * the database server generates a new and unique identifier for
614
				 * that record. This ID can be used for retrieving, updating and
615
				 * deleting that specific record from the table again.
616
				 *
617
				 * For MySQL:
618
				 *  SELECT LAST_INSERT_ID()
619
				 * For PostgreSQL:
620
				 *  SELECT currval('seq_mcus_id')
621
				 * For SQL Server:
622
				 *  SELECT SCOPE_IDENTITY()
623
				 * For Oracle:
624
				 *  SELECT "seq_mcus_id".CURRVAL FROM DUAL
625
				 *
626
				 * There's no way to retrive the new ID by a SQL statements that
627
				 * fits for most database servers as they implement their own
628
				 * specific way.
629
				 *
630
				 * @param string SQL statement for retrieving the last inserted record ID
631
				 * @since 2014.03
632
				 * @category Developer
633
				 * @see mshop/customer/manager/insert/ansi
634
				 * @see mshop/customer/manager/update/ansi
635
				 * @see mshop/customer/manager/delete/ansi
636
				 * @see mshop/customer/manager/search/ansi
637
				 * @see mshop/customer/manager/count/ansi
638
				 */
639
				$path = 'mshop/customer/manager/newid';
640
				$id = $this->newId( $conn, $path );
641
			}
642
643
			$item->setId( $id );
644
645
			$dbm->release( $conn, $dbname );
646
		}
647
		catch( \Exception $e )
648
		{
649
			$dbm->release( $conn, $dbname );
650
			throw $e;
651
		}
652
653
		$item = $this->savePropertyItems( $item, 'customer', $fetch );
654
		$item = $this->saveAddressItems( $item, 'customer', $fetch );
655
		return $this->saveListItems( $item, 'customer', $fetch );
656
	}
657
658
659
	/**
660
	 * Returns the item objects matched by the given search criteria.
661
	 *
662
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
663
	 * @param string[] $ref List of domains to fetch list items and referenced items for
664
	 * @param int|null &$total Number of items that are available in total
665
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Customer\Item\Iface with ids as keys
666
	 */
667
	public function search( \Aimeos\MW\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
668
	{
669
		$map = [];
670
		$context = $this->context();
671
672
		$dbm = $context->db();
673
		$dbname = $this->getResourceName();
674
		$conn = $dbm->acquire( $dbname );
675
676
		try
677
		{
678
			$required = array( 'customer' );
679
680
			/** mshop/customer/manager/sitemode
681
			 * Mode how items from levels below or above in the site tree are handled
682
			 *
683
			 * By default, only items from the current site are fetched from the
684
			 * storage. If the ai-sites extension is installed, you can create a
685
			 * tree of sites. Then, this setting allows you to define for the
686
			 * whole customer domain if items from parent sites are inherited,
687
			 * sites from child sites are aggregated or both.
688
			 *
689
			 * Available constants for the site mode are:
690
			 * * 0 = only items from the current site
691
			 * * 1 = inherit items from parent sites
692
			 * * 2 = aggregate items from child sites
693
			 * * 3 = inherit and aggregate items at the same time
694
			 *
695
			 * You also need to set the mode in the locale manager
696
			 * (mshop/locale/manager/sitelevel) to one of the constants.
697
			 * If you set it to the same value, it will work as described but you
698
			 * can also use different modes. For example, if inheritance and
699
			 * aggregation is configured the locale manager but only inheritance
700
			 * in the domain manager because aggregating items makes no sense in
701
			 * this domain, then items wil be only inherited. Thus, you have full
702
			 * control over inheritance and aggregation in each domain.
703
			 *
704
			 * @param int Constant from Aimeos\MShop\Locale\Manager\Base class
705
			 * @category Developer
706
			 * @since 2018.01
707
			 * @see mshop/locale/manager/sitelevel
708
			 */
709
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
710
			$level = $context->config()->get( 'mshop/customer/manager/sitemode', $level );
711
712
			/** mshop/customer/manager/search/mysql
713
			 * Retrieves the records matched by the given criteria in the database
714
			 *
715
			 * @see mshop/customer/manager/search/ansi
716
			 */
717
718
			/** mshop/customer/manager/search/ansi
719
			 * Retrieves the records matched by the given criteria in the database
720
			 *
721
			 * Fetches the records matched by the given criteria from the customer
722
			 * database. The records must be from one of the sites that are
723
			 * configured via the context item. If the current site is part of
724
			 * a tree of sites, the SELECT statement can retrieve all records
725
			 * from the current site and the complete sub-tree of sites.
726
			 *
727
			 * As the records can normally be limited by criteria from sub-managers,
728
			 * their tables must be joined in the SQL context. This is done by
729
			 * using the "internaldeps" property from the definition of the ID
730
			 * column of the sub-managers. These internal dependencies specify
731
			 * the JOIN between the tables and the used columns for joining. The
732
			 * ":joins" placeholder is then replaced by the JOIN strings from
733
			 * the sub-managers.
734
			 *
735
			 * To limit the records matched, conditions can be added to the given
736
			 * criteria object. It can contain comparisons like column names that
737
			 * must match specific values which can be combined by AND, OR or NOT
738
			 * operators. The resulting string of SQL conditions replaces the
739
			 * ":cond" placeholder before the statement is sent to the database
740
			 * server.
741
			 *
742
			 * If the records that are retrieved should be ordered by one or more
743
			 * columns, the generated string of column / sort direction pairs
744
			 * replaces the ":order" placeholder. In case no ordering is required,
745
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
746
			 * markers is removed to speed up retrieving the records. Columns of
747
			 * sub-managers can also be used for ordering the result set but then
748
			 * no index can be used.
749
			 *
750
			 * The number of returned records can be limited and can start at any
751
			 * number between the begining and the end of the result set. For that
752
			 * the ":size" and ":start" placeholders are replaced by the
753
			 * corresponding values from the criteria object. The default values
754
			 * are 0 for the start and 100 for the size value.
755
			 *
756
			 * The SQL statement should conform to the ANSI standard to be
757
			 * compatible with most relational database systems. This also
758
			 * includes using double quotes for table and column names.
759
			 *
760
			 * @param string SQL statement for searching items
761
			 * @since 2014.03
762
			 * @category Developer
763
			 * @see mshop/customer/manager/insert/ansi
764
			 * @see mshop/customer/manager/update/ansi
765
			 * @see mshop/customer/manager/newid/ansi
766
			 * @see mshop/customer/manager/delete/ansi
767
			 * @see mshop/customer/manager/count/ansi
768
			 */
769
			$cfgPathSearch = 'mshop/customer/manager/search';
770
771
			/** mshop/customer/manager/count/mysql
772
			 * Counts the number of records matched by the given criteria in the database
773
			 *
774
			 * @see mshop/customer/manager/count/ansi
775
			 */
776
777
			/** mshop/customer/manager/count/ansi
778
			 * Counts the number of records matched by the given criteria in the database
779
			 *
780
			 * Counts all records matched by the given criteria from the customer
781
			 * database. The records must be from one of the sites that are
782
			 * configured via the context item. If the current site is part of
783
			 * a tree of sites, the statement can count all records from the
784
			 * current site and the complete sub-tree of sites.
785
			 *
786
			 * As the records can normally be limited by criteria from sub-managers,
787
			 * their tables must be joined in the SQL context. This is done by
788
			 * using the "internaldeps" property from the definition of the ID
789
			 * column of the sub-managers. These internal dependencies specify
790
			 * the JOIN between the tables and the used columns for joining. The
791
			 * ":joins" placeholder is then replaced by the JOIN strings from
792
			 * the sub-managers.
793
			 *
794
			 * To limit the records matched, conditions can be added to the given
795
			 * criteria object. It can contain comparisons like column names that
796
			 * must match specific values which can be combined by AND, OR or NOT
797
			 * operators. The resulting string of SQL conditions replaces the
798
			 * ":cond" placeholder before the statement is sent to the database
799
			 * server.
800
			 *
801
			 * Both, the strings for ":joins" and for ":cond" are the same as for
802
			 * the "search" SQL statement.
803
			 *
804
			 * Contrary to the "search" statement, it doesn't return any records
805
			 * but instead the number of records that have been found. As counting
806
			 * thousands of records can be a long running task, the maximum number
807
			 * of counted records is limited for performance reasons.
808
			 *
809
			 * The SQL statement should conform to the ANSI standard to be
810
			 * compatible with most relational database systems. This also
811
			 * includes using double quotes for table and column names.
812
			 *
813
			 * @param string SQL statement for counting items
814
			 * @since 2014.03
815
			 * @category Developer
816
			 * @see mshop/customer/manager/insert/ansi
817
			 * @see mshop/customer/manager/update/ansi
818
			 * @see mshop/customer/manager/newid/ansi
819
			 * @see mshop/customer/manager/delete/ansi
820
			 * @see mshop/customer/manager/search/ansi
821
			 */
822
			$cfgPathCount = 'mshop/customer/manager/count';
823
824
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
825
826
			while( ( $row = $results->fetch() ) !== null ) {
827
				$map[$row['customer.id']] = $row;
828
			}
829
830
			$dbm->release( $conn, $dbname );
831
		}
832
		catch( \Exception $e )
833
		{
834
			$dbm->release( $conn, $dbname );
835
			throw $e;
836
		}
837
838
		$addrItems = [];
839
		if( in_array( 'customer/address', $ref, true ) ) {
840
			$addrItems = $this->getAddressItems( array_keys( $map ), 'customer' );
841
		}
842
843
		$propItems = []; $name = 'customer/property';
844
		if( isset( $ref[$name] ) || in_array( $name, $ref, true ) )
845
		{
846
			$propTypes = isset( $ref[$name] ) && is_array( $ref[$name] ) ? $ref[$name] : null;
847
			$propItems = $this->getPropertyItems( array_keys( $map ), 'customer', $propTypes );
848
		}
849
850
		return $this->buildItems( $map, $ref, 'customer', $addrItems, $propItems );
851
	}
852
853
854
	/**
855
	 * Returns a new manager for customer extensions.
856
	 *
857
	 * @param string $manager Name of the sub manager type in lower case
858
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
859
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g stock, tags, locations, etc.
860
	 */
861
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
862
	{
863
		return $this->getSubManagerBase( 'customer', $manager, $name );
864
	}
865
}
866