Passed
Push — master ( 9a0039...2f021f )
by Aimeos
07:58
created

Standard::allOf()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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