Skip to content

Commit

Permalink
Update develop with develop-1.x (#999)
Browse files Browse the repository at this point in the history
* Update B2B / Catalog Permissions raw SQL  (#977)

* update bundle product collection for subproducts (#982)

* add adminhtml event for flush catalog image cache (#983)
  • Loading branch information
bsuravech authored Apr 15, 2020
1 parent 53dadcc commit c4e060b
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 48 deletions.
25 changes: 12 additions & 13 deletions Factory/CatalogPermissionsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public function getCategoryPermissionsCollection()
$indexResource = $this->getPermissionsIndexResource();
$connection = $indexResource->getConnection();

$query = "
SELECT category_id, GROUP_CONCAT(CONCAT(customer_group_id, '_', grant_catalog_category_view) SEPARATOR ',') AS permissions
FROM {$indexResource->getMainTable()}
GROUP BY category_id;
";
$select = $connection->select()
->from($indexResource->getMainTable(), [])
->columns('category_id')
->columns(['permissions' => new \Zend_Db_Expr("GROUP_CONCAT(CONCAT(customer_group_id, '_', grant_catalog_category_view) SEPARATOR ',')")])
->group('category_id');

$this->categoryPermissionsCollection = $connection->fetchPairs($query);
$this->categoryPermissionsCollection = $connection->fetchPairs($select);
}

return $this->categoryPermissionsCollection;
Expand All @@ -77,14 +77,13 @@ public function getProductPermissionsCollection()
$indexResource = $this->getPermissionsIndexResource();
$connection = $indexResource->getConnection();

$query = "
SELECT product_id,
GROUP_CONCAT(CONCAT(store_id, '_', customer_group_id, '_', grant_catalog_category_view) SEPARATOR ', ') AS permissions
FROM {$indexResource->getTable([$indexResource->getMainTable(), 'product'])}
GROUP BY product_id;
";
$select = $connection->select()
->from($indexResource->getTable([$indexResource->getMainTable(), 'product']), [])
->columns('product_id')
->columns(['permissions' => new \Zend_Db_Expr("GROUP_CONCAT(CONCAT(store_id, '_', customer_group_id, '_', grant_catalog_category_view) SEPARATOR ', ')")])
->group('product_id');

$this->productPermissionsCollection = $connection->fetchPairs($query);
$this->productPermissionsCollection = $connection->fetchPairs($select);
}

return $this->productPermissionsCollection;
Expand Down
52 changes: 34 additions & 18 deletions Factory/SharedCatalogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,31 @@ public function getSharedCatalogConfig()
return $this->objectManager->create('\Magento\SharedCatalog\Model\Config');
}

private function getSharedCatalogCustomerGroups()
{
$sharedCatalogResource = $this->getSharedCatalogResource();
$connection = $sharedCatalogResource->getConnection();

$select = $connection->select()
->from(($sharedCatalogResource->getMainTable()), ['customer_group_id']);

return $connection->fetchAll($select);
}

public function getSharedCategoryCollection()
{
if (!$this->sharedCategoryCollection) {
$indexResource = $this->getSharedCatalogCategoryResource();
$connection = $indexResource->getConnection();

$query = "
SELECT category_id, GROUP_CONCAT(CONCAT(customer_group_id, '_', permission) SEPARATOR ',') AS permissions
FROM {$indexResource->getMainTable()}
WHERE customer_group_id IN (SELECT customer_group_id FROM shared_catalog)
GROUP BY category_id;
";
$select = $connection->select()
->from($indexResource->getMainTable(), [])
->columns('category_id')
->columns(['permissions' => new \Zend_Db_Expr("GROUP_CONCAT(CONCAT(customer_group_id, '_', permission) SEPARATOR ',')")])
->where('customer_group_id IN (?)', $this->getSharedCatalogCustomerGroups())
->group('category_id');

$this->sharedCategoryCollection = $connection->fetchPairs($query);
$this->sharedCategoryCollection = $connection->fetchPairs($select);
}

return $this->sharedCategoryCollection;
Expand All @@ -85,17 +96,22 @@ public function getSharedProductItemCollection()
$indexResource = $this->getSharedCatalogProductItemResource();
$connection = $indexResource->getConnection();

$query = "
SELECT cpe.entity_id, GROUP_CONCAT(pi.customer_group_id SEPARATOR ',') as groups
FROM {$indexResource->getMainTable()} as pi
INNER JOIN {$this->getSharedCatalogResource()->getMainTable()} AS sc
ON sc.customer_group_id = pi.customer_group_id
LEFT JOIN {$indexResource->getTable('catalog_product_entity')} AS cpe
ON pi.sku = cpe.sku
GROUP BY pi.sku
";

$productItems = $connection->fetchPairs($query);
$select = $connection->select()
->from(['pi' => $indexResource->getMainTable()], [])
->columns('cpe.entity_id')
->columns(['groups' => new \Zend_Db_Expr("GROUP_CONCAT(pi.customer_group_id SEPARATOR ',')")])
->joinInner(
['sc' => $this->getSharedCatalogResource()->getMainTable()],
'sc.customer_group_id = pi.customer_group_id',
[]
)
->joinLeft(
['cpe' => $indexResource->getTable('catalog_product_entity')],
'pi.sku = cpe.sku',
[]
)->group('pi.sku');

$productItems = $connection->fetchPairs($select);
$groups = $this->getSharedCatalogGroups();

foreach ($productItems as $productId => $permissions) {
Expand Down
2 changes: 1 addition & 1 deletion Helper/Entity/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ private function getSubProducts(Product $product)
if ($typeInstance instanceof Configurable) {
$subProducts = $typeInstance->getUsedProducts($product);
} elseif ($typeInstance instanceof BundleProductType) {
$subProducts = $typeInstance->getOptions($product);
$subProducts = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product);
} else { // Grouped product
$subProducts = $typeInstance->getAssociatedProducts($product);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public function __construct(
public function execute(Observer $observer)
{
$storeId = $this->storeManager->getStore()->getId();
if (!$this->permissionsFactory->isCatalogPermissionsEnabled($storeId)
|| ($this->permissionsFactory->getCatalogPermissionsHelper()->isAllowedCategoryView($storeId)
&& !$this->sharedCatalogFactory->isSharedCatalogEnabled($storeId))
) {

// shared catalog is dependant on catalog permissions settings,
// should only check against catalog permissions
if (!$this->permissionsFactory->isCatalogPermissionsEnabled($storeId)) {
return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ protected function addCatalogPermissionsData($collection, $categoryIds)
}
list($customerGroupId, $level) = $permission;
if ($category = $collection->getItemById($categoryId)) {
$category->setData('customer_group_permission_' . $customerGroupId, (($level == -2 || $level != -1
&& !$catalogPermissionsHelper->isAllowedCategoryView()) ? 0 : 1));
$category->setData(
'customer_group_permission_' . $customerGroupId,
$level == -2 ? 0 : 1
);
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions Model/Observer/CatalogPermissions/CategoryPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ public function execute(Observer $observer)

foreach ($collection as $customerGroup) {
$customerGroupId = $customerGroup->getCustomerGroupId();
$permissions['customer_group_' . $customerGroupId] =
!is_null($category->getData('shared_catalog_permission_' . $customerGroupId))
? (int) $category->getData('shared_catalog_permission_' . $customerGroupId)
: (int) $category->getData('customer_group_permission_' . $customerGroupId);

$isVisible = (int) $this->permissionsFactory->getCatalogPermissionsHelper()->isAllowedCategoryView($storeId, $customerGroupId);
if (!is_null($category->getData('shared_catalog_permission_' . $customerGroupId))) {
$isVisible = (int) $category->getData('shared_catalog_permission_' . $customerGroupId);
} elseif (!is_null($category->getData('customer_group_permission_' . $customerGroupId))) {
$isVisible = (int) $category->getData('customer_group_permission_' . $customerGroupId);
}

$permissions['customer_group_' . $customerGroupId] = $isVisible;
}

$transport->setData('catalog_permissions', $permissions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ protected function addProductPermissionsData($additionalData, $productIds, $stor
list($permissionStoreId, $customerGroupId, $level) = $permission;
if ($permissionStoreId == $storeId) {
$additionalData->addProductData($productId, [
'customer_group_permission_' . $customerGroupId => (($level == -2 || $level != -1
&& !$catalogPermissionsHelper->isAllowedCategoryView()) ? 0 : 1),
'customer_group_permission_' . $customerGroupId => ($level == -2 ? 0 : 1),
]);
}
}
Expand Down
12 changes: 8 additions & 4 deletions Model/Observer/CatalogPermissions/ProductPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ public function execute(Observer $observer)
foreach ($collection as $customerGroup) {
$customerGroupId = $customerGroup->getCustomerGroupId();

$permissions['customer_group_' . $customerGroupId] =
!is_null($product->getData('customer_group_permission_' . $customerGroupId))
? (int) $product->getData('customer_group_permission_' . $customerGroupId)
: (int) $product->getData('shared_catalog_permission_' . $customerGroupId);
$isVisible = (int) $this->permissionsFactory->getCatalogPermissionsHelper()->isAllowedCategoryView($storeId, $customerGroupId);
if (!is_null($product->getData('shared_catalog_permission_' . $customerGroupId))) {
$isVisible = (int) $product->getData('shared_catalog_permission_' . $customerGroupId);
} elseif (!is_null($product->getData('customer_group_permission_' . $customerGroupId))) {
$isVisible = (int) $product->getData('customer_group_permission_' . $customerGroupId);
}

$permissions['customer_group_' . $customerGroupId] = $isVisible;
}

if (count($permissions)) {
Expand Down
43 changes: 43 additions & 0 deletions Model/Observer/CleanCatalogImagesCacheAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Algolia\AlgoliaSearch\Model\Observer;

use Algolia\AlgoliaSearch\Helper\ConfigHelper;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface;

class CleanCatalogImagesCacheAfter implements ObserverInterface
{
/** @var ConfigHelper */
private $configHelper;

/** @var ManagerInterface */
private $messageManager;

/**
* @param ConfigHelper $configHelper
* @param ManagerInterface $messageManager
*/
public function __construct(
ConfigHelper $configHelper,
ManagerInterface $messageManager
) {
$this->configHelper = $configHelper;
$this->messageManager = $messageManager;
}

/**
* Add Notice for Product Reindexing after image flush
*
* @param Observer $observer
*/
public function execute(Observer $observer)
{
$this->messageManager->addWarningMessage(__('
Algolia Warning: The image cache has been cleared.
All indexed image links will become invalid because the file will be nonexistent.
Please run a full reindex of your catalog data to resolve broken images in your Algolia Search.
'));
}
}
5 changes: 5 additions & 0 deletions etc/adminhtml/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@
<event name="algolia_after_create_category_object">
<observer name="algoliasearch_category_permissions" instance="Algolia\AlgoliaSearch\Model\Observer\CatalogPermissions\CategoryPermissions" />
</event>

<event name="clean_catalog_images_cache_after">
<observer name="algoliasearch_flush_catalog_image_cache_after" instance="Algolia\AlgoliaSearch\Model\Observer\CleanCatalogImagesCacheAfter" />
</event>

</config>

0 comments on commit c4e060b

Please sign in to comment.