Passed
Push — master ( 494e53...aeeda4 )
by Aimeos
09:49
created

Standard::getSubManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 115
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 115
rs 10
c 0
b 0
f 0

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, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package MShop
8
 * @subpackage Index
9
 */
10
11
namespace Aimeos\MShop\Index\Manager\Attribute;
12
13
14
/**
15
 * Index sub-manager for product attributes.
16
 *
17
 * @package MShop
18
 * @subpackage Index
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Index\Manager\DBBase
22
	implements \Aimeos\MShop\Index\Manager\Attribute\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	private $searchConfig = array(
25
		'index.attribute.id' => array(
26
			'code' => 'index.attribute.id',
27
			'internalcode' => 'mindat."attrid"',
28
			'internaldeps'=>array( 'LEFT JOIN "mshop_index_attribute" AS mindat ON mindat."prodid" = mpro."id"' ),
29
			'label' => 'Product index attribute ID',
30
			'type' => 'string',
31
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_STR,
32
			'public' => false,
33
		),
34
		'index.attribute:allof' => array(
35
			'code' => 'index.attribute:allof()',
36
			'internalcode' => '( SELECT mpro_allof."id" FROM mshop_product AS mpro_allof
37
				WHERE mpro."id" = mpro_allof."id" AND (
38
					SELECT COUNT(DISTINCT mindat_allof."attrid")
39
					FROM "mshop_index_attribute" AS mindat_allof
40
					WHERE mpro."id" = mindat_allof."prodid" AND :site AND mindat_allof."attrid" IN ( $1 )
41
					GROUP BY mindat_allof."prodid"
42
					HAVING COUNT(DISTINCT mindat_allof."artid") > 0
43
					) = $2
44
				)',
45
			'label' => 'Number of product attributes, parameter(<attribute IDs>)',
46
			'type' => 'null',
47
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_NULL,
48
			'public' => false,
49
		),
50
		'index.attribute:oneof' => array(
51
			'code' => 'index.attribute:oneof()',
52
			'internalcode' => '( SELECT mpro_oneof."id" FROM mshop_product AS mpro_oneof
53
				WHERE mpro."id" = mpro_oneof."id" AND (
54
					SELECT COUNT(DISTINCT mindat_oneof."attrid")
55
					FROM "mshop_index_attribute" AS mindat_oneof
56
					WHERE mpro."id" = mindat_oneof."prodid" AND :site
57
					AND mindat_oneof."attrid" IN ( $1 ) ) > 0
58
				)',
59
			'label' => 'Number of product attributes, parameter(<attribute IDs>)',
60
			'type' => 'null',
61
			'internaltype' => \Aimeos\Base\DB\Statement\Base::PARAM_NULL,
62
			'public' => false,
63
		),
64
	);
65
66
	private $subManagers;
67
68
69
	/**
70
	 * Initializes the manager instance.
71
	 *
72
	 * @param \Aimeos\MShop\ContextIface $context Context object
73
	 */
74
	public function __construct( \Aimeos\MShop\ContextIface $context )
75
	{
76
		parent::__construct( $context );
77
78
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
79
		$level = $context->config()->get( 'mshop/index/manager/sitemode', $level );
80
81
		$this->searchConfig['index.attribute:allof']['function'] = function( $source, array $params ) {
82
			return [$params[0], count( $params[0] )];
83
		};
84
85
		$name = 'index.attribute:allof';
86
		$expr = $this->siteString( 'mindat_allof."siteid"', $level );
87
		$this->searchConfig[$name]['internalcode'] = str_replace( ':site', $expr, $this->searchConfig[$name]['internalcode'] );
88
89
		$name = 'index.attribute:oneof';
90
		$expr = $this->siteString( 'mindat_oneof."siteid"', $level );
91
		$this->searchConfig[$name]['internalcode'] = str_replace( ':site', $expr, $this->searchConfig[$name]['internalcode'] );
92
	}
93
94
95
	/**
96
	 * Counts the number products that are available for the values of the given key.
97
	 *
98
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria
99
	 * @param string $key Search key (usually the ID) to aggregate products for
100
	 * @param string|null $value Search key for aggregating the value column
101
	 * @param string|null $type Type of the aggregation, empty string for count or "sum" or "avg" (average)
102
	 * @return \Aimeos\Map List of ID values as key and the number of counted products as value
103
	 */
