Passed
Push — master ( d65935...df26ff )
by Aimeos
07:21
created

Standard::applyRules()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 20
rs 9.6111
cc 5
nc 7
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Product;
12
13
14
/**
15
 * Default implementation of the product frontend controller.
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Frontend\Base
22
	implements Iface, \Aimeos\Controller\Frontend\Common\Iface
23
{
24
	private $conditions = [];
25
	private $domains = [];
26
	private $sort = [];
27
	private $filter;
28
	private $manager;
29
	private $rules;
30
31
32
	/**
33
	 * Common initialization for controller classes
34
	 *
35
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
36
	 */
37
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
38
	{
39
		parent::__construct( $context );
40
41
		$this->manager = \Aimeos\MShop::create( $context, 'index' );
42
		$this->filter = $this->manager->filter( true );
43
		$this->conditions[] = $this->filter->compare( '!=', 'index.catalog.id', null );
44
		$this->conditions[] = $this->filter->getConditions();
45
	}
46
47
48
	/**
49
	 * Clones objects in controller and resets values
50
	 */
51
	public function __clone()
52
	{
53
		$this->filter = clone $this->filter;
54
	}
55
56
57
	/**
58
	 * Returns the aggregated count of products for the given key.
59
	 *
60
	 * @param string $key Search key to aggregate for, e.g. "index.attribute.id"
61
	 * @param string|null $value Search key for aggregating the value column
62
	 * @param string|null $type Type of the aggregation, empty string for count or "sum" or "avg" (average)
63
	 * @return \Aimeos\Map Associative list of key values as key and the product count for this key as value
64
	 * @since 2019.04
65
	 */
66
	public function aggregate( string $key, string $value = null, string $type = null ) : \Aimeos\Map
67
	{
68
		$this->filter->setConditions( $this->filter->and( $this->conditions ) );
69
		return $this->manager->aggregate( $this->filter, $key, $value, $type );
70
	}
71
72
73
	/**
74
	 * Adds attribute IDs for filtering where products must reference all IDs
75
	 *
76
	 * @param array|string $attrIds Attribute ID or list of IDs
77
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
78
	 * @since 2019.04
79
	 */
80
	public function allOf( $attrIds ) : Iface
81
	{
82
		if( !empty( $attrIds ) && ( $ids = $this->validateIds( (array) $attrIds ) ) !== [] )
83
		{
84
			$func = $this->filter->make( 'index.attribute:allof', [$ids] );
85
			$this->conditions[] = $this->filter->compare( '!=', $func, null );
86
		}
87
88
		return $this;
89
	}
90
91
92
	/**
93
	 * Adds catalog IDs for filtering
94
	 *
95
	 * @param array|string $catIds Catalog ID or list of IDs
96
	 * @param string $listtype List type of the products referenced by the categories
97
	 * @param int $level Constant from \Aimeos\MW\Tree\Manager\Base if products in subcategories are matched too
98
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
99
	 * @since 2019.04
100
	 */
101
	public function category( $catIds, string $listtype = 'default', int $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE ) : Iface
102
	{
103
		if( !empty( $catIds ) && ( $ids = $this->validateIds( (array) $catIds ) ) !== [] )
104
		{
105
			if( $level != \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE )
106
			{
107
				$list = map();
108
				$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'catalog' );
109
110
				foreach( $ids as $catId ) {
111
					$list->union( $cntl->root( $catId )->getTree( $level )->toList() );
112
				}
113
114
				$ids = $this->validateIds( $list->keys()->toArray() );
115
			}
116
117
			$func = $this->filter->make( 'index.catalog:position', [$listtype, $ids] );
118
119
			$this->conditions[] = $this->filter->compare( '==', 'index.catalog.id', $ids );
120
			$this->conditions[] = $this->filter->compare( '>=', $func, 0 );
121
122
			$func = $this->filter->make( 'sort:index.catalog:position', [$listtype, $ids] );
123
			$this->sort[] = $this->filter->sort( '+', $func );
124
			$this->sort[] = $this->filter->sort( '+', 'product.id' ); // prevent flaky order if products have same position
125
		}
