Passed
Push — master ( 32b43c...00ff20 )
by Aimeos
01:45
created

Typo3::deleteItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 19
rs 9.7998
cc 1
nc 1
nop 1
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), 2014-2018
7
 * @package MShop
8
 * @subpackage Customer
9
 */
10
11
12
namespace Aimeos\MShop\Customer\Manager;
13
14
15
/**
16
 * Typo3 implementation of the customer class.
17
 *
18
 * @package MShop
19
 * @subpackage Customer
20
 */
21
class Typo3
22
	extends \Aimeos\MShop\Customer\Manager\Standard
23
{
24
	private $searchConfig = array(
25
		// customer.siteid is only for informational purpuse, not for filtering
26
		'customer.id' => array(
27
			'label' => 'Customer ID',
28
			'code' => 'customer.id',
29
			'internalcode' => 't3feu."uid"',
30
			'type' => 'integer',
31
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
32
			'public' => false,
33
		),
34
		'customer.code' => array(
35
			'label' => 'Customer username',
36
			'code' => 'customer.code',
37
			'internalcode' => 't3feu."username"',
38
			'type' => 'string',
39
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR
40
		),
41
		'customer.label' => array(
42
			'label' => 'Customer name',
43
			'code' => 'customer.label',
44
			'internalcode' => 't3feu."name"',
45
			'type' => 'string',
46
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR
47
		),
48
		'customer.salutation' => array(
49
			'label' => 'Customer salutation',
50
			'code' => 'customer.salutation',
51
			'internalcode' => 't3feu."gender"',
52
			'type' => 'string',
53
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
54
		),
55
		'customer.company'=> array(
56
			'label' => 'Customer company',
57
			'code' => 'customer.company',
58
			'internalcode' => 't3feu."company"',
59
			'type' => 'string',
60
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
61
		),
62
		'customer.vatid'=> array(
63
			'label' => 'Customer VAT ID',
64
			'code' => 'customer.vatid',
65
			'internalcode' => 't3feu."vatid"',
66
			'type' => 'string',
67
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
68
		),
69
		'customer.title' => array(
70
			'label' => 'Customer title',
71
			'code' => 'customer.title',
72
			'internalcode' => 't3feu."title"',
73
			'type' => 'string',
74
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
75
		),
76
		'customer.firstname' => array(
77
			'label' => 'Customer firstname',
78
			'code' => 'customer.firstname',
79
			'internalcode' => 't3feu."first_name"',
80
			'type' => 'string',
81
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
82
		),
83
		'customer.lastname' => array(
84
			'label' => 'Customer lastname',
85
			'code' => 'customer.lastname',
86
			'internalcode' => 't3feu."last_name"',
87
			'type' => 'string',
88
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
89
		),
90
		'customer.address1' => array(
91
			'label' => 'Customer address part one',
92
			'code' => 'customer.address1',
93
			'internalcode' => 't3feu."address"',
94
			'type' => 'string',
95
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
96
		),
97
		'customer.address2' => array(
98
			'label' => 'Customer address part two',
99
			'code' => 'customer.address2',
100
			'internalcode' => 't3feu."address"',
101
			'type' => 'string',
102
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
103
		),
104
		'customer.address3' => array(
105
			'label' => 'Customer address part three',
106
			'code' => 'customer.address3',
107
			'internalcode' => 't3feu."address"',
108
			'type' => 'string',
109
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
110
		),
111
		'customer.postal' => array(
112
			'label' => 'Customer postal',
113
			'code' => 'customer.postal',
114
			'internalcode' => 't3feu."zip"',
115
			'type' => 'string',
116
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
117
		),
118
		'customer.city' => array(
119
			'label' => 'Customer city',
120
			'code' => 'customer.city',
121
			'internalcode' => 't3feu."city"',
122
			'type' => 'string',
123
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
124
		),
125
		'customer.state' => array(
126
			'label' => 'Customer state',
127
			'code' => 'customer.state',
128
			'internalcode' => 't3feu."zone"',
129
			'type' => 'string',
130
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
131
		),
132
		'customer.languageid' => array(
133
			'label' => 'Customer language',
134
			'code' => 'customer.languageid',
135
			'internalcode' => 't3feu."language"',
136
			'type' => 'string',
137
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
138
		),
139
		'customer.countryid' => array(
140
			'label' => 'Customer country',
141
			'code' => 'customer.countryid',
142
			'internalcode' => 'tsc."cn_iso_2"',
143
			'type' => 'string',
144
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
145
		),
146
		'customer.telephone' => array(
147
			'label' => 'Customer telephone',
148
			'code' => 'customer.telephone',
149
			'internalcode' => 't3feu."telephone"',
150
			'type' => 'string',
151
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
152
		),
153
		'customer.email' => array(
154
			'label' => 'Customer email',
155
			'code' => 'customer.email',
156
			'internalcode' => 't3feu."email"',
157
			'type' => 'string',
158
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
159
		),
160
		'customer.telefax' => array(
161
			'label' => 'Customer telefax',
162
			'code' => 'customer.telefax',
163
			'internalcode' => 't3feu."fax"',
164
			'type' => 'string',
165
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
166
		),
167
		'customer.website' => array(
168
			'label' => 'Customer website',
169
			'code' => 'customer.website',
170
			'internalcode' => 't3feu."www"',
171
			'type' => 'string',
172
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
173
		),
174
		'customer.longitude' => array(
175
			'label' => 'Customer longitude',
176
			'code' => 'customer.longitude',
177
			'internalcode' => 't3feu."longitude"',
178
			'type' => 'float',
179
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT,
180
		),
181
		'customer.latitude' => array(
182
			'label' => 'Customer latitude',
183
			'code' => 'customer.latitude',
184
			'internalcode' => 't3feu."latitude"',
185
			'type' => 'float',
186
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT,
187
		),
188
		'customer.birthday' => array(
189
			'label' => 'Customer birthday',
190
			'code' => 'customer.birthday',
191
			'internalcode' => 't3feu."date_of_birth"',
192
			'type' => 'string',
193
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
194
		),
195
		'customer.password'=> array(
196
			'label' => 'Customer password',
197
			'code' => 'customer.password',
198
			'internalcode' => 't3feu."password"',
199
			'type' => 'string',
200
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
201
		),
202
		'customer.status'=> array(
203
			'label' => 'Customer status',
204
			'code' => 'customer.status',
205
			'internalcode' => 't3feu."disable"',
206
			'type' => 'integer',
207
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT
208
		),
209
		'customer.dateverified'=> array(
210
			'label' => 'Customer verification date',
211
			'code' => 'customer.dateverified',
212
			'internalcode' => 't3feu."vdate"',
213
			'type' => 'date',
214
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
215
		),
216
		'customer.ctime'=> array(
217
			'label' => 'Customer creation time',
218
			'code' => 'customer.ctime',
219
			'internalcode' => 't3feu."crdate"',
220
			'type' => 'datetime',
221
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
222
		),
223
		'customer.mtime'=> array(
224
			'label' => 'Customer modification time',
225
			'code' => 'customer.mtime',
226
			'internalcode' => 't3feu."tstamp"',
227
			'type' => 'datetime',
228
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
229
		),
230
		// not available
231
		'customer.editor'=> array(
232
			'label' => 'Customer editor',
233
			'code' => 'customer.editor',
234
			'internalcode' => null,
235
			'type' => 'string',
236
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
237
		),
238
		'customer:has' => array(
239
			'code' => 'customer:has()',
240
			'internalcode' => '(
241
				SELECT t3feuli_has."id" FROM fe_users_list AS t3feuli_has
242
				WHERE t3feu."uid" = t3feuli_has."parentid" AND :site AND :key LIMIT 1
243
			)',
244
			'label' => 'Customer has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
245
			'type' => 'null',
246
			'internaltype' => 'null',
247
			'public' => false,
248
		),
249
		'customer:prop' => array(
250
			'code' => 'customer:prop()',
251
			'internalcode' => '(
252
				SELECT t3feupr_prop."id" FROM fe_users_property AS t3feupr_prop
253
				WHERE t3feu."uid" = t3feupr_prop."parentid" AND :site AND :key LIMIT 1
254
			)',
255
			'label' => 'Customer has property item, parameter(<property type>[,<language code>[,<property value>]])',
256
			'type' => 'null',
257
			'internaltype' => 'null',
258
			'public' => false,
259
		),