104
	public function aggregate( \Aimeos\Base\Criteria\Iface $search, $key, string $value = null, string $type = null ) : \Aimeos\Map
105
	{
106
		return $this->aggregateBase( $search, $key, 'mshop/index/manager/aggregate', [], $value, $type );
107
	}
108
109
110
	/**
111
	 * Removes old entries from the storage.
112
	 *
113
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
114
	 * @return \Aimeos\MShop\Index\Manager\Iface Manager object for chaining method calls
115
	 */
116
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
117
	{
118
		parent::clear( $siteids );
119
120
		return $this->clearBase( $siteids, 'mshop/index/manager/attribute/delete' );
121
	}
122
123
124
	/**
125
	 * Removes all entries not touched after the given timestamp in the index.
126
	 * This can be a long lasting operation.
127
	 *
128
	 * @param string $timestamp Timestamp in ISO format (YYYY-MM-DD HH:mm:ss)
129
	 * @return \Aimeos\MShop\Index\Manager\Iface Manager object for chaining method calls
130
	 */
131
	public function cleanup( string $timestamp ) : \Aimeos\MShop\Index\Manager\Iface
132
	{
133
		/** mshop/index/manager/attribute/cleanup/mysql
134
		 * Deletes the index attribute records that haven't been touched
135
		 *
136
		 * @see mshop/index/manager/attribute/cleanup/ansi
137
		 */
138
139
		/** mshop/index/manager/attribute/cleanup/ansi
140
		 * Deletes the index attribute records that haven't been touched
141
		 *
142
		 * During the rebuild process of the product index, the entries of all
143
		 * active products will be removed and readded. Thus, no stale data for
144
		 * these products will remain in the database.
145
		 *
146
		 * All products that have been disabled since the last rebuild will be
147
		 * still part of the index. The cleanup statement removes all records
148
		 * that belong to products that haven't been touched during the index
149
		 * rebuild because these are the disabled ones.
150
		 *
151
		 * The SQL statement should conform to the ANSI standard to be
152
		 * compatible with most relational database systems. This also
153
		 * includes using double quotes for table and column names.
154
		 *
155
		 * @param string SQL statement for deleting the outdated attribute index records
156
		 * @since 2014.03
157
		 * @category Developer
158
		 * @see mshop/index/manager/attribute/count/ansi
159
		 * @see mshop/index/manager/attribute/delete/ansi
160
		 * @see mshop/index/manager/attribute/insert/ansi
161
		 * @see mshop/index/manager/attribute/search/ansi
162
		 */
163
		return $this->cleanupBase( $timestamp, 'mshop/index/manager/attribute/cleanup' );
164
	}
165
166
167
	/**
168
	 * Removes multiple items.
169
	 *
170
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
171
	 * @return \Aimeos\MShop\Index\Manager\Iface Manager object for chaining method calls
172
	 */
173
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
174
	{
175
		/** mshop/index/manager/attribute/delete/mysql
176
		 * Deletes the items matched by the given IDs from the database
177
		 *
178
		 * @see mshop/index/manager/attribute/delete/ansi
179
		 */
180
181
		/** mshop/index/manager/attribute/delete/ansi
182
		 * Deletes the items matched by the given IDs from the database
183
		 *
184
		 * Removes the records specified by the given IDs from the index database.
185
		 * The records must be from the site that is configured via the
186
		 * context item.
187
		 *
188
		 * The ":cond" placeholder is replaced by the name of the ID column and
189
		 * the given ID or list of IDs while the site ID is bound to the question
190
		 * mark.
191
		 *
192
		 * The SQL statement should conform to the ANSI standard to be
193
		 * compatible with most relational database systems. This also
194
		 * includes using double quotes for table and column names.
195
		 *
196
		 * @param string SQL statement for deleting index attribute records
197
		 * @since 2014.03
198
		 * @category Developer
199
		 * @see mshop/index/manager/attribute/count/ansi
200
		 * @see mshop/index/manager/attribute/cleanup/ansi
201
		 * @see mshop/index/manager/attribute/insert/ansi
202
		 * @see mshop/index/manager/attribute/search/ansi
203
		 */
204
		return $this->deleteItemsBase( $itemIds, 'mshop/index/manager/attribute/delete' );
205
	}
