Skip to content

Commit dfc733a

Browse files
author
Gabriel Galvao da Gama
committed
Implemented content status filter
1 parent 735579d commit dfc733a

File tree

13 files changed

+588
-0
lines changed

13 files changed

+588
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContent\Model;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\MediaContentApi\Model\GetAssetIdByContentStatusInterface;
12+
13+
/**
14+
* Class GetAssetIdByContentStatus
15+
*/
16+
class GetAssetIdByContentStatus implements GetAssetIdByContentStatusInterface
17+
{
18+
private const TABLE_CONTENT_ASSET = 'media_content_asset';
19+
20+
/**
21+
* @var ResourceConnection
22+
*/
23+
private $connection;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $entityType;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $contentTable;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $statusColumn;
39+
40+
/**
41+
* @var string
42+
*/
43+
private $idColumn;
44+
45+
/**
46+
* @var array
47+
*/
48+
private $valueMap;
49+
50+
/**
51+
* GetContentIdByStatus constructor.
52+
* @param ResourceConnection $resource
53+
* @param string $entityType
54+
* @param string $contentTable
55+
* @param string $idColumn
56+
* @param string $statusColumn
57+
* @param array $valueMap
58+
*/
59+
public function __construct(
60+
ResourceConnection $resource,
61+
string $entityType,
62+
string $contentTable,
63+
string $idColumn,
64+
string $statusColumn,
65+
array $valueMap = []
66+
) {
67+
$this->connection = $resource;
68+
$this->entityType = $entityType;
69+
$this->contentTable = $contentTable;
70+
$this->idColumn = $idColumn;
71+
$this->statusColumn = $statusColumn;
72+
$this->valueMap = $valueMap;
73+
}
74+
75+
/**
76+
* @param string $value
77+
* @return array
78+
*/
79+
public function execute(string $value): array
80+
{
81+
$sql = $this->connection->getConnection()->select()->from(
82+
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
83+
['asset_id']
84+
)->where(
85+
'entity_type = ?',
86+
$this->entityType
87+
)->joinInner(
88+
['content_table' => $this->connection->getTableName($this->contentTable)],
89+
'asset_content_table.entity_id = content_table.' . $this->idColumn,
90+
[]
91+
)->where(
92+
'content_table.' . $this->statusColumn . ' = ?',
93+
$this->getValueFromMap($value)
94+
);
95+
96+
$result = $this->connection->getConnection()->fetchAll($sql);
97+
98+
return array_map(function ($item) {
99+
return $item['asset_id'];
100+
}, $result);
101+
}
102+
103+
/**
104+
* @param string $value
105+
* @return string
106+
*/
107+
private function getValueFromMap(string $value): string
108+
{
109+
if (count($this->valueMap) > 0 && array_key_exists($value, $this->valueMap)) {
110+
return $this->valueMap[$value];
111+
}
112+
return $value;
113+
}
114+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContent\Model;
9+
10+
use Magento\MediaContentApi\Model\GetAssetIdByContentStatusInterface;
11+
12+
/**
13+
* Class GetAssetIdByContentStatusComposite
14+
*/
15+
class GetAssetIdByContentStatusComposite implements GetAssetIdByContentStatusInterface
16+
{
17+
/**
18+
* @var GetAssetIdByContentStatus[]
19+
*/
20+
private $getAssetIdByContentStatusArray;
21+
22+
/**
23+
* GetAssetIdByContentStatusComposite constructor.
24+
* @param array $getAssetIdByContentStatusArray
25+
*/
26+
public function __construct($getAssetIdByContentStatusArray = [])
27+
{
28+
$this->getAssetIdByContentStatusArray = $getAssetIdByContentStatusArray;
29+
}
30+
31+
/**
32+
* @param string $value
33+
* @return array
34+
*/
35+
public function execute(string $value): array
36+
{
37+
$ids = [];
38+
foreach ($this->getAssetIdByContentStatusArray as $getAssetIdByContentStatus) {
39+
$ids = array_merge($ids, $getAssetIdByContentStatus->execute($value));
40+
}
41+
return array_unique($ids);
42+
}
43+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContent\Model;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\MediaContentApi\Model\GetAssetIdByContentStatusInterface;
12+
13+
/**
14+
* Class GetAssetIdByEavContentStatus
15+
*/
16+
class GetAssetIdByEavContentStatus implements GetAssetIdByContentStatusInterface
17+
{
18+
private const TABLE_CONTENT_ASSET = 'media_content_asset';
19+
private const TABLE_EAV_ATTRIBUTE = 'eav_attribute';
20+
21+
/**
22+
* @var ResourceConnection
23+
*/
24+
private $connection;
25+
26+
/**
27+
* @var string
28+
*/
29+
private $entityEavTypeTable;
30+
31+
/**
32+
* @var string
33+
*/
34+
private $attributeCode;
35+
36+
/**
37+
* @var string
38+
*/
39+
private $entityType;
40+
41+
/**
42+
* @var int
43+
*/
44+
private $entityTypeId;
45+
46+
/**
47+
* @var array
48+
*/
49+
private $valueMap;
50+
51+
/**
52+
* GetEavContentIdByStatus constructor.
53+
* @param ResourceConnection $resource
54+
* @param string $entityEavTypeTable
55+
* @param string $attributeCode
56+
* @param string $entityType
57+
* @param int $entityTypeId
58+
* @param array $valueMap
59+
*/
60+
public function __construct(
61+
ResourceConnection $resource,
62+
string $entityEavTypeTable,
63+
string $attributeCode,
64+
string $entityType,
65+
int $entityTypeId,
66+
array $valueMap = []
67+
) {
68+
$this->connection = $resource;
69+
$this->entityEavTypeTable = $entityEavTypeTable;
70+
$this->attributeCode = $attributeCode;
71+
$this->entityType = $entityType;
72+
$this->entityTypeId = $entityTypeId;
73+
$this->valueMap = $valueMap;
74+
}
75+
76+
/**
77+
* @param string $value
78+
* @return array
79+
*/
80+
public function execute(string $value): array
81+
{
82+
$statusAttributeId = $this->getAttributeId();
83+
$sql = $this->connection->getConnection()->select()->from(
84+
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
85+
['asset_id']
86+
)->where(
87+
'entity_type = ?',
88+
$this->entityType
89+
)->joinInner(
90+
['entity_eav_type' => $this->connection->getTableName($this->entityEavTypeTable)],
91+
'asset_content_table.entity_id = entity_eav_type.entity_id AND entity_eav_type.attribute_id = ' .
92+
$statusAttributeId,
93+
[]
94+
)->where(
95+
'entity_eav_type.value = ?',
96+
$this->getValueFromMap($value)
97+
);
98+
99+
$result = $this->connection->getConnection()->fetchAll($sql);
100+
101+
return array_map(function ($item) {
102+
return $item['asset_id'];
103+
}, $result);
104+
}
105+
106+
/**
107+
* @return string
108+
*/
109+
private function getAttributeId(): string
110+
{
111+
$sql = $this->connection->getConnection()->select()->from(
112+
['eav' => $this->connection->getTableName(self::TABLE_EAV_ATTRIBUTE)],
113+
['attribute_id']
114+
)->where(
115+
'entity_type_id = ?',
116+
$this->entityTypeId
117+
)->where(
118+
'attribute_code = ?',
119+
$this->attributeCode
120+
);
121+
122+
return $this->connection->getConnection()->fetchOne($sql);
123+
}
124+
125+
/**
126+
* @param string $value
127+
* @return string
128+
*/
129+
private function getValueFromMap(string $value): string
130+
{
131+
if (count($this->valueMap) > 0 && array_key_exists($value, $this->valueMap)) {
132+
return $this->valueMap[$value];
133+
}
134+
return $value;
135+
}
136+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\MediaContent\Model\GetAssetIdByContentStatusComposite">
10+
<arguments>
11+
<argument name="getAssetIdByContentStatusArray" xsi:type="array">
12+
<item name="getAssetIdByProductStatus" xsi:type="object">Magento\MediaContent\Model\GetAssetIdByProductStatus</item>
13+
<item name="getAssetIdByCategoryStatus" xsi:type="object">Magento\MediaContent\Model\GetAssetIdByCategoryStatus</item>
14+
<item name="getAssetIdByPageStatus" xsi:type="object">Magento\MediaContent\Model\GetAssetIdByPageStatus</item>
15+
<item name="getAssetIdByBlockStatus" xsi:type="object">Magento\MediaContent\Model\GetAssetIdByBlockStatus</item>
16+
</argument>
17+
</arguments>
18+
</type>
19+
<virtualType name="Magento\MediaContent\Model\GetAssetIdByProductStatus" type="Magento\MediaContent\Model\GetAssetIdByEavContentStatus">
20+
<arguments>
21+
<argument name="entityEavTypeTable" xsi:type="string">catalog_product_entity_int</argument>
22+
<argument name="attributeCode" xsi:type="string">status</argument>
23+
<argument name="entityType" xsi:type="string">catalog_product</argument>
24+
<argument name="entityTypeId" xsi:type="string">4</argument>
25+
<argument name="valueMap" xsi:type="array">
26+
<item name="1" xsi:type="string">1</item>
27+
<item name="0" xsi:type="string">2</item>
28+
</argument>
29+
</arguments>
30+
</virtualType>
31+
<virtualType name="Magento\MediaContent\Model\GetAssetIdByCategoryStatus" type="Magento\MediaContent\Model\GetAssetIdByEavContentStatus">
32+
<arguments>
33+
<argument name="entityEavTypeTable" xsi:type="string">catalog_category_entity_int</argument>
34+
<argument name="attributeCode" xsi:type="string">is_active</argument>
35+
<argument name="entityType" xsi:type="string">catalog_category</argument>
36+
<argument name="entityTypeId" xsi:type="string">3</argument>
37+
</arguments>
38+
</virtualType>
39+
<virtualType name="Magento\MediaContent\Model\GetAssetIdByPageStatus" type="Magento\MediaContent\Model\GetAssetIdByContentStatus">
40+
<arguments>
41+
<argument name="entityType" xsi:type="string">cms_page</argument>
42+
<argument name="contentTable" xsi:type="string">cms_page</argument>
43+
<argument name="idColumn" xsi:type="string">page_id</argument>
44+
<argument name="statusColumn" xsi:type="string">is_active</argument>
45+
</arguments>
46+
</virtualType>
47+
<virtualType name="Magento\MediaContent\Model\GetAssetIdByBlockStatus" type="Magento\MediaContent\Model\GetAssetIdByContentStatus">
48+
<arguments>
49+
<argument name="entityType" xsi:type="string">cms_block</argument>
50+
<argument name="contentTable" xsi:type="string">cms_block</argument>
51+
<argument name="idColumn" xsi:type="string">block_id</argument>
52+
<argument name="statusColumn" xsi:type="string">is_active</argument>
53+
</arguments>
54+
</virtualType>
55+
</config>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentApi\Model;
9+
10+
/**
11+
* Interface used to return Asset id by content status (enabled, disabled).
12+
*/
13+
interface GetAssetIdByContentStatusInterface
14+
{
15+
/**
16+
* @param string $status
17+
* @return int[]
18+
*/
19+
public function execute(string $status): array;
20+
}

0 commit comments

Comments
 (0)