260
	);
261
262
263
	private $plugins = [];
264
	private $reverse = [];
265
	private $helper;
266
	private $pid;
267
268
269
270
	/**
271
	 * Initializes a new customer manager object using the given context object.
272
	 *
273
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects
274
	 */
275
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
276
	{
277
		parent::__construct( $context );
278
279
		$plugin = new \Aimeos\MW\Criteria\Plugin\T3Salutation();
280
		$this->plugins['customer.salutation'] = $this->reverse['gender'] = $plugin;
281
282
		$plugin = new \Aimeos\MW\Criteria\Plugin\T3Status();
283
		$this->plugins['customer.status'] = $this->reverse['disable'] = $plugin;
284
285
		$plugin = new \Aimeos\MW\Criteria\Plugin\T3Date();
286
		$this->plugins['customer.birthday'] = $this->reverse['date_of_birth'] = $plugin;
287
288
		$plugin = new \Aimeos\MW\Criteria\Plugin\T3Datetime();
289
		$this->plugins['customer.ctime'] = $this->reverse['crdate'] = $plugin;
290
		$this->plugins['customer.mtime'] = $this->reverse['tstamp'] = $plugin;
291
292
		$this->pid = $context->getConfig()->get( 'mshop/customer/manager/typo3/pid-default', 0 );
293
294
295
		$self = $this;
296
		$locale = $context->getLocale();
297
298
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
299
		$level = $context->getConfig()->get( 'mshop/customer/manager/sitemode', $level );
300
301
		$siteIds = [$locale->getSiteId()];
302
303
		if( $level & \Aimeos\MShop\Locale\Manager\Base::SITE_PATH ) {
304
			$siteIds = array_merge( $siteIds, $locale->getSitePath() );
305
		}
306
307
		if( $level & \Aimeos\MShop\Locale\Manager\Base::SITE_SUBTREE ) {
308
			$siteIds = array_merge( $siteIds, $locale->getSiteSubTree() );
309
		}
310
311
312
		$this->searchConfig['customer:has']['function'] = function( &$source, array $params ) use ( $self, $siteIds ) {
313
314
			foreach( $params as $key => $param ) {
315
				$params[$key] = trim( $param, '\'' );
316
			}
317
318
			$source = str_replace( ':site', $self->toExpression( 't3feuli_has."siteid"', $siteIds ), $source );
319
			$str = $self->toExpression( 't3feuli_has."key"', join( '|', $params ), isset( $params[2] ) ? '==' : '=~' );
320
			$source = str_replace( ':key', $str, $source );
321
322
			return $params;
323
		};
324
325
326
		$this->searchConfig['customer:prop']['function'] = function( &$source, array $params ) use ( $self, $siteIds ) {
327
328
			foreach( $params as $key => $param ) {
329
				$params[$key] = trim( $param, '\'' );
330
			}
331
332
			$params[2] = ( isset( $params[2] ) ? md5( $params[2] ) : null );
333
			$source = str_replace( ':site', $self->toExpression( 't3feupr_prop."siteid"', $siteIds ), $source );
334
			$str = $self->toExpression( 't3feupr_prop."key"', join( '|', $params ), isset( $params[2] ) ? '==' : '=~' );
335
			$source = str_replace( ':key', $str, $source );
336
337
			return $params;
338
		};
339
	}