206
207
208
	/**
209
	 * Returns the available manager types
210
	 *
211
	 * @param bool $withsub Return also the resource type of sub-managers if true
212
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
213
	 */
214
	public function getResourceType( bool $withsub = true ) : array
215
	{
216
		$path = 'mshop/index/manager/attribute/submanagers';
217
218
		return $this->getResourceTypeBase( 'index/attribute', $path, [], $withsub );
219
	}
220
221
222
	/**
223
	 * Returns a list of objects describing the available criterias for searching.
224
	 *
225
	 * @param bool $withsub Return also attributes of sub-managers if true
226
	 * @return array List of items implementing \Aimeos\Base\Criteria\Attribute\Iface
227
	 */
228
	public function getSearchAttributes( bool $withsub = true ) : array
229
	{
230
		$list = parent::getSearchAttributes( $withsub );
231
232
		/** mshop/index/manager/attribute/submanagers
233
		 * List of manager names that can be instantiated by the index attribute manager
234
		 *
235
		 * Managers provide a generic interface to the underlying storage.
236
		 * Each manager has or can have sub-managers caring about particular
237
		 * aspects. Each of these sub-managers can be instantiated by its
238
		 * parent manager using the getSubManager() method.
239
		 *
240
		 * The search keys from sub-managers can be normally used in the
241
		 * manager as well. It allows you to search for items of the manager
242
		 * using the search keys of the sub-managers to further limit the
243
		 * retrieved list of items.
244
		 *
245
		 * @param array List of sub-manager names
246
		 * @since 2014.03
247
		 * @category Developer
248
		 */
249
		$path = 'mshop/index/manager/attribute/submanagers';
250
251
		return $list + $this->getSearchAttributesBase( $this->searchConfig, $path, [], $withsub );
252
	}
253
254
255
	/**
256
	 * Returns a new manager for product extensions.
257
	 *
258
	 * @param string $manager Name of the sub manager type in lower case
259
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
260
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g stock, tags, locations, etc.
261
	 */