126
127
		return $this;
128
	}
129
130
131
	/**
132
	 * Adds generic condition for filtering products
133
	 *
134
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
135
	 * @param string $key Search key defined by the product manager, e.g. "product.status"
136
	 * @param array|string $value Value or list of values to compare to
137
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
138
	 * @since 2019.04
139
	 */
140
	public function compare( string $operator, string $key, $value ) : Iface
141
	{
142
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
143
		return $this;
144
	}
145
146
147
	/**
148
	 * Returns the product for the given product code
149
	 *
150
	 * @param string $code Unique product code
151
	 * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items
152
	 * @since 2019.04
153
	 */
154
	public function find( string $code ) : \Aimeos\MShop\Product\Item\Iface
155
	{
156
		return $this->applyRules( $this->manager->find( $code, $this->domains, 'product', null, true ) );
157
	}
158
159
160
	/**
161
	 * Creates a search function string for the given name and parameters
162
	 *
163
	 * @param string $name Name of the search function without parenthesis, e.g. "product:has"
164
	 * @param array $params List of parameters for the search function with numeric keys starting at 0
165
	 * @return string Search function string that can be used in compare()
166
	 */
167
	public function function( string $name, array $params ) : string
168
	{
169
		return $this->filter->make( $name, $params );
170
	}
171
172
173
	/**
174
	 * Returns the product for the given product ID
175
	 *
176
	 * @param string $id Unique product ID
177
	 * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items
178
	 * @since 2019.04
179
	 */
180
	public function get( string $id ) : \Aimeos\MShop\Product\Item\Iface
181
	{
182
		return $this->applyRules( $this->manager->get( $id, $this->domains, true ) );
183
	}
184
185
186
	/**
187
	 * Adds a filter to return only items containing a reference to the given ID
188
	 *
189
	 * @param string $domain Domain name of the referenced item, e.g. "attribute"
190
	 * @param string|null $type Type code of the reference, e.g. "variant" or null for all types
191
	 * @param string|null $refId ID of the referenced item of the given domain or null for all references
192
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
193
	 * @since 2019.04
194
	 */
195
	public function has( string $domain, string $type = null, string $refId = null ) : Iface
196
	{
197
		$params = [$domain];
198
		!$type ?: $params[] = $type;
199
		!$refId ?: $params[] = $refId;
200
201
		$func = $this->filter->make( 'product:has', $params );
202
		$this->conditions[] = $this->filter->compare( '!=', $func, null );
203
		return $this;
204
	}
205
206
207
	/**
208
	 * Adds attribute IDs for filtering where products must reference at least one ID
209
	 *
210
	 * If an array of ID lists is given, each ID list is added separately as condition.
211
	 *
212
	 * @param array|string $attrIds Attribute ID, list of IDs or array of lists with IDs
213
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
214
	 * @since 2019.04
215
	 */
216
	public function oneOf( $attrIds ) : Iface
217
	{
218
		$attrIds = (array) $attrIds;
219
220
		foreach( $attrIds as $key => $entry )
221
		{
222
			if( is_array( $entry ) && ( $ids = $this->validateIds( $entry ) ) !== [] )
223
			{
224
				$func = $this->filter->make( 'index.attribute:oneof', [$ids] );
225
				$this->conditions[] = $this->filter->compare( '!=', $func, null );
226
				unset( $attrIds[$key] );
227
			}
228
		}
229
230
		if( ( $ids = $this->validateIds( $attrIds ) ) !== [] )
231
		{
232
			$func = $this->filter->make( 'index.attribute:oneof', [$ids] );
233
			$this->conditions[] = $this->filter->compare( '!=', $func, null );
234
		}
235
236
		return $this;
237
	}
