Skip to content

Commit c2697a9

Browse files
committed
Merge branch 'ACP2E-632' of https://github.com/magento-l3/magento2ce into L3-PR-2022-04-19
2 parents 25973d7 + 35b6c93 commit c2697a9

File tree

4 files changed

+401
-0
lines changed

4 files changed

+401
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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\Sales\Model;
9+
10+
use Exception;
11+
use Magento\Framework\View\Element\UiComponentInterface;
12+
use Magento\Ui\Api\BookmarkManagementInterface;
13+
use Magento\Ui\Api\Data\BookmarkInterface;
14+
use Magento\Ui\Component\Listing\Columns;
15+
16+
/**
17+
* Processor for checking custom view columns
18+
*/
19+
class ExportViewFilterProcessor
20+
{
21+
/**
22+
* @var BookmarkManagementInterface
23+
*/
24+
private $bookmarkManagement;
25+
26+
/**
27+
* @var array
28+
*/
29+
private $currentConfig;
30+
31+
/**
32+
* @var array
33+
*/
34+
private $columnHeaders;
35+
36+
/**
37+
* @var string
38+
*/
39+
private $currentGridView;
40+
41+
/**
42+
* @param BookmarkManagementInterface $bookmarkManagement
43+
*/
44+
public function __construct(
45+
BookmarkManagementInterface $bookmarkManagement
46+
) {
47+
$this->bookmarkManagement = $bookmarkManagement;
48+
}
49+
50+
/**
51+
* Process activeColumns for current view for sales order reporting purpose
52+
*
53+
* @param UiComponentInterface $component
54+
* @param string $namespace
55+
* @param bool $isForHeading
56+
* @return array $activeColumnHeaders
57+
* @throws Exception
58+
*/
59+
public function execute(UiComponentInterface $component, string $namespace, bool $isForHeading = false): array
60+
{
61+
return $this->getActiveColumnsForGrid($component, $namespace, $isForHeading);
62+
}
63+
64+
/**
65+
* Returns column headers list
66+
*
67+
* @param UiComponentInterface $component
68+
*
69+
* @return array $columnHeaders[]
70+
* @throws Exception
71+
*/
72+
private function getColumnsHeader(UiComponentInterface $component): array
73+
{
74+
$columnHeaders = [];
75+
$columns = $this->getColumnsComponent($component);
76+
foreach ($columns->getChildComponents() as $column) {
77+
if ($column->getData('config/label') && $column->getData('config/dataType') !== 'actions') {
78+
$columnHeaders[$column->getName()] = $column->getData('config/label');
79+
}
80+
}
81+
return $columnHeaders;
82+
}
83+
84+
/**
85+
* Returns Columns component
86+
*
87+
* @param UiComponentInterface $component
88+
*
89+
* @return UiComponentInterface
90+
* @throws Exception
91+
*/
92+
private function getColumnsComponent(UiComponentInterface $component): UiComponentInterface
93+
{
94+
foreach ($component->getChildComponents() as $childComponent) {
95+
if ($childComponent instanceof Columns) {
96+
return $childComponent;
97+
}
98+
}
99+
throw new Exception('No columns found'); // @codingStandardsIgnoreLine
100+
}
101+
102+
/**
103+
* Get the current config details from the bookmark management
104+
*
105+
* @param string $namespace
106+
* @return array
107+
*/
108+
private function getCurrentConfig(string $namespace): array
109+
{
110+
$currentConfig = [];
111+
$bookmarks = $this->bookmarkManagement->loadByNamespace($namespace);
112+
/** @var BookmarkInterface $bookmark */
113+
foreach ($bookmarks->getItems() as $bookmark) {
114+
if ($bookmark->isCurrent()) {
115+
$this->currentGridView = $bookmark->getIdentifier();
116+
}
117+
$currentConfig = array_merge_recursive($currentConfig, $bookmark->getConfig());
118+
}
119+
return $currentConfig;
120+
}
121+
122+
/**
123+
* Retrieve activeColumns list for the grid
124+
*
125+
* @param UiComponentInterface $component
126+
* @param string $namespace
127+
* @param bool $isForHeading
128+
* @return array
129+
* @throws Exception
130+
*/
131+
private function getActiveColumnsForGrid(
132+
UiComponentInterface $component,
133+
string $namespace,
134+
bool $isForHeading = false
135+
): array {
136+
$activeColumns = [];
137+
$this->columnHeaders = $this->columnHeaders ?? $this->getColumnsHeader($component);
138+
$this->currentConfig = $this->currentConfig ?? $this->getCurrentConfig($namespace);
139+
if ($this->currentConfig && $this->currentGridView !== 'default'
140+
&& $this->currentConfig['current']['columns']) {
141+
array_walk(
142+
$this->currentConfig['current']['columns'],
143+
function ($column, $key) use (&$activeColumns, &$isForHeading) {
144+
if ($column['visible'] && array_key_exists($key, $this->columnHeaders)) {
145+
$activeColumns[$this->currentConfig['current']['positions'][$key]] = $isForHeading
146+
? $this->columnHeaders[$key] : $key;
147+
}
148+
}
149+
);
150+
if ($activeColumns) {
151+
ksort($activeColumns);
152+
}
153+
}
154+
return $activeColumns;
155+
}
156+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\Sales\Plugin\Model\Export;
9+
10+
use Exception;
11+
use Magento\Framework\View\Element\UiComponentInterface;
12+
use Magento\Sales\Model\ExportViewFilterProcessor;
13+
use Magento\Ui\Model\Export\MetadataProvider;
14+
15+
/**
16+
* Process and filter order grid export columns according to view
17+
*/
18+
class OrderGridExportFilterColumn
19+
{
20+
/**
21+
* @var ExportViewFilterProcessor
22+
*/
23+
private $exportViewFilterProcessor;
24+
25+
/**
26+
* @param ExportViewFilterProcessor $exportViewFilterProcessor
27+
*/
28+
public function __construct(
29+
ExportViewFilterProcessor $exportViewFilterProcessor
30+
) {
31+
$this->exportViewFilterProcessor = $exportViewFilterProcessor;
32+
}
33+
34+
/**
35+
* Plugin which will check getHeaders and update headers according to the custom view
36+
*
37+
* @param MetadataProvider $subject
38+
* @param array $result
39+
* @param UiComponentInterface $component
40+
* @return array
41+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
42+
* @throws Exception
43+
*/
44+
public function afterGetHeaders(
45+
MetadataProvider $subject,
46+
array $result,
47+
UiComponentInterface $component
48+
): array {
49+
$namespace = $component->getContext()->getNamespace();
50+
if ($namespace === 'sales_order_grid') {
51+
$activeColumns = $this->exportViewFilterProcessor->execute($component, $namespace, true);
52+
$result = !empty($activeColumns) ? $activeColumns : $result;
53+
}
54+
return $result;
55+
}
56+
57+
/**
58+
* Plugin which will check getFields and update fields according to the custom view
59+
*
60+
* @param MetadataProvider $subject
61+
* @param array $result
62+
* @param UiComponentInterface $component
63+
* @return array
64+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
65+
* @throws Exception
66+
*/
67+
public function afterGetFields(
68+
MetadataProvider $subject,
69+
array $result,
70+
UiComponentInterface $component
71+
): array {
72+
$namespace = $component->getContext()->getNamespace();
73+
if ($namespace === 'sales_order_grid') {
74+
$activeColumns = $this->exportViewFilterProcessor->execute($component, $namespace);
75+
$result = !empty($activeColumns) ? $activeColumns : $result;
76+
}
77+
return $result;
78+
}
79+
}

0 commit comments

Comments
 (0)