Passed
Push — master ( 91b6db...c8efcd )
by Aimeos
27:02 queued 24:35
created

Standard::function()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2021
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
	private $domains = [];
25
	private $filter;
26
	private $manager;
27
	private $root;
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, 'catalog' );
40
		$this->filter = $this->manager->filter( true );
41
		$this->addExpression( $this->filter->getConditions() );
42
	}
43
44
45
	/**
46
	 * Clones objects in controller and resets values
47
	 */
48
	public function __clone()
49
	{
50
		$this->filter = clone $this->filter;
51
	}
52
53
54
	/**
55
	 * Adds generic condition for filtering attributes
56
	 *
57
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
58
	 * @param string $key Search key defined by the catalog manager, e.g. "catalog.status"
59
	 * @param array|string $value Value or list of values to compare to
60
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
61
	 * @since 2019.04
62
	 */
63
	public function compare( string $operator, string $key, $value ) : Iface
64
	{
65
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
66
		return $this;
67
	}
68
69
70
	/**
71
	 * Returns the category for the given catalog code
72
	 *
73
	 * @param string $code Unique catalog code
74
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item including the referenced domains items
75
	 * @since 2019.04
76
	 */
77
	public function find( string $code ) : \Aimeos\MShop\Catalog\Item\Iface
78
	{
79
		return $this->manager->find( $code, $this->domains, null, null, null );
80
	}
81
82
83
	/**
84
	 * Creates a search function string for the given name and parameters
85
	 *
86
	 * @param string $name Name of the search function without parenthesis, e.g. "catalog:has"
87
	 * @param array $params List of parameters for the search function with numeric keys starting at 0
88
	 * @return string Search function string that can be used in compare()
89
	 */
90
	public function function( string $name, array $params ) : string
91
	{
92
		return $this->filter->make( $name, $params );
93
	}
94
95
96
	/**
97
	 * Returns the category for the given catalog ID
98
	 *
99
	 * @param string $id Unique catalog ID
100
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item including the referenced domains items
101
	 * @since 2019.04
102
	 */
103
	public function get( string $id ) : \Aimeos\MShop\Catalog\Item\Iface
104
	{
105
		return $this->manager->get( $id, $this->domains, null );
106
	}
107
108
109
	/**
110
	 * Returns the list of categories up to the root node including the node given by its ID
111
	 *
112
	 * @param string $id Current category ID
113
	 * @return \Aimeos\MShop\Catalog\Item\Iface[] Associative list of categories
114
	 * @since 2017.03
115
	 */
116
	public function getPath( string $id )
117
	{
118
		$list = $this->manager->getPath( $id, $this->domains );
119
120
		if( $list->isAvailable()->search( false ) ) {
121
			throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Category is not available' ) );
122
		}
123
124
		if( $this->root )
125
		{
126
			foreach( $list as $key => $item )
127
			{
128
				if( $key == $this->root ) {
129
					break;
130
				}
131
				unset( $list[$key] );
132
			}
133
		}
134
135
		return $list;
136
	}
137
138
139
	/**
140
	 * Returns the categories filtered by the previously assigned conditions
141
	 *
142
	 * @param int $level Tree level constant, e.g. ONE, LIST or TREE
143
	 * @return \Aimeos\MShop\Catalog\Item\Iface Category tree
144
	 * @since 2019.04
145
	 */
146
	public function getTree( int $level = Iface::TREE ) : \Aimeos\MShop\Catalog\Item\Iface
147
	{
148
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) );
149
		return $this->manager->getTree( $this->root, $this->domains, $level, $this->filter );
150
	}
151
152
153
	/**
154
	 * Adds a filter to return only items containing a reference to the given ID
155
	 *
156
	 * @param string $domain Domain name of the referenced item, e.g. "media"
157
	 * @param string|null $type Type code of the reference, e.g. "default" or null for all types
158
	 * @param string|null $refId ID of the referenced item of the given domain or null for all references
159
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
160
	 * @since 2019.10
161
	 */
162
	public function has( string $domain, string $type = null, string $refId = null ) : Iface
163
	{
164
		$params = [$domain];
165
		!$type ?: $params[] = $type;
166
		!$refId ?: $params[] = $refId;
167
168
		$func = $this->filter->make( 'catalog:has', $params );
169
		$this->addExpression( $this->filter->compare( '!=', $func, null ) );
170
		return $this;
171
	}
172
173
174
	/**
175
	 * Parses the given array and adds the conditions to the list of conditions
176
	 *
177
	 * @param array $conditions List of conditions, e.g. ['>' => ['catalog.status' => 0]]
178
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
179
	 * @since 2019.04
180
	 */
181
	public function parse( array $conditions ) : Iface
182
	{
183
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
184
			$this->addExpression( $cond );
185
		}
186
187
		return $this;
188
	}
189
190
191
	/**
192
	 * Sets the catalog ID of node that is used as root node
193
	 *
194
	 * @param string|null $id Catalog ID
195
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
196
	 * @since 2019.04
197
	 */
198
	public function root( string $id = null ) : Iface
199
	{
200
		$this->root = ( $id ? $id : null );
201
		return $this;
202
	}
