|
| 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 | +} |
0 commit comments