Skip to content

Commit

Permalink
Fixes: U4-5943 Cannot render a macro on a virtual page when using Umb…
Browse files Browse the repository at this point in the history
…racoVirtualNodeRouteHandler and assigning virtual Ids
  • Loading branch information
Shazwazza committed Jan 2, 2015
1 parent 201464c commit 73e9ff0
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 173 deletions.
8 changes: 7 additions & 1 deletion src/Umbraco.Web/Macros/PartialViewMacroEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using umbraco.cms.businesslogic.macro;
using Umbraco.Core.Models;
using umbraco.interfaces;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using Umbraco.Core;
using System.Web.Mvc.Html;
Expand Down Expand Up @@ -102,7 +103,12 @@ public bool Validate(string code, string tempFileName, INode currentPage, out st
public string Execute(MacroModel macro, INode node)
{
var umbCtx = _getUmbracoContext();
return Execute(macro, umbCtx.ContentCache.GetById(node.Id));
//NOTE: This is a bit of a nasty hack to check if the INode is actually already based on an IPublishedContent
// (will be the case when using LegacyConvertedNode )
return Execute(macro,
(node is IPublishedContent)
? (IPublishedContent)node
: umbCtx.ContentCache.GetById(node.Id));
}

public string Execute(MacroModel macro, IPublishedContent content)
Expand Down
182 changes: 182 additions & 0 deletions src/Umbraco.Web/Models/LegacyConvertedNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using umbraco.interfaces;

namespace Umbraco.Web.Models
{
/// <summary>
/// A legacy INode that wraps IPublishedContent
/// </summary>
internal class LegacyConvertedNode : PublishedContentWrapped, INode
{
private readonly IPublishedContent _publishedContent;
private readonly int _id;
private readonly int _template;
private readonly int _sortOrder;
private readonly string _name;
private readonly string _urlName;
private readonly string _nodeTypeAlias;
private readonly string _writerName;
private readonly string _creatorName;
private readonly int _writerId;
private readonly int _creatorId;
private readonly string _path;
private readonly DateTime _createDate;
private readonly DateTime _updateDate;
private readonly Guid _version;
private readonly int _level;

public LegacyConvertedNode(IPublishedContent publishedContent) : base(publishedContent)
{
_publishedContent = publishedContent;

if (publishedContent == null)
{
_id = 0;
return;
}

_template = publishedContent.TemplateId;
_id = publishedContent.Id;
_path = publishedContent.Path;
_creatorName = publishedContent.CreatorName;
_sortOrder = publishedContent.SortOrder;
_updateDate = publishedContent.UpdateDate;
_name = publishedContent.Name;
_nodeTypeAlias = publishedContent.DocumentTypeAlias;
_createDate = publishedContent.CreateDate;
_creatorId = publishedContent.CreatorId;
_level = publishedContent.Level;
_urlName = publishedContent.UrlName;
_version = publishedContent.Version;
_writerId = publishedContent.WriterId;
_writerName = publishedContent.WriterName;
}

INode INode.Parent
{
get { return _publishedContent.Parent == null ? null : LegacyNodeHelper.ConvertToNode(_publishedContent.Parent); }
}

int INode.Id
{
get { return _id; }
}

int INode.template
{
get { return _template; }
}

int INode.SortOrder
{
get { return _sortOrder; }
}

string INode.Name
{
get { return _name; }
}

string INode.UrlName
{
get { return _urlName; }
}

string INode.NodeTypeAlias
{
get { return _nodeTypeAlias; }
}

string INode.WriterName
{
get { return _writerName; }
}

string INode.CreatorName
{
get { return _creatorName; }
}

int INode.WriterID
{
get { return _writerId; }
}

int INode.CreatorID
{
get { return _creatorId; }
}

string INode.Path
{
get { return _path; }
}

DateTime INode.CreateDate
{
get { return _createDate; }
}

DateTime INode.UpdateDate
{
get { return _updateDate; }
}

Guid INode.Version
{
get { return _version; }
}

string INode.NiceUrl
{
get { return _publishedContent.Url; }
}

string INode.Url
{
get { return _publishedContent.Url; }
}

int INode.Level
{
get { return _level; }
}

List<IProperty> INode.PropertiesAsList
{
get { return _publishedContent.Properties.Select(LegacyNodeHelper.ConvertToNodeProperty).ToList(); }
}

List<INode> INode.ChildrenAsList
{
get { return _publishedContent.Children.Select(LegacyNodeHelper.ConvertToNode).ToList(); }
}

IProperty INode.GetProperty(string alias)
{
return ((INode)this).PropertiesAsList.Cast<global::umbraco.NodeFactory.Property>().FirstOrDefault(p => p.Alias == alias);
}

IProperty INode.GetProperty(string alias, out bool propertyExists)
{
var prop = _publishedContent.GetProperty(alias);
propertyExists = prop != null;
return prop == null ? null : LegacyNodeHelper.ConvertToNodeProperty(prop);
}

DataTable INode.ChildrenAsTable()
{
return _publishedContent.ChildrenAsTable();
}

DataTable INode.ChildrenAsTable(string nodeTypeAliasFilter)
{
return _publishedContent.ChildrenAsTable(nodeTypeAliasFilter);
}
}
}
54 changes: 54 additions & 0 deletions src/Umbraco.Web/Models/LegacyConvertedNodeProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Web;
using Umbraco.Core.Models;
using umbraco.interfaces;

namespace Umbraco.Web.Models
{
/// <summary>
/// A legacy IProperty that wraps IPublishedProperty
/// </summary>
internal class LegacyConvertedNodeProperty : IProperty, IHtmlString
{
public IPublishedProperty PublishedProperty { get; private set; }

public LegacyConvertedNodeProperty(IPublishedProperty prop)
{
PublishedProperty = prop;
}

public string Alias
{
get { return PublishedProperty.PropertyTypeAlias; }
}

public string Value
{
get { return PublishedProperty.DataValue == null ? null : PublishedProperty.DataValue.ToString(); }
}

public Guid Version
{
get { return Guid.Empty; }
}

public bool IsNull()
{
return Value == null;
}

public bool HasValue()
{
return PublishedProperty.HasValue;
}

public int ContextId { get; set; }
public string ContextAlias { get; set; }

// implements IHtmlString.ToHtmlString
public string ToHtmlString()
{
return Value;
}
}
}
25 changes: 25 additions & 0 deletions src/Umbraco.Web/Models/LegacyNodeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Umbraco.Core.Models;
using umbraco.interfaces;

namespace Umbraco.Web.Models
{
/// <summary>
/// Used to convert to/from the legacy INode from IPublishedContent
/// </summary>
internal static class LegacyNodeHelper
{
// NOTE - moved from umbraco.MacroEngines to avoid circ. references

public static INode ConvertToNode(IPublishedContent doc)
{
var node = new LegacyConvertedNode(doc);
return node;
}

public static IProperty ConvertToNodeProperty(IPublishedProperty prop)
{
return new LegacyConvertedNodeProperty(prop);
}

}
}
4 changes: 3 additions & 1 deletion src/Umbraco.Web/Umbraco.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@
</Compile>
<Compile Include="ApplicationContextExtensions.cs" />
<Compile Include="AreaRegistrationContextExtensions.cs" />
<Compile Include="Models\LegacyConvertedNode.cs" />
<Compile Include="Models\LegacyConvertedNodeProperty.cs" />
<Compile Include="Scheduling\BackgroundTaskRunner.cs" />
<Compile Include="BatchedServerMessenger.cs" />
<Compile Include="CacheHelperExtensions.cs" />
Expand Down Expand Up @@ -739,7 +741,7 @@
<Compile Include="UI\Bundles\JsUmbracoApplicationUI.cs" />
<Compile Include="UI\Bundles\JsUmbracoTree.cs" />
<Compile Include="UI\CdfLogger.cs" />
<Compile Include="umbraco.presentation\CompatibilityHelper.cs" />
<Compile Include="Models\LegacyNodeHelper.cs" />
<Compile Include="umbraco.presentation\umbraco\controls\PasswordChanger.ascx.cs">
<SubType>ASPXCodeBehind</SubType>
</Compile>
Expand Down

0 comments on commit 73e9ff0

Please sign in to comment.