Passed
Push — master ( 2f39eb...6aed5e )
by Aimeos
01:41
created

Typo3::saveItem()   F

Complexity

Conditions 10
Paths 710

Size

Total Lines 190
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 71
c 6
b 0
f 0
dl 0
loc 190
rs 3.7549
cc 10
nc 710
nop 2

How to fix   Long Method    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, 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 cleanup( 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 )->cleanup( $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\Common\Item\Iface $item Updated item including the generated ID
417
	 */
418
	public function saveItem( \Aimeos\MShop\Common\Item\Iface $item, $fetch = true )
419
	{
420
		self::checkClass( '\\Aimeos\\MShop\\Customer\\Item\\Iface', $item );
421
422
		if( !$item->isModified() )
423
		{
424
			$item = $this->savePropertyItems( $item, 'customer' );
425
			$item = $this->saveAddressItems( $item, 'customer' );
426
			return $this->saveListItems( $item, 'customer' );
427
		}
428
429
		$context = $this->getContext();
430
		$dbm = $context->getDatabaseManager();
431
		$dbname = $this->getResourceName();
432
		$conn = $dbm->acquire( $dbname );
433
434
		try
435
		{
436
			$id = $item->getId();
437
			$billingAddress = $item->getPaymentAddress();
438
			$columns = $this->getObject()->getSaveAttributes();
0 ignored issues
show
Bug introduced by
The method getSaveAttributes() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean getSearchAttributes()? ( Ignorable by Annotation )

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

438
			$columns = $this->getObject()->/** @scrutinizer ignore-call */ getSaveAttributes();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
439
440
			if( $id === null )
441
			{
442
				/** mshop/customer/manager/typo3/insert
443
				 * Inserts a new customer record into the database table
444
				 *
445
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
446
				 * the database and the newly created ID retrieved afterwards
447
				 * using the "newid" SQL statement.
448
				 *
449
				 * The SQL statement must be a string suitable for being used as
450
				 * prepared statement. It must include question marks for binding
451
				 * the values from the customer item to the statement before they are
452
				 * sent to the database server. The number of question marks must
453
				 * be the same as the number of columns listed in the INSERT
454
				 * statement. The order of the columns must correspond to the
455
				 * order in the saveItems() method, so the correct values are
456
				 * bound to the columns.
457
				 *
458
				 * The SQL statement should conform to the ANSI standard to be
459
				 * compatible with most relational database systems. This also
460
				 * includes using double quotes for table and column names.
461
				 *
462
				 * @param string SQL statement for inserting records
463
				 * @since 2014.03
464
				 * @category Developer
465
				 * @see mshop/customer/manager/typo3/update
466
				 * @see mshop/customer/manager/typo3/newid
467
				 * @see mshop/customer/manager/typo3/delete
468
				 * @see mshop/customer/manager/typo3/search
469
				 * @see mshop/customer/manager/typo3/count
470
				 */
471
				$path = 'mshop/customer/manager/typo3/insert';
472
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) );
473
			}
474
			else
475
			{
476
				/** mshop/customer/manager/typo3/update
477
				 * Updates an existing customer record in the database
478
				 *
479
				 * Items which already have an ID (i.e. the ID is not NULL) will
480
				 * be updated in the database.
481
				 *
482
				 * The SQL statement must be a string suitable for being used as
483
				 * prepared statement. It must include question marks for binding
484
				 * the values from the customer item to the statement before they are
485
				 * sent to the database server. The order of the columns must
486
				 * correspond to the order in the saveItems() method, so the
487
				 * correct values are bound to the columns.
488
				 *
489
				 * The SQL statement should conform to the ANSI standard to be
490
				 * compatible with most relational database systems. This also
491
				 * includes using double quotes for table and column names.
492
				 *
493
				 * @param string SQL statement for updating records
494
				 * @since 2014.03
495
				 * @category Developer
496
				 * @see mshop/customer/manager/typo3/insert
497
				 * @see mshop/customer/manager/typo3/newid
498
				 * @see mshop/customer/manager/typo3/delete
499
				 * @see mshop/customer/manager/typo3/search
500
				 * @see mshop/customer/manager/typo3/count
501
				 */
502
				$path = 'mshop/customer/manager/typo3/update';
503
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
504
			}
505
506
			$address = $billingAddress->getAddress1();
507
508
			if( ( $part = $billingAddress->getAddress2() ) != '' ) {
509
				$address .= ' ' . $part;
510
			}
511
512
			if( ( $part = $billingAddress->getAddress3() ) != '' ) {
513
				$address .= ' ' . $part;
514
			}
515
516
			$idx = 1;
517
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
518
519
			foreach( $columns as $name => $entry ) {
520
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
0 ignored issues
show
Bug introduced by
The method get() does not exist on Aimeos\MShop\Common\Item\Iface. Did you maybe mean getId()? ( Ignorable by Annotation )

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

520
				$stmt->bind( $idx++, $item->/** @scrutinizer ignore-call */ get( $name ), $entry->getInternalType() );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
521
			}
522
523
			// TYPO3 fe_users.static_info_country is a three letter ISO code instead a two letter one
524
			$stmt->bind( $idx++, $context->getLocale()->getSiteId(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
525
			$stmt->bind( $idx++, $item->getLabel() );
526
			$stmt->bind( $idx++, $item->getCode() );
527
			$stmt->bind( $idx++, $this->plugins['customer.salutation']->translate( $billingAddress->getSalutation() ), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
528
			$stmt->bind( $idx++, $billingAddress->getCompany() );
529
			$stmt->bind( $idx++, $billingAddress->getVatID() );
530
			$stmt->bind( $idx++, $billingAddress->getTitle() );
531
			$stmt->bind( $idx++, $billingAddress->getFirstname() );
532
			$stmt->bind( $idx++, $billingAddress->getLastname() );
533
			$stmt->bind( $idx++, $address );
534
			$stmt->bind( $idx++, $billingAddress->getPostal() );
535
			$stmt->bind( $idx++, $billingAddress->getCity() );
536
			$stmt->bind( $idx++, $billingAddress->getState() );
537
			$stmt->bind( $idx++, $billingAddress->getLanguageId() );
538
			$stmt->bind( $idx++, $billingAddress->getTelephone() );
539
			$stmt->bind( $idx++, $billingAddress->getEmail() );
540
			$stmt->bind( $idx++, $billingAddress->getTelefax() );
541
			$stmt->bind( $idx++, $billingAddress->getWebsite() );
542
			$stmt->bind( $idx++, $billingAddress->getLongitude(), \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT );
543
			$stmt->bind( $idx++, $billingAddress->getLatitude(), \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT );
544
			$stmt->bind( $idx++, $this->plugins['customer.birthday']->translate( $item->getBirthday() ), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
545
			$stmt->bind( $idx++, $this->plugins['customer.status']->translate( $item->getStatus() ), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
546
			$stmt->bind( $idx++, $item->getPassword() );
547
			$stmt->bind( $idx++, time(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // Modification time
548
			$stmt->bind( $idx++, $billingAddress->getCountryId() );
549
			$stmt->bind( $idx++, implode( ',', $item->getGroups() ) );
550
			$stmt->bind( $idx++, $this->pid, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // TYPO3 PID value
551
552
			if( $id !== null ) {
553
				$stmt->bind( $idx, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
554
				$item->setId( $id );
555
			} else {
556
				$stmt->bind( $idx, time(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // Creation time
557
			}
558
559
			$stmt->execute()->finish();
560
561
			if( $id === null && $fetch === true )
562
			{
563
				/** mshop/customer/manager/typo3/newid
564
				 * Retrieves the ID generated by the database when inserting a new record
565
				 *
566
				 * As soon as a new record is inserted into the database table,
567
				 * the database server generates a new and unique identifier for
568
				 * that record. This ID can be used for retrieving, updating and
569
				 * deleting that specific record from the table again.
570
				 *
571
				 * For MySQL:
572
				 *  SELECT LAST_INSERT_ID()
573
				 * For PostgreSQL:
574
				 *  SELECT currval('seq_mcus_id')
575
				 * For SQL Server:
576
				 *  SELECT SCOPE_IDENTITY()
577
				 * For Oracle:
578
				 *  SELECT "seq_mcus_id".CURRVAL FROM DUAL
579
				 *
580
				 * There's no way to retrive the new ID by a SQL statements that
581
				 * fits for most database servers as they implement their own
582
				 * specific way.
583
				 *
584
				 * @param string SQL statement for retrieving the last inserted record ID
585
				 * @since 2014.03
586
				 * @category Developer
587
				 * @see mshop/customer/manager/typo3/insert
588
				 * @see mshop/customer/manager/typo3/update
589
				 * @see mshop/customer/manager/typo3/delete
590
				 * @see mshop/customer/manager/typo3/search
591
				 * @see mshop/customer/manager/typo3/count
592
				 */
593
				$path = 'mshop/customer/manager/typo3/newid';
594
				$item->setId( $this->newId( $conn, $path ) );
595
			}
596
597
			$dbm->release( $conn, $dbname );
598
		}
599
		catch( \Exception $e )
600
		{
601
			$dbm->release( $conn, $dbname );
602
			throw $e;
603
		}
604
605
		$item = $this->savePropertyItems( $item, 'customer' );
606
		$item = $this->saveAddressItems( $item, 'customer' );
607
		return $this->saveListItems( $item, 'customer' );
608
	}
609
610
611
	/**
612
	 * Returns the item objects matched by the given search criteria.
613
	 *
614
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
615
	 * @param integer &$total Number of items that are available in total
616
	 * @return array List of items implementing \Aimeos\MShop\Customer\Item\Iface
617
	 * @throws \Aimeos\MShop\Customer\Exception If creating items failed
618
	 */
619
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
620
	{
621
		$dbm = $this->getContext()->getDatabaseManager();
622
		$dbname = $this->getResourceName();
623
		$conn = $dbm->acquire( $dbname );
624
		$map = [];
625
626
		try
627
		{
628
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
629
			$cfgPathSearch = 'mshop/customer/manager/typo3/search';
630
			$cfgPathCount = 'mshop/customer/manager/typo3/count';
631
			$required = array( 'customer' );
632
633
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level, $this->plugins );
634
			while( ( $row = $results->fetch() ) !== false ) {
635
				$map[(string) $row['customer.id']] = $row;
636
			}
637
638
			$dbm->release( $conn, $dbname );
639
		}
640
		catch( \Exception $e )
641
		{
642
			$dbm->release( $conn, $dbname  );
643
			throw $e;
644
		}
645
646
		$addrItems = [];
647
		if( in_array( 'customer/address', $ref, true ) ) {
648
			$addrItems = $this->getAddressItems( array_keys( $map ), 'customer' );
649
		}
650
651
		$propItems = [];
652
		if( in_array( 'customer/property', $ref, true ) ) {
653
			$propItems = $this->getPropertyItems( array_keys( $map ), 'customer' );
654
		}
655
656
		return $this->buildItems( $map, $ref, 'customer', $addrItems, $propItems );
657
	}
658
659
660
	/**
661
	 * Returns a new manager for customer extensions
662
	 *
663
	 * @param string $manager Name of the sub manager type in lower case
664
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
665
	 * @return mixed Manager for different extensions, e.g stock, tags, locations, etc.
666
	 */
667
	public function getSubManager( $manager, $name = null )
668
	{
669
		return $this->getSubManagerBase( 'customer', $manager, ( $name === null ? 'Typo3' : $name ) );
670
	}
671
672
673
	/**
674
	 * Creates a new customer item.
675
	 *
676
	 * @param array $values List of attributes for customer item
677
	 * @param \Aimeos\MShop\Common\Lists\Item\Iface[] $listItems List of list items
678
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
679
	 * @param \Aimeos\MShop\Common\Item\Address\Iface[] $addrItems List of referenced address items
680
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items
681
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item
682
	 */
683
	protected function createItemBase( array $values = [], array $listItems = [], array $refItems = [],
684
		array $addrItems = [], array $propItems = [] )
685
	{
686
		$helper = $this->getPasswordHelper();
687
		$values['customer.siteid'] = $this->getContext()->getLocale()->getSiteId();
688
689
		if( array_key_exists( 'date_of_birth', $values ) ) {
690
			$values['customer.birthday'] = $this->reverse['date_of_birth']->reverse( $values['date_of_birth'] );
691
		}
692
693
		if( array_key_exists( 'gender', $values ) ) {
694
			$values['customer.salutation'] = $this->reverse['gender']->reverse( $values['gender'] );
695
		}
696
697
		if( array_key_exists( 'disable', $values ) ) {
698
			$values['customer.status'] = $this->reverse['disable']->reverse( $values['disable'] );
699
		}
700
701
		if( array_key_exists( 'tstamp', $values ) ) {
702
			$values['customer.mtime'] = $this->reverse['tstamp']->reverse( $values['tstamp'] );
703
		}
704
705
		if( array_key_exists( 'crdate', $values ) ) {
706
			$values['customer.ctime'] = $this->reverse['crdate']->reverse( $values['crdate'] );
707
		}
708
709
		if( array_key_exists( 'groups', $values ) && $values['groups'] !== '' ) {
710
			$values['customer.groups'] = explode( ',', $values['groups'] );
711
		}
712
713
714
		$address = new \Aimeos\MShop\Common\Item\Address\Simple( 'customer.', $values );
715
716
		return new \Aimeos\MShop\Customer\Item\Standard(
717
			$address, $values, $listItems, $refItems, $addrItems, $propItems, $helper
718
		);
719
	}
720
721
722
	/**
723
	 * Returns a password helper object based on the configuration.
724
	 *
725
	 * @return \Aimeos\MShop\Common\Helper\Password\Iface Password helper object
726
	 * @throws \Aimeos\MShop\Exception If the name is invalid or the class isn't found
727
	 */
728
	protected function getPasswordHelper()
729
	{
730
		if( $this->helper ) {
731
			return $this->helper;
732
		}
733
734
		$classname = \Aimeos\MShop\Common\Helper\Password\Typo3::class;
735
736
		if( class_exists( $classname ) === false ) {
737
			throw new \Aimeos\MShop\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
738
		}
739
740
		$context = $this->getContext();
741
		$object = ( method_exists( $context, 'getHasherTypo3' ) ? $context->getHasherTypo3() : null );
742
743
		$helper = new $classname( array( 'object' => $object ) );
744
745
		return $this->helper = self::checkClass( \Aimeos\MShop\Common\Helper\Password\Iface::class, $helper );
746
	}
747
}
748