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

Typo3::getSearchAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 * @package MShop
7
 * @subpackage Customer
8
 */
9
10
11
namespace Aimeos\MShop\Customer\Manager\Group;
12
13
14
/**
15
 * TYPO3 implementation of the customer group class
16
 *
17
 * @package MShop
18
 * @subpackage Customer
19
 */
20
class Typo3
21
	extends \Aimeos\MShop\Customer\Manager\Group\Standard
22
{
23
	private $searchConfig = array(
24
		'customer.group.id' => array(
25
			'code' => 'customer.group.id',
26
			'internalcode' => 't3feg."uid"',
27
			'label' => 'Customer group ID',
28
			'type' => 'integer',
29
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
30
		),
31
		'customer.group.code' => array(
32
			'code' => 'customer.group.code',
33
			'internalcode' => 't3feg."title"',
34
			'label' => 'Customer group code',
35
			'type' => 'string',
36
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
37
		),
38
		'customer.group.label' => array(
39
			'code' => 'customer.group.label',
40
			'internalcode' => 't3feg."title"',
41
			'label' => 'Customer group label',
42
			'type' => 'string',
43
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
44
		),
45
		'customer.group.ctime'=> array(
46
			'code' => 'customer.group.ctime',
47
			'internalcode' => 't3feg."crdate"',
48
			'label' => 'Customer group creation time',
49
			'type' => 'datetime',
50
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
51
		),
52
		'customer.group.mtime'=> array(
53
			'code' => 'customer.group.mtime',
54
			'internalcode' => 't3feg."tstamp"',
55
			'label' => 'Customer group modification time',
56
			'type' => 'datetime',
57
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
58
		),
59
		'customer.group.editor'=> array(
60
			'code' => 'customer.group.editor',
61
			'internalcode' => '1',
62
			'label' => 'Customer group editor',
63
			'type' => 'string',
64
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
65
		),
66
	);
67
68
	private $plugins = [];
69
	private $reverse = [];
70
	private $pid;
71
72
73
	/**
74
	 * Initializes the customer group manager object
75
	 *
76
	 * @param \Aimeos\MShop\Context\Iface $context Context object with required objects
0 ignored issues
show
Bug introduced by
The type Aimeos\MShop\Context\Iface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
77
	 */
78
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
79
	{
80
		parent::__construct( $context );
81
82
		$plugin = new \Aimeos\MW\Criteria\Plugin\T3Datetime();
83
		$this->plugins['customer.ctime'] = $this->reverse['crdate'] = $plugin;
84
		$this->plugins['customer.mtime'] = $this->reverse['tstamp'] = $plugin;
85
86
		$this->pid = $context->getConfig()->get( 'mshop/customer/manager/typo3/pid-default', 0 );
87
		$this->pid = $context->getConfig()->get( 'mshop/customer/manager/group/typo3/pid-default', $this->pid );
88
	}
89
90
91
	/**
92
	 * Removes old entries from the database
93
	 *
94
	 * @param integer[] $siteids List of IDs for sites whose entries should be deleted
95
	 */
96
	public function clear( array $siteids )
97
	{
98
		$path = 'mshop/customer/manager/group/submanagers';
99
100
		foreach( $this->getContext()->getConfig()->get( $path, [] ) as $domain ) {
101
			$this->getObject()->getSubManager( $domain )->clear( $siteids );
102
		}
103
	}
104
105
106
	/**
107
	 * Removes multiple items specified by their IDs
108
	 *
109
	 * @param array $ids List of IDs
110
	 */
111
	public function deleteItems( array $ids )
112
	{
113
		/** mshop/customer/manager/group/typo3/delete/mysql
114
		 * Deletes the items matched by the given IDs from the database
115
		 *
116
		 * @see mshop/customer/manager/group/typo3/delete/ansi
117
		 */
118
119
		/** mshop/customer/manager/group/typo3/delete/ansi
120
		 * Deletes the items matched by the given IDs from the database
121
		 *
122
		 * Removes the records specified by the given IDs from the customer group
123
		 * database. The records must be from the site that is configured via the
124
		 * context item.
125
		 *
126
		 * The ":cond" placeholder is replaced by the name of the ID column and
127
		 * the given ID or list of IDs while the site ID is bound to the question
128
		 * mark.
129
		 *
130
		 * The SQL statement should conform to the ANSI standard to be
131
		 * compatible with most relational database systems. This also
132
		 * includes using double quotes for table and column names.
133
		 *
134
		 * @param string SQL statement for deleting items
135
		 * @since 2015.08
136
		 * @category Developer
137
		 * @see mshop/customer/manager/group/typo3/insert/ansi
138
		 * @see mshop/customer/manager/group/typo3/update/ansi
139
		 * @see mshop/customer/manager/group/typo3/newid/ansi
140
		 * @see mshop/customer/manager/group/typo3/search/ansi
141
		 * @see mshop/customer/manager/group/typo3/count/ansi
142
		 */
143
		$path = 'mshop/customer/manager/group/typo3/delete';
144
145
		return $this->deleteItemsBase( $ids, $path, false, 'uid' );
146
	}
147
148
149
	/**
150
	 * Returns the attributes that can be used for searching
151
	 *
152
	 * @param boolean $withsub Return attributes of sub-managers too if true
153
	 * @return array List of attribute items implementing \Aimeos\MW\Criteria\Attribute\Iface
154
	 */
155
	public function getSearchAttributes( $withsub = true )
156
	{
157
		$path = 'mshop/customer/manager/group/submanagers';
158
159
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
160
	}
161
162
163
	/**
164
	 * Returns a new manager for customer group extensions
165
	 *
166
	 * @param string $manager Name of the sub manager type in lower case
167
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
168
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions
169
	 */
170
	public function getSubManager( $manager, $name = null )
171
	{
172
		return $this->getSubManagerBase( 'customer/group', $manager, ( $name === null ? 'Typo3' : $name ) );
173
	}
174
175
176
	/**
177
	 * Inserts a new or updates an existing customer group item
178
	 *
179
	 * @param \Aimeos\MShop\Customer\Item\Group\Iface $item Customer group item
180
	 * @param boolean $fetch True if the new ID should be returned in the item
181
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface $item Updated item including the generated ID
182
	 */
183
	public function saveItem( \Aimeos\MShop\Customer\Item\Group\Iface $item, $fetch = true )
184
	{
185
		if( !$item->isModified() ) {
186
			return $item;
187
		}
188
189
		$context = $this->getContext();
190
191
		$dbm = $context->getDatabaseManager();
192
		$dbname = $this->getResourceName();
193
		$conn = $dbm->acquire( $dbname );
194
195
		try
196
		{
197
			$id = $item->getId();
198
			$columns = $this->getObject()->getSaveAttributes();
199
200
			if( $id === null )
201
			{
202
				/** mshop/customer/manager/group/typo3/insert/mysql
203
				 * Inserts a new customer group record into the database table
204
				 *
205
				 * @see mshop/customer/manager/group/typo3/insert/ansi
206
				 */
207
208
				/** mshop/customer/manager/group/typo3/insert/ansi
209
				 * Inserts a new customer group record into the database table
210
				 *
211
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
212
				 * the database and the newly created ID retrieved afterwards
213
				 * using the "newid" SQL statement.
214
				 *
215
				 * The SQL statement must be a string suitable for being used as
216
				 * prepared statement. It must include question marks for binding
217
				 * the values from the customer group item to the statement before
218
				 * they are sent to the database server. The number of question
219
				 * marks must be the same as the number of columns listed in the
220
				 * INSERT statement. The order of the columns must correspond to
221
				 * the order in the saveItems() method, so the correct values are
222
				 * bound to the columns.
223
				 *
224
				 * The SQL statement should conform to the ANSI standard to be
225
				 * compatible with most relational database systems. This also
226
				 * includes using double quotes for table and column names.
227
				 *
228
				 * @param string SQL statement for inserting records
229
				 * @since 2015.08
230
				 * @category Developer
231
				 * @see mshop/customer/manager/group/typo3/update/ansi
232
				 * @see mshop/customer/manager/group/typo3/newid/ansi
233
				 * @see mshop/customer/manager/group/typo3/delete/ansi
234
				 * @see mshop/customer/manager/group/typo3/search/ansi
235
				 * @see mshop/customer/manager/group/typo3/count/ansi
236
				 */
237
				$path = 'mshop/customer/manager/group/typo3/insert';
238
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ) );
239
			}
240
			else
241
			{
242
				/** mshop/customer/manager/group/typo3/update/mysql
243
				 * Updates an existing customer group record in the database
244
				 *
245
				 * @see mshop/customer/manager/group/typo3/update/ansi
246
				 */
247
248
				/** mshop/customer/manager/group/typo3/update/ansi
249
				 * Updates an existing customer group record in the database
250
				 *
251
				 * Items which already have an ID (i.e. the ID is not NULL) will
252
				 * be updated in the database.
253
				 *
254
				 * The SQL statement must be a string suitable for being used as
255
				 * prepared statement. It must include question marks for binding
256
				 * the values from the customer group item to the statement before
257
				 * they are sent to the database server. The order of the columns
258
				 * must correspond to the order in the saveItems() method, so the
259
				 * correct values are bound to the columns.
260
				 *
261
				 * The SQL statement should conform to the ANSI standard to be
262
				 * compatible with most relational database systems. This also
263
				 * includes using double quotes for table and column names.
264
				 *
265
				 * @param string SQL statement for updating records
266
				 * @since 2015.08
267
				 * @category Developer
268
				 * @see mshop/customer/manager/group/typo3/insert/ansi
269
				 * @see mshop/customer/manager/group/typo3/newid/ansi
270
				 * @see mshop/customer/manager/group/typo3/delete/ansi
271
				 * @see mshop/customer/manager/group/typo3/search/ansi
272
				 * @see mshop/customer/manager/group/typo3/count/ansi
273
				 */
274
				$path = 'mshop/customer/manager/group/typo3/update';
275
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
276
			}
277
278
			$idx = 1;
279
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
280
281
			foreach( $columns as $name => $entry ) {
282
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
283
			}
284
285
			$stmt->bind( $idx++, $this->pid, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
286
			$stmt->bind( $idx++, $item->getCode() );
287
			$stmt->bind( $idx++, $item->getLabel() );
288
			$stmt->bind( $idx++, time(), \Aimeos\MW\DB\Statement\Base::PARAM_INT ); // mtime
289
290
			if( $id !== null ) {
291
				$stmt->bind( $idx++, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
292
				$item->setId( $id );
293
			} else {
294
				$stmt->bind( $idx++, time() ); // ctime
295
			}
296
297
			$stmt->execute()->finish();
298
299
			if( $id === null && $fetch === true )
300
			{
301
				/** mshop/customer/manager/group/typo3/newid/mysql
302
				 * Retrieves the ID generated by the database when inserting a new record
303
				 *
304
				 * @see mshop/customer/manager/group/typo3/newid/ansi
305
				 */
306
307
				/** mshop/customer/manager/group/typo3/newid/ansi
308
				 * Retrieves the ID generated by the database when inserting a new record
309
				 *
310
				 * As soon as a new record is inserted into the database table,
311
				 * the database server generates a new and unique identifier for
312
				 * that record. This ID can be used for retrieving, updating and
313
				 * deleting that specific record from the table again.
314
				 *
315
				 * For MySQL:
316
				 *  SELECT LAST_INSERT_ID()
317
				 * For PostgreSQL:
318
				 *  SELECT currval('seq_mcus_id')
319
				 * For SQL Server:
320
				 *  SELECT SCOPE_IDENTITY()
321
				 * For Oracle:
322
				 *  SELECT "seq_mcus_id".CURRVAL FROM DUAL
323
				 *
324
				 * There's no way to retrive the new ID by a SQL statements that
325
				 * fits for most database servers as they implement their own
326
				 * specific way.
327
				 *
328
				 * @param string SQL statement for retrieving the last inserted record ID
329
				 * @since 2015.08
330
				 * @category Developer
331
				 * @see mshop/customer/manager/group/typo3/insert/ansi
332
				 * @see mshop/customer/manager/group/typo3/update/ansi
333
				 * @see mshop/customer/manager/group/typo3/delete/ansi
334
				 * @see mshop/customer/manager/group/typo3/search/ansi
335
				 * @see mshop/customer/manager/group/typo3/count/ansi
336
				 */
337
				$path = 'mshop/customer/manager/group/typo3/newid';
338
				$item->setId( $this->newId( $conn, $path ) );
339
			}
340
341
			$dbm->release( $conn, $dbname );
342
		}
343
		catch( \Exception $e )
344
		{
345
			$dbm->release( $conn, $dbname );
346
			throw $e;
347
		}
348
349
		return $item;
350
	}
351
352
353
	/**
354
	 * Returns the item objects matched by the given search criteria.
355
	 *
356
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
357
	 * @param array $ref List of domain items that should be fetched too
358
	 * @param integer &$total Number of items that are available in total
359
	 * @return array List of items implementing \Aimeos\MShop\Customer\Item\Group\Iface
360
	 * @throws \Aimeos\MShop\Exception If retrieving items failed
361
	 */
362
	public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [], &$total = null )
