Last active
August 29, 2015 14:03
-
-
Save kgiszewski/67787c06629723c7b329 to your computer and use it in GitHub Desktop.
Store Hours Prop Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//model | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace StoreHours.Models | |
{ | |
public class StoreHoursModel | |
{ | |
public string Sunday { get; set; } | |
public string Monday { get; set; } | |
public string Tuesday { get; set; } | |
public string Wednesday { get; set; } | |
public string Thursday { get; set; } | |
public string Friday { get; set; } | |
public string Saturday { get; set; } | |
} | |
} | |
//Prop converter | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using Umbraco.Core; | |
using Umbraco.Web; | |
using Umbraco.Core.Models.PublishedContent; | |
using Umbraco.Core.PropertyEditors; | |
using Umbraco.Core.Logging; | |
using StoreHours.Models; | |
namespace StoreHours.PropertyConverter | |
{ | |
[PropertyValueType(typeof(StoreHoursModel))] | |
[PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)] | |
public class TableEditorValueConverter : PropertyValueConverterBase | |
{ | |
public override bool IsConverter(PublishedPropertyType propertyType) | |
{ | |
return propertyType.PropertyEditorAlias.Equals("Imulus.StoreHours"); | |
} | |
public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview) | |
{ | |
if (source == null) | |
{ | |
return new StoreHoursModel(); | |
} | |
var sourceString = source.ToString(); | |
try | |
{ | |
var value = JsonConvert.DeserializeObject<StoreHoursModel>(sourceString); | |
return value; | |
} | |
catch (Exception ex) | |
{ | |
LogHelper.Error<StoreHoursModel>(ex.Message, ex); | |
return new StoreHoursModel(); | |
} | |
return sourceString; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment