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

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