340
341
342
	/**
343
	 * Removes old entries from the storage.
344
	 *
345
	 * @param array $siteids List of IDs for sites whose entries should be deleted
346
	 */
347
	public function clear( array $siteids )
348
	{
349
		$path = 'mshop/customer/manager/submanagers';
350
		$default = ['address', 'group', 'lists', 'property'];
351
352
		foreach( $this->getContext()->getConfig()->get( $path, $default ) as $domain ) {
353
			$this->getObject()->getSubManager( $domain )->clear( $siteids );
354
		}
355
	}
356
357
358
	/**
359
	 * Creates a new empty item instance
360
	 *
361
	 * @param array $values Values the item should be initialized with
362
	 * @return \Aimeos\MShop\Customer\Item\Iface New site item object
363
	 */
364
	public function createItem( array $values = [] )
365
	{
366
		$values['customer.siteid'] = $this->getContext()->getLocale()->getSiteId();
367
		return $this->createItemBase( $values );
368
	}
369
370
371
	/**
372
	 * Deletes a customer item object from the permanent storage.
373
	 *
374
	 * @param array $ids List of customer IDs
375
	 */
376
	public function deleteItems( array $ids )
377
	{
378
		$path = 'mshop/customer/manager/typo3/delete';
379
		$this->deleteItemsBase( $ids, $path, false, 'uid' );
380
381
		$manager = $this->getObject()->getSubManager( 'address' );
382
		$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
383
		$search->setConditions( $search->compare( '==', 'customer.address.parentid', $ids ) );
384
		$manager->deleteItems( array_keys( $manager->searchItems( $search ) ) );
385
386
		$manager = $this->getObject()->getSubManager( 'lists' );
387
		$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
388
		$search->setConditions( $search->compare( '==', 'customer.lists.parentid', $ids ) );
389
		$manager->deleteItems( array_keys( $manager->searchItems( $search ) ) );
390
391
		$manager = $this->getObject()->getSubManager( 'property' );
392
		$search = $manager->createSearch()->setSlice( 0, 0x7fffffff );
393
		$search->setConditions( $search->compare( '==', 'customer.property.parentid', $ids ) );
394
		$manager->deleteItems( array_keys( $manager->searchItems( $search ) ) );
395
	}
