Skip to content

Commit

Permalink
Simplified methods for adding product filters
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Nov 28, 2018
1 parent eb1cb87 commit a47bac1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,14 @@ public function addFilterAttribute( \Aimeos\MW\Criteria\Iface $filter, array $at
* @param \Aimeos\MW\Criteria\Iface $filter Criteria object used for product search
* @param string|array $catId Selected category by the user
* @param integer $level Constant for current category only, categories of next level (LEVEL_LIST) or whole subtree (LEVEL_SUBTREE)
* @param string|null $sort Sortation of the product list like "name", "code", "price" and "position", null for no sortation
* @param string $direction Sort direction of the product list ("+", "-")
* @param string $listtype List type of the product associated to the category, usually "default"
* @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
* @since 2017.03
*/
public function addFilterCategory( \Aimeos\MW\Criteria\Iface $filter, $catId,
$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE, $sort = null, $direction = '+', $listtype = 'default' )
$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE, $listtype = 'default' )
{
return $this->controller->addFilterCategory( $filter, $catId, $level, $sort, $direction, $listtype );
return $this->controller->addFilterCategory( $filter, $catId, $level, $listtype );
}


Expand All @@ -108,15 +106,12 @@ public function addFilterSupplier( \Aimeos\MW\Criteria\Iface $filter, array $sup
*
* @param \Aimeos\MW\Criteria\Iface $filter Criteria object used for product search
* @param string $input Search string entered by the user
* @param string|null $sort Sortation of the product list like "name", "code", "price" and "position", null for no sortation
* @param string $direction Sort direction of the product list ("+", "-")
* @param string $listtype List type of the text associated to the product, usually "default"
* @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
* @since 2017.03
*/
public function addFilterText( \Aimeos\MW\Criteria\Iface $filter, $input, $sort = null, $direction = '+', $listtype = 'default' )
public function addFilterText( \Aimeos\MW\Criteria\Iface $filter, $input )
{
return $this->controller->addFilterText( $filter, $input, $sort, $direction, $listtype );
return $this->controller->addFilterText( $filter, $input );
}


Expand All @@ -141,13 +136,12 @@ public function aggregate( \Aimeos\MW\Criteria\Iface $filter, $key )
* @param string $direction Sort direction of the product list ("+", "-")
* @param integer $start Position in the list of found products where to begin retrieving the items
* @param integer $size Number of products that should be returned
* @param string $listtype Type of the product list, e.g. default, promotion, etc.
* @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
* @since 2015.08
*/
public function createFilter( $sort = null, $direction = '+', $start = 0, $size = 100, $listtype = 'default' )
public function createFilter( $sort = null, $direction = '+', $start = 0, $size = 100 )
{
return $this->controller->createFilter( $sort, $direction, $start, $size, $listtype );
return $this->controller->createFilter( $sort, $direction, $start, $size );
}


Expand Down
12 changes: 3 additions & 9 deletions controller/frontend/src/Controller/Frontend/Product/Iface.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ public function addFilterAttribute( \Aimeos\MW\Criteria\Iface $filter, array $at
* @param \Aimeos\MW\Criteria\Iface $filter Criteria object used for product search
* @param string|array $catId Selected category by the user
* @param integer $level Constant for current category only, categories of next level (LEVEL_LIST) or whole subtree (LEVEL_SUBTREE)
* @param string|null $sort Sortation of the product list like "name", "code", "price" and "position", null for no sortation
* @param string $direction Sort direction of the product list ("+", "-")
* @param string $listtype List type of the product associated to the category, usually "default"
* @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
* @since 2017.03
*/
public function addFilterCategory( \Aimeos\MW\Criteria\Iface $filter, $catId,
$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE, $sort = null, $direction = '+', $listtype = 'default' );
$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE, $listtype = 'default' );


/**
Expand All @@ -65,13 +63,10 @@ public function addFilterSupplier( \Aimeos\MW\Criteria\Iface $filter, array $sup
*
* @param \Aimeos\MW\Criteria\Iface $filter Criteria object used for product search
* @param string $input Search string entered by the user
* @param string|null $sort Sortation of the product list like "name", "code", "price" and "position", null for no sortation
* @param string $direction Sort direction of the product list ("+", "-")
* @param string $listtype List type of the text associated to the product, usually "default"
* @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
* @since 2017.03
*/
public function addFilterText( \Aimeos\MW\Criteria\Iface $filter, $input, $sort = null, $direction = '+', $listtype = 'default' );
public function addFilterText( \Aimeos\MW\Criteria\Iface $filter, $input );


/**
Expand All @@ -92,11 +87,10 @@ public function aggregate( \Aimeos\MW\Criteria\Iface $filter, $key );
* @param string $direction Sort direction of the product list ("+", "-")
* @param integer $start Position in the list of found products where to begin retrieving the items
* @param integer $size Number of products that should be returned
* @param string $listtype Type of the product list, e.g. default, promotion, etc.
* @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
* @since 2017.03
*/
public function createFilter( $sort = null, $direction = '+', $start = 0, $size = 100, $listtype = 'default' );
public function createFilter( $sort = null, $direction = '+', $start = 0, $size = 100 );


/**
Expand Down
23 changes: 10 additions & 13 deletions controller/frontend/src/Controller/Frontend/Product/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,12 @@ public function addFilterAttribute( \Aimeos\MW\Criteria\Iface $filter, array $at
* @param \Aimeos\MW\Criteria\Iface $filter Criteria object used for product search
* @param string|array $catId Selected category by the user
* @param integer $level Constant for current category only, categories of next level (LEVEL_LIST) or whole subtree (LEVEL_SUBTREE)
* @param string|null $sort Sortation of the product list like "name", "code", "price" and "position", null for no sortation
* @param string $direction Sort direction of the product list ("+", "-")
* @param string $listtype List type of the product associated to the category, usually "default"
* @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
* @since 2017.03
*/
public function addFilterCategory( \Aimeos\MW\Criteria\Iface $filter, $catId,
$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE, $sort = null, $direction = '+', $listtype = 'default' )
$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE, $listtype = 'default' )
{
$catIds = ( !is_array( $catId ) ? explode( ',', $catId ) : $catId );

Expand All @@ -102,16 +100,17 @@ public function addFilterCategory( \Aimeos\MW\Criteria\Iface $filter, $catId,
$expr = array( $filter->compare( '==', 'index.catalog.id', array_unique( $catIds ) ) );
$expr[] = $filter->getConditions();

if( $sort === 'relevance' )
if( isset( $filter->sortname ) && $filter->sortname === 'relevance' )
{
$start = $filter->getSliceStart();
$end = $start + $filter->getSliceSize();
$dir = ( isset( $filter->sortdir ) ? $filter->sortdir : '+' );

$cmpfunc = $filter->createFunction( 'index.catalog:position', array( $listtype, $catIds, $start, $end ) );
$expr[] = $filter->compare( '>=', $cmpfunc, 0 );

$sortfunc = $filter->createFunction( 'sort:index.catalog:position', array( $listtype, $catIds, $start, $end ) );
$filter->setSortations( [$filter->sort( $direction, $sortfunc ), $filter->sort( '+', 'product.id' )] );
$filter->setSortations( [$filter->sort( $dir, $sortfunc ), $filter->sort( '+', 'product.id' )] );
}

$filter->setConditions( $filter->combine( '&&', $expr ) );
Expand Down Expand Up @@ -149,13 +148,10 @@ public function addFilterSupplier( \Aimeos\MW\Criteria\Iface $filter, array $sup
*
* @param \Aimeos\MW\Criteria\Iface $filter Criteria object used for product search
* @param string $input Search string entered by the user
* @param string|null $sort Sortation of the product list like "name", "code", "price" and "position", null for no sortation
* @param string $direction Sort direction of the product list ("+", "-")
* @param string $listtype List type of the text associated to the product, usually "default"
* @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
* @since 2017.03
*/
public function addFilterText( \Aimeos\MW\Criteria\Iface $filter, $input, $sort = null, $direction = '+', $listtype = 'default' )
public function addFilterText( \Aimeos\MW\Criteria\Iface $filter, $input )
{
$langid = $this->getContext()->getLocale()->getLanguageId();
$cmpfunc = $filter->createFunction( 'index.text:relevance', [$langid, $input] );
Expand Down Expand Up @@ -186,11 +182,10 @@ public function aggregate( \Aimeos\MW\Criteria\Iface $filter, $key )
* @param string $direction Sort direction of the product list ("+", "-")
* @param integer $start Position in the list of found products where to begin retrieving the items
* @param integer $size Number of products that should be returned
* @param string $listtype Type of the product list, e.g. default, promotion, etc.
* @return \Aimeos\MW\Criteria\Iface Criteria object containing the conditions for searching
* @since 2017.03
*/
public function createFilter( $sort = null, $direction = '+', $start = 0, $size = 100, $listtype = 'default' )
public function createFilter( $sort = null, $direction = '+', $start = 0, $size = 100 )
{
$sortations = [];
$context = $this->getContext();
Expand Down Expand Up @@ -245,10 +240,10 @@ public function createFilter( $sort = null, $direction = '+', $start = 0, $size
case 'price':
$currencyid = $context->getLocale()->getCurrencyId();

$cmpfunc = $search->createFunction( 'index.price:value', array( $listtype, $currencyid, 'default' ) );
$cmpfunc = $search->createFunction( 'index.price:value', ['default', $currencyid, 'default'] );
$expr[] = $search->compare( '!=', $cmpfunc, null );

$sortfunc = $search->createFunction( 'sort:index.price:value', array( $listtype, $currencyid, 'default' ) );
$sortfunc = $search->createFunction( 'sort:index.price:value', ['default', $currencyid, 'default'] );
$sortations[] = $search->sort( $direction, $sortfunc );
break;
}
Expand All @@ -258,6 +253,8 @@ public function createFilter( $sort = null, $direction = '+', $start = 0, $size
$search->setConditions( $search->combine( '&&', $expr ) );
$search->setSortations( $sortations );
$search->setSlice( $start, $size );
$search->sortdir = $direction;
$search->sortname = $sort;

return $search;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public function testCreateFilterSortRelevanceCategory()
{
$level = \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE;

$filter = $this->object->createFilter();
$filter = $this->object->addFilterCategory( $filter, 0, $level, 'relevance', '-', 'test' );
$filter = $this->object->createFilter( 'relevance', '-' );
$filter = $this->object->addFilterCategory( $filter, 0, $level, 'test' );

$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );

Expand All @@ -186,7 +186,7 @@ public function testCreateFilterSortRelevanceCategory()

public function testCreateFilterSortRelevanceText()
{
$filter = $this->object->createFilter( 'relevance', '-', 1, 2, 'test' );
$filter = $this->object->createFilter( 'relevance', '-', 1, 2 );
$filter = $this->object->addFilterText( $filter, 'Espresso' );

$this->assertInstanceOf( '\\Aimeos\\MW\\Criteria\\Iface', $filter );
Expand Down Expand Up @@ -311,10 +311,9 @@ public function testGetItems()
public function testSearchItemsCategory()
{
$catalogManager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
$search = $catalogManager->createSearch();

$search = $catalogManager->createSearch()->setSlice( 0, 1 );
$search->setConditions( $search->compare( '==', 'catalog.code', 'new' ) );
$search->setSlice( 0, 1 );
$items = $catalogManager->searchItems( $search );

if( ( $item = reset( $items ) ) === false ) {
Expand All @@ -334,8 +333,8 @@ public function testSearchItemsCategory()

public function testSearchItemsText()
{
$filter = $this->object->createFilter( 'relevance', '+', 0, 1, 'unittype13' );
$filter = $this->object->addFilterText( $filter, 'Expresso', 'relevance', '+', 'unittype13' );
$filter = $this->object->createFilter( 'relevance', '+', 0, 1 );
$filter = $this->object->addFilterText( $filter, 'Expresso' );

$total = 0;
$results = $this->object->searchItems( $filter, [], $total );
Expand Down

0 comments on commit a47bac1

Please sign in to comment.