Passed
Push — master ( eade66...a4d1b5 )
by Aimeos
03:55
created

FosUser::searchItems()   B

Complexity

Conditions 8
Paths 37

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 25
c 3
b 0
f 0
dl 0
loc 42
rs 8.4444
cc 8
nc 37
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2020
6
 * @package MShop
7
 * @subpackage Customer
8
 */
9
10
11
namespace Aimeos\MShop\Customer\Manager;
12
13
14
/**
15
 * Customer class implementation for Friends of Symfony user bundle.
16
 *
17
 * @package MShop
18
 * @subpackage Customer
19
 */
20
class FosUser
21
	extends \Aimeos\MShop\Customer\Manager\Standard
22
{
23
	private $searchConfig = array(
24
		'customer.id' => array(
25
			'label' => 'Customer ID',
26
			'code' => 'customer.id',
27
			'internalcode' => 'fos."id"',
28
			'type' => 'integer',
29
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
30
			'public' => false,
31
		),
32
		'customer.siteid' => array(
33
			'code' => 'customer.siteid',
34
			'internalcode' => 'fos."siteid"',
35
			'label' => 'Customer site ID',
36
			'type' => 'string',
37
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
38
			'public' => false,
39
		),
40
		'customer.code' => array(
41
			'label' => 'Customer username',
42
			'code' => 'customer.code',
43
			'internalcode' => 'fos."username"',
44
			'type' => 'string',
45
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR
46
		),
47
		'customer.label' => array(
48
			'label' => 'Customer label',
49
			'code' => 'customer.label',
50
			'internalcode' => 'fos."username_canonical"',
51
			'type' => 'string',
52
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR
53
		),
54
		'customer.salutation' => array(
55
			'label' => 'Customer salutation',
56
			'code' => 'customer.salutation',
57
			'internalcode' => 'fos."salutation"',
58
			'type' => 'string',
59
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
60
		),
61
		'customer.company'=> array(
62
			'label' => 'Customer company',
63
			'code' => 'customer.company',
64
			'internalcode' => 'fos."company"',
65
			'type' => 'string',
66
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
67
		),
68
		'customer.vatid'=> array(
69
			'label' => 'Customer VAT ID',
70
			'code' => 'customer.vatid',
71
			'internalcode' => 'fos."vatid"',
72
			'type' => 'string',
73
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
74
		),
75
		'customer.title' => array(
76
			'label' => 'Customer title',
77
			'code' => 'customer.title',
78
			'internalcode' => 'fos."title"',
79
			'type' => 'string',
80
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
81
		),
82
		'customer.firstname' => array(
83
			'label' => 'Customer firstname',
84
			'code' => 'customer.firstname',
85
			'internalcode' => 'fos."firstname"',
86
			'type' => 'string',
87
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
88
		),
89
		'customer.lastname' => array(
90
			'label' => 'Customer lastname',
91
			'code' => 'customer.lastname',
92
			'internalcode' => 'fos."lastname"',
93
			'type' => 'string',
94
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
95
		),
96
		'customer.address1' => array(
97
			'label' => 'Customer address part one',
98
			'code' => 'customer.address1',
99
			'internalcode' => 'fos."address1"',
100
			'type' => 'string',
101
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
102
		),
103
		'customer.address2' => array(
104
			'label' => 'Customer address part two',
105
			'code' => 'customer.address2',
106
			'internalcode' => 'fos."address2"',
107
			'type' => 'string',
108
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
109
		),
110
		'customer.address3' => array(
111
			'label' => 'Customer address part three',
112
			'code' => 'customer.address3',
113
			'internalcode' => 'fos."address3"',
114
			'type' => 'string',
115
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
116
		),
117
		'customer.postal' => array(
118
			'label' => 'Customer postal',
119
			'code' => 'customer.postal',
120
			'internalcode' => 'fos."postal"',
121
			'type' => 'string',
122
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
123
		),
124
		'customer.city' => array(
125
			'label' => 'Customer city',
126
			'code' => 'customer.city',
127
			'internalcode' => 'fos."city"',
128
			'type' => 'string',
129
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
130
		),
131
		'customer.state' => array(
132
			'label' => 'Customer state',
133
			'code' => 'customer.state',
134
			'internalcode' => 'fos."state"',
135
			'type' => 'string',
136
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
137
		),
138
		'customer.languageid' => array(
139
			'label' => 'Customer language',
140
			'code' => 'customer.languageid',
141
			'internalcode' => 'fos."langid"',
142
			'type' => 'string',
143
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
144
		),
145
		'customer.countryid' => array(
146
			'label' => 'Customer country',
147
			'code' => 'customer.countryid',
148
			'internalcode' => 'fos."countryid"',
149
			'type' => 'string',
150
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
151
		),
152
		'customer.telephone' => array(
153
			'label' => 'Customer telephone',
154
			'code' => 'customer.telephone',
155
			'internalcode' => 'fos."telephone"',
156
			'type' => 'string',
157
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
158
		),
159
		'customer.email' => array(
160
			'label' => 'Customer email',
161
			'code' => 'customer.email',
162
			'internalcode' => 'fos."email"',
163
			'type' => 'string',
164
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
165
		),
166
		'customer.telefax' => array(
167
			'label' => 'Customer telefax',
168
			'code' => 'customer.telefax',
169
			'internalcode' => 'fos."telefax"',
170
			'type' => 'string',
171
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
172
		),
173
		'customer.website' => array(
174
			'label' => 'Customer website',
175
			'code' => 'customer.website',
176
			'internalcode' => 'fos."website"',
177
			'type' => 'string',
178
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
179
		),
180
		'customer.longitude' => array(
181
			'label' => 'Customer longitude',
182
			'code' => 'customer.longitude',
183
			'internalcode' => 'fos."longitude"',
184
			'type' => 'float',
185
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT,
186
		),
187
		'customer.latitude' => array(
188
			'label' => 'Customer latitude',
189
			'code' => 'customer.latitude',
190
			'internalcode' => 'fos."latitude"',
191
			'type' => 'float',
192
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT,
193
		),
194
		'customer.birthday' => array(
195
			'label' => 'Customer birthday',
196
			'code' => 'customer.birthday',
197
			'internalcode' => 'fos."birthday"',
198
			'type' => 'string',
199
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
200
		),
201
		'customer.password'=> array(
202
			'label' => 'Customer password',
203
			'code' => 'customer.password',
204
			'internalcode' => 'fos."password"',
205
			'type' => 'string',
206
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
207
		),
208
		'customer.status'=> array(
209
			'label' => 'Customer status',
210
			'code' => 'customer.status',
211
			'internalcode' => 'fos."enabled"',
212
			'type' => 'integer',
213
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_BOOL,
214
		),
215
		'customer.dateverified'=> array(
216
			'label' => 'Customer verification date',
217
			'code' => 'customer.dateverified',
218
			'internalcode' => 'fos."vdate"',
219
			'type' => 'date',
220
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
221
		),
222
		'customer.ctime'=> array(
223
			'label' => 'Customer creation time',
224
			'code' => 'customer.ctime',
225
			'internalcode' => 'fos."ctime"',
226
			'type' => 'datetime',
227
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
228
		),
229
		'customer.mtime'=> array(
230
			'label' => 'Customer modification time',
231
			'code' => 'customer.mtime',
232
			'internalcode' => 'fos."mtime"',
233
			'type' => 'datetime',
234
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
235
		),
236
		'customer.editor'=> array(
237
			'label' => 'Customer editor',
238
			'code' => 'customer.editor',
239
			'internalcode' => 'fos."editor"',
240
			'type'=> 'string',
241
			'internaltype'=> \Aimeos\MW\DB\Statement\Base::PARAM_STR,
242
		),
243
		'customer:has' => array(
244
			'code' => 'customer:has()',
245
			'internalcode' => ':site AND :key AND fosli."id"',
246
			'internaldeps' => ['LEFT JOIN "fos_user_list" AS fosli ON ( fosli."parentid" = fos."id" )'],
247
			'label' => 'Customer has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
248
			'type' => 'null',
249
			'internaltype' => 'null',
250
			'public' => false,
251
		),
252
		'customer:prop' => array(
253
			'code' => 'customer:prop()',
254
			'internalcode' => ':site AND :key AND fospr."id"',
255
			'internaldeps' => ['LEFT JOIN "fos_user_property" AS fospr ON ( fospr."parentid" = fos."id" )'],
256
			'label' => 'Customer has property item, parameter(<property type>[,<language code>[,<property value>]])',
257
			'type' => 'null',
258
			'internaltype' => 'null',
259
			'public' => false,
260
		),
261
	);
262
263
264
	/**
265
	 * Initializes the object.
266
	 *
267
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
268
	 */
269
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
270
	{
271
		parent::__construct( $context );
272
273
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
274
		$level = $context->getConfig()->get( 'mshop/customer/manager/sitemode', $level );
275
276
277
		$this->searchConfig['customer:has']['function'] = function( &$source, array $params ) use ( $level ) {
278
279
			array_walk_recursive( $params, function( &$v ) {
280
				$v = trim( $v, '\'' );
281
			} );
282
283
			$keys = [];
284
			$params[1] = isset( $params[1] ) ? $params[1] : '';
285
			$params[2] = isset( $params[2] ) ? $params[2] : '';
286
287
			foreach( (array) $params[1] as $type ) {
288
				foreach( (array) $params[2] as $id ) {
289
					$keys[] = $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id;
290
				}
291
			}
292
293
			$sitestr = $this->getSiteString( 'fosli."siteid"', $level );
294
			$keystr = $this->toExpression( 'fosli."key"', $keys, $params[2] !== '' ? '==' : '=~' );
295
			$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
296
297
			return $params;
298
		};
299
300
301
		$this->searchConfig['customer:prop']['function'] = function( &$source, array $params ) use ( $level ) {
302
303
			array_walk_recursive( $params, function( &$v ) {
304
				$v = trim( $v, '\'' );
305
			} );
306
307
			$keys = [];
308
			$params[1] = array_key_exists( 1, $params ) ? $params[1] : '';
309
			$params[2] = isset( $params[2] ) ? $params[2] : '';
310
311
			foreach( (array) $params[1] as $lang ) {
312
				foreach( (array) $params[2] as $id ) {
313
					$keys[] = $params[0] . '|' . ( $lang ? $lang . '|' : '' ) . ( $id !== '' ?  md5( $id ) : '' );
314
				}
315
			}
316
317
			$sitestr = $this->getSiteString( 'fospr."siteid"', $level );
318
			$keystr = $this->toExpression( 'fospr."key"', $keys, $params[2] !== '' ? '==' : '=~' );
319
			$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
320
321
			return $params;
322
		};
323
	}
324
325
326
	/**
327
	 * Removes old entries from the storage.
328
	 *
329
	 * @param string[] $siteids List of IDs for sites whose entries should be deleted
330
	 * @return \Aimeos\MShop\Common\Manager\Iface Same object for fluent interface
331
	 */
332
	public function clear( array $siteids ) : \Aimeos\MShop\Common\Manager\Iface
333
	{
334
		$path = 'mshop/customer/manager/submanagers';
335
		$default = ['address', 'group', 'lists', 'property'];
336
337
		foreach( $this->getContext()->getConfig()->get( $path, $default ) as $domain ) {
338
			$this->getObject()->getSubManager( $domain )->clear( $siteids );
339
		}
340
341
		return $this->clearBase( $siteids, 'mshop/customer/manager/fosuser/delete' );
342
	}
343
344
345
	/**
346
	 * Removes multiple items.
347
	 *
348
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
349
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
350
	 */
351
	public function deleteItems( array $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
352
	{
353
		$path = 'mshop/customer/manager/fosuser/delete';
354
		return $this->deleteItemsBase( $itemIds, $path )->deleteRefItems( $itemIds );
355
	}
356
357
358
	/**
359
	 * Returns the list attributes that can be used for searching.
360
	 *
361
	 * @param bool $withsub Return also attributes of sub-managers if true
362
	 * @return array List of attribute items implementing \Aimeos\MW\Criteria\Attribute\Iface
363
	 */
364
	public function getSearchAttributes( bool $withsub = true ) : array
365
	{
366
		$path = 'mshop/customer/manager/submanagers';
367
		return $this->getSearchAttributesBase( $this->searchConfig, $path, ['address'], $withsub );
368
	}
369
370
371
	/**
372
	 * Saves a customer item object.
373
	 *
374
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
375
	 * @param bool $fetch True if the new ID should be returned in the item
376
	 * @return \Aimeos\MShop\Customer\Item\Iface $item Updated item including the generated ID
377
	 */
378
	public function saveItem( \Aimeos\MShop\Customer\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Customer\Item\Iface
379
	{
380
		$item = $this->addGroups( $item );
381
382
		if( !$item->isModified() )
383
		{
384
			$item = $this->savePropertyItems( $item, 'customer' );
385
			$item = $this->saveAddressItems( $item, 'customer' );
386
			return $this->saveListItems( $item, 'customer' );
387
		}
388
389
		$context = $this->getContext();
390
		$dbm = $context->getDatabaseManager();
391
		$dbname = $this->getResourceName();
392
		$conn = $dbm->acquire( $dbname );
393
394
		try
395
		{
396
			$id = $item->getId();
397
			$date = date( 'Y-m-d H:i:s' );
398
			$billingAddress = $item->getPaymentAddress();
399
			$columns = $this->getObject()->getSaveAttributes();
400
401
			if( $id === null )
402
			{
403
				/** mshop/customer/manager/fosuser/insert
404
				 * Inserts a new customer record into the database table
405
				 *
406
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
407
				 * the database and the newly created ID retrieved afterwards
408
				 * using the "newid" SQL statement.
409
				 *
410
				 * The SQL statement must be a string suitable for being used as
411
				 * prepared statement. It must include question marks for binding
412
				 * the values from the customer item to the statement before they are
413
				 * sent to the database server. The number of question marks must
414
				 * be the same as the number of columns listed in the INSERT
415
				 * statement. The order of the columns must correspond to the
416
				 * order in the saveItems() method, so the correct values are
417
				 * bound to the columns.
418
				 *
419
				 * The SQL statement should conform to the ANSI standard to be
420
				 * compatible with most relational database systems. This also
421
				 * includes using double quotes for table and column names.
422
				 *
423
				 * @param string SQL statement for inserting records
424
				 * @since 2015.01
425
				 * @category Developer
426
				 * @see mshop/customer/manager/fosuser/update
427
				 * @see mshop/customer/manager/fosuser/newid
428
				 * @see mshop/customer/manager/fosuser/delete
429
				 * @see mshop/customer/manager/fosuser/search
430
				 * @see mshop/customer/manager/fosuser/count
431
				 */
432
				$path = 'mshop/customer/manager/fosuser/insert';
433
				$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

433
				$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
434
			}
435
			else
436
			{
437
				/** mshop/customer/manager/fosuser/update
438
				 * Updates an existing customer record in the database
439
				 *
440
				 * Items which already have an ID (i.e. the ID is not NULL) will
441
				 * be updated in the database.
442
				 *
443
				 * The SQL statement must be a string suitable for being used as
444
				 * prepared statement. It must include question marks for binding
445
				 * the values from the customer item to the statement before they are
446
				 * sent to the database server. The order of the columns must
447
				 * correspond to the order in the saveItems() method, so the
448
				 * correct values are bound to the columns.
449
				 *
450
				 * The SQL statement should conform to the ANSI standard to be
451
				 * compatible with most relational database systems. This also
452
				 * includes using double quotes for table and column names.
453
				 *
454
				 * @param string SQL statement for updating records
455
				 * @since 2015.01
456
				 * @category Developer
457
				 * @see mshop/customer/manager/fosuser/insert
458
				 * @see mshop/customer/manager/fosuser/newid
459
				 * @see mshop/customer/manager/fosuser/delete
460
				 * @see mshop/customer/manager/fosuser/search
461
				 * @see mshop/customer/manager/fosuser/count
462
				 */
463
				$path = 'mshop/customer/manager/fosuser/update';
464
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
465
			}
466
467
			$idx = 1;
468
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
469
470
			foreach( $columns as $name => $entry ) {
471
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
472
			}
473
474
			$stmt->bind( $idx++, $item->getCode() ); // canonical username
475
			$stmt->bind( $idx++, $item->getCode() ); // username
476
			$stmt->bind( $idx++, $billingAddress->getCompany() );
477
			$stmt->bind( $idx++, $billingAddress->getVatID() );
478
			$stmt->bind( $idx++, $billingAddress->getSalutation() );
479
			$stmt->bind( $idx++, $billingAddress->getTitle() );
480
			$stmt->bind( $idx++, $billingAddress->getFirstname() );
481
			$stmt->bind( $idx++, $billingAddress->getLastname() );
482
			$stmt->bind( $idx++, $billingAddress->getAddress1() );
483
			$stmt->bind( $idx++, $billingAddress->getAddress2() );
484
			$stmt->bind( $idx++, $billingAddress->getAddress3() );
485
			$stmt->bind( $idx++, $billingAddress->getPostal() );
486
			$stmt->bind( $idx++, $billingAddress->getCity() );
487
			$stmt->bind( $idx++, $billingAddress->getState() );
488
			$stmt->bind( $idx++, $billingAddress->getCountryId() );
489
			$stmt->bind( $idx++, $billingAddress->getLanguageId() );
490
			$stmt->bind( $idx++, $billingAddress->getTelephone() );
491
			$stmt->bind( $idx++, $billingAddress->getEmail() );
492
			$stmt->bind( $idx++, $billingAddress->getEmail() );
493
			$stmt->bind( $idx++, $billingAddress->getTelefax() );
494
			$stmt->bind( $idx++, $billingAddress->getWebsite() );
495
			$stmt->bind( $idx++, $billingAddress->getLongitude(), \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT );
496
			$stmt->bind( $idx++, $billingAddress->getLatitude(), \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT );
497
			$stmt->bind( $idx++, $billingAddress->getBirthday() );
498
			$stmt->bind( $idx++, ( $item->getStatus() > 0 ? true : false ), \Aimeos\MW\DB\Statement\Base::PARAM_BOOL );
499
			$stmt->bind( $idx++, $item->getDateVerified() );
500
			$stmt->bind( $idx++, $item->getPassword() );
501
			$stmt->bind( $idx++, $date ); // Modification time
502
			$stmt->bind( $idx++, $context->getEditor() );
503
			$stmt->bind( $idx++, serialize( $item->getRoles() ) );
504
			$stmt->bind( $idx++, $item->getSalt() );
505
			$stmt->bind( $idx++, $context->getLocale()->getSiteId() );
506
507
			if( $id !== null ) {
508
				$stmt->bind( $idx, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
509
				$item->setId( $id );
510
			} else {
511
				$stmt->bind( $idx, $date ); // Creation time
512
			}
513
514
			$stmt->execute()->finish();
515
516
			if( $id === null && $fetch === true )
517
			{
518
				/** mshop/customer/manager/fosuser/newid
519
				 * Retrieves the ID generated by the database when inserting a new record
520
				 *
521
				 * As soon as a new record is inserted into the database table,
522
				 * the database server generates a new and unique identifier for
523
				 * that record. This ID can be used for retrieving, updating and
524
				 * deleting that specific record from the table again.
525
				 *
526
				 * For MySQL:
527
				 *  SELECT LAST_INSERT_ID()
528
				 * For PostgreSQL:
529
				 *  SELECT currval('seq_mcus_id')
530
				 * For SQL Server:
531
				 *  SELECT SCOPE_IDENTITY()
532
				 * For Oracle:
533
				 *  SELECT "seq_mcus_id".CURRVAL FROM DUAL
534
				 *
535
				 * There's no way to retrive the new ID by a SQL statements that
536
				 * fits for most database servers as they implement their own
537
				 * specific way.
538
				 *
539
				 * @param string SQL statement for retrieving the last inserted record ID
540
				 * @since 2015.01
541
				 * @category Developer
542
				 * @see mshop/customer/manager/fosuser/insert
543
				 * @see mshop/customer/manager/fosuser/update
544
				 * @see mshop/customer/manager/fosuser/delete
545
				 * @see mshop/customer/manager/fosuser/search
546
				 * @see mshop/customer/manager/fosuser/count
547
				 */
548
				$path = 'mshop/customer/manager/fosuser/newid';
549
				$item->setId( $this->newId( $conn, $path ) );
550
			}
551
552
			$dbm->release( $conn, $dbname );
553
		}
554
		catch( \Exception $e )
555
		{
556
			$dbm->release( $conn, $dbname );
557
			throw $e;
558
		}
559
560
		$item = $this->savePropertyItems( $item, 'customer' );
561
		$item = $this->saveAddressItems( $item, 'customer' );
562
		return $this->saveListItems( $item, 'customer' );
563
	}
564
565
566
	/**
567
	 * Returns the item objects matched by the given search criteria.
568
	 *
569
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
570
	 * @param integer &$total Number of items that are available in total
571
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Customer\Item\Iface
572
	 * @throws \Aimeos\MShop\Customer\Exception If creating items failed
573
	 */
574
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
575
	{
576
		$dbm = $this->getContext()->getDatabaseManager();
577
		$dbname = $this->getResourceName();
578
		$conn = $dbm->acquire( $dbname );
579
		$map = [];
580
581
		try
582
		{
583
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
584
			$cfgPathSearch = 'mshop/customer/manager/fosuser/search';
585
			$cfgPathCount = 'mshop/customer/manager/fosuser/count';
586
			$ref[] = 'customer/group';
587
			$required = ['customer'];
588
589
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
590
591
			while( ( $row = $results->fetch() ) !== null ) {
592
				$map[(string) $row['customer.id']] = $row;
593
			}
594
595
			$dbm->release( $conn, $dbname );
596
		}
597
		catch( \Exception $e )
598
		{
599
			$dbm->release( $conn, $dbname );
600
			throw $e;
601
		}
602
603
		$addrItems = [];
604
		if( in_array( 'customer/address', $ref, true ) ) {
605
			$addrItems = $this->getAddressItems( array_keys( $map ), 'customer' );
606
		}
607
608
		$propItems = []; $name = 'customer/property';
609
		if( isset( $ref[$name] ) || in_array( $name, $ref, true ) )
610
		{
611
			$propTypes = isset( $ref[$name] ) && is_array( $ref[$name] ) ? $ref[$name] : null;
612
			$propItems = $this->getPropertyItems( array_keys( $map ), 'customer', $propTypes );
613
		}
614
615
		return $this->buildItems( $map, $ref, 'customer', $addrItems, $propItems );
616
	}
617
618
619
	/**
620
	 * Returns a new manager for customer extensions
621
	 *
622
	 * @param string $manager Name of the sub manager type in lower case
623
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
624
	 * @return mixed Manager for different extensions, e.g stock, tags, locations, etc.
625
	 */
626
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
627
	{
628
		return $this->getSubManagerBase( 'customer', $manager, ( $name === null ? 'FosUser' : $name ) );
629
	}
630
631
632
	/**
633
	 * Creates a new customer item.
634
	 *
635
	 * @param array $values List of attributes for customer item
636
	 * @param \Aimeos\MShop\Common\Lists\Item\Iface[] $listItems List of list items
637
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
638
	 * @param \Aimeos\MShop\Common\Item\Address\Iface[] $addrItems List of referenced address items
639
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items
640
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item
641
	 */
642
	protected function createItemBase( array $values = [], array $listItems = [], array $refItems = [],
643
		array $addrItems = [], array $propItems = [] ) : \Aimeos\MShop\Common\Item\Iface
644
	{
645
		if( isset( $values['roles'] ) ) {
646
			$values['roles'] = unserialize( $values['roles'] );
647
		}
648
649
		$helper = $this->getPasswordHelper();
650
		$address = new \Aimeos\MShop\Common\Item\Address\Simple( 'customer.', $values );
651
652
		return new \Aimeos\MShop\Customer\Item\FosUser(
653
			$address, $values, $listItems, $refItems, $addrItems, $propItems, $helper
654
		);
655
	}
656
}
657