Passed
Push — master ( 65b0a7...5308fc )
by Aimeos
02:36
created

Standard::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
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-2018
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
	 * Returns the category for the given catalog ID
86
	 *
87
	 * @param string $id Unique catalog ID
88
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item including the referenced domains items
89
	 * @since 2019.04
90
	 */
91
	public function get( string $id ) : \Aimeos\MShop\Catalog\Item\Iface
92
	{
93
		return $this->manager->getItem( $id, $this->domains, true );
94
	}
95
96
97
	/**
98
	 * Returns the list of categories up to the root node including the node given by its ID
99
	 *
100
	 * @param string $id Current category ID
101
	 * @return \Aimeos\MShop\Catalog\Item\Iface[] Associative list of categories
102
	 * @since 2017.03
103
	 */
104
	public function getPath( string $id )
105
	{
106
		$list = $this->manager->getPath( $id, $this->domains );
107
108
		if( $this->root )
109
		{
110
			foreach( $list as $key => $item )
111
			{
112
				if( $key == $this->root ) {
113
					break;
114
				}
115
				unset( $list[$key] );
116
			}
117
		}
118
119
		return $list;
120
	}
121
122
123
	/**
124
	 * Returns the categories filtered by the previously assigned conditions
125
	 *
126
	 * @param integer $level Constant from \Aimeos\MW\Tree\Manager\Base, e.g. LEVEL_ONE, LEVEL_LIST or LEVEL_TREE
127
	 * @return \Aimeos\MShop\Catalog\Item\Iface Category tree
128
	 * @since 2019.04
129
	 */
130
	public function getTree( int $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE ) : \Aimeos\MShop\Catalog\Item\Iface
131
	{
132
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
133
		return $this->manager->getTree( $this->root, $this->domains, $level, $this->filter );
134
	}
135
136
137
	/**
138
	 * Parses the given array and adds the conditions to the list of conditions
139
	 *
140
	 * @param array $conditions List of conditions, e.g. ['>' => ['catalog.status' => 0]]
141
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
142
	 * @since 2019.04
143
	 */
144
	public function parse( array $conditions ) : Iface
145
	{
146
		if( ( $cond = $this->filter->toConditions( $conditions ) ) !== null ) {
147
			$this->conditions[] = $cond;
148
		}
149
150
		return $this;
151
	}
152
153
154
	/**
155
	 * Sets the catalog ID of node that is used as root node
156
	 *
157
	 * @param string|null $id Catalog ID
158
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
159
	 * @since 2019.04
160
	 */
161
	public function root( string $id = null ) : Iface
162
	{
163
		$this->root = ( $id ? $id : null );
164
		return $this;
165
	}
166
167
168
	/**
169
	 * Returns the categories filtered by the previously assigned conditions
170
	 *
171
	 * @param int &$total Parameter where the total number of found categories will be stored in
172
	 * @return \Aimeos\MShop\Catalog\Item\Iface[] Ordered list of catalog items
173
	 * @since 2019.10
174
	 */
175
	public function search( int &$total = null )
176
	{
177
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
178
		return $this->manager->searchItems( $this->filter, $this->domains, $total );
179
	}
180
181
182
	/**
183
	 * Sets the referenced domains that will be fetched too when retrieving items
184
	 *
185
	 * @param array $domains Domain names of the referenced items that should be fetched too
186
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
187
	 * @since 2019.04
188
	 */
189
	public function uses( array $domains ) : Iface
190
	{
191
		$this->domains = $domains;
192
		return $this;
193
	}
194
195
196
	/**
197
	 * Limits categories returned to only visible ones depending on the given category IDs
198
	 *
199
	 * @param array $catIds List of category IDs
200
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
201
	 */
202
	public function visible( array $catIds ) : Iface
203
	{
204
		if( empty( $catIds ) ) {
205
			return $this;
206
		}
207
208
		$config = $this->getContext()->getConfig();
209
210
		$expr = [
211
			$this->filter->compare( '==', 'catalog.parentid', $catIds ),
212
			$this->filter->compare( '==', 'catalog.id', $catIds )
213
		];
214
215
		/** controller/frontend/catalog/levels-always
216
		 * The number of levels in the category tree that should be always displayed
217
		 *
218
		 * Usually, only the root node and the first level of the category
219
		 * tree is shown in the frontend. Only if the user clicks on a
220
		 * node in the first level, the page reloads and the sub-nodes of
221
		 * the chosen category are rendered as well.
222
		 *
223
		 * Using this configuration option you can enforce the given number
224
		 * of levels to be always displayed. The root node uses level 0, the
225
		 * categories below level 1 and so on.
226
		 *
227
		 * In most cases you can set this value via the administration interface
228
		 * of the shop application. In that case you often can configure the
229
		 * levels individually for each catalog filter.
230
		 *
231
		 * Note: This setting was available between 2014.03 and 2019.04 as
232
		 * client/html/catalog/filter/tree/levels-always
233
		 *
234
		 * @param integer Number of tree levels
235
		 * @since 2019.04
236
		 * @category User
237
		 * @category Developer
238
		 * @see controller/frontend/catalog/levels-only
239
		 */
240
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-always' ) ) != null ) {
241
			$expr[] = $this->filter->compare( '<=', 'catalog.level', $levels );
242
		}
243
244
		/** controller/frontend/catalog/levels-only
245
		 * No more than this number of levels in the category tree should be displayed
246
		 *
247
		 * If the user clicks on a category node, the page reloads and the
248
		 * sub-nodes of the chosen category are rendered as well.
249
		 * Using this configuration option you can enforce that no more than
250
		 * the given number of levels will be displayed at all. The root
251
		 * node uses level 0, the categories below level 1 and so on.
252
		 *
253
		 * In most cases you can set this value via the administration interface
254
		 * of the shop application. In that case you often can configure the
255
		 * levels individually for each catalog filter.
256
		 *
257
		 * Note: This setting was available between 2014.03 and 2019.04 as
258
		 * client/html/catalog/filter/tree/levels-only
259
		 *
260
		 * @param integer Number of tree levels
261
		 * @since 2014.03
262
		 * @category User
263
		 * @category Developer
264
		 * @see controller/frontend/catalog/levels-always
265
		 */
266
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-only' ) ) != null ) {
267
			$this->conditions[] = $this->filter->compare( '<=', 'catalog.level', $levels );
268
		}
269
270
		$this->conditions[] = $this->filter->combine( '||', $expr );
271
		return $this;
272
	}
273
}
274