Passed
Branch master (901174)
by Aimeos
03:14
created

Standard::parse()   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 $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 );
1 ignored issue
show
Bug introduced by
The method getPath() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of Aimeos\MShop\Common\Manager\Iface such as Aimeos\MShop\Catalog\Manager\Iface or Aimeos\MShop\Locale\Manager\Site\Iface or Aimeos\MShop\Common\Manager\Decorator\Base or Aimeos\MShop\Service\Manager\Lists\Type\Standard or Aimeos\MShop\Price\Manager\Standard or Aimeos\MShop\Attribute\Manager\Type\Standard or Aimeos\MShop\Price\Manager\Lists\Type\Standard or Aimeos\MShop\Media\Manager\Type\Standard or Aimeos\MShop\Coupon\Manager\Code\Standard or Aimeos\MShop\Order\Manager\Base\Coupon\Standard or Aimeos\MShop\Product\Manager\Standard or Aimeos\MShop\Index\Manager\Standard or Aimeos\MShop\Index\Manager\Attribute\Standard or Aimeos\MShop\Index\Manager\Text\Standard or Aimeos\MShop\Index\Manager\Supplier\Standard or Aimeos\MShop\Index\Manager\Catalog\Standard or Aimeos\MShop\Index\Manager\Price\Standard or Aimeos\MShop\Supplier\Manager\Standard or Aimeos\MShop\Customer\Manager\Property\Standard or Aimeos\MShop\Order\Manager\Base\Service\Standard or Aimeos\MShop\Order\Manager\Base\Standard or Aimeos\MShop\Price\Manager\Lists\Standard or Aimeos\MShop\Supplier\Manager\Lists\Type\Standard or Aimeos\MShop\Order\Manag...vice\Attribute\Standard or Aimeos\MShop\Service\Manager\Lists\Standard or Aimeos\MShop\Tag\Manager\Type\Standard or Aimeos\MShop\Text\Manager\Lists\Standard or Aimeos\MShop\Price\Manager\Type\Standard or Aimeos\MShop\Locale\Manager\Currency\Standard or Aimeos\MShop\Order\Manag...duct\Attribute\Standard or Aimeos\MShop\Media\Manager\Lists\Type\Standard or Aimeos\MShop\Catalog\Manager\Lists\Standard or Aimeos\MShop\Tag\Manager\Standard or Aimeos\MShop\Coupon\Manager\Standard or Aimeos\MShop\Attribute\Manager\Standard or Aimeos\MShop\Attribute\M...\Property\Type\Standard or Aimeos\MShop\Service\Manager\Type\Standard or Aimeos\MShop\Product\Manager\Lists\Standard or Aimeos\MShop\Customer\Ma...\Property\Type\Standard or Aimeos\MShop\Order\Manager\Standard or Aimeos\MShop\Customer\Manager\Standard or Aimeos\MShop\Media\Manager\Standard or Aimeos\MShop\Customer\Manager\Lists\Type\Standard or Aimeos\MShop\Attribute\Manager\Lists\Standard or Aimeos\MShop\Product\Man...\Property\Type\Standard or Aimeos\MShop\Media\Manager\Lists\Standard or Aimeos\MShop\Plugin\Manager\Standard or Aimeos\MShop\Order\Manager\Base\Address\Standard or Aimeos\MShop\Product\Manager\Type\Standard or Aimeos\MShop\Supplier\Manager\Lists\Standard or Aimeos\MShop\Stock\Manager\Type\Standard or Aimeos\MShop\Text\Manager\Standard or Aimeos\MAdmin\Job\Manager\Standard or Aimeos\MShop\Customer\Manager\Group\Standard or Aimeos\MShop\Product\Manager\Lists\Type\Standard or Aimeos\MShop\Text\Manager\Lists\Type\Standard or Aimeos\MShop\Text\Manager\Type\Standard or Aimeos\MShop\Order\Manager\Status\Standard or Aimeos\MShop\Supplier\Manager\Address\Standard or Aimeos\MShop\Customer\Manager\Address\Standard or Aimeos\MShop\Plugin\Manager\Type\Standard or Aimeos\MShop\Stock\Manager\Standard or Aimeos\MShop\Stock\Manager\Nolimit or Aimeos\MShop\Subscription\Manager\Standard or Aimeos\MShop\Attribute\Manager\Property\Standard or Aimeos\MShop\Media\Manager\Property\Type\Standard or Aimeos\MShop\Product\Manager\Property\Standard or Aimeos\MShop\Locale\Manager\Language\Standard or Aimeos\MShop\Media\Manager\Property\Standard or Aimeos\MShop\Service\Manager\Standard or Aimeos\MShop\Attribute\Manager\Lists\Type\Standard or Aimeos\MAdmin\Log\Manager\Standard or Aimeos\MShop\Locale\Manager\Standard or Aimeos\MAdmin\Cache\Manager\Standard or Aimeos\MAdmin\Cache\Manager\None or Aimeos\MShop\Order\Manager\Base\Product\Standard or Aimeos\MShop\Customer\Manager\Lists\Standard or Aimeos\MShop\Catalog\Manager\Lists\Type\Standard. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
		/** @scrutinizer ignore-call */ 
95
  $list = $this->manager->getPath( $id, $domains );