363
	{
364
		$map = [];
365
		$context = $this->getContext();
366
367
		$dbm = $context->getDatabaseManager();
368
		$dbname = $this->getResourceName();
369
		$conn = $dbm->acquire( $dbname );
370
371
		try
372
		{
373
			$required = array( 'customer.group' );
374
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
375
376
			/** mshop/customer/manager/group/typo3/search
377
			 * Retrieves the records matched by the given criteria in the database
378
			 *
379
			 * Fetches the records matched by the given criteria from the customer
380
			 * database. The records must be from one of the sites that are
381
			 * configured via the context item. If the current site is part of
382
			 * a tree of sites, the SELECT statement can retrieve all records
383
			 * from the current site and the complete sub-tree of sites.
384
			 *
385
			 * As the records can normally be limited by criteria from sub-managers,
386
			 * their tables must be joined in the SQL context. This is done by
387
			 * using the "internaldeps" property from the definition of the ID
388
			 * column of the sub-managers. These internal dependencies specify
389
			 * the JOIN between the tables and the used columns for joining. The
390
			 * ":joins" placeholder is then replaced by the JOIN strings from
391
			 * the sub-managers.
392
			 *
393
			 * To limit the records matched, conditions can be added to the given
394
			 * criteria object. It can contain comparisons like column names that
395
			 * must match specific values which can be combined by AND, OR or NOT
396
			 * operators. The resulting string of SQL conditions replaces the
397
			 * ":cond" placeholder before the statement is sent to the database
398
			 * server.
399
			 *
400
			 * If the records that are retrieved should be ordered by one or more
401
			 * columns, the generated string of column / sort direction pairs
402
			 * replaces the ":order" placeholder. In case no ordering is required,
403
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
404
			 * markers is removed to speed up retrieving the records. Columns of
405
			 * sub-managers can also be used for ordering the result set but then
406
			 * no index can be used.
407
			 *
408
			 * The number of returned records can be limited and can start at any
409
			 * number between the begining and the end of the result set. For that
410
			 * the ":size" and ":start" placeholders are replaced by the
411
			 * corresponding values from the criteria object. The default values
412
			 * are 0 for the start and 100 for the size value.
413
			 *
414
			 * The SQL statement should conform to the ANSI standard to be
415
			 * compatible with most relational database systems. This also
416
			 * includes using double quotes for table and column names.
417
			 *
418
			 * @param string SQL statement for searching items
419
			 * @since 2015.08
420
			 * @category Developer
421
			 * @see mshop/customer/manager/group/typo3/count
422
			 */
423
			$cfgPathSearch = 'mshop/customer/manager/group/typo3/search';
424
425
			/** mshop/customer/manager/group/typo3/count
426
			 * Counts the number of records matched by the given criteria in the database
427
			 *
428
			 * Counts all records matched by the given criteria from the customer
429
			 * database. The records must be from one of the sites that are
430
			 * configured via the context item. If the current site is part of
431
			 * a tree of sites, the statement can count all records from the
432
			 * current site and the complete sub-tree of sites.
433
			 *
434
			 * As the records can normally be limited by criteria from sub-managers,
435
			 * their tables must be joined in the SQL context. This is done by
436
			 * using the "internaldeps" property from the definition of the ID
437
			 * column of the sub-managers. These internal dependencies specify
438
			 * the JOIN between the tables and the used columns for joining. The
439
			 * ":joins" placeholder is then replaced by the JOIN strings from
440
			 * the sub-managers.
441
			 *
442
			 * To limit the records matched, conditions can be added to the given
443
			 * criteria object. It can contain comparisons like column names that
444
			 * must match specific values which can be combined by AND, OR or NOT
445
			 * operators. The resulting string of SQL conditions replaces the
446
			 * ":cond" placeholder before the statement is sent to the database
447
			 * server.
448
			 *
449
			 * Both, the strings for ":joins" and for ":cond" are the same as for
450
			 * the "search" SQL statement.
451
			 *
452
			 * Contrary to the "search" statement, it doesn't return any records
453
			 * but instead the number of records that have been found. As counting
454
			 * thousands of records can be a long running task, the maximum number
455
			 * of counted records is limited for performance reasons.
456
			 *
457
			 * The SQL statement should conform to the ANSI standard to be
458
			 * compatible with most relational database systems. This also
459
			 * includes using double quotes for table and column names.
460
			 *
461
			 * @param string SQL statement for counting items
462
			 * @since 2015.08
463
			 * @category Developer
464
			 * @see mshop/customer/manager/group/typo3/search
465
			 */
466
			$cfgPathCount = 'mshop/customer/manager/group/typo3/count';
467
468
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level );
469
470
			while( ( $row = $results->fetch() ) !== false ) {
471
				$map[(string) $row['customer.group.id']] = $this->createItemBase( $row );
472
			}
473
474
			$dbm->release( $conn, $dbname );
475
		}
476
		catch( \Exception $e )
477
		{
478
			$dbm->release( $conn, $dbname );
479
			throw $e;
480
		}
481
482
		return $map;
483
	}
484
485
486
	/**
487
	 * Creates a new customer item.
488
	 *
489
	 * @param array $values List of attributes for customer item
490
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item
491
	 */
492
	protected function createItemBase( array $values = [] )
493
	{
494
		$values['customer.group.siteid'] = $this->getContext()->getLocale()->getSiteId();
495
496
		if( array_key_exists( 'tstamp', $values ) ) {
497
			$values['customer.group.mtime'] = $this->reverse['tstamp']->reverse( $values['tstamp'] );
498
		}
499
500
		if( array_key_exists( 'crdate', $values ) ) {
501
			$values['customer.group.ctime'] = $this->reverse['crdate']->reverse( $values['crdate'] );
502
		}
503
504
		return new \Aimeos\MShop\Customer\Item\Group\Standard( $values );
505
	}
506
}
507