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

Standard::__construct()   B

Complexity

Conditions 10
Paths 1

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package MShop
8
 * @subpackage Attribute
9
 */
10
11
12
namespace Aimeos\MShop\Attribute\Manager;
13
14
15
/**
16
 * Default attribute manager for creating and handling attributes.
17
 * @package MShop
18
 * @subpackage Attribute
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Manager\Base
22
	implements \Aimeos\MShop\Attribute\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	use \Aimeos\MShop\Common\Manager\ListsRef\Traits;
25
	use \Aimeos\MShop\Common\Manager\PropertyRef\Traits;
26
27
28
	private $searchConfig = array(
29
		'attribute.id' => array(
30
			'code' => 'attribute.id',
31
			'internalcode' => 'matt."id"',
32
			'label' => 'ID',
33
			'type' => 'integer',
34
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
35
			'public' => false,
36
		),
37
		'attribute.siteid' => array(
38
			'code' => 'attribute.siteid',
39
			'internalcode' => 'matt."siteid"',
40
			'label' => 'Site ID',
41
			'type' => 'string',
42
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
43
			'public' => false,
44
		),
45
		'attribute.key' => array(
46
			'code' => 'attribute.key',
47
			'internalcode' => 'matt."key"',
48
			'label' => 'Unique key',
49
			'type' => 'string',
50
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
51
			'public' => false,
52
		),
53
		'attribute.type' => array(
54
			'code' => 'attribute.type',
55
			'internalcode' => 'matt."type"',
56
			'label' => 'Type',
57
			'type' => 'integer',
58
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
59
			'public' => false,
60
		),
61
		'attribute.label' => array(
62
			'code' => 'attribute.label',
63
			'internalcode' => 'matt."label"',
64
			'label' => 'Label',
65
			'type' => 'string',
66
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
67
		),
68
		'attribute.code' => array(
69
			'code' => 'attribute.code',
70
			'internalcode' => 'matt."code"',
71
			'label' => 'Code',
72
			'type' => 'string',
73
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
74
		),
75
		'attribute.domain' => array(
76
			'code' => 'attribute.domain',
77
			'internalcode' => 'matt."domain"',
78
			'label' => 'Domain',
79
			'type' => 'string',
80
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
81
		),
82
		'attribute.position' => array(
83
			'code' => 'attribute.position',
84
			'internalcode' => 'matt."pos"',
85
			'label' => 'Position',
86
			'type' => 'integer',
87
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
88
			'public' => false,
89
		),
90
		'attribute.status' => array(
91
			'code' => 'attribute.status',
92
			'internalcode' => 'matt."status"',
93
			'label' => 'Status',
94
			'type' => 'integer',
95
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
96
		),
97
		'attribute.ctime' => array(
98
			'code' => 'attribute.ctime',
99
			'internalcode' => 'matt."ctime"',
100
			'label' => 'Create date/time',
101
			'type' => 'datetime',
102
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
103
			'public' => false,
104
		),
105
		'attribute.mtime' => array(
106
			'code' => 'attribute.mtime',
107
			'internalcode' => 'matt."mtime"',
108
			'label' => 'Modification date/time',
109
			'type' => 'datetime',
110
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
111
			'public' => false,
112
		),
113
		'attribute.editor' => array(
114
			'code' => 'attribute.editor',
115
			'internalcode' => 'matt."editor"',
116
			'label' => 'Editor',
117
			'type' => 'string',
118
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
119
			'public' => false,
120
		),
121
		'attribute:has' => array(
122
			'code' => 'attribute:has()',
123
			'internalcode' => ':site AND :key AND mattli."id"',
124
			'internaldeps' => ['LEFT JOIN "mshop_attribute_list" AS mattli ON ( mattli."parentid" = matt."id" )'],
125
			'label' => 'Attribute has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
126
			'type' => 'null',
127
			'internaltype' => 'null',
128
			'public' => false,
129
		),
130
		'attribute:prop' => array(
131
			'code' => 'attribute:prop()',
132
			'internalcode' => ':site AND :key AND mattpr."id"',
133
			'internaldeps' => ['LEFT JOIN "mshop_attribute_property" AS mattpr ON ( mattpr."parentid" = matt."id" )'],
134
			'label' => 'Attribute has property item, parameter(<property type>[,<language code>[,<property value>]])',
135
			'type' => 'null',
136
			'internaltype' => 'null',
137
			'public' => false,
138
		),
139
	);
140
141
142
143
	/**
144
	 * Initializes the object.
145
	 *
146
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
147
	 */