238
239
240
	/**
241
	 * Parses the given array and adds the conditions to the list of conditions
242
	 *
243
	 * @param array $conditions List of conditions, e.g. [['>' => ['product.status' => 0]], ['==' => ['product.type' => 'default']]]
244
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
245
	 * @since 2019.04
246
	 */
247
	public function parse( array $conditions ) : Iface
248
	{
249
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
250
			$this->conditions[] = $cond;
251
		}
252
253
		return $this;
254
	}
255
256
257
	/**
258
	 * Adds price restrictions for filtering
259
	 *
260
	 * @param array|string $value Upper price limit, list of lower and upper price or NULL for no restrictions
261
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
262
	 * @since 2020.10
263
	 */
264
	public function price( $value = null ) : Iface
265
	{
266
		if( $value )
267
		{
268
			$value = (array) $value;
269
			$func = $this->filter->make( 'index.price:value', [$this->getContext()->getLocale()->getCurrencyId()] );
270
271
			$this->conditions['price-high'] = $this->filter->compare( '<=', $func, sprintf( '%013.2F', end( $value ) ) );
272
273
			if( count( $value ) > 1 ) {
274
				$this->conditions['price-low'] = $this->filter->compare( '>=', $func, sprintf( '%013.2F', reset( $value ) ) );
275
			}
276
		}
277
278
		return $this;
279
	}
280
281
282
	/**
283
	 * Adds product IDs for filtering
284
	 *
285
	 * @param array|string $prodIds Product ID or list of IDs
286
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
287
	 * @since 2019.04
288
	 */
289
	public function product( $prodIds ) : Iface
290
	{
291
		if( !empty( $prodIds ) && ( $ids = array_unique( $this->validateIds( (array) $prodIds ) ) ) !== [] ) {
292
			$this->conditions[] = $this->filter->compare( '==', 'product.id', $ids );
293
		}
294
295
		return $this;
296
	}
297
298
299
	/**
300
	 * Adds a filter to return only items containing the property
301
	 *
302
	 * @param string $type Type code of the property, e.g. "isbn"
303
	 * @param string|null $value Exact value of the property
304
	 * @param string|null $langid ISO country code (en or en_US) or null if not language specific
305
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
306
	 * @since 2019.04
307
	 */
308
	public function property( string $type, string $value = null, string $langid = null ) : Iface
309
	{
310
		$func = $this->filter->make( 'product:prop', [$type, $langid, $value] );
311
		$this->conditions[] = $this->filter->compare( '!=', $func, null );
312
		return $this;
313
	}
314
315
316
	/**
317
	 * Returns the product for the given product URL name
318
	 *
319
	 * @param string $name Product URL name
320
	 * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items
321
	 * @since 2019.04
322
	 */
323
	public function resolve( string $name ) : \Aimeos\MShop\Product\Item\Iface
324
	{
325
		$search = $this->manager->filter( true )->slice( 0, 1 )->add( ['index.text:url()' => $name] );
326
327
		if( ( $item = $this->manager->search( $search, $this->domains )->first() ) === null )
328
		{
329
			$msg = $this->getContext()->getI18n()->dt( 'controller/frontend', 'Unable to find product "%1$s"' );
330
			throw new \Aimeos\Controller\Frontend\Product\Exception( sprintf( $msg, $name ) );
331
		}
332
333
		return $this->applyRules( $item );
334
	}
335
336
337
	/**
338
	 * Returns the products filtered by the previously assigned conditions
339
	 *
340
	 * @param int &$total Parameter where the total number of found products will be stored in
341
	 * @return \Aimeos\Map Ordered list of product items implementing \Aimeos\MShop\Product\Item\Iface
342
	 * @since 2019.04
343
	 */
344
	public function search( int &$total = null ) : \Aimeos\Map
345
	{
346
		$this->filter->setSortations( $this->sort );
347
		$this->filter->setConditions( $this->filter->and( $this->conditions ) );
348
349
		return $this->applyRules( $this->manager->search( $this->filter, $this->domains, $total ) );
350
	}
