Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbrailsford committed Nov 9, 2017
2 parents 5b1e5ba + d482bc0 commit 53d134b
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ packages/
TEMP/
Logs/
NuGetBackup/
src/.vs/
.vs/
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
os: Visual Studio 2015

# version format
version: 1.5.3.{build}
version: 1.6.0.{build}

# UMBRACO_PACKAGE_PRERELEASE_SUFFIX if a rtm release build this should be blank, otherwise if empty will default to alpha
# example UMBRACO_PACKAGE_PRERELEASE_SUFFIX=beta
Expand Down
4 changes: 2 additions & 2 deletions src/Our.Umbraco.Vorto/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Our.Umbraco.Vorto
{
internal static class Constants
{
public const string CacheKey_GetTargetDataTypeDefinition = "Vorto_GetTargetDataTypeDefinition_";
{
public const string CacheKey_GetTargetDataTypeDefinition = "Vorto_GetTargetDataTypeDefinition_";
}
}
148 changes: 80 additions & 68 deletions src/Our.Umbraco.Vorto/Extensions/IPublishedContentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading;
using Newtonsoft.Json;
using Our.Umbraco.Vorto.Helpers;
using Our.Umbraco.Vorto.Models;
using Our.Umbraco.Vorto.Web.PropertyEditors;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
Expand All @@ -13,17 +12,15 @@ namespace Our.Umbraco.Vorto.Extensions
{
public static class IPublishedContentExtensions
{
#region HasValue
#region HasValue

private static bool DoInnerHasVortoValue(this IPublishedContent content, string propertyAlias,
string cultureName = null, bool recursive = false)
{
if (content.HasValue(propertyAlias))
{
object dataValue = content.Properties
.First(p => p.PropertyTypeAlias.InvariantEquals(propertyAlias))
.DataValue;

var prop = content.GetProperty(propertyAlias);
var dataValue = prop.DataValue;
if (dataValue == null)
{
return false;
Expand All @@ -33,14 +30,17 @@ private static bool DoInnerHasVortoValue(this IPublishedContent content, string

try
{
// We purposfully parse the raw data value ourselves bypassing the property
// value converters so that we don't require an UmbracoContext during a
// HasValue check. As we won't actually use the value, this is ok here.
vortoModel = JsonConvert.DeserializeObject<VortoValue>(dataValue.ToString());
}
catch
{
return false;
}

if (vortoModel != null && vortoModel.Values != null)
if (vortoModel?.Values != null)
{
var bestMatchCultureName = vortoModel.FindBestMatchCulture(cultureName);
if (!bestMatchCultureName.IsNullOrWhiteSpace()
Expand Down Expand Up @@ -87,70 +87,67 @@ public static bool HasVortoValue(this IPublishedContent content, string property
return hasValue;
}

#endregion
#endregion

#region GetValue
#region GetValue

private static T DoInnerGetVortoValue<T>(this IPublishedContent content, string propertyAlias, string cultureName = null,
bool recursive = false, T defaultValue = default(T))
{
if (content.HasValue(propertyAlias))
var prop = content.GetProperty(propertyAlias);
var vortoModel = prop.Value as VortoValue;
if (vortoModel?.Values != null)
{
var prop = content.GetProperty(propertyAlias);
var vortoModel = prop.Value as VortoValue;
if (vortoModel != null && vortoModel.Values != null)
// Get the serialized value
var bestMatchCultureName = vortoModel.FindBestMatchCulture(cultureName);
if (!bestMatchCultureName.IsNullOrWhiteSpace()
&& vortoModel.Values.ContainsKey(bestMatchCultureName)
&& vortoModel.Values[bestMatchCultureName] != null
&& !vortoModel.Values[bestMatchCultureName].ToString().IsNullOrWhiteSpace())
{
// Get the serialized value
var bestMatchCultureName = vortoModel.FindBestMatchCulture(cultureName);
if (!bestMatchCultureName.IsNullOrWhiteSpace()
&& vortoModel.Values.ContainsKey(bestMatchCultureName)
&& vortoModel.Values[bestMatchCultureName] != null
&& !vortoModel.Values[bestMatchCultureName].ToString().IsNullOrWhiteSpace())
{
var value = vortoModel.Values[bestMatchCultureName];

// Get target datatype
var targetDataType = VortoHelper.GetTargetDataTypeDefinition(vortoModel.DtdGuid);

// Umbraco has the concept of a IPropertyEditorValueConverter which it
// also queries for property resolvers. However I'm not sure what these
// are for, nor can I find any implementations in core, so am currently
// just ignoring these when looking up converters.
// NB: IPropertyEditorValueConverter not to be confused with
// IPropertyValueConverter which are the ones most people are creating
var properyType = CreateDummyPropertyType(
targetDataType.Id,
targetDataType.PropertyEditorAlias,
content.ContentType);

var inPreviewMode = UmbracoContext.Current != null ? UmbracoContext.Current.InPreviewMode : false;

// Try convert data to source
// We try this first as the value is stored as JSON not
// as XML as would occur in the XML cache as in the act
// of concerting to XML this would ordinarily get called
// but with JSON it doesn't, so we try this first
var converted1 = properyType.ConvertDataToSource(value, inPreviewMode);
if (converted1 is T) return (T)converted1;

var convertAttempt = converted1.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Try convert source to object
// If the source value isn't right, try converting to object
var converted2 = properyType.ConvertSourceToObject(converted1, inPreviewMode);
if (converted2 is T) return (T)converted2;

convertAttempt = converted2.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Try just converting
convertAttempt = value.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Still not right type so return default value
return defaultValue;
}
var value = vortoModel.Values[bestMatchCultureName];

// Get target datatype
var targetDataType = VortoHelper.GetTargetDataTypeDefinition(vortoModel.DtdGuid);

// Umbraco has the concept of a IPropertyEditorValueConverter which it
// also queries for property resolvers. However I'm not sure what these
// are for, nor can I find any implementations in core, so am currently
// just ignoring these when looking up converters.
// NB: IPropertyEditorValueConverter not to be confused with
// IPropertyValueConverter which are the ones most people are creating
var properyType = CreateDummyPropertyType(
targetDataType.Id,
targetDataType.PropertyEditorAlias,
content.ContentType);

var inPreviewMode = UmbracoContext.Current != null && UmbracoContext.Current.InPreviewMode;

// Try convert data to source
// We try this first as the value is stored as JSON not
// as XML as would occur in the XML cache as in the act
// of converting to XML this would ordinarily get called
// but with JSON it doesn't, so we try this first
var converted1 = properyType.ConvertDataToSource(value, inPreviewMode);
if (converted1 is T) return (T)converted1;

var convertAttempt = converted1.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Try convert source to object
// If the source value isn't right, try converting to object
var converted2 = properyType.ConvertSourceToObject(converted1, inPreviewMode);
if (converted2 is T) return (T)converted2;

convertAttempt = converted2.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Try just converting
convertAttempt = value.TryConvertTo<T>();
if (convertAttempt.Success) return convertAttempt.Result;

// Still not right type so return default value
return defaultValue;
}
}

Expand Down Expand Up @@ -206,9 +203,24 @@ public static object GetVortoValue(this IPublishedContent content, string proper
return content.GetVortoValue<object>(propertyAlias, cultureName, recursive, defaultValue, fallbackCultureName);
}

#endregion
#endregion

#region IsVortoProperty

/// <summary>
/// Determines if the given property is a Vorto based property.
/// </summary>
/// <param name="content"></param>
/// <param name="propertyAlias"></param>
public static bool IsVortoProperty(this IPublishedContent content, string propertyAlias)
{
var propertyType = content.ContentType?.GetPropertyType(propertyAlias);
return propertyType?.PropertyEditorAlias.InvariantEquals(VortoPropertyEditor.PropertyEditorAlias) ?? false;
}

#endregion

private static PublishedPropertyType CreateDummyPropertyType(int dataTypeId, string propertyEditorAlias, PublishedContentType contentType)
private static PublishedPropertyType CreateDummyPropertyType(int dataTypeId, string propertyEditorAlias, PublishedContentType contentType)
{
return new PublishedPropertyType(contentType,
new PropertyType(new DataTypeDefinition(-1, propertyEditorAlias)
Expand Down
5 changes: 3 additions & 2 deletions src/Our.Umbraco.Vorto/Our.Umbraco.Vorto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\Our.Umbraco.Vorto.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down Expand Up @@ -266,8 +267,8 @@ REM #################################################

IF %25ComputerName%25 == MBP13-PC-BC (
IF NOT "$(SolutionDir)" == "*Undefined*" (
xcopy /s /y "$(TargetPath)" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoCms.7.4.2\bin"
xcopy /s /y "$(TargetDir)Our.Umbraco.Vorto.pdb" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoCms.7.4.2\bin"
xcopy /s /y "$(TargetPath)" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoCms.7.4.0\bin"
xcopy /s /y "$(TargetDir)Our.Umbraco.Vorto.pdb" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoCms.7.4.0\bin"
xcopy /s /y "$(ProjectDir)Web\UI\*" "C:\Users\Matt\Work\Sandbox\Umbraco\UmbracoCms.7.4.0"
)
)</PostBuildEvent>
Expand Down
68 changes: 38 additions & 30 deletions src/Our.Umbraco.Vorto/Web/Controllers/VortoApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net;
using System.Threading;
using System.Web.Http;
using Our.Umbraco.Vorto.Web.PropertyEditors;
using umbraco;
using Umbraco.Core;
using Umbraco.Core.Models;
Expand All @@ -21,7 +22,7 @@ public class VortoApiController : UmbracoAuthorizedJsonController
public IEnumerable<object> GetNonVortoDataTypes()
{
return Services.DataTypeService.GetAllDataTypeDefinitions()
.Where(x => x.PropertyEditorAlias != "Our.Umbraco.Vorto")
.Where(x => x.PropertyEditorAlias != VortoPropertyEditor.PropertyEditorAlias)
.OrderBy(x => x.SortOrder)
.Select(x => new
{
Expand Down Expand Up @@ -96,36 +97,43 @@ public IEnumerable<object> GetLanguages(string section, int id, int parentId, Gu

if (languageSource == "inuse")
{
var xpath = preValues.ContainsKey("xpath") ? preValues["xpath"].Value : "";

// Grab languages by xpath (only if in content section)
if (!string.IsNullOrWhiteSpace(xpath) && section == "content")
{
xpath = xpath.Replace("$currentPage",
$"//*[@id={id} and @isDoc]").Replace("$parentPage",
$"//*[@id={parentId} and @isDoc]").Replace("$ancestorOrSelf",
$"//*[@id={(id != 0 ? id : parentId)} and @isDoc]");

// Lookup language nodes
var nodeIds = uQuery.GetNodesByXPath(xpath).Select(x => x.Id).ToArray();
if (nodeIds.Any())
{
var db = ApplicationContext.Current.DatabaseContext.Database;
languages.AddRange(db.Query<string>(
string.Format(
"SELECT DISTINCT [languageISOCode] FROM [umbracoLanguage] JOIN [umbracoDomains] ON [umbracoDomains].[domainDefaultLanguage] = [umbracoLanguage].[id] WHERE [umbracoDomains].[domainRootStructureID] in ({0})",
string.Join(",", nodeIds)))
.Select(CultureInfo.GetCultureInfo)
.Select(x => new Language
{
IsoCode = x.Name,
Name = x.DisplayName,
NativeName = x.NativeName,
var currentNode = id != 0 ? Umbraco.TypedContent(id) : null;
var currentNodeIsUnpublished = currentNode == null;
var parentOrSelfId = currentNodeIsUnpublished ? parentId : id;

// trying to add/publish a home node, so no "in use" languages have been defined/are accessible - display all installed in the interim
var currentNodeIsUnpublishedRootNode = currentNodeIsUnpublished && parentId == -1;

var xpath = preValues.ContainsKey("xpath") ? preValues["xpath"].Value : "";

// Grab languages by xpath (only if in content section)
if (!currentNodeIsUnpublishedRootNode && !string.IsNullOrWhiteSpace(xpath) && section == "content")
{
xpath = xpath.Replace("$currentPage",
$"//*[@id={id} and @isDoc]").Replace("$parentPage",
$"//*[@id={parentId} and @isDoc]").Replace("$ancestorOrSelf",
$"//*[@id={parentOrSelfId} and @isDoc]");

// Lookup language nodes
var nodeIds = uQuery.GetNodesByXPath(xpath).Select(x => x.Id).ToArray();
if (nodeIds.Any())
{
var db = ApplicationContext.Current.DatabaseContext.Database;
languages.AddRange(db.Query<string>(
string.Format(
"SELECT DISTINCT [languageISOCode] FROM [umbracoLanguage] JOIN [umbracoDomains] ON [umbracoDomains].[domainDefaultLanguage] = [umbracoLanguage].[id] WHERE [umbracoDomains].[domainRootStructureID] in ({0})",
string.Join(",", nodeIds)))
.Select(CultureInfo.GetCultureInfo)
.Select(x => new Language
{
IsoCode = x.Name,
Name = x.DisplayName,
NativeName = x.NativeName,
IsRightToLeft = x.TextInfo.IsRightToLeft
}));
}
}
else
}));
}
}
else
{
// No language node xpath so just return a list of all languages in use
var db = ApplicationContext.Current.DatabaseContext.Database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ void ServerVariablesParser_Parsing(object sender, Dictionary<string, object> e)
var mainDictionary = new Dictionary<string, object>
{
{
"apiBaseUrl",
urlHelper.GetUmbracoApiServiceBaseUrl<VortoApiController>(
"apiBaseUrl", urlHelper.GetUmbracoApiServiceBaseUrl<VortoApiController>(
controller => controller.GetInstalledLanguages())
}
};
Expand Down
Loading

0 comments on commit 53d134b

Please sign in to comment.