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

Standard::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
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-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, true );
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, true );
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
	 * Parses the given array and adds the conditions to the list of conditions
155
	 *
156
	 * @param array $conditions List of conditions, e.g. ['>' => ['catalog.status' => 0]]
157
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
158
	 * @since 2019.04
159
	 */
160
	public function parse( array $conditions ) : Iface
161
	{
162
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
163
			$this->addExpression( $cond );
164
		}
165
166
		return $this;
167
	}
168
169
170
	/**
171
	 * Sets the catalog ID of node that is used as root node
172
	 *
173
	 * @param string|null $id Catalog ID
174
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
175
	 * @since 2019.04
176
	 */
177
	public function root( string $id = null ) : Iface
178
	{
179
		$this->root = ( $id ? $id : null );
180
		return $this;
181
	}
182
183
184
	/**
185
	 * Returns the categories filtered by the previously assigned conditions
186
	 *
187
	 * @param int &$total Parameter where the total number of found categories will be stored in
188
	 * @return \Aimeos\Map Ordered list of catalog items implementing \Aimeos\MShop\Catalog\Item\Iface
189
	 * @since 2019.10
190
	 */
191
	public function search( int &$total = null ) : \Aimeos\Map
192
	{
193
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) );
194
		$this->filter->setSortations( $this->getSortations() );
195
196
		return $this->manager->search( $this->filter, $this->domains, $total );
197
	}
198
199
200
	/**
201
	 * Sets the start value and the number of returned products for slicing the list of found products
202
	 *
203
	 * @param int $start Start value of the first product in the list
204
	 * @param int $limit Number of returned products
205
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
206
	 * @since 2019.10
207
	 */
208
	public function slice( int $start, int $limit ) : Iface
209
	{
210
		$maxsize = $this->getContext()->config()->get( 'controller/frontend/common/max-size', 250 );
211
		$this->filter->slice( $start, min( $limit, $maxsize ) );
212
		return $this;
213
	}
214
215
216
	/**
217
	 * Sets the sorting of the result list
218
	 *
219
	 * @param string|null $key Search key for sorting of the result list and null for no sorting
220
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
221
	 * @since 2019.10
222
	 */
223
	public function sort( ?string $key = null ) : Iface
224
	{
225
		$list = $this->splitKeys( $key );
226
227
		foreach( $list as $sortkey )
228
		{
229
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
230
			$this->addExpression( $this->filter->sort( $direction, ltrim( $sortkey, '+-' ) ) );
231
		}
232
233
		return $this;
234
	}
235
236
237
	/**
238
	 * Sets the referenced domains that will be fetched too when retrieving items
239
	 *
240
	 * @param array $domains Domain names of the referenced items that should be fetched too
241
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
242
	 * @since 2019.04
243
	 */
244
	public function uses( array $domains ) : Iface
245
	{
246
		$this->domains = $domains;
247
		return $this;
248
	}
249
250
251
	/**
252
	 * Limits categories returned to only visible ones depending on the given category IDs
253
	 *
254
	 * @param array $catIds List of category IDs
255
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
256
	 */
257
	public function visible( array $catIds ) : Iface
258
	{
259
		$expr = [];
260
		$config = $this->getContext()->getConfig();
261
262
		if( !empty( $catIds ) )
263
		{
264
			$expr[] = $this->filter->compare( '==', 'catalog.parentid', $catIds );
265
			$expr[] = $this->filter->compare( '==', 'catalog.id', $catIds );
266
		}
267
268
		/** controller/frontend/catalog/levels-always
269
		 * The number of levels in the category tree that should be always displayed
270
		 *
271
		 * Usually, only the root node and the first level of the category
272
		 * tree is shown in the frontend. Only if the user clicks on a
273
		 * node in the first level, the page reloads and the sub-nodes of
274
		 * the chosen category are rendered as well.
275
		 *
276
		 * Using this configuration option you can enforce the given number
277
		 * of levels to be always displayed. The root node uses level 0, the
278
		 * categories below level 1 and so on.
279
		 *
280
		 * In most cases you can set this value via the administration interface
281
		 * of the shop application. In that case you often can configure the
282
		 * levels individually for each catalog filter.
283
		 *
284
		 * Note: This setting was available between 2014.03 and 2019.04 as
285
		 * client/html/catalog/filter/tree/levels-always
286
		 *
287
		 * @param integer Number of tree levels
288
		 * @since 2019.04
289
		 * @category User
290
		 * @category Developer
291
		 * @see controller/frontend/catalog/levels-only
292
		 */
293
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-always' ) ) != null ) {
294
			$expr[] = $this->filter->compare( '<=', 'catalog.level', $levels );
295
		}
296
297
		/** controller/frontend/catalog/levels-only
298
		 * No more than this number of levels in the category tree should be displayed
299
		 *
300
		 * If the user clicks on a category node, the page reloads and the
301
		 * sub-nodes of the chosen category are rendered as well.
302
		 * Using this configuration option you can enforce that no more than
303
		 * the given number of levels will be displayed at all. The root
304
		 * node uses level 0, the categories below level 1 and so on.
305
		 *
306
		 * In most cases you can set this value via the administration interface
307
		 * of the shop application. In that case you often can configure the
308
		 * levels individually for each catalog filter.
309
		 *
310
		 * Note: This setting was available between 2014.03 and 2019.04 as
311
		 * client/html/catalog/filter/tree/levels-only
312
		 *
313
		 * @param integer Number of tree levels
314
		 * @since 2014.03
315
		 * @category User
316
		 * @category Developer
317
		 * @see controller/frontend/catalog/levels-always
318
		 */
319
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-only' ) ) != null ) {
320
			$this->addExpression( $this->filter->compare( '<=', 'catalog.level', $levels ) );
321
		}
322
323
		$this->addExpression( $this->filter->or( $expr ) );
324
		return $this;
325
	}
326
327
328
	/**
329
	 * Returns the manager used by the controller
330
	 *
331
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
332
	 */
333
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
334
	{
335
		return $this->manager;
336
	}
337
}
338