396
397
398
	/**
399
	 * Returns the list attributes that can be used for searching.
400
	 *
401
	 * @param boolean $withsub Return also attributes of sub-managers if true
402
	 * @return array List of attribute items implementing \Aimeos\MW\Criteria\Attribute\Iface
403
	 */
404
	public function getSearchAttributes( $withsub = true )
405
	{
406
		$path = 'mshop/customer/manager/submanagers';
407
		return $this->getSearchAttributesBase( $this->searchConfig, $path, ['address'], $withsub );
408
	}
409
410
411
	/**
412
	 * Saves a customer item object.
413
	 *
414
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
415
	 * @param boolean $fetch True if the new ID should be returned in the item
416
	 * @return \Aimeos\MShop\Customer\Item\Iface $item Updated item including the generated ID
417
	 */
418
	public function saveItem( \Aimeos\MShop\Customer\Item\Iface $item, $fetch = true )
419
	{
420
		if( !$item->isModified() )
421
		{
422
			$item = $this->savePropertyItems( $item, 'customer' );
423
			$item = $this->saveAddressItems( $item, 'customer' );
424
			return $this->saveListItems( $item, 'customer' );
425
		}
426
427
		$context = $this->getContext();
428
		$dbm = $context->getDatabaseManager();
429
		$dbname = $this->getResourceName();
430
		$conn = $dbm->acquire( $dbname );
431
432
		try
433
		{
434
			$id = $item->getId();
435
			$billingAddress = $item->getPaymentAddress();
436
			$columns = $this->getObject()->getSaveAttributes();
437
438
			if( $id === null )
439
			{
440
				/** mshop/customer/manager/typo3/insert
441
				 * Inserts a new customer record into the database table
442
				 *
443
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
444
				 * the database and the newly created ID retrieved afterwards
445
				 * using the "newid" SQL statement.
446
				 *
447
				 * The SQL statement must be a string suitable for being used as
448
				 * prepared statement. It must include question marks for binding
449
				 * the values from the customer item to the statement before they are
450
				 * sent to the database server. The number of question marks must
451
				 * be the same as the number of columns listed in the INSERT
452
				 * statement. The order of the columns must correspond to the
453
				 * order in the saveItems() method, so the correct values are
454
				 * bound to the columns.
455
				 *
456
				 * The SQL statement should conform to the ANSI standard to be
457
				 * compatible with most relational database systems. This also
458
				 * includes using double quotes for table and column names.
459
				 *
460
				 * @param string SQL statement for inserting records
461
				 * @since 2014.03
462
				 * @category Developer
463
				 * @see mshop/customer/manager/typo3/update
464
				 * @see mshop/customer/manager/typo3/newid
465
				 * @see mshop/customer/manager/typo3/delete
466
				 * @see mshop/customer/manager/typo3/search
467
				 * @see mshop/customer/manager/typo3/count
468
				 */
469
				$path = 'mshop/customer/manager/typo3/insert';
470
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) );
471
			}
