Passed
Pull Request — master (#297)
by Aimeos
10:30
created

Standard::iterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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
	 * Iterates over all matching items and returns the found ones
382
	 *
383
	 * @param \Aimeos\MShop\Common\Iterator\Iface $iterator Iterator object with conditions, sortations, etc.
384
	 * @param string[] $ref List of domains to fetch list items and referenced items for
385
	 * @param int $count Maximum number of items which should be returned
386
	 * @return \Aimeos\Map|null List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys
387
	 */
388
	public function iterate( \Aimeos\MShop\Common\Iterator\Iface $iterator, array $ref = [], int $count = 100 ) : ?\Aimeos\Map
389
	{
390
		return $this->iterateIndexBase( $iterator, $ref, $count );
391
	}
392
393
394
	/**
395
	 * Creates a new iterator based on the filter criteria
396
	 *
397
	 * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc.
398
	 * @return \Aimeos\MShop\Common\Iterator\Iface Iterator object
399
	 */
400
	public function iterator( \Aimeos\Base\Criteria\Iface $filter ) : \Aimeos\MShop\Common\Iterator\Iface
401
	{
402
		return $this->iteratorIndexBase( $filter, 'mshop/index/manager/iterate' );
403
	}
404
405
406
	/**
407
	 * Optimizes the index if necessary.
408
	 * Execution of this operation can take a very long time and shouldn't be
409
	 * called through a web server enviroment.
410
	 *
411
	 * @return \Aimeos\MShop\Index\Manager\Iface Manager object for chaining method calls
412
	 */
413
	public function optimize() : \Aimeos\MShop\Index\Manager\Iface
414
	{
415
		/** mshop/index/manager/attribute/optimize/mysql
416
		 * Optimizes the stored attribute data for retrieving the records faster
417
		 *
418
		 * @see mshop/index/manager/attribute/optimize/ansi
419
		 */
420
421
		/** mshop/index/manager/attribute/optimize/ansi
422
		 * Optimizes the stored attribute data for retrieving the records faster
423
		 *
424
		 * The SQL statement should reorganize the data in the DBMS storage to
425
		 * optimize access to the records of the table or tables. Some DBMS
426
		 * offer specialized statements to optimize indexes and records. This
427
		 * statement doesn't return any records.
428
		 *
429
		 * The SQL statement should conform to the ANSI standard to be
430
		 * compatible with most relational database systems. This also
431
		 * includes using double quotes for table and column names.
432
		 *
433
		 * @param string SQL statement for optimizing the stored attribute data
434
		 * @since 2014.09
435
		 * @category Developer
436
		 * @see mshop/index/manager/attribute/count/ansi
437
		 * @see mshop/index/manager/attribute/search/ansi
438
		 * @see mshop/index/manager/attribute/aggregate/ansi
439
		 */
440
		return $this->optimizeBase( 'mshop/index/manager/attribute/optimize' );
441
	}
442
443
444
	/**
445
	 * Rebuilds the index attribute for searching products or specified list of products.
446
	 * This can be a long lasting operation.
447
	 *
448
	 * @param \Aimeos\MShop\Product\Item\Iface[] $items Associative list of product IDs as keys and items as values
449
	 * @return \Aimeos\MShop\Index\Manager\Iface Manager object for chaining method calls
450
	 */
451
	public function rebuild( iterable $items = [] ) : \Aimeos\MShop\Index\Manager\Iface
452
	{
453
		if( ( $items = map( $items ) )->isEmpty() ) { return $this; }
454
455
		$items->implements( \Aimeos\MShop\Product\Item\Iface::class, true );
456
457
		$context = $this->context();
458
		$conn = $context->db( $this->getResourceName() );
459
460
			/** mshop/index/manager/attribute/insert/mysql
461
			 * Inserts a new attribute record into the product index database
462
			 *
463
			 * @see mshop/index/manager/attribute/insert/ansi
464
			 */
465
466
			/** mshop/index/manager/attribute/insert/ansi
467
			 * Inserts a new attribute record into the product index database
468
			 *
469
			 * During the product index rebuild, attributes related to a product
470
			 * will be stored in the index for this product. All records
471
			 * are deleted before the new ones are inserted.
472
			 *
473
			 * The SQL statement must be a string suitable for being used as
474
			 * prepared statement. It must include question marks for binding
475
			 * the values from the order item to the statement before they are
476
			 * sent to the database server. The number of question marks must
477
			 * be the same as the number of columns listed in the INSERT
478
			 * statement. The order of the columns must correspond to the
479
			 * order in the rebuild() method, so the correct values are
480
			 * bound to the columns.
481
			 *
482
			 * The SQL statement should conform to the ANSI standard to be
483
			 * compatible with most relational database systems. This also
484
			 * includes using double quotes for table and column names.
485
			 *
486
			 * @param string SQL statement for inserting records
487
			 * @since 2014.03
488
			 * @category Developer
489
			 * @see mshop/index/manager/attribute/cleanup/ansi
490
			 * @see mshop/index/manager/attribute/delete/ansi
491
			 * @see mshop/index/manager/attribute/search/ansi
492
			 * @see mshop/index/manager/attribute/count/ansi
493
			 */
494
			$stmt = $this->getCachedStatement( $conn, 'mshop/index/manager/attribute/insert' );
495
496
			foreach( $items as $item ) {
497
				$this->saveAttributes( $stmt, $item );
498
			}
499
500
		foreach( $this->getSubManagers() as $submanager ) {
501
			$submanager->rebuild( $items );
502
		}
503
504
		return $this;
505
	}
506
507
508
	/**
509
	 * Removes the products from the product index.
510
	 *
511
	 * @param array|string $ids Product ID or list of IDs
512
	 * @return \Aimeos\MShop\Index\Manager\Iface Manager object for chaining method calls
513
	 */
514
	public function remove( $ids ) : \Aimeos\MShop\Index\Manager\Iface
515
	{
516
		parent::remove( $ids )->delete( $ids );
517
		return $this;
518
	}
519
520
521
	/**
522
	 * Searches for items matching the given criteria.
523
	 *
524
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
525
	 * @param string[] $ref List of domains to fetch list items and referenced items for
526
	 * @param int|null &$total Number of items that are available in total
527
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Product\Item\Iface with ids as keys
528
	 */
529
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
530
	{
531
		/** mshop/index/manager/attribute/search/mysql
532
		 * Retrieves the records matched by the given criteria in the database
533
		 *
534
		 * @see mshop/index/manager/attribute/search/ansi
535
		 */
536
537
		/** mshop/index/manager/attribute/search/ansi
538
		 * Retrieves the records matched by the given criteria in the database
539
		 *
540
		 * Fetches the records matched by the given criteria from the product index
541
		 * database. The records must be from one of the sites that are
542
		 * configured via the context item. If the current site is part of
543
		 * a tree of sites, the SELECT statement can retrieve all records
544
		 * from the current site and the complete sub-tree of sites.
545
		 *
546
		 * As the records can normally be limited by criteria from sub-managers,
547
		 * their tables must be joined in the SQL context. This is done by
548
		 * using the "internaldeps" property from the definition of the ID
549
		 * column of the sub-managers. These internal dependencies specify
550
		 * the JOIN between the tables and the used columns for joining. The
551
		 * ":joins" placeholder is then replaced by the JOIN strings from
552
		 * the sub-managers.
553
		 *
554
		 * To limit the records matched, conditions can be added to the given
555
		 * criteria object. It can contain comparisons like column names that
556
		 * must match specific values which can be combined by AND, OR or NOT
557
		 * operators. The resulting string of SQL conditions replaces the
558
		 * ":cond" placeholder before the statement is sent to the database
559
		 * server.
560
		 *
561
		 * If the records that are retrieved should be ordered by one or more
562
		 * columns, the generated string of column / sort direction pairs
563
		 * replaces the ":order" placeholder. In case no ordering is required,
564
		 * the complete ORDER BY part including the "\/*-orderby*\/...\/*orderby-*\/"
565
		 * markers is removed to speed up retrieving the records. Columns of
566
		 * sub-managers can also be used for ordering the result set but then
567
		 * no index can be used.
568
		 *
569
		 * The number of returned records can be limited and can start at any
570
		 * number between the begining and the end of the result set. For that
571
		 * the ":size" and ":start" placeholders are replaced by the
572
		 * corresponding values from the criteria object. The default values
573
		 * are 0 for the start and 100 for the size value.
574
		 *
575
		 * The SQL statement should conform to the ANSI standard to be
576
		 * compatible with most relational database systems. This also
577
		 * includes using double quotes for table and column names.
578
		 *
579
		 * @param string SQL statement for searching items
580
		 * @since 2014.03
581
		 * @category Developer
582
		 * @see mshop/index/manager/attribute/count/ansi
583
		 * @see mshop/index/manager/attribute/optimize/ansi
584
		 * @see mshop/index/manager/attribute/aggregate/ansi
585
		 */
586
		$cfgPathSearch = 'mshop/index/manager/attribute/search';
587
588
		/** mshop/index/manager/attribute/count/mysql
589
		 * Counts the number of records matched by the given criteria in the database
590
		 *
591
		 * @see mshop/index/manager/attribute/count/ansi
592
		 */
593
594
		/** mshop/index/manager/attribute/count/ansi
595
		 * Counts the number of records matched by the given criteria in the database
596
		 *
597
		 * Counts all records matched by the given criteria from the product index
598
		 * database. The records must be from one of the sites that are
599
		 * configured via the context item. If the current site is part of
600
		 * a tree of sites, the statement can count all records from the
601
		 * current site and the complete sub-tree of sites.
602
		 *
603
		 * As the records can normally be limited by criteria from sub-managers,
604
		 * their tables must be joined in the SQL context. This is done by
605
		 * using the "internaldeps" property from the definition of the ID
606
		 * column of the sub-managers. These internal dependencies specify
607
		 * the JOIN between the tables and the used columns for joining. The
608
		 * ":joins" placeholder is then replaced by the JOIN strings from
609
		 * the sub-managers.
610
		 *
611
		 * To limit the records matched, conditions can be added to the given
612
		 * criteria object. It can contain comparisons like column names that
613
		 * must match specific values which can be combined by AND, OR or NOT
614
		 * operators. The resulting string of SQL conditions replaces the
615
		 * ":cond" placeholder before the statement is sent to the database
616
		 * server.
617
		 *
618
		 * Both, the strings for ":joins" and for ":cond" are the same as for
619
		 * the "search" SQL statement.
620
		 *
621
		 * Contrary to the "search" statement, it doesn't return any records
622
		 * but instead the number of records that have been found. As counting
623
		 * thousands of records can be a long running task, the maximum number
624
		 * of counted records is limited for performance reasons.
625
		 *
626
		 * The SQL statement should conform to the ANSI standard to be
627
		 * compatible with most relational database systems. This also
628
		 * includes using double quotes for table and column names.
629
		 *
630
		 * @param string SQL statement for counting items
631
		 * @since 2014.03
632
		 * @category Developer
633
		 * @see mshop/index/manager/attribute/search/ansi
634
		 * @see mshop/index/manager/attribute/optimize/ansi
635
		 * @see mshop/index/manager/attribute/aggregate/ansi
636
		 */
637
		$cfgPathCount = 'mshop/index/manager/attribute/count';
638
639
		return $this->searchItemsIndexBase( $search, $ref, $total, $cfgPathSearch, $cfgPathCount );
640
	}
641
642
643
	/**
644
	 * Returns the list of sub-managers available for the index attribute manager.
645
	 *
646
	 * @return \Aimeos\MShop\Index\Manager\Iface[] Associative list of the sub-domain as key and the manager object as value
647
	 */
648
	protected function getSubManagers() : array
649
	{
650
		if( $this->subManagers === null )
651
		{
652
			$this->subManagers = [];
653
			$config = $this->context()->config();
654
655
			/** mshop/index/manager/attribute/submanagers
656
			 * A list of sub-manager names used for indexing associated items to attributes
657
			 *
658
			 * All items referenced by a product (e.g. texts, prices, media,
659
			 * etc.) are added to the product index via specialized index
660
			 * managers. You can add the name of new sub-managers to add more
661
			 * data to the index or remove existing ones if you don't want to
662
			 * index that data at all.
663
			 *
664
			 * This option configures the sub-managers that cares about
665
			 * indexing data associated to product attributes.
666
			 *
667
			 * @param string List of index sub-manager names
668
			 * @since 2014.09
669
			 * @category User
670
			 * @category Developer
671
			 * @see mshop/index/manager/submanagers
672
			 */
673
			foreach( $config->get( 'mshop/index/manager/attribute/submanagers', [] ) as $domain )
674
			{
675
				$name = $config->get( 'mshop/index/manager/attribute/' . $domain . '/name' );
676
				$this->subManagers[$domain] = $this->object()->getSubManager( $domain, $name );
677
			}
678
679
			return $this->subManagers;
680
		}
681
682
		return $this->subManagers;
683
	}
684
685
686
	/**
687
	 * Saves the attribute items referenced by products
688
	 *
689
	 * @param \Aimeos\Base\DB\Statement\Iface $stmt Prepared SQL statement with place holders
690
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item containing associated attribute items
691
	 */
692
	protected function saveAttributes( \Aimeos\Base\DB\Statement\Iface $stmt, \Aimeos\MShop\Product\Item\Iface $item )
693
	{
694
		$context = $this->context();
695
		$siteid = $context->locale()->getSiteId();
696
		$products = $item->getRefItems( 'product', null, 'default' )->push( $item );
697
		$date = date( 'Y-m-d H:i:s' );
698
699
		foreach( $products as $product )
700
		{
701
			foreach( $product->getListItems( 'attribute' ) as $listItem )
702
			{
703
				if( ( $refItem = $listItem->getRefItem() ) === null ) {
704
					continue;
705
				}
706
707
				$stmt->bind( 1, $item->getId(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
708
				$stmt->bind( 2, $product->getId(), \Aimeos\Base\DB\Statement\Base::PARAM_INT );
709
				$stmt->bind( 3, $refItem->getId() );
710
				$stmt->bind( 4, $listItem->getType() );
711
				$stmt->bind( 5, $refItem->getType() );
712
				$stmt->bind( 6, $refItem->getCode() );
713
				$stmt->bind( 7, $date ); // mtime
714
				$stmt->bind( 8, $siteid );
715
716
				try {
717
					$stmt->execute()->finish();
718
				} catch( \Aimeos\Base\DB\Exception $e ) { ; } // Ignore duplicates
719
			}
720
		}
721
	}
722
}
723