7
7
8
8
namespace Magento \MediaContentCatalog \Model ;
9
9
10
+ use Magento \Catalog \Api \CategoryManagementInterface ;
10
11
use Magento \Framework \App \ResourceConnection ;
11
12
use Magento \Framework \Exception \LocalizedException ;
12
13
use Magento \MediaContentApi \Model \GetAssetIdByContentFieldInterface ;
13
14
use Magento \Store \Api \GroupRepositoryInterface ;
14
15
use Magento \Store \Api \StoreRepositoryInterface ;
15
16
16
17
/**
17
- * Class responsible to return Asset id by content field
18
+ * Class responsible to return Asset id by category store
18
19
*/
19
20
class GetAssetIdByCategoryStore implements GetAssetIdByContentFieldInterface
20
21
{
21
22
private const TABLE_CONTENT_ASSET = 'media_content_asset ' ;
23
+ private const TABLE_CATALOG_CATEGORY = 'catalog_category_entity ' ;
22
24
23
25
/**
24
26
* @var ResourceConnection
@@ -67,7 +69,7 @@ public function execute(string $value): array
67
69
{
68
70
$ storeView = $ this ->storeRepository ->getById ($ value );
69
71
$ storeGroup = $ this ->storeGroupRepository ->get ($ storeView ->getStoreGroupId ());
70
- $ categoryIds = $ this ->getCategoryIdsByRootCategory ($ storeGroup ->getRootCategoryId ());
72
+ $ categoryIds = $ this ->getCategoryIdsByRootCategory (( int ) $ storeGroup ->getRootCategoryId ());
71
73
$ sql = $ this ->connection ->getConnection ()->select ()->from (
72
74
['asset_content_table ' => $ this ->connection ->getTableName (self ::TABLE_CONTENT_ASSET )],
73
75
['asset_id ' ]
@@ -86,21 +88,15 @@ public function execute(string $value): array
86
88
}, $ result );
87
89
}
88
90
89
- private function getCategoryIdsByRootCategory ($ rootCategoryId )
91
+ /**
92
+ * This function returns an array of category ids that have content and are under the root parameter
93
+ *
94
+ * @param $rootCategoryId
95
+ * @return array
96
+ */
97
+ private function getCategoryIdsByRootCategory (int $ rootCategoryId ): array
90
98
{
91
- $ contentCategoriesSql = $ this ->connection ->getConnection ()->select ()->from (
92
- ['asset_content_table ' => $ this ->connection ->getTableName (self ::TABLE_CONTENT_ASSET )],
93
- ['entity_id ' ]
94
- )->where (
95
- 'entity_type = ? ' ,
96
- $ this ->entityType
97
- )->joinInner (
98
- ['category_table ' => $ this ->connection ->getTableName ('catalog_category_entity ' )],
99
- 'asset_content_table.entity_id = category_table.entity_id ' ,
100
- ['path ' ]
101
- );
102
-
103
- $ result = $ this ->connection ->getConnection ()->fetchAll ($ contentCategoriesSql );
99
+ $ result = $ this ->getCategoryIdsAndPath ();
104
100
105
101
$ result = array_filter ($ result , function ($ item ) use ($ rootCategoryId ) {
106
102
$ pathArray = explode ("/ " , $ item ['path ' ]);
@@ -117,4 +113,26 @@ private function getCategoryIdsByRootCategory($rootCategoryId)
117
113
return $ item ['entity_id ' ];
118
114
}, $ result );
119
115
}
116
+
117
+ /**
118
+ * This function returns an array of category_id and path of categories that have content
119
+ *
120
+ * @return array
121
+ */
122
+ private function getCategoryIdsAndPath (): array
123
+ {
124
+ $ contentCategoriesSql = $ this ->connection ->getConnection ()->select ()->from (
125
+ ['asset_content_table ' => $ this ->connection ->getTableName (self ::TABLE_CONTENT_ASSET )],
126
+ ['entity_id ' ]
127
+ )->where (
128
+ 'entity_type = ? ' ,
129
+ $ this ->entityType
130
+ )->joinInner (
131
+ ['category_table ' => $ this ->connection ->getTableName (self ::TABLE_CATALOG_CATEGORY )],
132
+ 'asset_content_table.entity_id = category_table.entity_id ' ,
133
+ ['path ' ]
134
+ );
135
+
136
+ return $ this ->connection ->getConnection ()->fetchAll ($ contentCategoriesSql );
137
+ }
120
138
}
0 commit comments