-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathStoreDependingSettingHelper.cs
More file actions
254 lines (221 loc) · 9.02 KB
/
StoreDependingSettingHelper.cs
File metadata and controls
254 lines (221 loc) · 9.02 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using SmartStore.ComponentModel;
using SmartStore.Core.Infrastructure;
using SmartStore.Services.Configuration;
using SmartStore.Services.Localization;
using SmartStore.Web.Framework.Localization;
namespace SmartStore.Web.Framework.Settings
{
public class StoreDependingSettingHelper
{
private ViewDataDictionary _viewData;
public StoreDependingSettingHelper(ViewDataDictionary viewData)
{
_viewData = viewData;
}
public static string ViewDataKey => "StoreDependingSettingData";
public StoreDependingSettingData Data => _viewData[ViewDataKey] as StoreDependingSettingData;
public bool IsOverrideChecked(object settings, string name, FormCollection form)
{
var key = settings.GetType().Name + "." + name;
return IsOverrideChecked(key, form);
}
private bool IsOverrideChecked(string settingKey, FormCollection form)
{
var rawOverrideKey = form.AllKeys.FirstOrDefault(k => k.IsCaseInsensitiveEqual(settingKey + "_OverrideForStore"));
if (rawOverrideKey.HasValue())
{
var checkboxValue = form[rawOverrideKey].EmptyNull().ToLower();
return checkboxValue.Contains("on") || checkboxValue.Contains("true");
}
return false;
}
public void AddOverrideKey(object settings, string name)
{
if (Data == null)
{
throw new SmartException("You must call GetOverrideKeys or CreateViewDataObject before AddOverrideKey.");
}
var key = settings.GetType().Name + "." + name;
Data.OverrideSettingKeys.Add(key);
}
public void CreateViewDataObject(int activeStoreScopeConfiguration, string rootSettingClass = null)
{
_viewData[ViewDataKey] = new StoreDependingSettingData
{
ActiveStoreScopeConfiguration = activeStoreScopeConfiguration,
RootSettingClass = rootSettingClass
};
}
public void GetOverrideKeys(
object settings,
object model,
int storeId,
ISettingService settingService,
bool isRootModel = true,
Func<string, string> propertyNameMapper = null)
{
GetOverrideKeysInternal(settings, model, storeId, settingService, isRootModel, propertyNameMapper, null);
}
private void GetOverrideKeysInternal(
object settings,
object model,
int storeId,
ISettingService settingService,
bool isRootModel,
Func<string, string> propertyNameMapper,
int? localeIndex)
{
if (storeId <= 0)
{
// Single store mode -> there are no overrides.
return;
}
var data = Data ?? new StoreDependingSettingData();
var settingType = settings.GetType();
var modelType = model.GetType();
var settingName = settingType.Name;
var modelProperties = modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var localizedModelLocal = model as ILocalizedModelLocal;
var localizedEntityService = EngineContext.Current.Resolve<ILocalizedEntityService>();
foreach (var prop in modelProperties)
{
var name = propertyNameMapper?.Invoke(prop.Name) ?? prop.Name;
var settingProperty = settingType.GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
if (settingProperty == null)
{
// Setting is not configurable or missing or whatever... however we don't need the override info.
continue;
}
string key = null;
if (localizedModelLocal == null)
{
key = settingName + "." + name;
if (settingService.GetSettingByKey<string>(key, storeId: storeId) == null)
{
key = null;
}
}
else if (localeIndex.HasValue)
{
var value = localizedEntityService.GetLocalizedValue(localizedModelLocal.LanguageId, storeId, settingName, name);
if (!string.IsNullOrEmpty(value))
{
key = settingName + "." + "Locales[" + localeIndex.ToString() + "]." + name;
}
}
if (key != null)
{
data.OverrideSettingKeys.Add(key);
}
}
if (isRootModel)
{
data.ActiveStoreScopeConfiguration = storeId;
data.RootSettingClass = settingName;
_viewData[ViewDataKey] = data;
}
if (model is ILocalizedModel)
{
var localesProperty = modelType.GetProperty("Locales", BindingFlags.Public | BindingFlags.Instance);
if (localesProperty != null)
{
if (localesProperty.GetValue(model) is IEnumerable<ILocalizedModelLocal> locales)
{
int i = 0;
foreach (var locale in locales)
{
GetOverrideKeysInternal(settings, locale, storeId, settingService, false, propertyNameMapper, i);
i++;
}
}
}
}
}
public void GetOverrideKey(string formKey, string settingName, object settings, int storeId, ISettingService settingService)
{
if (storeId <= 0)
{
// Single store mode -> there are no overrides.
return;
}
var key = formKey;
if (settingService.GetSettingByKey<string>(string.Concat(settings.GetType().Name, ".", settingName), storeId: storeId) == null)
{
key = null;
}
if (key != null)
{
var data = Data ?? new StoreDependingSettingData();
data.OverrideSettingKeys.Add(key);
}
}
/// <summary>
/// Updates settings for a store.
/// </summary>
/// <param name="settings">Settings class instance.</param>
/// <param name="form">Form value collection.</param>
/// <param name="storeId">Store identifier.</param>
/// <param name="settingService">Setting service.</param>
/// <param name="propertyNameMapper">Function to map property names. Return <c>null</c> to skip a property.</param>
public void UpdateSettings(
object settings,
FormCollection form,
int storeId,
ISettingService settingService,
Func<string, string> propertyNameMapper = null)
{
var settingType = settings.GetType();
var settingName = settingType.Name;
var settingProperties = FastProperty.GetProperties(settingType).Values;
foreach (var prop in settingProperties)
{
var name = propertyNameMapper?.Invoke(prop.Name) ?? prop.Name;
if (string.IsNullOrWhiteSpace(name) || name == "CookieInfos")
{
continue;
}
var key = string.Concat(settingName, ".", name);
if (storeId == 0 || IsOverrideChecked(key, form))
{
dynamic value = prop.GetValue(settings);
settingService.SetSetting(key, value ?? string.Empty, storeId, false);
}
else if (storeId > 0)
{
settingService.DeleteSetting(key, storeId);
}
}
}
public void UpdateSetting(
string formKey,
string settingName,
object settings,
FormCollection form,
int storeId,
ISettingService settingService)
{
var settingType = settings.GetType();
if (storeId == 0 || IsOverrideChecked(formKey, form))
{
var prop = FastProperty.GetProperty(settingType, settingName);
if (prop != null)
{
dynamic value = prop.GetValue(settings);
var key = string.Concat(settingType.Name, ".", settingName);
settingService.SetSetting(key, value ?? string.Empty, storeId, false);
}
}
else if (storeId > 0)
{
var key = string.Concat(settingType.Name, ".", settingName);
settingService.DeleteSetting(key, storeId);
}
}
}
}