Skip to content

Commit

Permalink
Implements :has and :prop search function with optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Feb 4, 2019
1 parent f11a29f commit 41b8444
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
34 changes: 27 additions & 7 deletions lib/custom/src/MShop/Customer/Manager/FosUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ class FosUser
'code' => 'customer:has()',
'internalcode' => '(
SELECT fosli_has."id" FROM fos_user_list AS fosli_has
WHERE fos."id" = fosli_has."parentid" AND :site
AND fosli_has."domain" = $1 AND fosli_has."type" = $2 AND fosli_has."refid" = $3
WHERE fos."id" = fosli_has."parentid" AND :site AND fosli_has."domain" = $1 :type :refid
LIMIT 1
)',
'label' => 'Customer has list item, parameter(<domain>,<list type>,<reference ID>)',
'label' => 'Customer has list item, parameter(<domain>[,<list type>[,<reference ID>)]]',
'type' => 'null',
'internaltype' => 'null',
'public' => false,
Expand All @@ -249,11 +249,10 @@ class FosUser
'code' => 'customer:prop()',
'internalcode' => '(
SELECT fospr_prop."id" FROM fos_user_property AS fospr_prop
WHERE fos."id" = fospr_prop."parentid" AND :site
AND fospr_prop."type" = $1 AND fospr_prop."value" = $3
AND ( fospr_prop."langid" = $2 OR fospr_prop."langid" IS NULL )
WHERE fos."id" = fospr_prop."parentid" AND :site AND fospr_prop."type" = $1 :langid :value
LIMIT 1
)',
'label' => 'Customer has property item, parameter(<property type>,<language code>,<property value>)',
'label' => 'Customer has property item, parameter(<property type>[,<language code>[,<property value>]])',
'type' => 'null',
'internaltype' => 'null',
'public' => false,
Expand Down Expand Up @@ -286,6 +285,27 @@ public function __construct( \Aimeos\MShop\Context\Item\Iface $context )

$this->replaceSiteMarker( $this->searchConfig['customer:has'], 'fosli_has."siteid"', $siteIds, ':site' );
$this->replaceSiteMarker( $this->searchConfig['customer:prop'], 'fospr_prop."siteid"', $siteIds, ':site' );


$this->searchConfig['customer:has']['function'] = function( &$source, array $params ) {

$source = str_replace( ':type', isset( $params[1] ) ? 'AND fosli_has."type" = $2' : '', $source );
$source = str_replace( ':refid', isset( $params[2] ) ? 'AND fosli_has."refid" = $3' : '', $source );

return $params;
};


$this->searchConfig['customer:prop']['function'] = function( &$source, array $params ) {

$lang = 'AND fospr_prop."langid"';
$lang = isset( $params[1] ) ? ( $params[1] !== 'null' ? $lang . ' = $2' : $lang . ' IS NULL' ) : '';

$source = str_replace( ':langid', $lang, $source );
$source = str_replace( ':value', isset( $params[2] ) ? 'AND fospr_prop."value" = $3' : '', $source );

return $params;
};
}


Expand Down
42 changes: 16 additions & 26 deletions lib/custom/tests/MShop/Customer/Manager/FosUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,30 @@ public function testSearchItems()
$expr[] = $search->compare( '==', 'customer.longitude', '10.0' );
$expr[] = $search->compare( '==', 'customer.latitude', '50.0' );

$param = ['text','default', 0];
$expr[] = $search->compare( '==', $search->createFunction( 'customer:has', $param ), null );

$param = ['text','default', $listItem->getRefId()];
$expr[] = $search->compare( '!=', $search->createFunction( 'customer:has', $param ), null );

$param = ['text','default', 0];
$expr[] = $search->compare( '==', $search->createFunction( 'customer:has', $param ), null );
$param = ['text','default'];
$expr[] = $search->compare( '!=', $search->createFunction( 'customer:has', $param ), null );

$param = ['newsletter', null, '1'];
$expr[] = $search->compare( '!=', $search->createFunction( 'customer:prop', $param ), null );
$param = ['text'];
$expr[] = $search->compare( '!=', $search->createFunction( 'customer:has', $param ), null );

$param = ['newsletter', null, '0'];
$expr[] = $search->compare( '==', $search->createFunction( 'customer:prop', $param ), null );

$param = ['newsletter', null, '1'];
$expr[] = $search->compare( '!=', $search->createFunction( 'customer:prop', $param ), null );

$param = ['newsletter', null];
$expr[] = $search->compare( '!=', $search->createFunction( 'customer:prop', $param ), null );

$param = ['newsletter'];
$expr[] = $search->compare( '!=', $search->createFunction( 'customer:prop', $param ), null );

$expr[] = $search->compare( '!=', 'customer.address.id', null );
$expr[] = $search->compare( '!=', 'customer.address.parentid', null );
$expr[] = $search->compare( '==', 'customer.address.salutation', 'mr' );
Expand Down Expand Up @@ -259,28 +271,6 @@ public function testSearchItems()
$expr[] = $search->compare( '>=', 'customer.address.ctime', '1970-01-01 00:00:00' );
$expr[] = $search->compare( '==', 'customer.address.editor', $this->editor );

$expr[] = $search->compare( '!=', 'customer.lists.id', null );
$expr[] = $search->compare( '!=', 'customer.lists.siteid', null );
$expr[] = $search->compare( '!=', 'customer.lists.parentid', null );
$expr[] = $search->compare( '==', 'customer.lists.domain', 'text' );
$expr[] = $search->compare( '==', 'customer.lists.type', 'default' );
$expr[] = $search->compare( '>', 'customer.lists.refid', '' );
$expr[] = $search->compare( '==', 'customer.lists.datestart', '2010-01-01 00:00:00' );
$expr[] = $search->compare( '==', 'customer.lists.dateend', '2022-01-01 00:00:00' );
$expr[] = $search->compare( '!=', 'customer.lists.config', null );
$expr[] = $search->compare( '==', 'customer.lists.position', 0 );
$expr[] = $search->compare( '==', 'customer.lists.status', 1 );
$expr[] = $search->compare( '>=', 'customer.lists.mtime', '1970-01-01 00:00:00' );
$expr[] = $search->compare( '>=', 'customer.lists.ctime', '1970-01-01 00:00:00' );
$expr[] = $search->compare( '==', 'customer.lists.editor', $this->editor );

$expr[] = $search->compare( '!=', 'customer.property.id', null );
$expr[] = $search->compare( '!=', 'customer.property.siteid', null );
$expr[] = $search->compare( '==', 'customer.property.type', 'newsletter' );
$expr[] = $search->compare( '==', 'customer.property.languageid', null );
$expr[] = $search->compare( '==', 'customer.property.value', '1' );
$expr[] = $search->compare( '==', 'customer.property.editor', $this->editor );

$search->setConditions( $search->combine( '&&', $expr ) );
$result = $this->object->searchItems( $search );
$this->assertEquals( 1, count( $result ) );
Expand Down

0 comments on commit 41b8444

Please sign in to comment.