Loading history...
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 );
1 ignored issue
show
Bug introduced by
The method getTree() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of Aimeos\MShop\Common\Manager\Iface such as Aimeos\MShop\Catalog\Manager\Iface or Aimeos\MShop\Locale\Manager\Site\Iface or Aimeos\MShop\Common\Manager\Decorator\Base or Aimeos\MShop\Service\Manager\Lists\Type\Standard or Aimeos\MShop\Price\Manager\Standard or Aimeos\MShop\Attribute\Manager\Type\Standard or Aimeos\MShop\Price\Manager\Lists\Type\Standard or Aimeos\MShop\Media\Manager\Type\Standard or Aimeos\MShop\Coupon\Manager\Code\Standard or Aimeos\MShop\Order\Manager\Base\Coupon\Standard or Aimeos\MShop\Product\Manager\Standard or Aimeos\MShop\Index\Manager\Standard or Aimeos\MShop\Index\Manager\Attribute\Standard or Aimeos\MShop\Index\Manager\Text\Standard or Aimeos\MShop\Index\Manager\Supplier\Standard or Aimeos\MShop\Index\Manager\Catalog\Standard or Aimeos\MShop\Index\Manager\Price\Standard or Aimeos\MShop\Supplier\Manager\Standard or Aimeos\MShop\Customer\Manager\Property\Standard or Aimeos\MShop\Order\Manager\Base\Service\Standard or Aimeos\MShop\Order\Manager\Base\Standard or Aimeos\MShop\Price\Manager\Lists\Standard or Aimeos\MShop\Supplier\Manager\Lists\Type\Standard or Aimeos\MShop\Order\Manag...vice\Attribute\Standard or Aimeos\MShop\Service\Manager\Lists\Standard or Aimeos\MShop\Tag\Manager\Type\Standard or Aimeos\MShop\Text\Manager\Lists\Standard or Aimeos\MShop\Price\Manager\Type\Standard or Aimeos\MShop\Locale\Manager\Currency\Standard or Aimeos\MShop\Order\Manag...duct\Attribute\Standard or Aimeos\MShop\Media\Manager\Lists\Type\Standard or Aimeos\MShop\Catalog\Manager\Lists\Standard or Aimeos\MShop\Tag\Manager\Standard or Aimeos\MShop\Coupon\Manager\Standard or Aimeos\MShop\Attribute\Manager\Standard or Aimeos\MShop\Attribute\M...\Property\Type\Standard or Aimeos\MShop\Service\Manager\Type\Standard or Aimeos\MShop\Product\Manager\Lists\Standard or Aimeos\MShop\Customer\Ma...\Property\Type\Standard or Aimeos\MShop\Order\Manager\Standard or Aimeos\MShop\Customer\Manager\Standard or Aimeos\MShop\Media\Manager\Standard or Aimeos\MShop\Customer\Manager\Lists\Type\Standard or Aimeos\MShop\Attribute\Manager\Lists\Standard or Aimeos\MShop\Product\Man...\Property\Type\Standard or Aimeos\MShop\Media\Manager\Lists\Standard or Aimeos\MShop\Plugin\Manager\Standard or Aimeos\MShop\Order\Manager\Base\Address\Standard or Aimeos\MShop\Product\Manager\Type\Standard or Aimeos\MShop\Supplier\Manager\Lists\Standard or Aimeos\MShop\Stock\Manager\Type\Standard or Aimeos\MShop\Text\Manager\Standard or Aimeos\MAdmin\Job\Manager\Standard or Aimeos\MShop\Customer\Manager\Group\Standard or Aimeos\MShop\Product\Manager\Lists\Type\Standard or Aimeos\MShop\Text\Manager\Lists\Type\Standard or Aimeos\MShop\Text\Manager\Type\Standard or Aimeos\MShop\Order\Manager\Status\Standard or Aimeos\MShop\Supplier\Manager\Address\Standard or Aimeos\MShop\Customer\Manager\Address\Standard or Aimeos\MShop\Plugin\Manager\Type\Standard or Aimeos\MShop\Stock\Manager\Standard or Aimeos\MShop\Stock\Manager\Nolimit or Aimeos\MShop\Subscription\Manager\Standard or Aimeos\MShop\Attribute\Manager\Property\Standard or Aimeos\MShop\Media\Manager\Property\Type\Standard or Aimeos\MShop\Product\Manager\Property\Standard or Aimeos\MShop\Locale\Manager\Language\Standard or Aimeos\MShop\Media\Manager\Property\Standard or Aimeos\MShop\Service\Manager\Standard or Aimeos\MShop\Attribute\Manager\Lists\Type\Standard or Aimeos\MAdmin\Log\Manager\Standard or Aimeos\MShop\Locale\Manager\Standard or Aimeos\MAdmin\Cache\Manager\Standard or Aimeos\MAdmin\Cache\Manager\None or Aimeos\MShop\Order\Manager\Base\Product\Standard or Aimeos\MShop\Customer\Manager\Lists\Standard or Aimeos\MShop\Catalog\Manager\Lists\Type\Standard. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
		return $this->manager->/** @scrutinizer ignore-call */ getTree( $this->root, $domains, $level, $this->filter );
Loading history...
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 );
1 ignored issue
show
Bug introduced by
The method findItem() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Base\Coupon\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Order\Manager\Base\Service\Iface or Aimeos\MShop\Order\Manager\Base\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Order\Manag...Service\Attribute\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Order\Manag...Product\Attribute\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Order\Manager\Base\Address\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MAdmin\Job\Manager\Iface or Aimeos\MShop\Product\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Plugin\Manager\Type\Iface or Aimeos\MShop\Subscription\Manager\Iface or Aimeos\MShop\Attribute\Manager\Property\Iface or Aimeos\MShop\Product\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Language\Iface or Aimeos\MShop\Media\Manager\Property\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MAdmin\Log\Manager\Iface or Aimeos\MShop\Locale\Manager\Iface or Aimeos\MAdmin\Cache\Manager\Iface or Aimeos\MShop\Order\Manager\Base\Product\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Address\Iface or Aimeos\MShop\Customer\Manager\Address\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
		return $this->manager->/** @scrutinizer ignore-call */ findItem( $code, $domains, null, null, true );
Loading history...
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