Standard::uses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Catalog;
12
13
14
/**
15
 * Default implementation of the catalog 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
	/** controller/frontend/catalog/name
25
	 * Class name of the used catalog frontend controller implementation
26
	 *
27
	 * Each default frontend controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Frontend\Catalog\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Frontend\Catalog\Mycatalog
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/frontend/catalog/name = Mycatalog
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyCatalog"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 * @category Developer
56
	 */
57
58
	/** controller/frontend/catalog/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the catalog frontend controllers
60
	 *
61
	 * Decorators extend the functionality of a class by adding new aspects
62
	 * (e.g. log what is currently done), executing the methods of the underlying
63
	 * class only in certain conditions (e.g. only for logged in users) or
64
	 * modify what is returned to the caller.
65
	 *
66
	 * This option allows you to remove a decorator added via
67
	 * "controller/frontend/common/decorators/default" before they are wrapped
68
	 * around the frontend controller.
69
	 *
70
	 *  controller/frontend/catalog/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\Controller\Frontend\Common\Decorator\*") added via
74
	 * "controller/frontend/common/decorators/default" for the catalog frontend controller.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2014.03
78
	 * @category Developers
79
	 * @see controller/frontend/common/decorators/default
80
	 * @see controller/frontend/catalog/decorators/global
81
	 * @see controller/frontend/catalog/decorators/local
82
	 */
83
84
	/** controller/frontend/catalog/decorators/global
85
	 * Adds a list of globally available decorators only to the catalog frontend controllers
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\Controller\Frontend\Common\Decorator\*") around the frontend controller.
94
	 *
95
	 *  controller/frontend/catalog/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator1" only to the frontend controller.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2014.03
102
	 * @category Developers
103
	 * @see controller/frontend/common/decorators/default
104
	 * @see controller/frontend/catalog/decorators/excludes
105
	 * @see controller/frontend/catalog/decorators/local
106
	 */
107
108
	/** controller/frontend/catalog/decorators/local
109
	 * Adds a list of local decorators only to the catalog frontend controllers
110
	 *
111
	 * Decorators extend the functionality of a class by adding new aspects
112
	 * (e.g. log what is currently done), executing the methods of the underlying
113
	 * class only in certain conditions (e.g. only for logged in users) or
114
	 * modify what is returned to the caller.
115
	 *
116
	 * This option allows you to wrap local decorators
117
	 * ("\Aimeos\Controller\Frontend\Catalog\Decorator\*") around the frontend controller.
118
	 *
119
	 *  controller/frontend/catalog/decorators/local = array( 'decorator2' )
120
	 *
121
	 * This would add the decorator named "decorator2" defined by
122
	 * "\Aimeos\Controller\Frontend\Catalog\Decorator\Decorator2" only to the frontend
123
	 * controller.
124
	 *
125
	 * @param array List of decorator names
126
	 * @since 2014.03
127
	 * @category Developers
128
	 * @see controller/frontend/common/decorators/default
129
	 * @see controller/frontend/catalog/decorators/excludes
130
	 * @see controller/frontend/catalog/decorators/global
131
	 */
132
133
134
	 private \Aimeos\MShop\Common\Manager\Iface $manager;
135
	 private \Aimeos\Base\Criteria\Iface $filter;
136
	 private ?string $root = null;
137
	 private array $domains = [];
138
139
140
	/**
141
	 * Common initialization for controller classes
142
	 *
143
	 * @param \Aimeos\MShop\ContextIface $context Common MShop context object
144
	 */
145
	public function __construct( \Aimeos\MShop\ContextIface $context )
146
	{
147
		parent::__construct( $context );
148
149
		$this->manager = \Aimeos\MShop::create( $context, 'catalog' );
150
		$this->filter = $this->manager->filter( true );
151
	}
152
153
154
	/**
155
	 * Clones objects in controller
156
	 */
157
	public function __clone()
158
	{
159
		$this->filter = clone $this->filter;
160
		parent::__clone();
161
	}
162
163
164
	/**
165
	 * Adds generic condition for filtering attributes
166
	 *
167
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
168
	 * @param string $key Search key defined by the catalog manager, e.g. "catalog.status"
169
	 * @param array|string $value Value or list of values to compare to
170
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
171
	 * @since 2019.04
172
	 */
173
	public function compare( string $operator, string $key, $value ) : Iface
174
	{
175
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
176
		return $this;
177
	}
178
179
180
	/**
181
	 * Returns the category for the given catalog code
182
	 *
183
	 * @param string $code Unique catalog code
184
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item including the referenced domains items
185
	 * @since 2019.04
186
	 */
187
	public function find( string $code ) : \Aimeos\MShop\Catalog\Item\Iface
188
	{
189
		return $this->manager->find( $code, $this->domains, null, null, null );
190
	}