262
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
263
	{
264
		/** mshop/index/manager/attribute/name
265
		 * Class name of the used index attribute manager implementation
266
		 *
267
		 * Each default index attribute manager can be replaced by an alternative imlementation.
268
		 * To use this implementation, you have to set the last part of the class
269
		 * name as configuration value so the manager factory knows which class it
270
		 * has to instantiate.
271
		 *
272
		 * For example, if the name of the default class is
273
		 *
274
		 *  \Aimeos\MShop\Index\Manager\Attribute\Standard
275
		 *
276
		 * and you want to replace it with your own version named
277
		 *
278
		 *  \Aimeos\MShop\Index\Manager\Attribute\Myattribute
279
		 *
280
		 * then you have to set the this configuration option:
281
		 *
282
		 *  mshop/index/manager/attribute/name = Myattribute
283
		 *
284
		 * The value is the last part of your own class name and it's case sensitive,
285
		 * so take care that the configuration value is exactly named like the last
286
		 * part of the class name.
287
		 *
288
		 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
289
		 * characters are possible! You should always start the last part of the class
290
		 * name with an upper case character and continue only with lower case characters
291
		 * or numbers. Avoid chamel case names like "MyAttribute"!
292
		 *
293
		 * @param string Last part of the class name
294
		 * @since 2014.03
295
		 * @category Developer
296
		 */
297
298
		/** mshop/index/manager/attribute/decorators/excludes
299
		 * Excludes decorators added by the "common" option from the index attribute manager
300
		 *
301
		 * Decorators extend the functionality of a class by adding new aspects
302
		 * (e.g. log what is currently done), executing the methods of the underlying
303
		 * class only in certain conditions (e.g. only for logged in users) or
304
		 * modify what is returned to the caller.
305
		 *
306
		 * This option allows you to remove a decorator added via
307
		 * "mshop/common/manager/decorators/default" before they are wrapped
308
		 * around the index attribute manager.
309
		 *
310
		 *  mshop/index/manager/attribute/decorators/excludes = array( 'decorator1' )
311
		 *
312
		 * This would remove the decorator named "decorator1" from the list of
313
		 * common decorators ("\Aimeos\MShop\Common\Manager\Decorator\*") added via
314
		 * "mshop/common/manager/decorators/default" for the index attribute manager.
315
		 *
316
		 * @param array List of decorator names
317
		 * @since 2014.03
318
		 * @category Developer
319
		 * @see mshop/common/manager/decorators/default
320
		 * @see mshop/index/manager/attribute/decorators/global
321
		 * @see mshop/index/manager/attribute/decorators/local
322
		 */
323
324
		/** mshop/index/manager/attribute/decorators/global
325
		 * Adds a list of globally available decorators only to the index attribute manager
326
		 *
327
		 * Decorators extend the functionality of a class by adding new aspects
328
		 * (e.g. log what is currently done), executing the methods of the underlying
329
		 * class only in certain conditions (e.g. only for logged in users) or
330
		 * modify what is returned to the caller.
331
		 *
332
		 * This option allows you to wrap global decorators
333
		 * ("\Aimeos\MShop\Common\Manager\Decorator\*") around the index attribute
334
		 * manager.
335
		 *
336
		 *  mshop/index/manager/attribute/decorators/global = array( 'decorator1' )
337
		 *
338
		 * This would add the decorator named "decorator1" defined by
339
		 * "\Aimeos\MShop\Common\Manager\Decorator\Decorator1" only to the index
340
		 * attribute manager.
341
		 *
342
		 * @param array List of decorator names
343
		 * @since 2014.03
344
		 * @category Developer
345
		 * @see mshop/common/manager/decorators/default
346
		 * @see mshop/index/manager/attribute/decorators/excludes
347
		 * @see mshop/index/manager/attribute/decorators/local
348
		 */
349
350
		/** mshop/index/manager/attribute/decorators/local
351
		 * Adds a list of local decorators only to the index attribute manager
352
		 *
353
		 * Decorators extend the functionality of a class by adding new aspects
354
		 * (e.g. log what is currently done), executing the methods of the underlying
355
		 * class only in certain conditions (e.g. only for logged in users) or
356
		 * modify what is returned to the caller.
357
		 *
358
		 * This option allows you to wrap local decorators
359
		 * ("\Aimeos\MShop\Index\Manager\Attribute\Decorator\*") around the index
360
		 * attribute manager.
361
		 *
362
		 *  mshop/index/manager/attribute/decorators/local = array( 'decorator2' )
363
		 *
364
		 * This would add the decorator named "decorator2" defined by
365
		 * "\Aimeos\MShop\Index\Manager\Attribute\Decorator\Decorator2" only to th
366
		 * index attribute manager.
367
		 *
368
		 * @param array List of decorator names
369
		 * @since 2014.03
370
		 * @category Developer
371
		 * @see mshop/common/manager/decorators/default
372
		 * @see mshop/index/manager/attribute/decorators/excludes
373
		 * @see mshop/index/manager/attribute/decorators/global
374
		 */
375
376
		return $this->getSubManagerBase( 'index', 'attribute/' . $manager, $name );
377
	}
378
379
380
	/**
381
	 * Optimizes the index if necessary.
382
	 * Execution of this operation can take a very long time and shouldn't be
383
	 * called through a web server enviroment.
384
	 *
385
	 * @return \Aimeos\MShop\Index\Manager\Iface Manager object for chaining method calls
386
	 */
387
	public function optimize() : \Aimeos\MShop\Index\Manager\Iface