351
352
353
	/**
354
	 * Sets the start value and the number of returned products for slicing the list of found products
355
	 *
356
	 * @param int $start Start value of the first product in the list
357
	 * @param int $limit Number of returned products
358
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
359
	 * @since 2019.04
360
	 */
361
	public function slice( int $start, int $limit ) : Iface
362
	{
363
		$this->filter->slice( $start, $limit );
364
		return $this;
365
	}
366
367
368
	/**
369
	 * Sets the sorting of the result list
370
	 *
371
	 * @param string|null $key Sorting of the result list like "name", "-name", "price", "-price", "code", "-code",
372
	 * 	"ctime, "-ctime", "relevance" or comma separated combinations and null for no sorting
373
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
374
	 * @since 2019.04
375
	 */
376
	public function sort( string $key = null ) : Iface
377
	{
378
		$list = ( $key ? explode( ',', $key ) : $this->sort = [] );
379
380
		foreach( $list as $sortkey )
381
		{
382
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
383
			$sortkey = ltrim( $sortkey, '+-' );
384
385
			switch( $sortkey )
386
			{
387
				case 'relevance':
388
					break;
389
390
				case 'code':
391
					$this->sort[] = $this->filter->sort( $direction, 'product.code' );
392
					break;
393
394
				case 'ctime':
395
					$this->sort[] = $this->filter->sort( $direction, 'product.ctime' );
396
					break;
397
398
				case 'name':
399
					$langid = $this->getContext()->getLocale()->getLanguageId();
400
401
					$cmpfunc = $this->filter->make( 'index.text:name', [$langid] );
402
					$this->conditions[] = $this->filter->compare( '!=', $cmpfunc, null );
403
404
					$sortfunc = $this->filter->make( 'sort:index.text:name', [$langid] );
405
					$this->sort[] = $this->filter->sort( $direction, $sortfunc );
406
					break;
407
408
				case 'price':
409
					$currencyid = $this->getContext()->getLocale()->getCurrencyId();
410
					$sortfunc = $this->filter->make( 'sort:index.price:value', [$currencyid] );
411
412
					$cmpfunc = $this->filter->make( 'index.price:value', [$currencyid] );
413
					$this->conditions['price-sort'] = $this->filter->compare( '!=', $cmpfunc, null );
414
415
					$this->sort[] = $this->filter->sort( $direction, $sortfunc );
416
					break;
417
418
				default:
419
					$this->sort[] = $this->filter->sort( $direction, $sortkey );
420
			}
421
		}
422
423
		$this->filter->setSortations( $this->sort );
424
		return $this;
425
	}
426
427
428
	/**
429
	 * Adds supplier IDs for filtering
430
	 *
431
	 * @param array|string $supIds Supplier ID or list of IDs
432
	 * @param string $listtype List type of the products referenced by the suppliers
433
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
434
	 * @since 2019.04
435
	 */
436
	public function supplier( $supIds, string $listtype = 'default' ) : Iface
437
	{
438
		if( !empty( $supIds ) && ( $ids = array_unique( $this->validateIds( (array) $supIds ) ) ) !== [] )
439
		{
440
			$func = $this->filter->make( 'index.supplier:position', [$listtype, $ids] );
441
442
			$this->conditions[] = $this->filter->compare( '==', 'index.supplier.id', $ids );
443
			$this->conditions[] = $this->filter->compare( '>=', $func, 0 );
444
445
			$func = $this->filter->make( 'sort:index.supplier:position', [$listtype, $ids] );
446
			$this->sort[] = $this->filter->sort( '+', $func );
447
		}
448
449
		return $this;
450
	}
451
452
453
	/**
454
	 * Adds input string for full text search
455
	 *
456
	 * @param string|null $text User input for full text search
457
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
458
	 * @since 2019.04
459
	 */
460
	public function text( string $text = null ) : Iface
