Initially adding the Core Value Converters package into Core! Currently this will break all the existing things but it makes views insanely simple with ModelsBuilder!!
This commit is contained in:
184
src/Umbraco.Web/Models/RelatedLink.cs
Normal file
184
src/Umbraco.Web/Models/RelatedLink.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="RelatedLink.cs" company="Umbraco">
|
||||
// Umbraco
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Defines the RelatedLink type.
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Umbraco.Web.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// The related link model
|
||||
/// </summary>
|
||||
public class RelatedLink
|
||||
{
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
/// <summary>
|
||||
/// The _link item.
|
||||
/// </summary>
|
||||
private readonly JToken _linkItem;
|
||||
|
||||
/// <summary>
|
||||
/// The _caption.
|
||||
/// </summary>
|
||||
private string _caption;
|
||||
|
||||
/// <summary>
|
||||
/// The _new window.
|
||||
/// </summary>
|
||||
private bool? _newWindow;
|
||||
|
||||
/// <summary>
|
||||
/// The _is internal.
|
||||
/// </summary>
|
||||
private bool? _isInternal;
|
||||
|
||||
/// <summary>
|
||||
/// The _link.
|
||||
/// </summary>
|
||||
private string _link;
|
||||
|
||||
/// <summary>
|
||||
/// The _linkDeleted.
|
||||
/// </summary>
|
||||
private bool? _linkDeleted;
|
||||
|
||||
/// <summary>
|
||||
/// The _type.
|
||||
/// </summary>
|
||||
private RelatedLinkType _type;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RelatedLink"/> class.
|
||||
/// </summary>
|
||||
/// <param name="linkItem">
|
||||
/// The link item.
|
||||
/// </param>
|
||||
public RelatedLink(JToken linkItem)
|
||||
{
|
||||
this._linkItem = linkItem;
|
||||
|
||||
// get the current Link to set the _linkDeleted is a internal link
|
||||
var currentLink = this.Link;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the caption.
|
||||
/// </summary>
|
||||
public string Caption
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(this._caption))
|
||||
{
|
||||
this._caption = this._linkItem.Value<string>("caption");
|
||||
}
|
||||
|
||||
return this._caption;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether new window.
|
||||
/// </summary>
|
||||
public bool NewWindow
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._newWindow == null)
|
||||
{
|
||||
this._newWindow = this._linkItem.Value<bool>("newWindow");
|
||||
}
|
||||
|
||||
return this._newWindow.GetValueOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the link is internal.
|
||||
/// </summary>
|
||||
public bool IsInternal
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this._isInternal == null)
|
||||
{
|
||||
this._isInternal = this._linkItem.Value<bool>("isInternal");
|
||||
}
|
||||
|
||||
return this._isInternal.GetValueOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type.
|
||||
/// </summary>
|
||||
public RelatedLinkType? Type
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Enum.TryParse(this._linkItem.Value<string>("type"), true, out this._type))
|
||||
{
|
||||
return this._type;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the link.
|
||||
/// </summary>
|
||||
public string Link
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(this._link))
|
||||
{
|
||||
if (this.IsInternal)
|
||||
{
|
||||
if (UmbracoContext.Current == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
this._link = UmbracoContext.Current.UrlProvider.GetUrl(this._linkItem.Value<int>("internal"));
|
||||
if (this._link.Equals("#"))
|
||||
{
|
||||
this._linkDeleted = true;
|
||||
this._link = this._linkItem.Value<string>("internal");
|
||||
}
|
||||
else
|
||||
{
|
||||
this._linkDeleted = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this._link = this._linkItem.Value<string>("link");
|
||||
}
|
||||
}
|
||||
|
||||
return this._link;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether deleted.
|
||||
/// </summary>
|
||||
internal bool InternalLinkDeleted
|
||||
{
|
||||
get
|
||||
{
|
||||
var linkDeleted = this._linkDeleted;
|
||||
return linkDeleted != null && (bool)linkDeleted;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
src/Umbraco.Web/Models/RelatedLinkType.cs
Normal file
27
src/Umbraco.Web/Models/RelatedLinkType.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="RelatedLinkType.cs" company="Umbraco">
|
||||
// Umbraco
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Defines the RelatedLinkType type.
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace Umbraco.Web.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// The related link type.
|
||||
/// </summary>
|
||||
public enum RelatedLinkType
|
||||
{
|
||||
/// <summary>
|
||||
/// Internal link type
|
||||
/// </summary>
|
||||
Internal,
|
||||
|
||||
/// <summary>
|
||||
/// External link type
|
||||
/// </summary>
|
||||
External
|
||||
}
|
||||
}
|
||||
110
src/Umbraco.Web/Models/RelatedLinks.cs
Normal file
110
src/Umbraco.Web/Models/RelatedLinks.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="RelatedLinks.cs" company="Umbraco">
|
||||
// Umbraco
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// Defines the RelatedLinks type.
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Umbraco.Core.Logging;
|
||||
|
||||
namespace Umbraco.Web.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// The related links model
|
||||
/// </summary>
|
||||
public class RelatedLinks : IEnumerable<RelatedLink>
|
||||
{
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
/// <summary>
|
||||
/// The _property data.
|
||||
/// </summary>
|
||||
private readonly string _propertyData;
|
||||
|
||||
/// <summary>
|
||||
/// The _related links.
|
||||
/// </summary>
|
||||
private readonly List<RelatedLink> _relatedLinks = new List<RelatedLink>();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RelatedLinks"/> class.
|
||||
/// </summary>
|
||||
/// <param name="propertyData">
|
||||
/// The property data.
|
||||
/// </param>
|
||||
public RelatedLinks(string propertyData)
|
||||
{
|
||||
this._propertyData = propertyData;
|
||||
|
||||
if (!string.IsNullOrEmpty(propertyData))
|
||||
{
|
||||
var relatedLinks = JsonConvert.DeserializeObject<JArray>(propertyData);
|
||||
|
||||
foreach (var item in relatedLinks)
|
||||
{
|
||||
var relatedLink = new RelatedLink(item);
|
||||
if (!relatedLink.InternalLinkDeleted)
|
||||
{
|
||||
this._relatedLinks.Add(relatedLink);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogHelper.Warn<RelatedLinks>(
|
||||
string.Format("Related Links value converter skipped a link as the node has been unpublished/deleted (Internal Link NodeId: {0}, Link Caption: \"{1}\")", relatedLink.Link, relatedLink.Caption));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the property data.
|
||||
/// </summary>
|
||||
public string PropertyData
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._propertyData;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The any.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The <see cref="bool"/>.
|
||||
/// </returns>
|
||||
public bool Any()
|
||||
{
|
||||
return Enumerable.Any(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The get enumerator.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The <see cref="IEnumerator"/>.
|
||||
/// </returns>
|
||||
public IEnumerator<RelatedLink> GetEnumerator()
|
||||
{
|
||||
return this._relatedLinks.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The get enumerator.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The <see cref="IEnumerator"/>.
|
||||
/// </returns>
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return this.GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user