148
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
149
	{
150
		parent::__construct( $context );
151
		$this->setResourceName( 'db-attribute' );
152
153
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
154
		$level = $context->config()->get( 'mshop/attribute/manager/sitemode', $level );
155
156
157
		$this->searchConfig['attribute:has']['function'] = function( &$source, array $params ) use ( $level ) {
158
159
			$keys = [];
160
161
			foreach( (array) ( $params[1] ?? '' ) as $type ) {
162
				foreach( (array) ( $params[2] ?? '' ) as $id ) {
163
					$keys[] = $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id;
164
				}
165
			}
166
167
			$sitestr = $this->getSiteString( 'mattli."siteid"', $level );
168
			$keystr = $this->toExpression( 'mattli."key"', $keys, ( $params[2] ?? null ) ? '==' : '=~' );
169
			$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
170
171
			return $params;
172
		};
173
174
175
		$this->searchConfig['attribute:prop']['function'] = function( &$source, array $params ) use ( $level ) {
176
177
			$keys = [];
178
			$langs = array_key_exists( 1, $params ) ? ( $params[1] ?? 'null' ) : '';
179
180
			foreach( (array) $langs as $lang ) {
181
				foreach( (array) ( $params[2] ?? '' ) as $val ) {
182
					$keys[] = substr( $params[0] . '|' . ( $lang === null ? 'null|' : ( $lang ? $lang . '|' : '' ) ) . $val, 0, 255 );
183
				}
184
			}
185
186
			$sitestr = $this->getSiteString( 'mattpr."siteid"', $level );
187
			$keystr = $this->toExpression( 'mattpr."key"', $keys, '=~' );
188
			$source = str_replace( [':site', ':key'], [$sitestr, $keystr], $source );
189
190
			return $params;
191
		};
192
	}
193
194
195
	/**
196
	 * Removes old entries from the storage.
197
	 *
198
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
199
	 * @return \Aimeos\MShop\Attribute\Manager\Iface Manager object for chaining method calls
200
	 */
201
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
202
	{
203
		$path = 'mshop/attribute/manager/submanagers';
204
		$default = ['lists', 'property', 'type'];
205
206
		foreach( $this->context()->config()->get( $path, $default ) as $domain ) {
207
			$this->object()->getSubManager( $domain )->clear( $siteids );
208
		}
209
210
		return $this->clearBase( $siteids, 'mshop/attribute/manager/delete' );
211
	}
212
213
214
	/**
215
	 * Returns the available manager types
216
	 *
217
	 * @param bool $withsub Return also the resource type of sub-managers if true
218
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
219
	 */
220
	public function getResourceType( bool $withsub = true ) : array
221
	{
222
		$path = 'mshop/attribute/manager/submanagers';
223
		$default = ['lists', 'property'];
224
225
		return $this->getResourceTypeBase( 'attribute', $path, $default, $withsub );
226
	}
227
228
229
	/**
230
	 * Returns the attributes that can be used for searching.
231
	 *
232
	 * @param bool $withsub Return also attributes of sub-managers if true
233
	 * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of search attribute items
234
	 */
235
	public function getSearchAttributes( bool $withsub = true ) : array
236
	{
237
		/** mshop/attribute/manager/submanagers
238
		 * List of manager names that can be instantiated by the attribute manager
239
		 *
240
		 * Managers provide a generic interface to the underlying storage.
241
		 * Each manager has or can have sub-managers caring about particular
242
		 * aspects. Each of these sub-managers can be instantiated by its
243
		 * parent manager using the getSubManager() method.
244
		 *
245
		 * The search keys from sub-managers can be normally used in the
246
		 * manager as well. It allows you to search for items of the manager
247
		 * using the search keys of the sub-managers to further limit the
248
		 * retrieved list of items.
249
		 *
250
		 * @param array List of sub-manager names
251
		 * @since 2014.03
252
		 * @category Developer
253
		 */
254
		$path = 'mshop/attribute/manager/submanagers';
255
256
		return $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
257
	}