472
			else
473
			{
474
				/** mshop/customer/manager/typo3/update
475
				 * Updates an existing customer record in the database
476
				 *
477
				 * Items which already have an ID (i.e. the ID is not NULL) will
478
				 * be updated in the database.
479
				 *
480
				 * The SQL statement must be a string suitable for being used as
481
				 * prepared statement. It must include question marks for binding
482
				 * the values from the customer item to the statement before they are
483
				 * sent to the database server. The order of the columns must
484
				 * correspond to the order in the saveItems() method, so the
485
				 * correct values are bound to the columns.
486
				 *
487
				 * The SQL statement should conform to the ANSI standard to be
488
				 * compatible with most relational database systems. This also
489
				 * includes using double quotes for table and column names.
490
				 *
491
				 * @param string SQL statement for updating records
492
				 * @since 2014.03
493
				 * @category Developer
494
				 * @see mshop/customer/manager/typo3/insert
495
				 * @see mshop/customer/manager/typo3/newid
496
				 * @see mshop/customer/manager/typo3/delete
497
				 * @see mshop/customer/manager/typo3/search
498
				 * @see mshop/customer/manager/typo3/count
499
				 */
500
				$path = 'mshop/customer/manager/typo3/update';
501
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
502
			}
503
504
			$address = $billingAddress->getAddress1();
505
506
			if( ( $part = $billingAddress->getAddress2() ) != '' ) {
507
				$address .= ' ' . $part;
508
			}
509
510
			if( ( $part = $billingAddress->getAddress3() ) != '' ) {
511
				$address .= ' ' . $part;
512
			}
513
514
			$idx = 1;
515
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
516
517
			foreach( $columns as $name => $entry ) {
518
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
519
			}
520
521
			// TYPO3 fe_users.static_info_country is a three letter ISO code instead a two letter one
