Completed
Push — master ( 94eb6c...a336e2 )
by Aimeos
02:13
created

Standard::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
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
	 * 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
	 * @param string Search function string that can be used in compare()
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Frontend\Catalog\Search was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 integer $level Constant from \Aimeos\MW\Tree\Manager\Base, e.g. LEVEL_ONE, LEVEL_LIST or LEVEL_TREE
140
	 * @return \Aimeos\MShop\Catalog\Item\Iface Category tree
141
	 * @since 2019.04
142
	 */
143
	public function getTree( int $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_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\MShop\Catalog\Item\Iface[] Ordered list of catalog items
186
	 * @since 2019.10
187
	 */
188
	public function search( int &$total = null )
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 referenced domains that will be fetched too when retrieving items
197
	 *
198
	 * @param array $domains Domain names of the referenced items that should be fetched too
199
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
200
	 * @since 2019.04
201
	 */
202
	public function uses( array $domains ) : Iface
203
	{
204
		$this->domains = $domains;
205
		return $this;
206
	}
207
208
209
	/**
210
	 * Limits categories returned to only visible ones depending on the given category IDs
211
	 *
212
	 * @param array $catIds List of category IDs
213
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
214
	 */
215
	public function visible( array $catIds ) : Iface
216
	{
217
		if( empty( $catIds ) ) {
218
			return $this;
219
		}
220
221
		$config = $this->getContext()->getConfig();
222
223
		$expr = [
224
			$this->filter->compare( '==', 'catalog.parentid', $catIds ),
225
			$this->filter->compare( '==', 'catalog.id', $catIds )
226
		];
227
228
		/** controller/frontend/catalog/levels-always
229
		 * The number of levels in the category tree that should be always displayed
230
		 *
231
		 * Usually, only the root node and the first level of the category
232
		 * tree is shown in the frontend. Only if the user clicks on a
233
		 * node in the first level, the page reloads and the sub-nodes of
234
		 * the chosen category are rendered as well.
235
		 *
236
		 * Using this configuration option you can enforce the given number
237
		 * of levels to be always displayed. The root node uses level 0, the
238
		 * categories below level 1 and so on.
239
		 *
240
		 * In most cases you can set this value via the administration interface
241
		 * of the shop application. In that case you often can configure the
242
		 * levels individually for each catalog filter.
243
		 *
244
		 * Note: This setting was available between 2014.03 and 2019.04 as
245
		 * client/html/catalog/filter/tree/levels-always
246
		 *
247
		 * @param integer Number of tree levels
248
		 * @since 2019.04
249
		 * @category User
250
		 * @category Developer
251
		 * @see controller/frontend/catalog/levels-only
252
		 */
253
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-always' ) ) != null ) {
254
			$expr[] = $this->filter->compare( '<=', 'catalog.level', $levels );
255
		}
256
257
		/** controller/frontend/catalog/levels-only
258
		 * No more than this number of levels in the category tree should be displayed
259
		 *
260
		 * If the user clicks on a category node, the page reloads and the
261
		 * sub-nodes of the chosen category are rendered as well.
262
		 * Using this configuration option you can enforce that no more than
263
		 * the given number of levels will be displayed at all. The root
264
		 * node uses level 0, the categories below level 1 and so on.
265
		 *
266
		 * In most cases you can set this value via the administration interface
267
		 * of the shop application. In that case you often can configure the
268
		 * levels individually for each catalog filter.
269
		 *
270
		 * Note: This setting was available between 2014.03 and 2019.04 as
271
		 * client/html/catalog/filter/tree/levels-only
272
		 *
273
		 * @param integer Number of tree levels
274
		 * @since 2014.03
275
		 * @category User
276
		 * @category Developer
277
		 * @see controller/frontend/catalog/levels-always
278
		 */
279
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-only' ) ) != null ) {
280
			$this->conditions[] = $this->filter->compare( '<=', 'catalog.level', $levels );
281
		}
282
283
		$this->conditions[] = $this->filter->combine( '||', $expr );
284
		return $this;
285
	}
286
}
287