Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
Fix Settings resolving to do it more correct
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarincev committed May 10, 2017
1 parent 38ef89e commit 2a0e8c9
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions VirtoCommerce.LiquidThemeEngine/ShopifyLiquidThemeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using VirtoCommerce.LiquidThemeEngine.Tags;
using VirtoCommerce.Storefront.Model;
using VirtoCommerce.Storefront.Model.Common;
using VirtoCommerce.Storefront.Model.Common.Exceptions;
using VirtoCommerce.Storefront.Model.Services;

namespace VirtoCommerce.LiquidThemeEngine
Expand Down Expand Up @@ -315,32 +316,43 @@ public IDictionary GetSettings(string defaultValue = null)
var retVal = new DefaultableDictionary(defaultValue);

//Load all data from current theme config
JObject currentThemeSettings = InnerGetAllSettings(_themeBlobProvider, CurrentThemePath);
var currentThemeSettings = InnerGetAllSettings(_themeBlobProvider, CurrentThemePath);

//Load all data from global theme config
JObject globalSettings = InnerGetAllSettings(_globalThemeBlobProvider, string.Empty);
var resultSettings = InnerGetAllSettings(_globalThemeBlobProvider, string.Empty);

//Merge two configs
globalSettings.Merge(currentThemeSettings, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Merge });
if (currentThemeSettings != null)
{
//Merge two configs
resultSettings.Merge(currentThemeSettings, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Merge });
}

//Get actual preset from merged config
JValue currentPreset = globalSettings.GetValue("current") as JValue;
JObject presets = globalSettings.GetValue("presets") as JObject;
JObject resultSettings = null;

if (currentPreset != null && presets != null && presets.Children().Any())
var currentPreset = resultSettings.GetValue("current");
if(currentPreset is JValue)
{
string currentPresetName = currentPreset.Value.ToString();
string currentPresetName = ((JValue)currentPreset).Value.ToString();
var presets = resultSettings.GetValue("presets") as JObject;
if (presets == null || !presets.Children().Any())
{
throw new StorefrontException("Setting presets not defined");
}

IList<JProperty> allPresets = presets.Children().Cast<JProperty>().ToList();
resultSettings = allPresets.FirstOrDefault(p => p.Name == currentPresetName).Value as JObject;
if(resultSettings == null)
{
throw new StorefrontException($"Setting preset with name '{currentPresetName}' not found");
}
}

if (resultSettings != null)
if (currentPreset is JObject)
{
var dict = resultSettings.ToObject<Dictionary<string, object>>().ToDictionary(x => x.Key, x => x.Value);
retVal = new DefaultableDictionary(dict, defaultValue);
resultSettings = (JObject)currentPreset;
}

var dict = resultSettings.ToObject<Dictionary<string, object>>().ToDictionary(x => x.Key, x => x.Value);
retVal = new DefaultableDictionary(dict, defaultValue);

return retVal;
});
}
Expand Down

0 comments on commit 2a0e8c9

Please sign in to comment.