Skip to content

Commit

Permalink
Merge pull request #95 from lssweatherhead/XPath-In-Use-Language-Retr…
Browse files Browse the repository at this point in the history
…ieval-Issues-On-Unpublished-Nodes

XPath "in-use" language retrieval issues on unpublished nodes
  • Loading branch information
mattbrailsford authored Nov 9, 2017
2 parents 9cd29ac + 4d9105c commit a5d1fdd
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions src/Our.Umbraco.Vorto/Web/Controllers/VortoApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,42 @@ 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",
string.Format("//*[@id={0} and @isDoc]", id)).Replace("$parentPage",
string.Format("//*[@id={0} and @isDoc]", parentId)).Replace("$ancestorOrSelf",
string.Format("//*[@id={0} and @isDoc]", id != 0 ? id : parentId));

// 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;

//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",
string.Format("//*[@id={0} and @isDoc]", id)).Replace("$parentPage",
string.Format("//*[@id={0} and @isDoc]", parentId)).Replace("$ancestorOrSelf",
string.Format("//*[@id={0} and @isDoc]", currentNodeIsUnpublished ? parentId : id));

// 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

0 comments on commit a5d1fdd

Please sign in to comment.