388
	{
389
		/** mshop/index/manager/attribute/optimize/mysql
390
		 * Optimizes the stored attribute data for retrieving the records faster
391
		 *
392
		 * @see mshop/index/manager/attribute/optimize/ansi
393
		 */
394
395
		/** mshop/index/manager/attribute/optimize/ansi
396
		 * Optimizes the stored attribute data for retrieving the records faster
397
		 *
398
		 * The SQL statement should reorganize the data in the DBMS storage to
399
		 * optimize access to the records of the table or tables. Some DBMS
400
		 * offer specialized statements to optimize indexes and records. This
401
		 * statement doesn't return any records.
402
		 *
403
		 * The SQL statement should conform to the ANSI standard to be
404
		 * compatible with most relational database systems. This also
405
		 * includes using double quotes for table and column names.
406
		 *
407
		 * @param string SQL statement for optimizing the stored attribute data
408
		 * @since 2014.09
409
		 * @category Developer
410
		 * @see mshop/index/manager/attribute/count/ansi
411
		 * @see mshop/index/manager/attribute/search/ansi
412
		 * @see mshop/index/manager/attribute/aggregate/ansi
413
		 */
414
		return $this->optimizeBase( 'mshop/index/manager/attribute/optimize' );
415
	}
416
417
418
	/**
419
	 * Rebuilds the index attribute for searching products or specified list of products.
420
	 * This can be a long lasting operation.
421
	 *
422
	 * @param \Aimeos\MShop\Product\Item\Iface[] $items Associative list of product IDs as keys and items as values
423
	 * @return \Aimeos\MShop\Index\Manager\Iface Manager object for chaining method calls
424
	 */
425
	public function rebuild( iterable $items = [] ) : \Aimeos\MShop\Index\Manager\Iface
426
	{
427
		if( ( $items = map( $items ) )->isEmpty() ) { return $this; }
428
429
		$items->implements( \Aimeos\MShop\Product\Item\Iface::class, true );
430
431
		$context = $this->context();
432
		$conn = $context->db( $this->getResourceName() );
433
434
			/** mshop/index/manager/attribute/insert/mysql
435
			 * Inserts a new attribute record into the product index database
436
			 *
437
			 * @see mshop/index/manager/attribute/insert/ansi
438
			 */
439
440
			/** mshop/index/manager/attribute/insert/ansi
441
			 * Inserts a new attribute record into the product index database
442
			 *
443
			 * During the product index rebuild, attributes related to a product
444
			 * will be stored in the index for this product. All records
445
			 * are deleted before the new ones are inserted.
446
			 *
447
			 * The SQL statement must be a string suitable for being used as
448
			 * prepared statement. It must include question marks for binding
449
			 * the values from the order item to the statement before they are
450
			 * sent to the database server. The number of question marks must
451
			 * be the same as the number of columns listed in the INSERT
452
			 * statement. The order of the columns must correspond to the
453
			 * order in the rebuild() method, so the correct values are
454
			 * bound to the columns.
455
			 *
456
			 * The SQL statement should conform to the ANSI standard to be
457
			 * compatible with most relational database systems. This also
458
			 * includes using double quotes for table and column names.
459
			 *
460
			 * @param string SQL statement for inserting records
461
			 * @since 2014.03
462
			 * @category Developer
463
			 * @see mshop/index/manager/attribute/cleanup/ansi
464
			 * @see mshop/index/manager/attribute/delete/ansi
465
			 * @see mshop/index/manager/attribute/search/ansi
466
			 * @see mshop/index/manager/attribute/count/ansi
467
			 */
468
			$stmt = $this->getCachedStatement( $conn, 'mshop/index/manager/attribute/insert' );
469
470
			foreach( $items as $item ) {
471
				$this->saveAttributes( $stmt, $item );
472
			}
473
474
		foreach( $this->getSubManagers() as $submanager ) {
475
			$submanager->rebuild( $items );
476
		}
477
478
		return $this;
479
	}
480
481
482
	/**
483
	 * Removes the products from the product index.
484
	 *
485
	 * @param iterable|string $ids Product ID or list of IDs
486
	 * @return \Aimeos\MShop\Index\Manager\Iface Manager object for chaining method calls
487
	 */
488
	public function remove( $ids ) : \Aimeos\MShop\Index\Manager\Iface
489
	{
490
		parent::remove( $ids )->delete( $ids );
491
		return $this;
492
	}
493
494
495
	/**
496
	 * Searches for items matching the given criteria.
497
	 *
498
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
499
	 * @param string[] $ref List of domains to fetch list items and referenced items for
500
	 * @param int|null &$total Number of items that are available in total
501
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Product\Item\Iface with ids as keys
502
	 */
