Skip to content

Commit

Permalink
Implemented getCatalogItems() getSupplierItems() for products
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Oct 30, 2019
1 parent cee24a6 commit c5bab86
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/mshoplib/src/MShop/Catalog/Manager/Lists/Standard.php
Expand Up @@ -127,6 +127,14 @@ class Standard
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
'public' => false,
),
'catalog.lists.key' => array(
'code' => 'catalog.lists.key',
'internalcode' => 'mcatli."key"',
'label' => 'List key',
'type' => 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
'public' => false,
),
);


Expand Down
14 changes: 14 additions & 0 deletions lib/mshoplib/src/MShop/Product/Item/Iface.php
Expand Up @@ -24,6 +24,20 @@ interface Iface
\Aimeos\MShop\Common\Item\Status\Iface, \Aimeos\MShop\Common\Item\Time\Iface,
\Aimeos\MShop\Common\Item\TypeRef\Iface
{
/**
* Returns the catalog items referencing the product
*
* @return \Aimeos\MShop\Catalog\Item\Iface[] Catalog items
*/
public function getCatalogItems();

/**
* Returns the supplier items referencing the product
*
* @return \Aimeos\MShop\Supplier\Item\Iface[] Supplier items
*/
public function getSupplierItems();

/**
* Returns the code of the product item.
*
Expand Down
22 changes: 22 additions & 0 deletions lib/mshoplib/src/MShop/Product/Item/Standard.php
Expand Up @@ -64,6 +64,28 @@ public function __clone()
}


/**
* Returns the catalog items referencing the product
*
* @return \Aimeos\MShop\Catalog\Item\Iface[] Catalog items
*/
public function getCatalogItems()
{
return (array) $this->get( 'catalog', [] );
}


/**
* Returns the supplier items referencing the product
*
* @return \Aimeos\MShop\Supplier\Item\Iface[] Supplier items
*/
public function getSupplierItems()
{
return (array) $this->get( 'supplier', [] );
}


/**
* Returns the type of the product item.
*
Expand Down
73 changes: 73 additions & 0 deletions lib/mshoplib/src/MShop/Product/Manager/Standard.php
Expand Up @@ -789,6 +789,24 @@ public function searchItems( \Aimeos\MW\Criteria\Iface $search, array $ref = [],
$propItems = $this->getPropertyItems( array_keys( $map ), 'product', $propTypes );
}

if( isset( $ref['catalog'] ) || in_array( 'catalog', $ref, true ) )
{
$domains = isset( $ref['catalog'] ) && is_array( $ref['catalog'] ) ? $ref['catalog'] : [];

foreach( $this->getDomainRefItems( array_keys( $map ), 'catalog', $domains ) as $prodId => $list ) {
$map[$prodId]['catalog'] = $list;
}
}

if( isset( $ref['supplier'] ) || in_array( 'supplier', $ref, true ) )
{
$domains = isset( $ref['supplier'] ) && is_array( $ref['supplier'] ) ? $ref['supplier'] : [];

foreach( $this->getDomainRefItems( array_keys( $map ), 'supplier', $domains ) as $prodId => $list ) {
$map[$prodId]['supplier'] = $list;
}
}

return $this->buildItems( $map, $ref, 'product', $propItems );
}

Expand All @@ -809,4 +827,59 @@ protected function createItemBase( array $values = [], array $listItems = [],

return new \Aimeos\MShop\Product\Item\Standard( $values, $listItems, $refItems, $propertyItems );
}


/**
* Returns the associative list of domain items referencing the product
*
* @param array $ids List of product IDs
* @param string $domain Domain name, e.g. "catalog" or "supplier"
* @param array $ref List of referenced items that should be fetched too
* @return array Associative list of product IDs as keys and list of domain items as values
*/
protected function getDomainRefItems( array $ids, string $domain, array $ref )
{
$keys = $map = $result = [];
$context = $this->getContext();

foreach( $ids as $id ) {
$keys[] = 'product|default|' . $id;
}


$manager = \Aimeos\MShop::create( $context, $domain . '/lists' );

$search = $manager->createSearch( true )->setSlice( 0, 0x7fffffff );
$search->setConditions( $search->combine( '&&', [
$search->compare( '==', $domain . '.lists.key', $keys ),
$search->getConditions(),
] ) );

foreach( $manager->searchItems( $search ) as $listItem ) {
$map[$listItem->getParentId()][] = $listItem->getRefId();
}

$manager = \Aimeos\MShop::create( $context, $domain );

$search = $manager->createSearch( true )->setSlice( 0, 0x7fffffff );
$search->setConditions( $search->combine( '&&', [
$search->compare( '==', $domain . '.id', array_keys( $map ) ),
$search->getConditions(),
] ) );

$items = $manager->searchItems( $search, $ref );


foreach( $map as $parentId => $list )
{
if( isset( $items[$parentId] ) )
{
foreach( $list as $prodId ) {
$result[$prodId][$parentId] = $items[$parentId];
}
}
}

return $result;
}
}
8 changes: 8 additions & 0 deletions lib/mshoplib/src/MShop/Supplier/Manager/Lists/Standard.php
Expand Up @@ -126,6 +126,14 @@ class Standard
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
'public' => false,
),
'supplier.lists.key' => array(
'code' => 'supplier.lists.key',
'internalcode' => 'msupli."key"',
'label' => 'List key',
'type' => 'string',
'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_STR,
'public' => false,
),
);


Expand Down
9 changes: 9 additions & 0 deletions lib/mshoplib/tests/MShop/Product/Manager/StandardTest.php
Expand Up @@ -109,6 +109,15 @@ public function testFindItemDomainFilter()
}


public function testFindItemForeignDomains()
{
$item = $this->object->findItem( 'CNE', ['catalog', 'supplier'] );

$this->assertEquals( 1, count( $item->getSupplierItems() ) );
$this->assertEquals( 2, count( $item->getCatalogItems() ) );
}


public function testGetItem()
{
$domains = ['text', 'product', 'price', 'media' => ['unittype10'], 'attribute', 'product/property' => ['package-weight']];
Expand Down

0 comments on commit c5bab86

Please sign in to comment.