8
8
namespace Magento \MediaContentCatalog \Model \ResourceModel ;
9
9
10
10
use Magento \Catalog \Api \CategoryManagementInterface ;
11
+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
11
12
use Magento \Framework \App \ResourceConnection ;
12
13
use Magento \Framework \Exception \LocalizedException ;
13
14
use Magento \MediaContentApi \Model \GetAssetIdsByContentFieldInterface ;
@@ -22,6 +23,7 @@ class GetAssetIdsByCategoryStore implements GetAssetIdsByContentFieldInterface
22
23
private const TABLE_CONTENT_ASSET = 'media_content_asset ' ;
23
24
private const TABLE_CATALOG_CATEGORY = 'catalog_category_entity ' ;
24
25
private const ENTITY_TYPE = 'catalog_category ' ;
26
+ private const ID_COLUMN = 'entity_id ' ;
25
27
26
28
/**
27
29
* @var ResourceConnection
@@ -38,21 +40,29 @@ class GetAssetIdsByCategoryStore implements GetAssetIdsByContentFieldInterface
38
40
*/
39
41
private $ storeGroupRepository ;
40
42
43
+ /**
44
+ * @var CategoryRepositoryInterface
45
+ */
46
+ private $ categoryRepository ;
47
+
41
48
/**
42
49
* GetAssetIdsByCategoryStore constructor.
43
50
*
44
51
* @param ResourceConnection $resource
45
52
* @param StoreRepositoryInterface $storeRepository
46
53
* @param GroupRepositoryInterface $storeGroupRepository
54
+ * @param CategoryRepositoryInterface $categoryRepository
47
55
*/
48
56
public function __construct (
49
57
ResourceConnection $ resource ,
50
58
StoreRepositoryInterface $ storeRepository ,
51
- GroupRepositoryInterface $ storeGroupRepository
59
+ GroupRepositoryInterface $ storeGroupRepository ,
60
+ CategoryRepositoryInterface $ categoryRepository
52
61
) {
53
62
$ this ->connection = $ resource ;
54
63
$ this ->storeRepository = $ storeRepository ;
55
64
$ this ->storeGroupRepository = $ storeGroupRepository ;
65
+ $ this ->categoryRepository = $ categoryRepository ;
56
66
}
57
67
58
68
/**
@@ -62,66 +72,23 @@ public function execute(string $value): array
62
72
{
63
73
$ storeView = $ this ->storeRepository ->getById ($ value );
64
74
$ storeGroup = $ this ->storeGroupRepository ->get ($ storeView ->getStoreGroupId ());
65
- $ categoryIds = $ this ->getCategoryIdsByRootCategory ((int ) $ storeGroup ->getRootCategoryId ());
75
+ $ rootCategory = $ this ->categoryRepository ->get ($ storeGroup ->getRootCategoryId ());
76
+
66
77
$ sql = $ this ->connection ->getConnection ()->select ()->from (
67
78
['asset_content_table ' => $ this ->connection ->getTableName (self ::TABLE_CONTENT_ASSET )],
68
79
['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
+ []
69
84
)->where (
70
85
'entity_type = ? ' ,
71
86
self ::ENTITY_TYPE
72
87
)->where (
73
- 'entity_id IN (?) ' ,
74
- $ categoryIds
88
+ 'path LIKE ? ' ,
89
+ $ rootCategory -> getPath () . ' % '
75
90
);
76
91
77
92
return $ this ->connection ->getConnection ()->fetchCol ($ sql );
78
93
}
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
- }
127
94
}
0 commit comments