Passed
Push — master ( 9c9615...50d1d9 )
by Aimeos
10:13
created

Standard::rebuild()   C

Complexity

Conditions 12
Paths 27

Size

Total Lines 102
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 41
c 0
b 0
f 0
nc 27
nop 1
dl 0
loc 102
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

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