258
259
260
	/**
261
	 * Creates a new empty item instance
262
	 *
263
	 * @param array $values Values the item should be initialized with
264
	 * @return \Aimeos\MShop\Attribute\Item\Iface New attribute item object
265
	 */
266
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
267
	{
268
		$values['attribute.siteid'] = $this->context()->locale()->getSiteId();
269
		return $this->createItemBase( $values );
270
	}
271
272
273
	/**
274
	 * Returns the item specified by its code and domain/type if necessary
275
	 *
276
	 * @param string $code Code of the item
277
	 * @param string[] $ref List of domains to fetch list items and referenced items for
278
	 * @param string|null $domain Domain of the item if necessary to identify the item uniquely
279
	 * @param string|null $type Type code of the item if necessary to identify the item uniquely
280
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
281
	 * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item object
282
	 */
283
	public function find( string $code, array $ref = [], string $domain = null, string $type = null,
284
		?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
285
	{
286
		$find = array(
287
			'attribute.code' => $code,
288
			'attribute.domain' => $domain,
289
			'attribute.type' => $type,
290
		);
291
		return $this->findBase( $find, $ref, $default );
292
	}
293
294
295
	/**
296
	 * Returns the attributes item specified by its ID.
297
	 *
298
	 * @param string $id Unique ID of the attribute item in the storage
299
	 * @param string[] $ref List of domains to fetch list items and referenced items for
300
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
301
	 * @return \Aimeos\MShop\Attribute\Item\Iface Returns the attribute item of the given id
302
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
303
	 */
304
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
305
	{
306
		return $this->getItemBase( 'attribute.id', $id, $ref, $default );
307
	}
308
309
310
	/**
311
	 * Saves an attribute item to the storage.
312
	 *
313
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item
314
	 * @param bool $fetch True if the new ID should be returned in the item
315
	 * @return \Aimeos\MShop\Attribute\Item\Iface $item Updated item including the generated ID
316
	 */
317
	public function saveItem( \Aimeos\MShop\Attribute\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Attribute\Item\Iface
318
	{
319
		if( !$item->isModified() )
320
		{
321
			$item = $this->savePropertyItems( $item, 'attribute', $fetch );
322
			return $this->saveListItems( $item, 'attribute', $fetch );
323
		}
324
325
		$context = $this->context();
326
327
		$dbm = $context->db();
328
		$dbname = $this->getResourceName();
329
		$conn = $dbm->acquire( $dbname );
330
331
		try
332
		{
333
			$id = $item->getId();
334
			$date = date( 'Y-m-d H:i:s' );
335
			$columns = $this->object()->getSaveAttributes();
336
337
			if( $id === null )
338
			{
339
				/** mshop/attribute/manager/insert/mysql
340
				 * Inserts a new attribute record into the database table
341
				 *
342
				 * @see mshop/attribute/manager/insert/ansi
343
				 */
344
345
				/** mshop/attribute/manager/insert/ansi
346
				 * Inserts a new attribute record into the database table
347
				 *
348
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
349
				 * the database and the newly created ID retrieved afterwards
350
				 * using the "newid" SQL statement.
351
				 *
352
				 * The SQL statement must be a string suitable for being used as
353
				 * prepared statement. It must include question marks for binding
354
				 * the values from the attribute item to the statement before they are
355
				 * sent to the database server. The number of question marks must
356
				 * be the same as the number of columns listed in the INSERT
357
				 * statement. The order of the columns must correspond to the
358
				 * order in the save() method, so the correct values are
359
				 * bound to the columns.
360
				 *
361
				 * The SQL statement should conform to the ANSI standard to be
362
				 * compatible with most relational database systems. This also
363
				 * includes using double quotes for table and column names.
364
				 *
365
				 * @param string SQL statement for inserting records
366
				 * @since 2014.03
367
				 * @category Developer
368
				 * @see mshop/attribute/manager/update/ansi
369
				 * @see mshop/attribute/manager/newid/ansi
370
				 * @see mshop/attribute/manager/delete/ansi
371
				 * @see mshop/attribute/manager/search/ansi
372
				 * @see mshop/attribute/manager/count/ansi
373
				 */
374
				$path = 'mshop/attribute/manager/insert';
375
				$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

375
				$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
376
			}
377
			else
378
			{
379
				/** mshop/attribute/manager/update/mysql
380
				 * Updates an existing attribute record in the database
381
				 *
382
				 * @see mshop/attribute/manager/update/ansi
383
				 */
384
385
				/** mshop/attribute/manager/update/ansi
386
				 * Updates an existing attribute record in the database
387
				 *
388
				 * Items which already have an ID (i.e. the ID is not NULL) will
389
				 * be updated in the database.
390
				 *
391
				 * The SQL statement must be a string suitable for being used as
392
				 * prepared statement. It must include question marks for binding
393
				 * the values from the attribute item to the statement before they are
394
				 * sent to the database server. The order of the columns must
395
				 * correspond to the order in the save() method, so the
396
				 * correct values are bound to the columns.
397
				 *
398
				 * The SQL statement should conform to the ANSI standard to be
399
				 * compatible with most relational database systems. This also
400
				 * includes using double quotes for table and column names.
401
				 *
402
				 * @param string SQL statement for updating records
403
				 * @since 2014.03
404
				 * @category Developer
405
				 * @see mshop/attribute/manager/insert/ansi
406
				 * @see mshop/attribute/manager/newid/ansi
407
				 * @see mshop/attribute/manager/delete/ansi
408
				 * @see mshop/attribute/manager/search/ansi
409
				 * @see mshop/attribute/manager/count/ansi
410
				 */
411
				$path = 'mshop/attribute/manager/update';
412
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
413
			}
414
415
			$idx = 1;
416
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
417
418
			foreach( $columns as $name => $entry ) {
419
				$stmt->bind( $idx++, $item->get( $name ), $entry->getInternalType() );
0 ignored issues
show
Bug introduced by
It seems like $entry->getInternalType() can also be of type string; however, parameter $type of Aimeos\MW\DB\Statement\Iface::bind() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

419
				$stmt->bind( $idx++, $item->get( $name ), /** @scrutinizer ignore-type */ $entry->getInternalType() );
Loading history...
420
			}
421
422
			$stmt->bind( $idx++, $item->getKey() );
423
			$stmt->bind( $idx++, $item->getType() );
424
			$stmt->bind( $idx++, $item->getDomain() );
425
			$stmt->bind( $idx++, $item->getCode() );
426
			$stmt->bind( $idx++, $item->getStatus(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
427
			$stmt->bind( $idx++, $item->getPosition(), \Aimeos\MW\DB\Statement\Base::PARAM_INT );
428
			$stmt->bind( $idx++, $item->getLabel() );
429
			$stmt->bind( $idx++, $date ); // mtime
430
			$stmt->bind( $idx++, $context->editor() );
431
			$stmt->bind( $idx++, $context->locale()->getSiteId() );
432
433
			if( $id !== null ) {
434
				$stmt->bind( $idx++, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
435
			} else {
436
				$stmt->bind( $idx++, $date ); // ctime
437
			}
438
439
			$stmt->execute()->finish();
440
441
			if( $id === null )
442
			{
443
				/** mshop/attribute/manager/newid/mysql
444
				 * Retrieves the ID generated by the database when inserting a new record
445
				 *
446
				 * @see mshop/attribute/manager/newid/ansi
447
				 */
448
449
				/** mshop/attribute/manager/newid/ansi
450
				 * Retrieves the ID generated by the database when inserting a new record
451
				 *
452
				 * As soon as a new record is inserted into the database table,
453
				 * the database server generates a new and unique identifier for
454
				 * that record. This ID can be used for retrieving, updating and
455
				 * deleting that specific record from the table again.
456
				 *
457
				 * For MySQL:
458
				 *  SELECT LAST_INSERT_ID()
459
				 * For PostgreSQL:
460
				 *  SELECT currval('seq_matt_id')
461
				 * For SQL Server:
462
				 *  SELECT SCOPE_IDENTITY()
463
				 * For Oracle:
464
				 *  SELECT "seq_matt_id".CURRVAL FROM DUAL
465
				 *
466
				 * There's no way to retrive the new ID by a SQL statements that
467
				 * fits for most database servers as they implement their own
468
				 * specific way.
469
				 *
470
				 * @param string SQL statement for retrieving the last inserted record ID
471
				 * @since 2014.03
472
				 * @category Developer
473
				 * @see mshop/attribute/manager/insert/ansi
474
				 * @see mshop/attribute/manager/update/ansi
475
				 * @see mshop/attribute/manager/delete/ansi
476
				 * @see mshop/attribute/manager/search/ansi
477
				 * @see mshop/attribute/manager/count/ansi
478
				 */
479
				$path = 'mshop/attribute/manager/newid';
480
				$id = $this->newId( $conn, $path );
481
			}
482
483
			$item->setId( $id );
484
485
			$dbm->release( $conn, $dbname );
486
		}
487
		catch( \Exception $e )
488
		{
489
			$dbm->release( $conn, $dbname );
490
			throw $e;
491
		}
492
493
		$item = $this->savePropertyItems( $item, 'attribute', $fetch );
494
		return $this->saveListItems( $item, 'attribute', $fetch );
495
	}
496
497
498
	/**
499
	 * Removes multiple items.
500
	 *
501
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
502
	 * @return \Aimeos\MShop\Attribute\Manager\Iface Manager object for chaining method calls
503
	 */
504
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
505
	{
506
		/** mshop/attribute/manager/delete/mysql
507
		 * Deletes the items matched by the given IDs from the database
508
		 *
509
		 * @see mshop/attribute/manager/delete/ansi
510
		 */
511
512
		/** mshop/attribute/manager/delete/ansi
513
		 * Deletes the items matched by the given IDs from the database
514
		 *
515
		 * Removes the records specified by the given IDs from the attribute database.
516
		 * The records must be from the site that is configured via the
517
		 * context item.
518
		 *
519
		 * The ":cond" placeholder is replaced by the name of the ID column and
520
		 * the given ID or list of IDs while the site ID is bound to the question
521
		 * mark.
522
		 *
523
		 * The SQL statement should conform to the ANSI standard to be
524
		 * compatible with most relational database systems. This also
525
		 * includes using double quotes for table and column names.
526
		 *
527
		 * @param string SQL statement for deleting items
528
		 * @since 2014.03
529
		 * @category Developer
530
		 * @see mshop/attribute/manager/insert/ansi
531
		 * @see mshop/attribute/manager/update/ansi
532
		 * @see mshop/attribute/manager/newid/ansi
533
		 * @see mshop/attribute/manager/search/ansi
534
		 * @see mshop/attribute/manager/count/ansi
535
		 */
536
		$path = 'mshop/attribute/manager/delete';
537
538
		return $this->deleteItemsBase( $itemIds, $path )->deleteRefItems( $itemIds );
539
	}
540
541
542
	/**
543
	 * Searches for attribute items based on the given criteria.
544
	 *
545
	 * @param \Aimeos\MW\Criteria\Iface $search Search criteria object
546
	 * @param string[] $ref List of domains to fetch list items and referenced items for
547
	 * @param int|null &$total Number of items that are available in total
548
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Attribute\Item\Iface with ids as keys
549
	 */
550
	public function search( \Aimeos\MW\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
551
	{
552
		$map = [];
553
		$context = $this->context();
554
555
		$dbm = $context->db();
556
		$dbname = $this->getResourceName();
557
		$conn = $dbm->acquire( $dbname );
558
559
		try
560
		{
561
			$required = array( 'attribute' );
562
563
			/** mshop/attribute/manager/sitemode
564
			 * Mode how items from levels below or above in the site tree are handled
565
			 *
566
			 * By default, only items from the current site are fetched from the
567
			 * storage. If the ai-sites extension is installed, you can create a
568
			 * tree of sites. Then, this setting allows you to define for the
569
			 * whole attribute domain if items from parent sites are inherited,
570
			 * sites from child sites are aggregated or both.
571
			 *
572
			 * Available constants for the site mode are:
573
			 * * 0 = only items from the current site
574
			 * * 1 = inherit items from parent sites
575
			 * * 2 = aggregate items from child sites
576
			 * * 3 = inherit and aggregate items at the same time
577
			 *
578
			 * You also need to set the mode in the locale manager
579
			 * (mshop/locale/manager/sitelevel) to one of the constants.
580
			 * If you set it to the same value, it will work as described but you
581
			 * can also use different modes. For example, if inheritance and
582
			 * aggregation is configured the locale manager but only inheritance
583
			 * in the domain manager because aggregating items makes no sense in
584
			 * this domain, then items wil be only inherited. Thus, you have full
585
			 * control over inheritance and aggregation in each domain.
586
			 *
587
			 * @param int Constant from Aimeos\MShop\Locale\Manager\Base class
588
			 * @category Developer
589
			 * @since 2018.01
590
			 * @see mshop/locale/manager/sitelevel
591
			 */
592
			$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
593
			$level = $context->config()->get( 'mshop/attribute/manager/sitemode', $level );
594
595
			/** mshop/attribute/manager/search/mysql
596
			 * Retrieves the records matched by the given criteria in the database
597
			 *
598
			 * @see mshop/attribute/manager/search/ansi
599
			 */
600
601
			/** mshop/attribute/manager/search/ansi
602
			 * Retrieves the records matched by the given criteria in the database
603
			 *
604
			 * Fetches the records matched by the given criteria from the attribute
605
			 * database. The records must be from one of the sites that are
606
			 * configured via the context item. If the current site is part of
607
			 * a tree of sites, the SELECT statement can retrieve all records
608
			 * from the current site and the complete sub-tree of sites.
609
			 *
610
			 * As the records can normally be limited by criteria from sub-managers,
611
			 * their tables must be joined in the SQL context. This is done by
612
			 * using the "internaldeps" property from the definition of the ID
613
			 * column of the sub-managers. These internal dependencies specify
614
			 * the JOIN between the tables and the used columns for joining. The
615
			 * ":joins" placeholder is then replaced by the JOIN strings from
616
			 * the sub-managers.
617
			 *
618
			 * To limit the records matched, conditions can be added to the given
619
			 * criteria object. It can contain comparisons like column names that
620
			 * must match specific values which can be combined by AND, OR or NOT
621
			 * operators. The resulting string of SQL conditions replaces the
622
			 * ":cond" placeholder before the statement is sent to the database
623
			 * server.
624
			 *
625
			 * If the records that are retrieved should be ordered by one or more
626
			 * columns, the generated string of column / sort direction pairs
627
			 * replaces the ":order" placeholder. In case no ordering is required,
628
			 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
629
			 * markers is removed to speed up retrieving the records. Columns of
630
			 * sub-managers can also be used for ordering the result set but then
631
			 * no index can be used.
632
			 *
633
			 * The number of returned records can be limited and can start at any
634
			 * number between the begining and the end of the result set. For that
635
			 * the ":size" and ":start" placeholders are replaced by the
636
			 * corresponding values from the criteria object. The default values
637
			 * are 0 for the start and 100 for the size value.
638
			 *
639
			 * The SQL statement should conform to the ANSI standard to be
640
			 * compatible with most relational database systems. This also
641
			 * includes using double quotes for table and column names.
642
			 *
643
			 * @param string SQL statement for searching items
644
			 * @since 2014.03
645
			 * @category Developer
646
			 * @see mshop/attribute/manager/insert/ansi
647
			 * @see mshop/attribute/manager/update/ansi
648
			 * @see mshop/attribute/manager/newid/ansi
649
			 * @see mshop/attribute/manager/delete/ansi
650
			 * @see mshop/attribute/manager/count/ansi
651
			 */
652
			$cfgPathSearch = 'mshop/attribute/manager/search';
653
654
			/** mshop/attribute/manager/count/mysql
655
			 * Counts the number of records matched by the given criteria in the database
656
			 *
657
			 * @see mshop/attribute/manager/count/ansi
658
			 */
659
660
			/** mshop/attribute/manager/count/ansi
661
			 * Counts the number of records matched by the given criteria in the database
662
			 *
663
			 * Counts all records matched by the given criteria from the attribute
664
			 * database. The records must be from one of the sites that are
665
			 * configured via the context item. If the current site is part of
666
			 * a tree of sites, the statement can count all records from the
667
			 * current site and the complete sub-tree of sites.
668
			 *
669
			 * As the records can normally be limited by criteria from sub-managers,
670
			 * their tables must be joined in the SQL context. This is done by
671
			 * using the "internaldeps" property from the definition of the ID
672
			 * column of the sub-managers. These internal dependencies specify
673
			 * the JOIN between the tables and the used columns for joining. The
674
			 * ":joins" placeholder is then replaced by the JOIN strings from
675
			 * the sub-managers.
676
			 *
677
			 * To limit the records matched, conditions can be added to the given
678
			 * criteria object. It can contain comparisons like column names that
679
			 * must match specific values which can be combined by AND, OR or NOT
680
			 * operators. The resulting string of SQL conditions replaces the
681
			 * ":cond" placeholder before the statement is sent to the database
682
			 * server.
683
			 *
684
			 * Both, the strings for ":joins" and for ":cond" are the same as for
685
			 * the "search" SQL statement.
686
			 *
687
			 * Contrary to the "search" statement, it doesn't return any records
688
			 * but instead the number of records that have been found. As counting
689
			 * thousands of records can be a long running task, the maximum number
690
			 * of counted records is limited for performance reasons.
691
			 *
692
			 * The SQL statement should conform to the ANSI standard to be
693
			 * compatible with most relational database systems. This also
694
			 * includes using double quotes for table and column names.
695
			 *
696
			 * @param string SQL statement for counting items
697
			 * @since 2014.03
698
			 * @category Developer
699
			 * @see mshop/attribute/manager/insert/ansi
700
			 * @see mshop/attribute/manager/update/ansi
701
			 * @see mshop/attribute/manager/newid/ansi
702
			 * @see mshop/attribute/manager/delete/ansi
703
			 * @see mshop/attribute/manager/search/ansi
704
			 */
705
			$cfgPathCount = 'mshop/attribute/manager/count';
706
707
			$plugins = ['attribute.key' => new \Aimeos\MW\Criteria\Plugin\Md5()];
708
			$results = $this->searchItemsBase( $conn, $search, $cfgPathSearch, $cfgPathCount, $required, $total, $level, $plugins );
709
710
			while( ( $row = $results->fetch() ) !== null ) {
711
				$map[$row['attribute.id']] = $row;
712
			}
713
714
			$dbm->release( $conn, $dbname );
715
		}
716
		catch( \Exception $e )
717
		{
718
			$dbm->release( $conn, $dbname );
719
			throw $e;
720
		}
721
722
		$propItems = []; $name = 'attribute/property';
723
		if( isset( $ref[$name] ) || in_array( $name, $ref, true ) )
724
		{
725
			$propTypes = isset( $ref[$name] ) && is_array( $ref[$name] ) ? $ref[$name] : null;
726
			$propItems = $this->getPropertyItems( array_keys( $map ), 'attribute', $propTypes );
727
		}
728
729
		return $this->buildItems( $map, $ref, 'attribute', $propItems );
730
	}
731
732
733
	/**
734
	 * Creates a filter object.
735
	 *
736
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
737
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
738
	 * @return \Aimeos\MW\Criteria\Iface Returns the filter object
739
	 */
740
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\MW\Criteria\Iface
741
	{
742
		return $this->filterBase( 'attribute', $default );
743
	}
744
745
746
	/**
747
	 * Returns a new manager for attribute extensions
748
	 *
749
	 * @param string $manager Name of the sub manager type in lower case
750
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
751
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g Type, List's etc.
752
	 */
753
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
754
	{
755
		return $this->getSubManagerBase( 'attribute', $manager, $name );
756
	}
757
758
759
	/**
760
	 * Creates a new attribute item instance.
761
	 *
762
	 * @param array $values Associative list of key/value pairs
763
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items
764
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
765
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propertyItems List of property items
766
	 * @return \Aimeos\MShop\Attribute\Item\Iface New attribute item
767
	 */
768
	protected function createItemBase( array $values = [], array $listItems = [],
769
		array $refItems = [], array $propertyItems = [] ) : \Aimeos\MShop\Common\Item\Iface
770
	{
771
		return new \Aimeos\MShop\Attribute\Item\Standard( $values, $listItems, $refItems, $propertyItems );
772
	}
773
}
774