191
192
193
	/**
194
	 * Creates a search function string for the given name and parameters
195
	 *
196
	 * @param string $name Name of the search function without parenthesis, e.g. "catalog:has"
197
	 * @param array $params List of parameters for the search function with numeric keys starting at 0
198
	 * @return string Search function string that can be used in compare()
199
	 */
200
	public function function( string $name, array $params ) : string
201
	{
202
		return $this->filter->make( $name, $params );
203
	}
204
205
206
	/**
207
	 * Returns the category for the given catalog ID
208
	 *
209
	 * @param string $id Unique catalog ID
210
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item including the referenced domains items
211
	 * @since 2019.04
212
	 */
213
	public function get( string $id ) : \Aimeos\MShop\Catalog\Item\Iface
214
	{
215
		return $this->manager->get( $id, $this->domains, null );
216
	}
217
218
219
	/**
220
	 * Returns the list of categories up to the root node including the node given by its ID
221
	 *
222
	 * @param string $id Current category ID
223
	 * @return \Aimeos\MShop\Catalog\Item\Iface[] Associative list of categories
224
	 * @since 2017.03
225
	 */
226
	public function getPath( string $id )
227
	{
228
		$list = $this->manager->getPath( $id, $this->domains );
229
230
		if( $list->isAvailable()->search( false ) ) {
231
			throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Category is not available' ), 404 );
232
		}
233
234
		if( $this->root )
235
		{
236
			foreach( $list as $key => $item )
237
			{
238
				if( $key == $this->root ) {
239
					break;
240
				}
241
				unset( $list[$key] );
242
			}
243
		}
244
245
		return $list;
246
	}
247
248
249
	/**
250
	 * Returns the categories filtered by the previously assigned conditions
251
	 *
252
	 * @param int $level Tree level constant, e.g. ONE, LIST or TREE
253
	 * @return \Aimeos\MShop\Catalog\Item\Iface Category tree
254
	 * @since 2019.04
255
	 */
256
	public function getTree( int $level = Iface::TREE ) : \Aimeos\MShop\Catalog\Item\Iface
257
	{
258
		$this->addExpression( $this->filter->getConditions() );
259
		$this->filter->add( $this->filter->and( $this->getConditions() ) );
260
261
		return $this->manager->getTree( $this->root, $this->domains, $level, $this->filter );
262
	}
263
264
265
	/**
266
	 * Adds a filter to return only items containing a reference to the given ID
267
	 *
268
	 * @param string $domain Domain name of the referenced item, e.g. "media"
269
	 * @param string|null $type Type code of the reference, e.g. "default" or null for all types
270
	 * @param string|null $refId ID of the referenced item of the given domain or null for all references
271
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
272
	 * @since 2019.10
273
	 */
274
	public function has( string $domain, ?string $type = null, ?string $refId = null ) : Iface
275
	{
276
		$params = [$domain];
277
		!$type ?: $params[] = $type;
278
		!$refId ?: $params[] = $refId;
279
280
		$func = $this->filter->make( 'catalog:has', $params );
281
		$this->addExpression( $this->filter->compare( '!=', $func, null ) );
282
		return $this;
283
	}
284
285
286
	/**
287
	 * Parses the given array and adds the conditions to the list of conditions
288
	 *
289
	 * @param array $conditions List of conditions, e.g. ['>' => ['catalog.status' => 0]]
290
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
291
	 * @since 2019.04
292
	 */
293
	public function parse( array $conditions ) : Iface
294
	{
295
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
296
			$this->addExpression( $cond );
297
		}
298
299
		return $this;
300
	}
301
302
303
	/**
304
	 * Returns the category for the given category URL name
305
	 *
306
	 * @param string $name category URL name
307
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item including the referenced domains items
308
	 * @since 2023.10
309
	 */
310
	public function resolve( string $name ) : \Aimeos\MShop\Catalog\Item\Iface
311
	{
312
		$search = $this->manager->filter( null )->add( 'catalog.url', '==', $name )->slice( 0, 1 );
313
314
		if( ( $item = $this->manager->search( $search, $this->domains )->first() ) === null )
315
		{
316
			$msg = $this->context()->translate( 'controller/frontend', 'Unable to find category "%1$s"' );
317
			throw new \Aimeos\Controller\Frontend\Catalog\Exception( sprintf( $msg, $name ), 404 );
318
		}
319
320
		return $item;
321
	}
322
323
324
	/**
325
	 * Sets the catalog ID of node that is used as root node
326
	 *
327
	 * @param string|null $id Catalog ID
328
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
329
	 * @since 2019.04
330
	 */
331
	public function root( ?string $id = null ) : Iface
332
	{
333
		$this->root = ( $id ? $id : null );
334
		return $this;
335
	}
336
337
338
	/**
339
	 * Returns the categories filtered by the previously assigned conditions
340
	 *
341
	 * @param int &$total Parameter where the total number of found categories will be stored in
342
	 * @return \Aimeos\Map Ordered list of catalog items implementing \Aimeos\MShop\Catalog\Item\Iface
343
	 * @since 2019.10
344
	 */
