Skip to content

Commit

Permalink
Fixing EE bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Galvao da Gama committed Jul 14, 2020
1 parent 500acc3 commit acf425b
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class GetAssetIdsByEavContentField implements GetAssetIdsByContentFieldInterface
*/
private $entityType;

/**
* @var string
*/
private $entityTable;

/**
* @var array
*/
Expand All @@ -50,19 +55,22 @@ class GetAssetIdsByEavContentField implements GetAssetIdsByContentFieldInterface
* @param Config $config
* @param string $attributeCode
* @param string $entityType
* @param string $entityTable
* @param array $valueMap
*/
public function __construct(
ResourceConnection $resource,
Config $config,
string $attributeCode,
string $entityType,
string $entityTable,
array $valueMap = []
) {
$this->connection = $resource;
$this->config = $config;
$this->attributeCode = $attributeCode;
$this->entityType = $entityType;
$this->entityTable = $entityTable;
$this->valueMap = $valueMap;
}

Expand All @@ -80,10 +88,13 @@ public function execute(string $value): array
'entity_type = ?',
$this->entityType
)->joinInner(
['entity_eav_type' => $attribute->getBackendTable()],
'asset_content_table.entity_id = entity_eav_type.' . $attribute->getEntityIdField() .
' AND entity_eav_type.attribute_id = ' .
$attribute->getAttributeId(),
['entity_table' => $this->connection->getTableName($this->entityTable)],
'asset_content_table.entity_id = entity_table.entity_id',
[]
)->joinInner(
['entity_eav_type' => $this->connection->getTableName($attribute->getBackendTable())],
'entity_table.' . $attribute->getEntityIdField() . ' = entity_eav_type.' . $attribute->getEntityIdField() .
' AND entity_eav_type.attribute_id = ' . $attribute->getAttributeId(),
[]
)->where(
'entity_eav_type.value = ?',
Expand Down
2 changes: 2 additions & 0 deletions app/code/Magento/MediaContentCatalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<arguments>
<argument name="attributeCode" xsi:type="string">status</argument>
<argument name="entityType" xsi:type="string">catalog_product</argument>
<argument name="entityTable" xsi:type="string">catalog_product_entity</argument>
<argument name="valueMap" xsi:type="array">
<item name="1" xsi:type="string">1</item>
<item name="0" xsi:type="string">2</item>
Expand All @@ -60,6 +61,7 @@
<arguments>
<argument name="attributeCode" xsi:type="string">is_active</argument>
<argument name="entityType" xsi:type="string">catalog_category</argument>
<argument name="entityTable" xsi:type="string">catalog_category_entity</argument>
</arguments>
</virtualType>
<type name="Magento\MediaContentApi\Model\Composite\GetAssetIdsByContentField">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentCms\Model\ResourceModel;

use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Cms\Api\Data\BlockInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface;

/**
* Class responsible to return Asset id by content field
*/
class GetAssetIdsByBlockStore implements GetAssetIdsByContentFieldInterface
{
private const TABLE_CONTENT_ASSET = 'media_content_asset';
private const ENTITY_TYPE = 'cms_block';
private const STORE_FIELD = 'store_id';

/**
* @var ResourceConnection
*/
private $connection;

/**
* @var BlockRepositoryInterface
*/
private $blockRepository;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* GetAssetIdsByContentField constructor.
*
* @param ResourceConnection $resource
* @param BlockRepositoryInterface $blockRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
ResourceConnection $resource,
BlockRepositoryInterface $blockRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->connection = $resource;
$this->blockRepository = $blockRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* @inheritDoc
*/
public function execute(string $value): array
{
$sql = $this->connection->getConnection()->select()->from(
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
['asset_id']
)->where(
'entity_type = ?',
self::ENTITY_TYPE
)->where(
'entity_id IN (?)',
$this->getBlockIdsByStore((int) $value)
);

return $this->connection->getConnection()->fetchCol($sql);
}

/**
* @param int $storeId
* @return array
* @throws LocalizedException
*/
private function getBlockIdsByStore(int $storeId): array
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(self::STORE_FIELD, $storeId)
->create();

$searchResult = $this->blockRepository->getList($searchCriteria);

return array_map(function (BlockInterface $block) {
return $block->getId();
}, $searchResult->getItems());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentCms\Model\ResourceModel;

use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface;

/**
* Class responsible to return Asset id by content field
*/
class GetAssetIdsByPageStore implements GetAssetIdsByContentFieldInterface
{
private const TABLE_CONTENT_ASSET = 'media_content_asset';
private const ENTITY_TYPE = 'cms_page';
private const STORE_FIELD = 'store_id';

/**
* @var ResourceConnection
*/
private $connection;

/**
* @var PageRepositoryInterface
*/
private $pageRepository;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* GetAssetIdsByContentField constructor.
*
* @param ResourceConnection $resource
* @param PageRepositoryInterface $pageRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
ResourceConnection $resource,
PageRepositoryInterface $pageRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->connection = $resource;
$this->pageRepository = $pageRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* @inheritDoc
*/
public function execute(string $value): array
{
$sql = $this->connection->getConnection()->select()->from(
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
['asset_id']
)->where(
'entity_type = ?',
self::ENTITY_TYPE
)->where(
'entity_id IN (?)',
$this->getPageIdsByStore((int) $value)
);

return $this->connection->getConnection()->fetchCol($sql);
}

/**
* @param int $storeId
* @return array
* @throws LocalizedException
*/
private function getPageIdsByStore(int $storeId): array
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(self::STORE_FIELD, $storeId)
->create();

$searchResult = $this->pageRepository->getList($searchCriteria);

return array_map(function (PageInterface $page) {
return $page->getId();
}, $searchResult->getItems());
}
}
16 changes: 0 additions & 16 deletions app/code/Magento/MediaContentCms/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,6 @@
</argument>
</arguments>
</type>
<virtualType name="Magento\MediaContentCms\Model\ResourceModel\GetAssetIdsByBlockStore" type="Magento\MediaContentCms\Model\ResourceModel\GetAssetIdsByContentField">
<arguments>
<argument name="entityType" xsi:type="string">cms_block</argument>
<argument name="fieldTable" xsi:type="string">cms_block_store</argument>
<argument name="idColumn" xsi:type="string">block_id</argument>
<argument name="fieldColumn" xsi:type="string">store_id</argument>
</arguments>
</virtualType>
<virtualType name="Magento\MediaContentCms\Model\ResourceModel\GetAssetIdsByPageStore" type="Magento\MediaContentCms\Model\ResourceModel\GetAssetIdsByContentField">
<arguments>
<argument name="entityType" xsi:type="string">cms_page</argument>
<argument name="fieldTable" xsi:type="string">cms_page_store</argument>
<argument name="idColumn" xsi:type="string">page_id</argument>
<argument name="fieldColumn" xsi:type="string">store_id</argument>
</arguments>
</virtualType>
<virtualType name="Magento\MediaContentCms\Model\ResourceModel\GetAssetIdsByPageStatus" type="Magento\MediaContentCms\Model\ResourceModel\GetAssetIdsByContentField">
<arguments>
<argument name="entityType" xsi:type="string">cms_page</argument>
Expand Down

0 comments on commit acf425b

Please sign in to comment.