88namespace Magento \MediaContentCatalog \Model \ResourceModel ;
99
1010use Magento \Catalog \Api \CategoryManagementInterface ;
11+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
1112use Magento \Framework \App \ResourceConnection ;
1213use Magento \Framework \Exception \LocalizedException ;
1314use Magento \MediaContentApi \Model \GetAssetIdsByContentFieldInterface ;
@@ -22,6 +23,7 @@ class GetAssetIdsByCategoryStore implements GetAssetIdsByContentFieldInterface
2223 private const TABLE_CONTENT_ASSET = 'media_content_asset ' ;
2324 private const TABLE_CATALOG_CATEGORY = 'catalog_category_entity ' ;
2425 private const ENTITY_TYPE = 'catalog_category ' ;
26+ private const ID_COLUMN = 'entity_id ' ;
2527
2628 /**
2729 * @var ResourceConnection
@@ -38,21 +40,29 @@ class GetAssetIdsByCategoryStore implements GetAssetIdsByContentFieldInterface
3840 */
3941 private $ storeGroupRepository ;
4042
43+ /**
44+ * @var CategoryRepositoryInterface
45+ */
46+ private $ categoryRepository ;
47+
4148 /**
4249 * GetAssetIdsByCategoryStore constructor.
4350 *
4451 * @param ResourceConnection $resource
4552 * @param StoreRepositoryInterface $storeRepository
4653 * @param GroupRepositoryInterface $storeGroupRepository
54+ * @param CategoryRepositoryInterface $categoryRepository
4755 */
4856 public function __construct (
4957 ResourceConnection $ resource ,
5058 StoreRepositoryInterface $ storeRepository ,
51- GroupRepositoryInterface $ storeGroupRepository
59+ GroupRepositoryInterface $ storeGroupRepository ,
60+ CategoryRepositoryInterface $ categoryRepository
5261 ) {
5362 $ this ->connection = $ resource ;
5463 $ this ->storeRepository = $ storeRepository ;
5564 $ this ->storeGroupRepository = $ storeGroupRepository ;
65+ $ this ->categoryRepository = $ categoryRepository ;
5666 }
5767
5868 /**
@@ -62,66 +72,23 @@ public function execute(string $value): array
6272 {
6373 $ storeView = $ this ->storeRepository ->getById ($ value );
6474 $ storeGroup = $ this ->storeGroupRepository ->get ($ storeView ->getStoreGroupId ());
65- $ categoryIds = $ this ->getCategoryIdsByRootCategory ((int ) $ storeGroup ->getRootCategoryId ());
75+ $ rootCategory = $ this ->categoryRepository ->get ($ storeGroup ->getRootCategoryId ());
76+
6677 $ sql = $ this ->connection ->getConnection ()->select ()->from (
6778 ['asset_content_table ' => $ this ->connection ->getTableName (self ::TABLE_CONTENT_ASSET )],
6879 ['asset_id ' ]
80+ )->joinInner (
81+ ['category_table ' => $ this ->connection ->getTableName (self ::TABLE_CATALOG_CATEGORY )],
82+ 'asset_content_table.entity_id = category_table. ' . self ::ID_COLUMN ,
83+ []
6984 )->where (
7085 'entity_type = ? ' ,
7186 self ::ENTITY_TYPE
7287 )->where (
73- 'entity_id IN (?) ' ,
74- $ categoryIds
88+ 'path LIKE ? ' ,
89+ $ rootCategory -> getPath () . ' % '
7590 );
7691
7792 return $ this ->connection ->getConnection ()->fetchCol ($ sql );
7893 }
79-
80- /**
81- * This function returns an array of category ids that have content and are under the root parameter
82- *
83- * @param int $rootCategoryId
84- * @return array
85- */
86- private function getCategoryIdsByRootCategory (int $ rootCategoryId ): array
87- {
88- $ result = $ this ->getCategoryIdsAndPath ();
89-
90- $ result = array_filter ($ result , function ($ item ) use ($ rootCategoryId ) {
91- $ pathArray = explode ('/ ' , $ item ['path ' ]);
92- $ isInPath = false ;
93- foreach ($ pathArray as $ id ) {
94- if ($ id == $ rootCategoryId ) {
95- $ isInPath = true ;
96- }
97- }
98- return $ isInPath ;
99- });
100-
101- return array_map (function ($ item ) {
102- return $ item ['entity_id ' ];
103- }, $ result );
104- }
105-
106- /**
107- * This function returns an array of category_id and path of categories that have content
108- *
109- * @return array
110- */
111- private function getCategoryIdsAndPath (): array
112- {
113- $ contentCategoriesSql = $ this ->connection ->getConnection ()->select ()->from (
114- ['asset_content_table ' => $ this ->connection ->getTableName (self ::TABLE_CONTENT_ASSET )],
115- ['entity_id ' ]
116- )->where (
117- 'entity_type = ? ' ,
118- self ::ENTITY_TYPE
119- )->joinInner (
120- ['category_table ' => $ this ->connection ->getTableName (self ::TABLE_CATALOG_CATEGORY )],
121- 'asset_content_table.entity_id = category_table.entity_id ' ,
122- ['path ' ]
123- );
124-
125- return $ this ->connection ->getConnection ()->fetchAll ($ contentCategoriesSql );
126- }
12794}
0 commit comments