522
			$stmt->bind( $idx++, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
523
			$stmt->bind( $idx++, $item->getLabel() );
524
			$stmt->bind( $idx++, $item->getCode() );
525
			$stmt->bind( $idx++, $this->plugins['customer.salutation']->translate( $billingAddress->getSalutation() ), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
526
			$stmt->bind( $idx++, $billingAddress->getCompany() );
527
			$stmt->bind( $idx++, $billingAddress->getVatID() );
528
			$stmt->bind( $idx++, $billingAddress->getTitle() );
529
			$stmt->bind( $idx++, $billingAddress->getFirstname() );
530
			$stmt->bind( $idx++, $billingAddress->getLastname() );
531
			$stmt->bind( $idx++, $address );
532
			$stmt->bind( $idx++, $billingAddress->getPostal() );
533
			$stmt->bind( $idx++, $billingAddress->getCity() );
534
			$stmt->bind( $idx++, $billingAddress->getState() );
535
			$stmt->bind( $idx++, $billingAddress->getLanguageId() );
536
			$stmt->bind( $idx++, $billingAddress->getTelephone() );
537
			$stmt->bind( $idx++, $billingAddress->getEmail() );
538
			$stmt->bind( $idx++, $billingAddress->getTelefax() );
539
			$stmt->bind( $idx++, $billingAddress->getWebsite() );
540
			$stmt->bind( $idx++, $billingAddress->getLongitude(), \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT );
541
			$stmt->bind( $idx++, $billingAddress->getLatitude(), \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT );
542
			$stmt->bind( $idx++, $this->plugins['customer.birthday']->translate( $item->getBirthday() ), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
543
			$stmt->bind( $idx++, $this->plugins['customer.status']->translate( $item->getStatus() ), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
544
			$stmt->bind( $idx++, $item->getPassword() );
545
			$stmt->bind( $idx++, time(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // Modification time
546
			$stmt->bind( $idx++, $billingAddress->getCountryId() );
547
			$stmt->bind( $idx++, implode( ',', $item->getGroups() ) );
548
			$stmt->bind( $idx++, $this->pid, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // TYPO3 PID value
549
550
			if( $id !== null ) {
551
				$stmt->bind( $idx, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
552
				$item->setId( $id );
553
			} else {
554
				$stmt->bind( $idx, time(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // Creation time
555
			}
556
557
			$stmt->execute()->finish();
558
559
			if( $id === null && $fetch === true )
560
			{
561
				/** mshop/customer/manager/typo3/newid
562
				 * Retrieves the ID generated by the database when inserting a new record
563
				 *
564
				 * As soon as a new record is inserted into the database table,
565
				 * the database server generates a new and unique identifier for
566
				 * that record. This ID can be used for retrieving, updating and
567
				 * deleting that specific record from the table again.
568
				 *
569
				 * For MySQL:
570
				 *  SELECT LAST_INSERT_ID()
571
				 * For PostgreSQL:
572
				 *  SELECT currval('seq_mcus_id')
573
				 * For SQL Server:
574
				 *  SELECT SCOPE_IDENTITY()
575
				 * For Oracle:
576
				 *  SELECT "seq_mcus_id".CURRVAL FROM DUAL
577
				 *
578
				 * There's no way to retrive the new ID by a SQL statements that
579
				 * fits for most database servers as they implement their own
580
				 * specific way.
581
				 *
582
				 * @param string SQL statement for retrieving the last inserted record ID
583
				 * @since 2014.03
584
				 * @category Developer
585
				 * @see mshop/customer/manager/typo3/insert
586
				 * @see mshop/customer/manager/typo3/update
587
				 * @see mshop/customer/manager/typo3/delete
588
				 * @see mshop/customer/manager/typo3/search
589
				 * @see mshop/customer/manager/typo3/count
590
				 */
591
				$path = 'mshop/customer/manager/typo3/newid';
592
				$item->setId( $this->newId( $conn, $path ) );
593
			}
594
595
			$dbm->release( $conn, $dbname );
596
		}
597
		catch( \Exception $e )
598
		{
599
			$dbm->release( $conn, $dbname );
600
			throw $e;
601
		}
602
603
		$item = $this->savePropertyItems( $item, 'customer' );
604
		$item = $this->saveAddressItems( $item, 'customer' );
605
		return $this->saveListItems( $item, 'customer' );
606
	}
607
608
609
	/**
610
	 * Returns the item objects matched by the given search criteria.
611
	 *
612
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
613
	 * @param integer &$total Number of items that are available in total
614
	 * @return array List of items implementing \Aimeos\MShop\Customer\Item\Iface
615
	 * @throws \Aimeos\MShop\Customer\Exception If creating items failed
616
	 */
617
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
618
	{
619
		$dbm = $this->getContext()->getDatabaseManager();
620
		$dbname = $this->getResourceName();
621
		$conn = $dbm->acquire( $dbname );
622
		$map = [];
623
624
		try
625
		{
626
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
627
			$cfgPathSearch = 'mshop/customer/manager/typo3/search';
628
			$cfgPathCount = 'mshop/customer/manager/typo3/count';
629
			$required = array( 'customer' );
630
631
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level, $this->plugins );
632
			while( ( $row = $results->fetch() ) !== false ) {
633
				$map[(string) $row['customer.id']] = $row;
634
			}
635
636
			$dbm->release( $conn, $dbname );
637
		}
638
		catch( \Exception $e )
639
		{
640
			$dbm->release( $conn, $dbname  );
641
			throw $e;
642
		}
643
644
		$addrItems = [];
645
		if( in_array( 'customer/address', $ref, true ) ) {
646
			$addrItems = $this->getAddressItems( array_keys( $map ), 'customer' );
647
		}
648
649
		$propItems = [];
650
		if( in_array( 'customer/property', $ref, true ) ) {
651
			$propItems = $this->getPropertyItems( array_keys( $map ), 'customer' );
652
		}
653
654
		return $this->buildItems( $map, $ref, 'customer', $addrItems, $propItems );
655
	}
656
657
658
	/**
659
	 * Returns a new manager for customer extensions
660
	 *
661
	 * @param string $manager Name of the sub manager type in lower case
662
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
663
	 * @return mixed Manager for different extensions, e.g stock, tags, locations, etc.
664
	 */
665
	public function getSubManager( $manager, $name = null )
666
	{
667
		return $this->getSubManagerBase( 'customer', $manager, ( $name === null ? 'Typo3' : $name ) );
668
	}
669
670
671
	/**
672
	 * Creates a new customer item.
673
	 *
674
	 * @param array $values List of attributes for customer item
675
	 * @param \Aimeos\MShop\Common\Lists\Item\Iface[] $listItems List of list items
676
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
677
	 * @param \Aimeos\MShop\Common\Item\Address\Iface[] $addrItems List of referenced address items
678
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items
679
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item
680
	 */
681
	protected function createItemBase( array $values = [], array $listItems = [], array $refItems = [],
682
		array $addrItems = [], array $propItems = [] )
683
	{
684
		$helper = $this->getPasswordHelper();
685
		$values['customer.siteid'] = $this->getContext()->getLocale()->getSiteId();
686
687
		if( array_key_exists( 'date_of_birth', $values ) ) {
688
			$values['customer.birthday'] = $this->reverse['date_of_birth']->reverse( $values['date_of_birth'] );
689
		}
690
691
		if( array_key_exists( 'gender', $values ) ) {
692
			$values['customer.salutation'] = $this->reverse['gender']->reverse( $values['gender'] );
693
		}
694
695
		if( array_key_exists( 'disable', $values ) ) {
696
			$values['customer.status'] = $this->reverse['disable']->reverse( $values['disable'] );
697
		}
698
699
		if( array_key_exists( 'tstamp', $values ) ) {
700
			$values['customer.mtime'] = $this->reverse['tstamp']->reverse( $values['tstamp'] );
701
		}
702
703
		if( array_key_exists( 'crdate', $values ) ) {
704
			$values['customer.ctime'] = $this->reverse['crdate']->reverse( $values['crdate'] );
705
		}
706
707
		if( array_key_exists( 'groups', $values ) && $values['groups'] !== '' ) {
708
			$values['customer.groups'] = explode( ',', $values['groups'] );
709
		}
710
711
712
		$address = new \Aimeos\MShop\Common\Item\Address\Simple( 'customer.', $values );
713
714
		return new \Aimeos\MShop\Customer\Item\Standard(
715
			$address, $values, $listItems, $refItems, $addrItems, $propItems, $helper
716
		);
717
	}
718
719
720
	/**
721
	 * Returns a password helper object based on the configuration.
722
	 *
723
	 * @return \Aimeos\MShop\Common\Helper\Password\Iface Password helper object
724
	 * @throws \Aimeos\MShop\Exception If the name is invalid or the class isn't found
725
	 */
726
	protected function getPasswordHelper()
727
	{
728
		if( $this->helper ) {
729
			return $this->helper;
730
		}
731
732
		$classname = \Aimeos\MShop\Common\Helper\Password\Typo3::class;
733
734
		if( class_exists( $classname ) === false ) {
735
			throw new \Aimeos\MShop\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
736
		}
737
738
		$context = $this->getContext();
739
		$object = ( method_exists( $context, 'getHasherTypo3' ) ? $context->getHasherTypo3() : null );
740
741
		$helper = new $classname( array( 'object' => $object ) );
742
743
		return $this->helper = self::checkClass( \Aimeos\MShop\Common\Helper\Password\Iface::class, $helper );
744
	}
745
}
746