461
	{
462
		if( !empty( $text ) )
463
		{
464
			$langid = $this->getContext()->getLocale()->getLanguageId();
465
			$func = $this->filter->make( 'index.text:relevance', [$langid, $text] );
466
			$sortfunc = $this->filter->make( 'sort:index.text:relevance', [$langid, $text] );
467
468
			$this->conditions[] = $this->filter->compare( '>', $func, 0 );
469
			$this->sort[] = $this->filter->sort( '-', $sortfunc );
470
		}
471
472
		return $this;
473
	}
474
475
476
	/**
477
	 * Sets the referenced domains that will be fetched too when retrieving items
478
	 *
479
	 * @param array $domains Domain names of the referenced items that should be fetched too
480
	 * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface
481
	 * @since 2019.04
482
	 */
483
	public function uses( array $domains ) : Iface
484
	{
485
		$this->domains = $domains;
486
		return $this;
487
	}
488
489
490
491
	/**
492
	 * Applies the rules for modifying products (prices) dynamically
493
	 *
494
	 * @param \Aimeos\Map|\Aimeos\MShop\Product\Item\Iface $products Product or list of products
495
	 * @return \Aimeos\Map|\Aimeos\MShop\Product\Item\Iface Modified product or list of products
496
	 */
497
	protected function applyRules( $products )
498
	{
499
		$rules = $this->getRuleItems();
500
		$manager = \Aimeos\MShop::create( $this->getContext(), 'rule' );
501
502
		foreach( $rules as $key => $rule )
503
		{
504
			if( $rule instanceof \Aimeos\MShop\Rule\Item\Iface ) {
505
				$rules->set( $key, $rule = $manager->getProvider( $rule, 'catalog' ) );
506
			}
507
508
			foreach( map( $products ) as $product )
509
			{
510
				if( $rule->apply( $product ) ) {
511
					return $products;
512
				}
513
			}
514
		}
515
516
		return $products;
517
	}
518
519
520
	/**
521
	 * Returns the list of catalog IDs for the given catalog tree
522
	 *
523
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item with children
524
	 * @return array List of catalog IDs
525
	 */
526
	protected function getCatalogIdsFromTree( \Aimeos\MShop\Catalog\Item\Iface $item ) : array
527
	{
528
		if( $item->getStatus() < 1 ) {
529
			return [];
530
		}
531
532
		$list = [$item->getId()];
533
534
		foreach( $item->getChildren() as $child ) {
535
			$list = array_merge( $list, $this->getCatalogIdsFromTree( $child ) );
536
		}
537
538
		return $list;
539
	}
540
541
542
	/**
543
	 * Creates and returns the rule stack for modifying products (prices) dynamically
544
	 *
545
	 * @return \Aimeos\Map List of rule items implementing \Aimeos\MShop\Rule\Item\Iface
546
	 */
547
	protected function getRuleItems() : \Aimeos\Map
548
	{
549
		if( !isset( $this->rules ) )
550
		{
551
			$manager = \Aimeos\MShop::create( $this->getContext(), 'rule' );
552
			$filter = $manager->filter( true )->add( ['rule.type' => 'catalog'] )
553
				->order( 'rule.position' )->slice( 0, 10000 );
554
555
			$this->rules = $manager->search( $filter );
556
		}
557
558
		return $this->rules;
559
	}
560
561
562
	/**
563
	 * Validates the given IDs as integers
564
	 *
565
	 * @param array $ids List of IDs to validate
566
	 * @return array List of validated IDs
567
	 */
568
	protected function validateIds( array $ids ) : array
569
	{
570
		$list = [];
571
572
		foreach( $ids as $id )
573
		{
574
			if( is_array( $id ) ) {
575
				$list[] = $this->validateIds( $id );
576
			} elseif( $id != '' && preg_match( '/^[A-Za-z0-9\-\_]+$/', $id ) === 1 ) {
577
				$list[] = (string) $id;
578
			}
579
		}
580
581
		return $list;
582
	}
583
}
584