503
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
504
	{
505
		/** mshop/index/manager/attribute/search/mysql
506
		 * Retrieves the records matched by the given criteria in the database
507
		 *
508
		 * @see mshop/index/manager/attribute/search/ansi
509
		 */
510
511
		/** mshop/index/manager/attribute/search/ansi
512
		 * Retrieves the records matched by the given criteria in the database
513
		 *
514
		 * Fetches the records matched by the given criteria from the product index
515
		 * database. The records must be from one of the sites that are
516
		 * configured via the context item. If the current site is part of
517
		 * a tree of sites, the SELECT statement can retrieve all records
518
		 * from the current site and the complete sub-tree of sites.
519
		 *
520
		 * As the records can normally be limited by criteria from sub-managers,
521
		 * their tables must be joined in the SQL context. This is done by
522
		 * using the "internaldeps" property from the definition of the ID
523
		 * column of the sub-managers. These internal dependencies specify
524
		 * the JOIN between the tables and the used columns for joining. The
525
		 * ":joins" placeholder is then replaced by the JOIN strings from
526
		 * the sub-managers.
527
		 *
528
		 * To limit the records matched, conditions can be added to the given
529
		 * criteria object. It can contain comparisons like column names that
530
		 * must match specific values which can be combined by AND, OR or NOT
531
		 * operators. The resulting string of SQL conditions replaces the
532
		 * ":cond" placeholder before the statement is sent to the database
533
		 * server.
534
		 *
535
		 * If the records that are retrieved should be ordered by one or more
536
		 * columns, the generated string of column / sort direction pairs
537
		 * replaces the ":order" placeholder. In case no ordering is required,
538
		 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
539
		 * markers is removed to speed up retrieving the records. Columns of
540
		 * sub-managers can also be used for ordering the result set but then
541
		 * no index can be used.
542
		 *
543
		 * The number of returned records can be limited and can start at any
544
		 * number between the begining and the end of the result set. For that
545
		 * the ":size" and ":start" placeholders are replaced by the
546
		 * corresponding values from the criteria object. The default values
547
		 * are 0 for the start and 100 for the size value.
548
		 *
549
		 * The SQL statement should conform to the ANSI standard to be
550
		 * compatible with most relational database systems. This also
551
		 * includes using double quotes for table and column names.
552
		 *
553
		 * @param string SQL statement for searching items
554
		 * @since 2014.03
555
		 * @category Developer
556
		 * @see mshop/index/manager/attribute/count/ansi
557
		 * @see mshop/index/manager/attribute/optimize/ansi
558
		 * @see mshop/index/manager/attribute/aggregate/ansi
559
		 */
560
		$cfgPathSearch = 'mshop/index/manager/attribute/search';
561
562
		/** mshop/index/manager/attribute/count/mysql
563
		 * Counts the number of records matched by the given criteria in the database
564
		 *
565
		 * @see mshop/index/manager/attribute/count/ansi
566
		 */
567
568
		/** mshop/index/manager/attribute/count/ansi
569
		 * Counts the number of records matched by the given criteria in the database
570
		 *
571
		 * Counts all records matched by the given criteria from the product index
572
		 * database. The records must be from one of the sites that are
573
		 * configured via the context item. If the current site is part of
574
		 * a tree of sites, the statement can count all records from the
575
		 * current site and the complete sub-tree of sites.
576
		 *
577
		 * As the records can normally be limited by criteria from sub-managers,
578
		 * their tables must be joined in the SQL context. This is done by
579
		 * using the "internaldeps" property from the definition of the ID
580
		 * column of the sub-managers. These internal dependencies specify
581
		 * the JOIN between the tables and the used columns for joining. The
582
		 * ":joins" placeholder is then replaced by the JOIN strings from
583
		 * the sub-managers.
584
		 *
585
		 * To limit the records matched, conditions can be added to the given
586
		 * criteria object. It can contain comparisons like column names that
587
		 * must match specific values which can be combined by AND, OR or NOT
588
		 * operators. The resulting string of SQL conditions replaces the
589
		 * ":cond" placeholder before the statement is sent to the database
590
		 * server.
591
		 *
592
		 * Both, the strings for ":joins" and for ":cond" are the same as for
593
		 * the "search" SQL statement.
594
		 *
595
		 * Contrary to the "search" statement, it doesn't return any records
596
		 * but instead the number of records that have been found. As counting
597
		 * thousands of records can be a long running task, the maximum number
598
		 * of counted records is limited for performance reasons.
599
		 *
600
		 * The SQL statement should conform to the ANSI standard to be
601
		 * compatible with most relational database systems. This also
602
		 * includes using double quotes for table and column names.
603
		 *
604
		 * @param string SQL statement for counting items
605
		 * @since 2014.03
606
		 * @category Developer
607
		 * @see mshop/index/manager/attribute/search/ansi
608
		 * @see mshop/index/manager/attribute/optimize/ansi
609
		 * @see mshop/index/manager/attribute/aggregate/ansi
610
		 */
611
		$cfgPathCount = 'mshop/index/manager/attribute/count';
612
613
		return $this->searchItemsIndexBase( $search, $ref, $total, $cfgPathSearch, $cfgPathCount );
614
	}
