Completed
Push — master ( 921503...9109fd )
by Aimeos
02:03
created

Standard   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 226
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __clone() 0 4 1
A compare() 0 5 1
A get() 0 4 1
A getPath() 0 17 4
A getTree() 0 5 1
A find() 0 4 1
A parse() 0 5 1
A root() 0 5 2
B visible() 0 71 4
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 $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->createSearch( true );
41
		$this->conditions[] = $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( $operator, $key, $value )
64
	{
65
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
66
		return $this;
67
	}
68
69
70
	/**
71
	 * Returns the category for the given catalog ID
72
	 *
73
	 * @param string $id Unique catalog ID
74
	 * @param string[] $domains Domain names of items that are associated with the category and should be fetched too
75
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item including the referenced domains items
76
	 * @since 2019.04
77
	 */
78
	public function get( $id, array $domains = ['media', 'text'] )
79
	{
80
		return $this->manager->getItem( $id, $domains, true );
81
	}
82
83
84
	/**
85
	 * Returns the list of categories up to the root node including the node given by its ID
86
	 *
87
	 * @param integer $id Current category ID
88
	 * @param string[] $domains Domain names of items that are associated to the categories and should be fetched too
89
	 * @return \Aimeos\MShop\Catalog\Item\Iface[] Associative list of categories
90
	 * @since 2017.03
91
	 */
92
	public function getPath( $id, array $domains = ['text', 'media'] )
93
	{
94
		$list = $this->manager->getPath( $id, $domains );
95
96
		if( $this->root )
97
		{
98
			foreach( $list as $key => $item )
99
			{
100
				if( $key == $this->root ) {
101
					break;
102
				}
103
				unset( $list[$key] );
104
			}
105
		}
106
107
		return $list;
108
	}
109
110
111
	/**
112
	 * Returns the categories filtered by the previously assigned conditions
113
	 *
114
	 * @param string[] $domains Domain names of items that are associated to the categories and should be fetched too
115
	 * @param integer $level Constant from \Aimeos\MW\Tree\Manager\Base, e.g. LEVEL_ONE, LEVEL_LIST or LEVEL_TREE
116
	 * @return \Aimeos\MShop\Catalog\Item\Iface Category tree
117
	 * @since 2019.04
118
	 */
119
	public function getTree( array $domains = ['media', 'text'], $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE )
120
	{
121
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
122
		return $this->manager->getTree( $this->root, $domains, $level, $this->filter );
123
	}
124
125
126
	/**
127
	 * Returns the category for the given catalog code
128
	 *
129
	 * @param string $code Unique catalog code
130
	 * @param string[] $domains Domain names of items that are associated to the categories and should be fetched too
131
	 * @return \Aimeos\MShop\Catalog\Item\Iface Catalog item including the referenced domains items
132
	 * @since 2019.04
133
	 */
134
	public function find( $code, array $domains = ['media', 'text'] )
135
	{
136
		return $this->manager->findItem( $code, $domains, null, null, true );
137
	}
138
139
140
	/**
141
	 * Parses the given array and adds the conditions to the list of conditions
142
	 *
143
	 * @param array $conditions List of conditions, e.g. ['>' => ['catalog.status' => 0]]
144
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
145
	 * @since 2019.04
146
	 */
147
	public function parse( array $conditions )
148
	{
149
		$this->conditions[] = $this->filter->toConditions( $conditions );
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( $id )
162
	{
163
		$this->root = ( $id ? $id : null );
164
		return $this;
165
	}
166
167
168
	/**
169
	 * Limits categories returned to only visible ones depending on the given category IDs
170
	 *
171
	 * @param array $catIds List of category IDs
172
	 * @return \Aimeos\Controller\Frontend\Catalog\Iface Catalog controller for fluent interface
173
	 */
174
	public function visible( array $catIds )
175
	{
176
		if( empty( $catIds ) ) {
177
			return $this;
178
		}
179
180
		$config = $this->getContext()->getConfig();
181
182
		$expr = [
183
			$this->filter->compare( '==', 'catalog.parentid', $catIds ),
184
			$this->filter->compare( '==', 'catalog.id', $catIds )
185
		];
186
187
		/** controller/frontend/catalog/levels-always
188
		 * The number of levels in the category tree that should be always displayed
189
		 *
190
		 * Usually, only the root node and the first level of the category
191
		 * tree is shown in the frontend. Only if the user clicks on a
192
		 * node in the first level, the page reloads and the sub-nodes of
193
		 * the chosen category are rendered as well.
194
		 *
195
		 * Using this configuration option you can enforce the given number
196
		 * of levels to be always displayed. The root node uses level 0, the
197
		 * categories below level 1 and so on.
198
		 *
199
		 * In most cases you can set this value via the administration interface
200
		 * of the shop application. In that case you often can configure the
201
		 * levels individually for each catalog filter.
202
		 *
203
		 * Note: This setting was available between 2014.03 and 2019.04 as
204
		 * client/html/catalog/filter/tree/levels-always
205
		 *
206
		 * @param integer Number of tree levels
207
		 * @since 2019.04
208
		 * @category User
209
		 * @category Developer
210
		 * @see controller/frontend/catalog/levels-only
211
		*/
212
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-always' ) ) != null ) {
213
			$expr[] = $this->filter->compare( '<=', 'catalog.level', $levels );
214
		}
215
216
		/** controller/frontend/catalog/levels-only
217
		 * No more than this number of levels in the category tree should be displayed
218
		 *
219
		 * If the user clicks on a category node, the page reloads and the
220
		 * sub-nodes of the chosen category are rendered as well.
221
		 * Using this configuration option you can enforce that no more than
222
		 * the given number of levels will be displayed at all. The root
223
		 * node uses level 0, the categories below level 1 and so on.
224
		 *
225
		 * In most cases you can set this value via the administration interface
226
		 * of the shop application. In that case you often can configure the
227
		 * levels individually for each catalog filter.
228
		 *
229
		 * Note: This setting was available between 2014.03 and 2019.04 as
230
		 * client/html/catalog/filter/tree/levels-only
231
		 *
232
		 * @param integer Number of tree levels
233
		 * @since 2014.03
234
		 * @category User
235
		 * @category Developer
236
		 * @see controller/frontend/catalog/levels-always
237
		 */
238
		if( ( $levels = $config->get( 'controller/frontend/catalog/levels-only' ) ) != null ) {
239
			$this->conditions[] = $this->filter->compare( '<=', 'catalog.level', $levels );
240
		}
241
242
		$this->conditions[] = $this->filter->combine( '||', $expr );
243
		return $this;
244
	}
245
}
246