345
	public function search( ?int &$total = null ) : \Aimeos\Map
346
	{
347
		$filter = clone $this->filter;
348
349
		$this->addExpression( $filter->getConditions() );
350
351
		$filter->add( $filter->and( $this->getConditions() ) );
352
		$filter->setSortations( $this->getSortations() );
353
354
		return $this->manager->search( $filter, $this->domains, $total );
355
	}
356
357
358
	/**
359
	 * Sets the start value and the number of returned products for slicing the list of found products
360
	 *
361
	 * @param int $start Start value of the first product in the list
362
	 * @param int $limit Number of returned products
363
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
364
	 * @since 2019.10
365
	 */
366
	public function slice( int $start, int $limit ) : Iface
367
	{
368
		$maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 );
369
		$this->filter->slice( $start, min( $limit, $maxsize ) );
370
		return $this;
371
	}
372
373
374
	/**
375
	 * Sets the sorting of the result list
376
	 *
377
	 * @param string|null $key Search key for sorting of the result list and null for no sorting
378
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
379
	 * @since 2019.10
380
	 */
381
	public function sort( ?string $key = null ) : Iface
382
	{
383
		$list = $this->splitKeys( $key );
384
385
		foreach( $list as $sortkey )
386
		{
387
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
388
			$this->addExpression( $this->filter->sort( $direction, ltrim( $sortkey, '+-' ) ) );
389
		}
390
391
		return $this;
392
	}
393
394
395
	/**
396
	 * Sets the referenced domains that will be fetched too when retrieving items
397
	 *
398
	 * @param array $domains Domain names of the referenced items that should be fetched too
399
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
400
	 * @since 2019.04
401
	 */
402
	public function uses( array $domains ) : Iface
403
	{
404
		$this->domains = $domains;
405
		return $this;
406
	}
407
408
409
	/**
410
	 * Limits categories returned to only visible ones depending on the given category IDs
411
	 *
412
	 * @param array $catIds List of category IDs
413
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
414
	 */
415
	public function visible( array $catIds ) : Iface
416
	{
417
		$expr = [];
418
		$config = $this->context()->config();
419
420
		if( !empty( $catIds ) )
421
		{
422
			$expr[] = $this->filter->compare( '==', 'catalog.parentid', $catIds );
423
			$expr[] = $this->filter->compare( '==', 'catalog.id', $catIds );
424
		}
425
426
		/** controller/frontend/catalog/levels-always
427
		 * The number of levels in the category tree that should be always displayed
428
		 *
429
		 * Usually, only the root node and the first level of the category
430
		 * tree is shown in the frontend. Only if the user clicks on a
431
		 * node in the first level, the page reloads and the sub-nodes of
432
		 * the chosen category are rendered as well.
433
		 *
434
		 * Using this configuration option you can enforce the given number
435
		 * of levels to be always displayed. The root node uses level 0, the
436
		 * categories below level 1 and so on.
437
		 *
438
		 * In most cases you can set this value via the administration interface
439
		 * of the shop application. In that case you often can configure the
440
		 * levels individually for each catalog filter.
441
		 *
442
		 * Note: This setting was available between 2014.03 and 2019.04 as
443
		 * client/html/catalog/filter/tree/levels-always
444
		 *
445
		 * @param integer Number of tree levels
446
		 * @since 2019.04
447
		 * @category User
448
		 * @category Developer
449
		 * @see controller/frontend/catalog/levels-only
450
		 */
451
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-always' ) ) != null ) {
452
			$expr[] = $this->filter->compare( '<=', 'catalog.level', $levels );
453
		}
454
455
		/** controller/frontend/catalog/levels-only
456
		 * No more than this number of levels in the category tree should be displayed
457
		 *
458
		 * If the user clicks on a category node, the page reloads and the
459
		 * sub-nodes of the chosen category are rendered as well.
460
		 * Using this configuration option you can enforce that no more than
461
		 * the given number of levels will be displayed at all. The root
462
		 * node uses level 0, the categories below level 1 and so on.
463
		 *
464
		 * In most cases you can set this value via the administration interface
465
		 * of the shop application. In that case you often can configure the
466
		 * levels individually for each catalog filter.
467
		 *
468
		 * Note: This setting was available between 2014.03 and 2019.04 as
469
		 * client/html/catalog/filter/tree/levels-only
470
		 *
471
		 * @param integer Number of tree levels
472
		 * @since 2014.03
473
		 * @category User
474
		 * @category Developer
475
		 * @see controller/frontend/catalog/levels-always
476
		 */
477
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-only' ) ) != null ) {
478
			$this->addExpression( $this->filter->compare( '<=', 'catalog.level', $levels ) );
479
		}
480
481
		$this->addExpression( $this->filter->or( $expr ) );
482
		return $this;
483
	}
484
485
486
	/**
487
	 * Returns the manager used by the controller
488
	 *
489
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
490
	 */
491
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
492
	{
493
		return $this->manager;
494
	}
495
}
496