Passed
Push — master ( 6aab01...0294bc )
by Aimeos
09:44
created

Standard::search()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 166
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 166
rs 9.1111
c 0
b 0
f 0
cc 6
nc 10
nop 3

How to fix   Long Method   

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\Base\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\Base\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\Base\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\Base\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\Base\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\Base\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\Base\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\Base\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\Base\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\Base\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\Base\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\Base\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\ContextIface $context Context object
147
	 */
148
	public function __construct( \Aimeos\MShop\ContextIface $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[] = substr( $params[0] . '|' . ( $type ? $type . '|' : '' ) . $id, 0, 255 );
164
				}
165
			}
166
167
			$sitestr = $this->siteString( '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->siteString( '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\Base\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
		$conn = $context->db( $this->getResourceName() );
327
328
			$id = $item->getId();
329
			$date = date( 'Y-m-d H:i:s' );
330
			$columns = $this->object()->getSaveAttributes();
331
332
			if( $id === null )
333
			{
334
				/** mshop/attribute/manager/insert/mysql
335
				 * Inserts a new attribute record into the database table
336
				 *
337
				 * @see mshop/attribute/manager/insert/ansi
338
				 */
339
340
				/** mshop/attribute/manager/insert/ansi
341
				 * Inserts a new attribute record into the database table
342
				 *
343
				 * Items with no ID yet (i.e. the ID is NULL) will be created in
344
				 * the database and the newly created ID retrieved afterwards
345
				 * using the "newid" SQL statement.
346
				 *
347
				 * The SQL statement must be a string suitable for being used as
348
				 * prepared statement. It must include question marks for binding
349
				 * the values from the attribute item to the statement before they are
350
				 * sent to the database server. The number of question marks must
351
				 * be the same as the number of columns listed in the INSERT
352
				 * statement. The order of the columns must correspond to the
353
				 * order in the save() method, so the correct values are
354
				 * bound to the columns.
355
				 *
356
				 * The SQL statement should conform to the ANSI standard to be
357
				 * compatible with most relational database systems. This also
358
				 * includes using double quotes for table and column names.
359
				 *
360
				 * @param string SQL statement for inserting records
361
				 * @since 2014.03
362
				 * @category Developer
363
				 * @see mshop/attribute/manager/update/ansi
364
				 * @see mshop/attribute/manager/newid/ansi
365
				 * @see mshop/attribute/manager/delete/ansi
366
				 * @see mshop/attribute/manager/search/ansi
367
				 * @see mshop/attribute/manager/count/ansi
368
				 */
369
				$path = 'mshop/attribute/manager/insert';
370
				$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

370
				$sql = $this->addSqlColumns( array_keys( $columns ), /** @scrutinizer ignore-type */ $this->getSqlConfig( $path ) );
Loading history...
371
			}
372
			else
373
			{
374
				/** mshop/attribute/manager/update/mysql
375
				 * Updates an existing attribute record in the database
376
				 *
377
				 * @see mshop/attribute/manager/update/ansi
378
				 */
379
380
				/** mshop/attribute/manager/update/ansi
381
				 * Updates an existing attribute record in the database
382
				 *
383
				 * Items which already have an ID (i.e. the ID is not NULL) will
384
				 * be updated in the database.
385
				 *
386
				 * The SQL statement must be a string suitable for being used as
387
				 * prepared statement. It must include question marks for binding
388
				 * the values from the attribute item to the statement before they are
389
				 * sent to the database server. The order of the columns must
390
				 * correspond to the order in the save() method, so the
391
				 * correct values are bound to the columns.
392
				 *
393
				 * The SQL statement should conform to the ANSI standard to be
394
				 * compatible with most relational database systems. This also
395
				 * includes using double quotes for table and column names.
396
				 *
397
				 * @param string SQL statement for updating records
398
				 * @since 2014.03
399
				 * @category Developer
400
				 * @see mshop/attribute/manager/insert/ansi
401
				 * @see mshop/attribute/manager/newid/ansi
402
				 * @see mshop/attribute/manager/delete/ansi
403
				 * @see mshop/attribute/manager/search/ansi
404
				 * @see mshop/attribute/manager/count/ansi
405
				 */
406
				$path = 'mshop/attribute/manager/update';
407
				$sql = $this->addSqlColumns( array_keys( $columns ), $this->getSqlConfig( $path ), false );
408
			}
409
410
			$idx = 1;
411
			$stmt = $this->getCachedStatement( $conn, $path, $sql );
412
413
			foreach( $columns as $name => $entry ) {
414
				$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\Base\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

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