-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTabs.php
More file actions
265 lines (225 loc) · 7.11 KB
/
Tabs.php
File metadata and controls
265 lines (225 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/
declare(strict_types=1);
namespace Magefan\Community\Block\Adminhtml\System\Config;
use Magefan\Community\Model\Menu\MagefanGroupsProvider;
use Magento\Backend\Block\Template;
use Magento\Config\Model\Config\Structure\Element\Section;
use Magento\Config\Model\Config\Structure;
class Tabs extends Template
{
public const EXTENSIONS = 'extensions';
public const ITEM_TYPE = 'type';
public const TYPE_GROUP = 'group';
public const TYPE_SINGLE = 'single';
public const ITEM_NAME = 'name';
public const ITEM_CLASS = 'class';
public const ITEM_URL = 'url';
public const IS_ACTIVE = 'is_active';
public const SORT_ORDER = 'sort_order';
/**
* @var string
*/
protected $_template = 'Magefan_Community::magefan-config-tabs.phtml';
/**
* @var MagefanGroupsProvider
*/
private $groupsProvider;
/**
* @var Structure
*/
private $configStructure;
/**
* @var mixed
*/
private $currentSectionId;
/**
* @param Template\Context $context
* @param MagefanGroupsProvider $groupsProvider
* @param Structure $configStructure
* @param array $data
*/
public function __construct(
Template\Context $context,
MagefanGroupsProvider $groupsProvider,
Structure $configStructure,
array $data = []
) {
parent::__construct($context, $data);
$this->groupsProvider = $groupsProvider;
$this->configStructure = $configStructure;
$this->currentSectionId = $this->getRequest()->getParam('section');
}
/**
* @return array[]
*/
public function getConfigData(): array
{
return $this->buildConfigStructure();
}
/**
* Build a flat A-Z sorted list of groups and standalone extensions.
* @return array[]
*/
private function buildConfigStructure(): array
{
$allActive = $this->fetchMagefanSections();
$assignedModules = [];
$items = [];
foreach ($this->groupsProvider->get() as $group) {
list($groupItems, $baseItem, $assigned) = $this->buildGroupItems($allActive, $group);
$assignedModules = array_merge($assignedModules, $assigned);
if (empty($groupItems) && $baseItem === null) {
continue;
}
$this->applySortOrder($groupItems);
if ($baseItem !== null) {
array_unshift($groupItems, $baseItem);
}
$items[] = [
self::ITEM_TYPE => self::TYPE_GROUP,
self::ITEM_NAME => (string)__($group[self::ITEM_NAME]),
self::EXTENSIONS => $groupItems,
];
}
foreach ($this->excludeGroupedExtensions($allActive, $assignedModules) as $ext) {
$ext[self::ITEM_TYPE] = self::TYPE_SINGLE;
$items[] = $ext;
}
usort($items, function ($a, $b) {
return strcmp($a[self::ITEM_NAME], $b[self::ITEM_NAME]);
});
return ['items' => $items];
}
/**
* Extract sub-items and base item for a single group config entry.
* Returns [groupItems, baseItem|null, assignedModuleNames].
* @param array $allActive
* @param array $group
* @return array
*/
private function buildGroupItems(array $allActive, array $group): array
{
$groupItems = [];
$baseItem = null;
$assigned = [];
$baseModule = $group['base'] ?? null;
foreach ($group[self::EXTENSIONS] as $moduleName) {
if (!isset($allActive[$moduleName])) {
continue;
}
$item = $allActive[$moduleName];
$assigned[] = $moduleName;
if ($baseModule && $moduleName === $baseModule) {
$item[self::ITEM_NAME] = (string)__('General');
$baseItem = $item;
} else {
$groupItems[] = $item;
}
}
return [$groupItems, $baseItem, $assigned];
}
/**
* Filters out extensions already assigned to groups using hash map lookups
* @param array $allExtensions
* @param array $assignedModules
* @return array
*/
private function excludeGroupedExtensions(array $allExtensions, array $assignedModules): array
{
$removeMap = array_flip(array_unique($assignedModules));
$filtered = [];
foreach ($allExtensions as $key => $value) {
if (!isset($removeMap[$key])) {
$filtered[] = $value;
}
}
return $filtered;
}
/**
* Map Magento sections to internal data array
* @return array
*/
private function fetchMagefanSections(): array
{
$output = [];
$nodes = $this->getMagefanConfigChildrenNode();
if ($nodes) {
foreach ($nodes as $section) {
if (!$section->isVisible()) {
continue;
}
$resource = (string)$section->getAttribute('resource');
$moduleName = current(explode('::', $resource));
$output[$moduleName] = $this->mapSectionToData($section);
}
}
return $output;
}
/**
* Extract attributes from Section element
* @param Section $section
* @return array
*/
private function mapSectionToData(Section $section): array
{
return [
self::ITEM_NAME => $this->resolveLabel($section),
self::ITEM_CLASS => (string)$section->getClass(),
self::ITEM_URL => $this->generateUrl($section),
self::IS_ACTIVE => $section->getId() === $this->currentSectionId,
self::SORT_ORDER => (int)$section->getAttribute('sortOrder'),
];
}
/**
* @param Section $section
* @return string
*/
private function resolveLabel(Section $section): string
{
$label = $section->getLabel() ? (string)__($section->getLabel()) : '';
return $this->_escaper->escapeHtml($label);
}
/**
* @param Section $section
* @return string
*/
private function generateUrl(Section $section): string
{
return $this->getUrl('*/*/*', [
'_current' => true,
'section' => $section->getId()
]);
}
/**
* @param array $list
* @return void
*/
private function applySortOrder(array &$list): void
{
usort($list, function ($a, $b) {
$sortA = $a[self::SORT_ORDER];
$sortB = $b[self::SORT_ORDER];
if ($sortA == $sortB) {
return 0;
}
return ($sortA < $sortB) ? -1 : 1;
});
}
/**
* @return null
*/
public function getMagefanConfigChildrenNode()
{
$configTabs = $this->configStructure->getTabs();
foreach ($configTabs as $node) {
if ($node->getId() == 'magefan') {
return $node->getChildren();
}
}
return null;
}
}