203
204
205
	/**
206
	 * Returns the categories filtered by the previously assigned conditions
207
	 *
208
	 * @param int &$total Parameter where the total number of found categories will be stored in
209
	 * @return \Aimeos\Map Ordered list of catalog items implementing \Aimeos\MShop\Catalog\Item\Iface
210
	 * @since 2019.10
211
	 */
212
	public function search( int &$total = null ) : \Aimeos\Map
213
	{
214
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) );
215
		$this->filter->setSortations( $this->getSortations() );
216
217
		return $this->manager->search( $this->filter, $this->domains, $total );
218
	}
219
220
221
	/**
222
	 * Sets the start value and the number of returned products for slicing the list of found products
223
	 *
224
	 * @param int $start Start value of the first product in the list
225
	 * @param int $limit Number of returned products
226
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
227
	 * @since 2019.10
228
	 */
229
	public function slice( int $start, int $limit ) : Iface
230
	{
231
		$maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 );
232
		$this->filter->slice( $start, min( $limit, $maxsize ) );
233
		return $this;
234
	}
235
236
237
	/**
238
	 * Sets the sorting of the result list
239
	 *
240
	 * @param string|null $key Search key for sorting of the result list and null for no sorting
241
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
242
	 * @since 2019.10
243
	 */
244
	public function sort( ?string $key = null ) : Iface
245
	{
246
		$list = $this->splitKeys( $key );
247
248
		foreach( $list as $sortkey )
249
		{
250
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
251
			$this->addExpression( $this->filter->sort( $direction, ltrim( $sortkey, '+-' ) ) );
252
		}
253
254
		return $this;
255
	}
256
257
258
	/**
259
	 * Sets the referenced domains that will be fetched too when retrieving items
260
	 *
261
	 * @param array $domains Domain names of the referenced items that should be fetched too
262
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
263
	 * @since 2019.04
264
	 */
265
	public function uses( array $domains ) : Iface
266
	{
267
		$this->domains = $domains;
268
		return $this;
269
	}
270
271
272
	/**
273
	 * Limits categories returned to only visible ones depending on the given category IDs
274
	 *
275
	 * @param array $catIds List of category IDs
276
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
277
	 */
278
	public function visible( array $catIds ) : Iface
279
	{
280
		$expr = [];
281
		$config = $this->context()->config();
282
283
		if( !empty( $catIds ) )
284
		{
285
			$expr[] = $this->filter->compare( '==', 'catalog.parentid', $catIds );
286
			$expr[] = $this->filter->compare( '==', 'catalog.id', $catIds );
287
		}
288
289
		/** controller/frontend/catalog/levels-always
290
		 * The number of levels in the category tree that should be always displayed
291
		 *
292
		 * Usually, only the root node and the first level of the category
293
		 * tree is shown in the frontend. Only if the user clicks on a
294
		 * node in the first level, the page reloads and the sub-nodes of
295
		 * the chosen category are rendered as well.
296
		 *
297
		 * Using this configuration option you can enforce the given number
298
		 * of levels to be always displayed. The root node uses level 0, the
299
		 * categories below level 1 and so on.
300
		 *
301
		 * In most cases you can set this value via the administration interface
302
		 * of the shop application. In that case you often can configure the
303
		 * levels individually for each catalog filter.
304
		 *
305
		 * Note: This setting was available between 2014.03 and 2019.04 as
306
		 * client/html/catalog/filter/tree/levels-always
307
		 *
308
		 * @param integer Number of tree levels
309
		 * @since 2019.04
310
		 * @category User
311
		 * @category Developer
312
		 * @see controller/frontend/catalog/levels-only
313
		 */
314
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-always' ) ) != null ) {
315
			$expr[] = $this->filter->compare( '<=', 'catalog.level', $levels );
316
		}
317
318
		/** controller/frontend/catalog/levels-only
319
		 * No more than this number of levels in the category tree should be displayed
320
		 *
321
		 * If the user clicks on a category node, the page reloads and the
322
		 * sub-nodes of the chosen category are rendered as well.
323
		 * Using this configuration option you can enforce that no more than
324
		 * the given number of levels will be displayed at all. The root
325
		 * node uses level 0, the categories below level 1 and so on.
326
		 *
327
		 * In most cases you can set this value via the administration interface
328
		 * of the shop application. In that case you often can configure the
329
		 * levels individually for each catalog filter.
330
		 *
331
		 * Note: This setting was available between 2014.03 and 2019.04 as
332
		 * client/html/catalog/filter/tree/levels-only
333
		 *
334
		 * @param integer Number of tree levels
335
		 * @since 2014.03
336
		 * @category User
337
		 * @category Developer
338
		 * @see controller/frontend/catalog/levels-always
339
		 */
340
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-only' ) ) != null ) {
341
			$this->addExpression( $this->filter->compare( '<=', 'catalog.level', $levels ) );
342
		}
343
344
		$this->addExpression( $this->filter->or( $expr ) );
345
		return $this;
346
	}
347
348
349
	/**
350
	 * Returns the manager used by the controller
351
	 *
352
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
353
	 */
354
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
355
	{
356
		return $this->manager;
357
	}
358
}
359