-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathData.php
197 lines (171 loc) · 6.67 KB
/
Data.php
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
<?php
namespace EW\ConfigScopeHints\Helper;
use \Magento\Store\Model\Website;
use \Magento\Store\Model\Store;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/** @var \Magento\Framework\App\Helper\Context */
protected $context;
/** @var \Magento\Store\Model\StoreManagerInterface */
protected $storeManager;
/**
* Url Builder
*
* @var \Magento\Backend\Model\Url
*/
protected $urlBuilder;
/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Backend\Model\Url $urlBuilder
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Backend\Model\Url $urlBuilder
) {
$this->storeManager = $storeManager;
$this->context = $context;
// Ideally we would just retrieve the urlBuilder using $this->content->getUrlBuilder(), but since it retrieves
// an instance of \Magento\Framework\Url instead of \Magento\Backend\Model\Url, we must explicitly request it
// via DI.
$this->urlBuilder = $urlBuilder;
}
/**
* Gets store tree in a format easily walked over
* for config path value comparison
*
* @return array
*/
public function getScopeTree() {
$tree = array('websites' => array());
$websites = $this->storeManager->getWebsites();
/* @var $website Website */
foreach($websites as $website) {
$tree['websites'][$website->getId()] = array('stores' => array());
/* @var $store Store */
foreach($website->getStores() as $store) {
$tree['websites'][$website->getId()]['stores'][] = $store->getId();
}
}
return $tree;
}
/**
* Wrapper method to get config value at path, scope, and scope code provided
*
* @param $path
* @param $contextScope
* @param $contextScopeId
* @return mixed
*/
protected function _getConfigValue($path, $contextScope, $contextScopeId) {
return $this->context->getScopeConfig()->getValue($path, $contextScope, $contextScopeId);
}
/**
* Gets array of scopes and scope IDs where path value is different
* than supplied context scope and context scope ID.
* If no lower-level scopes override the value, return empty array.
*
* @param $path
* @param $contextScope
* @param $contextScopeId
* @return array
*/
public function getOverriddenLevels($path, $contextScope, $contextScopeId) {
$tree = $this->getScopeTree();
$currentValue = $this->_getConfigValue($path, $contextScope, $contextScopeId);
if(is_null($currentValue)) {
return array(); //something is off, let's bail gracefully.
}
$overridden = array();
switch($contextScope) {
case 'websites':
$stores = array_values($tree['websites'][$contextScopeId]['stores']);
foreach($stores as $storeId) {
$value = $this->_getConfigValue($path, 'stores', $storeId);
if($value != $currentValue) {
$overridden[] = array(
'scope' => 'store',
'scope_id' => $storeId
);
}
}
break;
case 'default':
foreach($tree['websites'] as $websiteId => $website) {
$websiteValue = $this->_getConfigValue($path, 'websites', $websiteId);
if($websiteValue != $currentValue) {
$overridden[] = array(
'scope' => 'website',
'scope_id' => $websiteId
);
}
foreach($website['stores'] as $storeId) {
$value = $this->_getConfigValue($path, 'stores', $storeId);
if($value != $currentValue && $value != $websiteValue) {
$overridden[] = array(
'scope' => 'store',
'scope_id' => $storeId
);
}
}
}
break;
}
return $overridden;
}
/**
* Get HTML output for override hint UI
*
* @param \Magento\Config\Block\System\Config\Form $form
* @param array $overridden
* @return string
*/
public function formatOverriddenScopes(\Magento\Config\Block\System\Config\Form $form, array $overridden) {
$title = __('This setting is overridden at a more specific scope. Click for details.');
$formatted = '<a class="overridden-hint-list-toggle" title="'. $title .'" href="#"><span>'. $title .'</span></a>'.
'<ul class="overridden-hint-list">';
foreach($overridden as $overriddenScope) {
$scope = $overriddenScope['scope'];
$scopeId = $overriddenScope['scope_id'];
$scopeLabel = $scopeId;
$url = '#';
$section = $form->getSectionCode();
switch($scope) {
case 'website':
$url = $this->urlBuilder->getUrl(
'*/*/*',
array(
'section' => $section,
'website' => $scopeId
)
);
$scopeLabel = sprintf(
'website <a href="%s">%s</a>',
$url,
$this->storeManager->getWebsite($scopeId)->getName()
);
break;
case 'store':
$store = $this->storeManager->getStore($scopeId);
$website = $store->getWebsite();
$url = $this->urlBuilder->getUrl(
'*/*/*',
array(
'section' => $section,
'store' => $store->getId()
)
);
$scopeLabel = sprintf(
'store view <a href="%s">%s</a>',
$url,
$website->getName() . ' / ' . $store->getName()
);
break;
}
$formatted .= "<li class='$scope'>Overridden on $scopeLabel</li>";
}
$formatted .= '</ul>';
return $formatted;
}
}