615
616
617
	/**
618
	 * Returns the list of sub-managers available for the index attribute manager.
619
	 *
620
	 * @return \Aimeos\MShop\Index\Manager\Iface[] Associative list of the sub-domain as key and the manager object as value
621
	 */
622
	protected function getSubManagers() : array
623
	{
624
		if( $this->subManagers === null )
625
		{
626
			$this->subManagers = [];
627
			$config = $this->context()->config();
628
629
			/** mshop/index/manager/attribute/submanagers
630
			 * A list of sub-manager names used for indexing associated items to attributes
631
			 *
632
			 * All items referenced by a product (e.g. texts, prices, media,
633
			 * etc.) are added to the product index via specialized index
634
			 * managers. You can add the name of new sub-managers to add more
635
			 * data to the index or remove existing ones if you don't want to
636
			 * index that data at all.
637
			 *
638
			 * This option configures the sub-managers that cares about
639
			 * indexing data associated to product attributes.
640
			 *
641
			 * @param string List of index sub-manager names
642
			 * @since 2014.09
643
			 * @category User
644
			 * @category Developer
645
			 * @see mshop/index/manager/submanagers
646
			 */
647
			foreach( $config->get( 'mshop/index/manager/attribute/submanagers', [] ) as $domain )
648
			{
649
				$name = $config->get( 'mshop/index/manager/attribute/' . $domain . '/name' );
650
				$this->subManagers[$domain] = $this->object()->getSubManager( $domain, $name );
651
			}
652
653
			return $this->subManagers;
654
		}
655
656
		return $this->subManagers;
657
	}
658
659
660
	/**
661
	 * Saves the attribute items referenced by products
662
	 *
663
	 * @param \Aimeos\Base\DB\Statement\Iface $stmt Prepared SQL statement with place holders
664
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item containing associated attribute items
665
	 */
666
	protected function saveAttributes( \Aimeos\Base\DB\Statement\Iface $stmt, \Aimeos\MShop\Product\Item\Iface $item )
667
	{
668
		$context = $this->context();
669
		$siteid = $context->locale()->getSiteId();
670
		$products = $item->getRefItems( 'product', null, 'default' )->push( $item );
671
		$date = date( 'Y-m-d H:i:s' );
672
673
		foreach( $products as $product )
674
		{
675
			foreach( $product->getListItems( 'attribute' ) as $listItem )
676
			{
677
				if( ( $refItem = $listItem->getRefItem() ) === null ) {
678
					continue;
679
				}
680
681
				$stmt->bind( 1, $item->getId(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
682
				$stmt->bind( 2, $product->getId(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
683
				$stmt->bind( 3, $refItem->getId() );
684
				$stmt->bind( 4, $listItem->getType() );
685
				$stmt->bind( 5, $refItem->getType() );
686
				$stmt->bind( 6, $refItem->getCode() );
687
				$stmt->bind( 7, $date ); // mtime
688
				$stmt->bind( 8, $siteid );
689
690
				try {
691
					$stmt->execute()->finish();
692
				} catch( \Aimeos\Base\DB\Exception $e ) { ; } // Ignore duplicates
693
			}
694
		}
695
	}
696
}
697