1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2021 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Frontend |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Frontend\Attribute; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default implementation of the attribute 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 $domain = 'product'; |
25
|
|
|
private $domains = []; |
26
|
|
|
private $filter; |
27
|
|
|
private $manager; |
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, 'attribute' ); |
40
|
|
|
$this->filter = $this->manager->filter( true ); |
41
|
|
|
$this->addExpression( $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 attribute IDs for filtering |
56
|
|
|
* |
57
|
|
|
* @param array|string $attrIds Attribute ID or list of IDs |
58
|
|
|
* @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface |
59
|
|
|
* @since 2019.04 |
60
|
|
|
*/ |
61
|
|
|
public function attribute( $attrIds ) : Iface |
62
|
|
|
{ |
63
|
|
|
if( !empty( $attrIds ) ) { |
64
|
|
|
$this->addExpression( $this->filter->compare( '==', 'attribute.id', $attrIds ) ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Adds generic condition for filtering attributes |
73
|
|
|
* |
74
|
|
|
* @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~=" |
75
|
|
|
* @param string $key Search key defined by the attribute manager, e.g. "attribute.status" |
76
|
|
|
* @param array|string $value Value or list of values to compare to |
77
|
|
|
* @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface |
78
|
|
|
* @since 2019.04 |
79
|
|
|
*/ |
80
|
|
|
public function compare( string $operator, string $key, $value ) : Iface |
81
|
|
|
{ |
82
|
|
|
$this->addExpression( $this->filter->compare( $operator, $key, $value ) ); |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Adds the domain of the attributes for filtering |
89
|
|
|
* |
90
|
|
|
* @param string $domain Domain of the attributes |
91
|
|
|
* @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface |
92
|
|
|
* @since 2019.04 |
93
|
|
|
*/ |
94
|
|
|
public function domain( string $domain ) : Iface |
95
|
|
|
{ |
96
|
|
|
$this->domain = $domain; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns the attribute for the given attribute code |
103
|
|
|
* |
104
|
|
|
* @param string $code Unique attribute code |
105
|
|
|
* @param string $type Type assigned to the attribute |
106
|
|
|
* @return \Aimeos\MShop\Attribute\Item\Iface Attribute item including the referenced domains items |
107
|
|
|
* @since 2019.04 |
108
|
|
|
*/ |
109
|
|
|
public function find( string $code, string $type ) : \Aimeos\MShop\Attribute\Item\Iface |
110
|
|
|
{ |
111
|
|
|
return $this->manager->find( $code, $this->domains, $this->domain, $type, true ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Creates a search function string for the given name and parameters |
117
|
|
|
* |
118
|
|
|
* @param string $name Name of the search function without parenthesis, e.g. "attribute:prop" |
119
|
|
|
* @param array $params List of parameters for the search function with numeric keys starting at 0 |
120
|
|
|
* @return string Search function string that can be used in compare() |
121
|
|
|
*/ |
122
|
|
|
public function function( string $name, array $params ) : string |
123
|
|
|
{ |
124
|
|
|
return $this->filter->make( $name, $params ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the attribute for the given attribute ID |
130
|
|
|
* |
131
|
|
|
* @param string $id Unique attribute ID |
132
|
|
|
* @return \Aimeos\MShop\Attribute\Item\Iface Attribute item including the referenced domains items |
133
|
|
|
* @since 2019.04 |
134
|
|
|
*/ |
135
|
|
|
public function get( string $id ) : \Aimeos\MShop\Attribute\Item\Iface |
136
|
|
|
{ |
137
|
|
|
return $this->manager->get( $id, $this->domains, true ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Adds a filter to return only items containing a reference to the given ID |
143
|
|
|
* |
144
|
|
|
* @param string $domain Domain name of the referenced item, e.g. "price" |
145
|
|
|
* @param string|null $type Type code of the reference, e.g. "default" or null for all types |
146
|
|
|
* @param string|null $refId ID of the referenced item of the given domain or null for all references |
147
|
|
|
* @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface |
148
|
|
|
* @since 2019.04 |
149
|
|
|
*/ |
150
|
|
|
public function has( string $domain, string $type = null, string $refId = null ) : Iface |
151
|
|
|
{ |
152
|
|
|
$params = [$domain]; |
153
|
|
|
!$type ?: $params[] = $type; |
154
|
|
|
!$refId ?: $params[] = $refId; |
155
|
|
|
|
156
|
|
|
$func = $this->filter->make( 'attribute:has', $params ); |
157
|
|
|
$this->addExpression( $this->filter->compare( '!=', $func, null ) ); |
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Parses the given array and adds the conditions to the list of conditions |
164
|
|
|
* |
165
|
|
|
* @param array $conditions List of conditions, e.g. ['&&' => [['>' => ['attribute.status' => 0]], ['==' => ['attribute.type' => 'color']]]] |
166
|
|
|
* @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface |
167
|
|
|
* @since 2019.04 |
168
|
|
|
*/ |
169
|
|
|
public function parse( array $conditions ) : Iface |
170
|
|
|
{ |
171
|
|
|
if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) { |
172
|
|
|
$this->addExpression( $cond ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Adds a filter to return only items containing the property |
181
|
|
|
* |
182
|
|
|
* @param string $type Type code of the property, e.g. "htmlcolor" |
183
|
|
|
* @param string|null $value Exact value of the property |
184
|
|
|
* @param string|null $langId ISO country code (en or en_US) or null if not language specific |
185
|
|
|
* @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface |
186
|
|
|
* @since 2019.04 |
187
|
|
|
*/ |
188
|
|
|
public function property( string $type, string $value = null, string $langId = null ) : Iface |
189
|
|
|
{ |
190
|
|
|
$func = $this->filter->make( 'attribute:prop', [$type, $langId, $value] ); |
191
|
|
|
$this->addExpression( $this->filter->compare( '!=', $func, null ) ); |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns the attributes filtered by the previously assigned conditions |
198
|
|
|
* |
199
|
|
|
* @param int &$total Parameter where the total number of found attributes will be stored in |
200
|
|
|
* @return \Aimeos\Map Ordered list of attribute items implementing \Aimeos\MShop\Attribute\Item\Iface |
201
|
|
|
* @since 2019.04 |
202
|
|
|
*/ |
203
|
|
|
public function search( int &$total = null ) : \Aimeos\Map |
204
|
|
|
{ |
205
|
|
|
$this->addExpression( $this->filter->compare( '==', 'attribute.domain', $this->domain ) ); |
206
|
|
|
|
207
|
|
|
$this->filter->setConditions( $this->filter->and( $this->getConditions() ) ); |
208
|
|
|
$this->filter->setSortations( $this->getSortations() ); |
209
|
|
|
|
210
|
|
|
return $this->manager->search( $this->filter, $this->domains, $total ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Sets the start value and the number of returned attributes for slicing the list of found attributes |
216
|
|
|
* |
217
|
|
|
* @param int $start Start value of the first attribute in the list |
218
|
|
|
* @param int $limit Number of returned attributes |
219
|
|
|
* @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface |
220
|
|
|
* @since 2019.04 |
221
|
|
|
*/ |
222
|
|
|
public function slice( int $start, int $limit ) : Iface |
223
|
|
|
{ |
224
|
|
|
$maxsize = $this->getContext()->config()->get( 'controller/frontend/common/max-size', 250 ); |
225
|
|
|
$this->filter->slice( $start, min( $limit, $maxsize ) ); |
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Sets the sorting of the result list |
232
|
|
|
* |
233
|
|
|
* @param string|null $key Sorting of the result list like "position", null for no sorting |
234
|
|
|
* @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface |
235
|
|
|
* @since 2019.04 |
236
|
|
|
*/ |
237
|
|
|
public function sort( string $key = null ) : Iface |
238
|
|
|
{ |
239
|
|
|
$list = $this->splitKeys( $key ); |
240
|
|
|
|
241
|
|
|
foreach( $list as $sortkey ) |
242
|
|
|
{ |
243
|
|
|
$direction = ( $sortkey[0] === '-' ? '-' : '+' ); |
244
|
|
|
$sortkey = ltrim( $sortkey, '+-' ); |
245
|
|
|
|
246
|
|
|
switch( $sortkey ) |
247
|
|
|
{ |
248
|
|
|
case 'position': |
249
|
|
|
$this->addExpression( $this->filter->sort( $direction, 'attribute.type' ) ); |
250
|
|
|
$this->addExpression( $this->filter->sort( $direction, 'attribute.position' ) ); |
251
|
|
|
break; |
252
|
|
|
default: |
253
|
|
|
$this->addExpression( $this->filter->sort( $direction, $sortkey ) ); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Adds attribute types for filtering |
263
|
|
|
* |
264
|
|
|
* @param array|string $codes Attribute ID or list of IDs |
265
|
|
|
* @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface |
266
|
|
|
* @since 2019.04 |
267
|
|
|
*/ |
268
|
|
|
public function type( $codes ) : Iface |
269
|
|
|
{ |
270
|
|
|
if( !empty( $codes ) ) { |
271
|
|
|
$this->addExpression( $this->filter->compare( '==', 'attribute.type', $codes ) ); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Sets the referenced domains that will be fetched too when retrieving items |
280
|
|
|
* |
281
|
|
|
* @param array $domains Domain names of the referenced items that should be fetched too |
282
|
|
|
* @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface |
283
|
|
|
* @since 2019.04 |
284
|
|
|
*/ |
285
|
|
|
public function uses( array $domains ) : Iface |
286
|
|
|
{ |
287
|
|
|
$this->domains = $domains; |
288
|
|
|
return $this; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Returns the manager used by the controller |
294
|
|
|
* |
295
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object |
296
|
|
|
*/ |
297
|
|
|
protected function getManager() : \Aimeos\MShop\Common\Manager\Iface |
298
|
|
|
{ |
299
|
|
|
return $this->manager; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|