Completed
Push — master ( 014a02...3c05c5 )
by Aimeos
03:14
created

Standard::sort()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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