| Total Complexity | 66 |
| Total Lines | 700 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Standard |
||
| 21 | extends \Aimeos\Controller\Frontend\Base |
||
| 22 | implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
||
| 23 | { |
||
| 24 | /** controller/frontend/product/name |
||
| 25 | * Class name of the used product frontend controller implementation |
||
| 26 | * |
||
| 27 | * Each default frontend controller can be replace by an alternative imlementation. |
||
| 28 | * To use this implementation, you have to set the last part of the class |
||
| 29 | * name as configuration value so the controller factory knows which class it |
||
| 30 | * has to instantiate. |
||
| 31 | * |
||
| 32 | * For example, if the name of the default class is |
||
| 33 | * |
||
| 34 | * \Aimeos\Controller\Frontend\Product\Standard |
||
| 35 | * |
||
| 36 | * and you want to replace it with your own version named |
||
| 37 | * |
||
| 38 | * \Aimeos\Controller\Frontend\Product\Myproduct |
||
| 39 | * |
||
| 40 | * then you have to set the this configuration option: |
||
| 41 | * |
||
| 42 | * controller/jobs/frontend/product/name = Myproduct |
||
| 43 | * |
||
| 44 | * The value is the last part of your own class name and it's case sensitive, |
||
| 45 | * so take care that the configuration value is exactly named like the last |
||
| 46 | * part of the class name. |
||
| 47 | * |
||
| 48 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
| 49 | * characters are possible! You should always start the last part of the class |
||
| 50 | * name with an upper case character and continue only with lower case characters |
||
| 51 | * or numbers. Avoid chamel case names like "MyProduct"! |
||
| 52 | * |
||
| 53 | * @param string Last part of the class name |
||
| 54 | * @since 2017.03 |
||
| 55 | * @category Developer |
||
| 56 | */ |
||
| 57 | |||
| 58 | /** controller/frontend/product/decorators/excludes |
||
| 59 | * Excludes decorators added by the "common" option from the product frontend controllers |
||
| 60 | * |
||
| 61 | * Decorators extend the functionality of a class by adding new aspects |
||
| 62 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 63 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 64 | * modify what is returned to the caller. |
||
| 65 | * |
||
| 66 | * This option allows you to remove a decorator added via |
||
| 67 | * "controller/frontend/common/decorators/default" before they are wrapped |
||
| 68 | * around the frontend controller. |
||
| 69 | * |
||
| 70 | * controller/frontend/product/decorators/excludes = array( 'decorator1' ) |
||
| 71 | * |
||
| 72 | * This would remove the decorator named "decorator1" from the list of |
||
| 73 | * common decorators ("\Aimeos\Controller\Frontend\Common\Decorator\*") added via |
||
| 74 | * "controller/frontend/common/decorators/default" for the product frontend controller. |
||
| 75 | * |
||
| 76 | * @param array List of decorator names |
||
| 77 | * @since 2017.03 |
||
| 78 | * @category Developers |
||
| 79 | * @see controller/frontend/common/decorators/default |
||
| 80 | * @see controller/frontend/product/decorators/global |
||
| 81 | * @see controller/frontend/product/decorators/local |
||
| 82 | */ |
||
| 83 | |||
| 84 | /** controller/frontend/product/decorators/global |
||
| 85 | * Adds a list of globally available decorators only to the product frontend controllers |
||
| 86 | * |
||
| 87 | * Decorators extend the functionality of a class by adding new aspects |
||
| 88 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 89 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 90 | * modify what is returned to the caller. |
||
| 91 | * |
||
| 92 | * This option allows you to wrap global decorators |
||
| 93 | * ("\Aimeos\Controller\Frontend\Common\Decorator\*") around the frontend controller. |
||
| 94 | * |
||
| 95 | * controller/frontend/product/decorators/global = array( 'decorator1' ) |
||
| 96 | * |
||
| 97 | * This would add the decorator named "decorator1" defined by |
||
| 98 | * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator1" only to the frontend controller. |
||
| 99 | * |
||
| 100 | * @param array List of decorator names |
||
| 101 | * @since 2017.03 |
||
| 102 | * @category Developers |
||
| 103 | * @see controller/frontend/common/decorators/default |
||
| 104 | * @see controller/frontend/product/decorators/excludes |
||
| 105 | * @see controller/frontend/product/decorators/local |
||
| 106 | */ |
||
| 107 | |||
| 108 | /** controller/frontend/product/decorators/local |
||
| 109 | * Adds a list of local decorators only to the product frontend controllers |
||
| 110 | * |
||
| 111 | * Decorators extend the functionality of a class by adding new aspects |
||
| 112 | * (e.g. log what is currently done), executing the methods of the underlying |
||
| 113 | * class only in certain conditions (e.g. only for logged in users) or |
||
| 114 | * modify what is returned to the caller. |
||
| 115 | * |
||
| 116 | * This option allows you to wrap local decorators |
||
| 117 | * ("\Aimeos\Controller\Frontend\Product\Decorator\*") around the frontend controller. |
||
| 118 | * |
||
| 119 | * controller/frontend/product/decorators/local = array( 'decorator2' ) |
||
| 120 | * |
||
| 121 | * This would add the decorator named "decorator2" defined by |
||
| 122 | * "\Aimeos\Controller\Frontend\Product\Decorator\Decorator2" only to the frontend |
||
| 123 | * controller. |
||
| 124 | * |
||
| 125 | * @param array List of decorator names |
||
| 126 | * @since 2017.03 |
||
| 127 | * @category Developers |
||
| 128 | * @see controller/frontend/common/decorators/default |
||
| 129 | * @see controller/frontend/product/decorators/excludes |
||
| 130 | * @see controller/frontend/product/decorators/global |
||
| 131 | */ |
||
| 132 | |||
| 133 | |||
| 134 | private array $domains = []; |
||
| 135 | private \Aimeos\Base\Criteria\Iface $filter; |
||
| 136 | private \Aimeos\MShop\Common\Manager\Iface $manager; |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Common initialization for controller classes |
||
| 141 | * |
||
| 142 | * @param \Aimeos\MShop\ContextIface $context Common MShop context object |
||
| 143 | */ |
||
| 144 | public function __construct( \Aimeos\MShop\ContextIface $context ) |
||
| 145 | { |
||
| 146 | parent::__construct( $context ); |
||
| 147 | |||
| 148 | $this->manager = \Aimeos\MShop::create( $context, 'index' ); |
||
| 149 | $this->filter = $this->manager->filter( true ); |
||
| 150 | |||
| 151 | /** controller/frontend/product/show-all |
||
| 152 | * Require products to be assigned to categories |
||
| 153 | * |
||
| 154 | * By default, products that are shown in the frontend must be assigned to |
||
| 155 | * at least one category. When changing this setting to TRUE, also products |
||
| 156 | * without categories will be shown in the frontend. |
||
| 157 | * |
||
| 158 | * Caution: If you have discount products or variant articles in selection |
||
| 159 | * products, these products/articles will be displayed in the frontend too |
||
| 160 | * when disabling this setting! |
||
| 161 | * |
||
| 162 | * @param bool FALSE if products must be assigned to categories, TRUE if not |
||
| 163 | * @since 2010.10 |
||
| 164 | */ |
||
| 165 | if( $context->config()->get( 'controller/frontend/product/show-all', false ) == false ) { |
||
| 166 | $this->addExpression( $this->filter->compare( '!=', 'index.catalog.id', null ) ); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * Clones objects in controller |
||
| 173 | */ |
||
| 174 | public function __clone() |
||
| 175 | { |
||
| 176 | $this->filter = clone $this->filter; |
||
| 177 | parent::__clone(); |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the aggregated count of products for the given key. |
||
| 183 | * |
||
| 184 | * @param string $key Search key to aggregate for, e.g. "index.attribute.id" |
||
| 185 | * @param string|null $value Search key for aggregating the value column |
||
| 186 | * @param string|null $type Type of the aggregation, empty string for count or "sum" or "avg" (average) |
||
| 187 | * @return \Aimeos\Map Associative list of key values as key and the product count for this key as value |
||
| 188 | * @since 2019.04 |
||
| 189 | */ |
||
| 190 | public function aggregate( string $key, ?string $value = null, ?string $type = null ) : \Aimeos\Map |
||
| 191 | { |
||
| 192 | $filter = clone $this->filter; |
||
| 193 | $filter->add( $filter->and( $this->getConditions() ) ); |
||
| 194 | |||
| 195 | return $this->manager->aggregate( $filter, $key, $value, $type ); |
||
| 196 | } |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Adds attribute IDs for filtering where products must reference all IDs |
||
| 201 | * |
||
| 202 | * @param array|string $attrIds Attribute ID or list of IDs |
||
| 203 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 204 | * @since 2019.04 |
||
| 205 | */ |
||
| 206 | public function allOf( $attrIds ) : Iface |
||
| 207 | { |
||
| 208 | if( !empty( $attrIds ) && ( $ids = $this->validateIds( (array) $attrIds ) ) !== [] ) |
||
| 209 | { |
||
| 210 | $func = $this->filter->make( 'index.attribute:allof', [$ids] ); |
||
| 211 | $this->addExpression( $this->filter->compare( '!=', $func, null ) ); |
||
| 212 | } |
||
| 213 | |||
| 214 | return $this; |
||
| 215 | } |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Adds catalog IDs for filtering |
||
| 220 | * |
||
| 221 | * @param array|string $catIds Catalog ID or list of IDs |
||
| 222 | * @param string $listtype List type of the products referenced by the categories |
||
| 223 | * @param int $level Constant from \Aimeos\MW\Tree\Manager\Base if products in subcategories are matched too |
||
| 224 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 225 | * @since 2019.04 |
||
| 226 | */ |
||
| 227 | public function category( $catIds, string $listtype = 'default', int $level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE ) : Iface |
||
| 228 | { |
||
| 229 | if( !empty( $catIds ) && ( $ids = $this->validateIds( (array) $catIds ) ) !== [] ) |
||
| 230 | { |
||
| 231 | if( $level != \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE ) |
||
| 232 | { |
||
| 233 | $list = map(); |
||
| 234 | $manager = \Aimeos\MShop::create( $this->context(), 'catalog' ); |
||
| 235 | |||
| 236 | foreach( $ids as $catId ) { |
||
| 237 | $list->union( $manager->getTree( $catId, [], $level )->toList() ); |
||
| 238 | } |
||
| 239 | |||
| 240 | $ids = $this->validateIds( $list->keys()->toArray() ); |
||
| 241 | } |
||
| 242 | |||
| 243 | $func = $this->filter->make( 'index.catalog:position', [$listtype, $ids] ); |
||
| 244 | |||
| 245 | $this->addExpression( $this->filter->compare( '==', 'index.catalog.id', $ids ) ); |
||
| 246 | $this->addExpression( $this->filter->compare( '>=', $func, 0 ) ); |
||
| 247 | |||
| 248 | $func = $this->filter->make( 'sort:index.catalog:position', [$listtype, $ids] ); |
||
| 249 | $this->addExpression( $this->filter->sort( '+', $func ) ); |
||
| 250 | $this->addExpression( $this->filter->sort( '+', 'product.id' ) ); // prevent flaky order if products have same position |
||
| 251 | } |
||
| 252 | |||
| 253 | return $this; |
||
| 254 | } |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * Adds generic condition for filtering products |
||
| 259 | * |
||
| 260 | * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~=" |
||
| 261 | * @param string $key Search key defined by the product manager, e.g. "product.status" |
||
| 262 | * @param array|string $value Value or list of values to compare to |
||
| 263 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 264 | * @since 2019.04 |
||
| 265 | */ |
||
| 266 | public function compare( string $operator, string $key, $value ) : Iface |
||
| 267 | { |
||
| 268 | $this->addExpression( $this->filter->compare( $operator, $key, $value ) ); |
||
| 269 | return $this; |
||
| 270 | } |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * Returns the product for the given product code |
||
| 275 | * |
||
| 276 | * @param string $code Unique product code |
||
| 277 | * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items |
||
| 278 | * @since 2019.04 |
||
| 279 | */ |
||
| 280 | public function find( string $code ) : \Aimeos\MShop\Product\Item\Iface |
||
| 281 | { |
||
| 282 | $item = $this->manager->find( $code, $this->domains, 'product', null, null ); |
||
| 283 | return \Aimeos\MShop::create( $this->context(), 'rule' )->apply( $item, 'catalog' ); |
||
| 284 | } |
||
| 285 | |||
| 286 | |||
| 287 | /** |
||
| 288 | * Creates a search function string for the given name and parameters |
||
| 289 | * |
||
| 290 | * @param string $name Name of the search function without parenthesis, e.g. "product:has" |
||
| 291 | * @param array $params List of parameters for the search function with numeric keys starting at 0 |
||
| 292 | * @return string Search function string that can be used in compare() |
||
| 293 | */ |
||
| 294 | public function function( string $name, array $params ) : string |
||
| 295 | { |
||
| 296 | return $this->filter->make( $name, $params ); |
||
| 297 | } |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns the product for the given product ID |
||
| 302 | * |
||
| 303 | * @param string $id Unique product ID |
||
| 304 | * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items |
||
| 305 | * @since 2019.04 |
||
| 306 | */ |
||
| 307 | public function get( string $id ) : \Aimeos\MShop\Product\Item\Iface |
||
| 308 | { |
||
| 309 | $item = $this->manager->get( $id, $this->domains, null ); |
||
| 310 | return \Aimeos\MShop::create( $this->context(), 'rule' )->apply( $item, 'catalog' ); |
||
| 311 | } |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * Adds a filter to return only items containing a reference to the given ID |
||
| 316 | * |
||
| 317 | * @param string $domain Domain name of the referenced item, e.g. "attribute" |
||
| 318 | * @param string|null $type Type code of the reference, e.g. "variant" or null for all types |
||
| 319 | * @param string|null $refId ID of the referenced item of the given domain or null for all references |
||
| 320 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 321 | * @since 2019.04 |
||
| 322 | */ |
||
| 323 | public function has( string $domain, ?string $type = null, ?string $refId = null ) : Iface |
||
| 324 | { |
||
| 325 | $params = [$domain]; |
||
| 326 | !$type ?: $params[] = $type; |
||
| 327 | !$refId ?: $params[] = $refId; |
||
| 328 | |||
| 329 | $func = $this->filter->make( 'product:has', $params ); |
||
| 330 | $this->addExpression( $this->filter->compare( '!=', $func, null ) ); |
||
| 331 | return $this; |
||
| 332 | } |
||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * Adds attribute IDs for filtering where products must reference at least one ID |
||
| 337 | * |
||
| 338 | * If an array of ID lists is given, each ID list is added separately as condition. |
||
| 339 | * |
||
| 340 | * @param array|string $attrIds Attribute ID, list of IDs or array of lists with IDs |
||
| 341 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 342 | * @since 2019.04 |
||
| 343 | */ |
||
| 344 | public function oneOf( $attrIds ) : Iface |
||
| 345 | { |
||
| 346 | $attrIds = (array) $attrIds; |
||
| 347 | |||
| 348 | foreach( $attrIds as $key => $entry ) |
||
| 349 | { |
||
| 350 | if( is_array( $entry ) && ( $ids = $this->validateIds( $entry ) ) !== [] ) |
||
| 351 | { |
||
| 352 | $func = $this->filter->make( 'index.attribute:oneof', [$ids] ); |
||
| 353 | $this->addExpression( $this->filter->compare( '!=', $func, null ) ); |
||
| 354 | unset( $attrIds[$key] ); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | if( ( $ids = $this->validateIds( $attrIds ) ) !== [] ) |
||
| 359 | { |
||
| 360 | $func = $this->filter->make( 'index.attribute:oneof', [$ids] ); |
||
| 361 | $this->addExpression( $this->filter->compare( '!=', $func, null ) ); |
||
| 362 | } |
||
| 363 | |||
| 364 | return $this; |
||
| 365 | } |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * Parses the given array and adds the conditions to the list of conditions |
||
| 370 | * |
||
| 371 | * @param array $conditions List of conditions, e.g. [['>' => ['product.status' => 0]], ['==' => ['product.type' => 'default']]] |
||
| 372 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 373 | * @since 2019.04 |
||
| 374 | */ |
||
| 375 | public function parse( array $conditions ) : Iface |
||
| 376 | { |
||
| 377 | if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) { |
||
| 378 | $this->addExpression( $cond ); |
||
| 379 | } |
||
| 380 | |||
| 381 | return $this; |
||
| 382 | } |
||
| 383 | |||
| 384 | |||
| 385 | /** |
||
| 386 | * Adds price restrictions for filtering |
||
| 387 | * |
||
| 388 | * @param array|string $value Upper price limit, list of lower and upper price or NULL for no restrictions |
||
| 389 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 390 | * @since 2020.10 |
||
| 391 | */ |
||
| 392 | public function price( $value = null ) : Iface |
||
| 393 | { |
||
| 394 | if( $value ) |
||
| 395 | { |
||
| 396 | $func = $this->filter->make( 'index.price:value', [$this->context()->locale()->getCurrencyId()] ); |
||
| 397 | $prec = (int) $this->context()->config()->get( 'mshop/price/precision', 2 ); |
||
| 398 | $format = '%0' . ( $prec > 0 ? 13 : 12 ) . '.' . $prec . 'F'; |
||
| 399 | $value = (array) $value; |
||
| 400 | |||
| 401 | $this->addExpression( $this->filter->compare( '<=', $func, sprintf( $format, end( $value ) ) ) ); |
||
| 402 | |||
| 403 | if( count( $value ) > 1 ) { |
||
| 404 | $this->addExpression( $this->filter->compare( '>=', $func, sprintf( $format, reset( $value ) ) ) ); |
||
| 405 | } |
||
| 406 | } |
||
| 407 | |||
| 408 | return $this; |
||
| 409 | } |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * Adds product IDs for filtering |
||
| 414 | * |
||
| 415 | * @param array|string $prodIds Product ID or list of IDs |
||
| 416 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 417 | * @since 2019.04 |
||
| 418 | */ |
||
| 419 | public function product( $prodIds ) : Iface |
||
| 420 | { |
||
| 421 | if( !empty( $prodIds ) && ( $ids = array_unique( $this->validateIds( (array) $prodIds ) ) ) !== [] ) { |
||
| 422 | $this->addExpression( $this->filter->compare( '==', 'product.id', $ids ) ); |
||
| 423 | } |
||
| 424 | |||
| 425 | return $this; |
||
| 426 | } |
||
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * Adds a filter to return only items containing the property |
||
| 431 | * |
||
| 432 | * @param string $type Type code of the property, e.g. "isbn" |
||
| 433 | * @param string|null $value Exact value of the property |
||
| 434 | * @param string|null $langid ISO country code (en or en_US) or null if not language specific |
||
| 435 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 436 | * @since 2019.04 |
||
| 437 | */ |
||
| 438 | public function property( string $type, ?string $value = null, ?string $langid = null ) : Iface |
||
| 443 | } |
||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * Adds radius restrictions for filtering |
||
| 448 | * |
||
| 449 | * @param array $latlon Latitude and longitude value or empty for no restrictions |
||
| 450 | * @param float|null $dist Distance around latitude/longitude or NULL for no restrictions |
||
| 451 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 452 | * @since 2021.10 |
||
| 453 | */ |
||
| 454 | public function radius( array $latlon, ?float $dist = null ) : \Aimeos\Controller\Frontend\Product\Iface |
||
| 463 | } |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * Returns the product for the given product URL name |
||
| 468 | * |
||
| 469 | * @param string $name Product URL name |
||
| 470 | * @return \Aimeos\MShop\Product\Item\Iface Product item including the referenced domains items |
||
| 471 | * @since 2019.04 |
||
| 472 | */ |
||
| 473 | public function resolve( string $name ) : \Aimeos\MShop\Product\Item\Iface |
||
| 474 | { |
||
| 475 | $search = $this->manager->filter( null ); |
||
| 476 | $func = $search->make( 'index.text:url', [$this->context()->locale()->getLanguageId()] ); |
||
| 477 | $search->add( $func, '==', $name )->slice( 0, 1 ); |
||
| 478 | |||
| 479 | if( ( $item = $this->manager->search( $search, $this->domains )->first() ) === null ) |
||
| 480 | { |
||
| 481 | $msg = $this->context()->translate( 'controller/frontend', 'Unable to find product "%1$s"' ); |
||
| 482 | throw new \Aimeos\Controller\Frontend\Product\Exception( sprintf( $msg, $name ), 404 ); |
||
| 483 | } |
||
| 484 | |||
| 485 | return \Aimeos\MShop::create( $this->context(), 'rule' )->apply( $item, 'catalog' ); |
||
| 486 | } |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * Returns the products filtered by the previously assigned conditions |
||
| 491 | * |
||
| 492 | * @param int &$total Parameter where the total number of found products will be stored in |
||
| 493 | * @return \Aimeos\Map Ordered list of product items implementing \Aimeos\MShop\Product\Item\Iface |
||
| 494 | * @since 2019.04 |
||
| 495 | */ |
||
| 496 | public function search( ?int &$total = null ) : \Aimeos\Map |
||
| 497 | { |
||
| 498 | $filter = clone $this->filter; |
||
| 499 | |||
| 500 | /** controller/frontend/common/max-size |
||
| 501 | * Maximum number of items that can be fetched at once |
||
| 502 | * |
||
| 503 | * This setting limits the number of items that is returned to the frontend. |
||
| 504 | * The frontend can request any number of items up to that hard limit to |
||
| 505 | * prevent denial of service attacks by requesting large amount of data. |
||
| 506 | * |
||
| 507 | * @param int Number of items |
||
| 508 | */ |
||
| 509 | $maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 ); |
||
| 510 | $filter->slice( $filter->getOffset(), min( $filter->getLimit(), $maxsize ) ); |
||
| 511 | |||
| 512 | $filter->setSortations( $this->getSortations() ); |
||
| 513 | $filter->add( $filter->and( $this->getConditions() ) ); |
||
| 514 | |||
| 515 | $items = $this->manager->search( $filter, $this->domains, $total ); |
||
| 516 | return \Aimeos\MShop::create( $this->context(), 'rule' )->apply( $items, 'catalog' ); |
||
| 517 | } |
||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * Sets the start value and the number of returned products for slicing the list of found products |
||
| 522 | * |
||
| 523 | * @param int $start Start value of the first product in the list |
||
| 524 | * @param int $limit Number of returned products |
||
| 525 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 526 | * @since 2019.04 |
||
| 527 | */ |
||
| 528 | public function slice( int $start, int $limit ) : Iface |
||
| 532 | } |
||
| 533 | |||
| 534 | |||
| 535 | /** |
||
| 536 | * Sets the sorting of the result list |
||
| 537 | * |
||
| 538 | * @param string|null $key Sorting of the result list like "name", "-name", "price", "-price", "code", "-code", |
||
| 539 | * "ctime, "-ctime", "relevance" or comma separated combinations and null for no sorting |
||
| 540 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 541 | * @since 2019.04 |
||
| 542 | */ |
||
| 543 | public function sort( ?string $key = null ) : Iface |
||
| 544 | { |
||
| 545 | $list = $this->splitKeys( $key ); |
||
| 546 | |||
| 547 | foreach( $list as $sortkey ) |
||
| 548 | { |
||
| 549 | $direction = ( $sortkey[0] === '-' ? '-' : '+' ); |
||
| 550 | $sortkey = ltrim( $sortkey, '+-' ); |
||
| 551 | |||
| 552 | switch( $sortkey ) |
||
| 553 | { |
||
| 554 | case 'relevance': |
||
| 555 | break; |
||
| 556 | |||
| 557 | case 'code': |
||
| 558 | $this->addExpression( $this->filter->sort( $direction, 'product.code' ) ); |
||
| 559 | break; |
||
| 560 | |||
| 561 | case 'ctime': |
||
| 562 | $this->addExpression( $this->filter->sort( $direction, 'product.ctime' ) ); |
||
| 563 | break; |
||
| 564 | |||
| 565 | case 'name': |
||
| 566 | $langid = $this->context()->locale()->getLanguageId(); |
||
| 567 | |||
| 568 | $cmpfunc = $this->filter->make( 'index.text:name', [$langid] ); |
||
| 569 | $this->addExpression( $this->filter->compare( '!=', $cmpfunc, null ) ); |
||
| 570 | |||
| 571 | $sortfunc = $this->filter->make( 'sort:index.text:name', [$langid] ); |
||
| 572 | $this->addExpression( $this->filter->sort( $direction, $sortfunc ) ); |
||
| 573 | break; |
||
| 574 | |||
| 575 | case 'price': |
||
| 576 | $currencyid = $this->context()->locale()->getCurrencyId(); |
||
| 577 | $sortfunc = $this->filter->make( 'sort:index.price:value', [$currencyid] ); |
||
| 578 | |||
| 579 | $cmpfunc = $this->filter->make( 'index.price:value', [$currencyid] ); |
||
| 580 | $this->addExpression( $this->filter->compare( '!=', $cmpfunc, null ) ); |
||
| 581 | |||
| 582 | $this->addExpression( $this->filter->sort( $direction, $sortfunc ) ); |
||
| 583 | break; |
||
| 584 | |||
| 585 | case 'rating': |
||
| 586 | $this->addExpression( $this->filter->sort( $direction, 'product.rating' ) ); |
||
| 587 | break; |
||
| 588 | |||
| 589 | case 'start': |
||
| 590 | $this->addExpression( $this->filter->sort( $direction, 'product.datestart' ) ); |
||
| 591 | break; |
||
| 592 | |||
| 593 | default: |
||
| 594 | $this->addExpression( $this->filter->sort( $direction, $sortkey ) ); |
||
| 595 | } |
||
| 596 | } |
||
| 597 | |||
| 598 | return $this; |
||
| 599 | } |
||
| 600 | |||
| 601 | |||
| 602 | /** |
||
| 603 | * Adds supplier IDs for filtering |
||
| 604 | * |
||
| 605 | * @param array|string $supIds Supplier ID or list of IDs |
||
| 606 | * @param string $listtype List type of the products referenced by the suppliers |
||
| 607 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 608 | * @since 2019.04 |
||
| 609 | */ |
||
| 610 | public function supplier( $supIds, string $listtype = 'default' ) : Iface |
||
| 611 | { |
||
| 612 | if( !empty( $supIds ) && ( $ids = array_unique( $this->validateIds( (array) $supIds ) ) ) !== [] ) |
||
| 613 | { |
||
| 614 | $func = $this->filter->make( 'index.supplier:position', [$listtype, $ids] ); |
||
| 615 | |||
| 616 | $this->addExpression( $this->filter->compare( '==', 'index.supplier.id', $ids ) ); |
||
| 617 | $this->addExpression( $this->filter->compare( '!=', $func, null ) ); |
||
| 618 | |||
| 619 | $func = $this->filter->make( 'sort:index.supplier:position', [$listtype, $ids] ); |
||
| 620 | $this->addExpression( $this->filter->sort( '+', $func ) ); |
||
| 621 | } |
||
| 622 | |||
| 623 | return $this; |
||
| 624 | } |
||
| 625 | |||
| 626 | |||
| 627 | /** |
||
| 628 | * Adds input string for full text search |
||
| 629 | * |
||
| 630 | * @param string|null $text User input for full text search |
||
| 631 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 632 | * @since 2019.04 |
||
| 633 | */ |
||
| 634 | public function text( ?string $text = null ) : Iface |
||
| 635 | { |
||
| 636 | if( !empty( $text ) ) |
||
| 637 | { |
||
| 638 | $langid = $this->context()->locale()->getLanguageId(); |
||
| 639 | $func = $this->filter->make( 'index.text:relevance', [$langid, $text] ); |
||
| 640 | $sortfunc = $this->filter->make( 'sort:index.text:relevance', [$langid, $text] ); |
||
| 641 | |||
| 642 | $this->addExpression( $this->filter->or( [ |
||
| 643 | $this->filter->compare( '>', $func, 0 ), |
||
| 644 | $this->filter->compare( '=~', 'product.code', $text ), |
||
| 645 | ] ) ); |
||
| 646 | $this->addExpression( $this->filter->sort( '-', $sortfunc ) ); |
||
| 647 | } |
||
| 648 | |||
| 649 | return $this; |
||
| 650 | } |
||
| 651 | |||
| 652 | |||
| 653 | /** |
||
| 654 | * Sets the referenced domains that will be fetched too when retrieving items |
||
| 655 | * |
||
| 656 | * @param array $domains Domain names of the referenced items that should be fetched too |
||
| 657 | * @return \Aimeos\Controller\Frontend\Product\Iface Product controller for fluent interface |
||
| 658 | * @since 2019.04 |
||
| 659 | */ |
||
| 660 | public function uses( array $domains ) : Iface |
||
| 661 | { |
||
| 662 | $this->domains = $domains; |
||
| 663 | return $this; |
||
| 664 | } |
||
| 665 | |||
| 666 | |||
| 667 | /** |
||
| 668 | * Returns the list of catalog IDs for the given catalog tree |
||
| 669 | * |
||
| 670 | * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item with children |
||
| 671 | * @return array List of catalog IDs |
||
| 672 | */ |
||
| 673 | protected function getCatalogIdsFromTree( \Aimeos\MShop\Catalog\Item\Iface $item ) : array |
||
| 674 | { |
||
| 675 | if( $item->getStatus() < 1 ) { |
||
| 676 | return []; |
||
| 677 | } |
||
| 678 | |||
| 679 | $list = [$item->getId()]; |
||
| 680 | |||
| 681 | foreach( $item->getChildren() as $child ) { |
||
| 682 | $list = array_merge( $list, $this->getCatalogIdsFromTree( $child ) ); |
||
| 683 | } |
||
| 684 | |||
| 685 | return $list; |
||
| 686 | } |
||
| 687 | |||
| 688 | |||
| 689 | /** |
||
| 690 | * Returns the manager used by the controller |
||
| 691 | * |
||
| 692 | * @return \Aimeos\MShop\Common\Manager\Iface Manager object |
||
| 693 | */ |
||
| 694 | protected function getManager() : \Aimeos\MShop\Common\Manager\Iface |
||
| 695 | { |
||
| 696 | return $this->manager; |
||
| 697 | } |
||
| 698 | |||
| 699 | |||
| 700 | /** |
||
| 701 | * Validates the given IDs as integers |
||
| 702 | * |
||
| 703 | * @param array $ids List of IDs to validate |
||
| 704 | * @return array List of validated IDs |
||
| 705 | */ |
||
| 706 | protected function validateIds( array $ids ) : array |
||
| 720 | } |
||
| 721 | } |
||
| 722 |