Passed
Push — master ( c9dfc7...a4ce90 )
by Aimeos
01:38
created

Standard::uses()   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
dl 0
loc 4
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-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( $operator, $key, $value )
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( $code )
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( $id )
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 integer $id Current category ID
101
	 * @return \Aimeos\MShop\Catalog\Item\Iface[] Associative list of categories
102
	 * @since 2017.03
103
	 */
104
	public function getPath( $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( $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE )
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 )
145
	{
146
		$this->conditions[] = $this->filter->toConditions( $conditions );
147
		return $this;
148
	}
149
150
151
	/**
152
	 * Sets the catalog ID of node that is used as root node
153
	 *
154
	 * @param string|null $id Catalog ID
155
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
156
	 * @since 2019.04
157
	 */
158
	public function root( $id )
159
	{
160
		$this->root = ( $id ? $id : null );
161
		return $this;
162
	}
163
164
165
	/**
166
	 * Sets the referenced domains that will be fetched too when retrieving items
167
	 *
168
	 * @param array $domains Domain names of the referenced items that should be fetched too
169
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
170
	 * @since 2019.04
171
	 */
172
	public function uses( array $domains )
173
	{
174
		$this->domains = $domains;
175
		return $this;
176
	}
177
178
179
	/**
180
	 * Limits categories returned to only visible ones depending on the given category IDs
181
	 *
182
	 * @param array $catIds List of category IDs
183
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
184
	 */
185
	public function visible( array $catIds )
186
	{
187
		if( empty( $catIds ) ) {
188
			return $this;
189
		}
190
191
		$config = $this->getContext()->getConfig();
192
193
		$expr = [
194
			$this->filter->compare( '==', 'catalog.parentid', $catIds ),
195
			$this->filter->compare( '==', 'catalog.id', $catIds )
196
		];
197
198
		/** controller/frontend/catalog/levels-always
199
		 * The number of levels in the category tree that should be always displayed
200
		 *
201
		 * Usually, only the root node and the first level of the category
202
		 * tree is shown in the frontend. Only if the user clicks on a
203
		 * node in the first level, the page reloads and the sub-nodes of
204
		 * the chosen category are rendered as well.
205
		 *
206
		 * Using this configuration option you can enforce the given number
207
		 * of levels to be always displayed. The root node uses level 0, the
208
		 * categories below level 1 and so on.
209
		 *
210
		 * In most cases you can set this value via the administration interface
211
		 * of the shop application. In that case you often can configure the
212
		 * levels individually for each catalog filter.
213
		 *
214
		 * Note: This setting was available between 2014.03 and 2019.04 as
215
		 * client/html/catalog/filter/tree/levels-always
216
		 *
217
		 * @param integer Number of tree levels
218
		 * @since 2019.04
219
		 * @category User
220
		 * @category Developer
221
		 * @see controller/frontend/catalog/levels-only
222
		*/
223
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-always' ) ) != null ) {
224
			$expr[] = $this->filter->compare( '<=', 'catalog.level', $levels );
225
		}
226
227
		/** controller/frontend/catalog/levels-only
228
		 * No more than this number of levels in the category tree should be displayed
229
		 *
230
		 * If the user clicks on a category node, the page reloads and the
231
		 * sub-nodes of the chosen category are rendered as well.
232
		 * Using this configuration option you can enforce that no more than
233
		 * the given number of levels will be displayed at all. The root
234
		 * node uses level 0, the categories below level 1 and so on.
235
		 *
236
		 * In most cases you can set this value via the administration interface
237
		 * of the shop application. In that case you often can configure the
238
		 * levels individually for each catalog filter.
239
		 *
240
		 * Note: This setting was available between 2014.03 and 2019.04 as
241
		 * client/html/catalog/filter/tree/levels-only
242
		 *
243
		 * @param integer Number of tree levels
244
		 * @since 2014.03
245
		 * @category User
246
		 * @category Developer
247
		 * @see controller/frontend/catalog/levels-always
248
		 */
249
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-only' ) ) != null ) {
250
			$this->conditions[] = $this->filter->compare( '<=', 'catalog.level', $levels );
251
		}
252
253
		$this->conditions[] = $this->filter->combine( '||', $expr );
254
		return $this;
255
	}
256
}
257