2012-12-09 03:22:11 +05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2013-05-07 18:56:24 -10:00
|
|
|
|
using System.Diagnostics;
|
2016-02-04 17:14:29 +01:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2012-12-09 03:22:11 +05:00
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
2013-09-05 17:47:13 +02:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
2018-01-24 13:37:14 +01:00
|
|
|
|
using Umbraco.Core.PropertyEditors.ValueConverters;
|
2012-12-09 03:22:11 +05:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models
|
|
|
|
|
|
{
|
2013-03-02 01:02:38 +06:00
|
|
|
|
/// <summary>
|
2013-09-05 17:47:13 +02:00
|
|
|
|
/// Provide an abstract base class for <c>IPublishedContent</c> implementations.
|
2017-07-20 11:21:28 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>This base class does which (a) consitently resolves and caches the Url, (b) provides an implementation
|
2013-09-05 17:47:13 +02:00
|
|
|
|
/// for this[alias], and (c) provides basic content set management.</remarks>
|
2013-05-07 18:56:24 -10:00
|
|
|
|
[DebuggerDisplay("Content Id: {Id}, Name: {Name}")]
|
2013-09-05 17:47:13 +02:00
|
|
|
|
public abstract class PublishedContentBase : IPublishedContent
|
|
|
|
|
|
{
|
|
|
|
|
|
#region Content
|
2012-12-10 03:40:50 +05:00
|
|
|
|
|
2013-09-05 17:47:13 +02:00
|
|
|
|
private string _url;
|
2013-03-02 01:02:38 +06:00
|
|
|
|
|
2017-07-20 11:21:28 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the url of the content.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// If this content is Content, the url that is returned is the one computed by the NiceUrlProvider, otherwise if
|
|
|
|
|
|
/// this content is Media, the url returned is the value found in the 'umbracoFile' property.
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public virtual string Url
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
// should be thread-safe although it won't prevent url from being resolved more than once
|
|
|
|
|
|
if (_url != null)
|
|
|
|
|
|
return _url;
|
|
|
|
|
|
|
|
|
|
|
|
switch (ItemType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case PublishedItemType.Content:
|
|
|
|
|
|
if (UmbracoContext.Current == null)
|
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
|
"Cannot resolve a Url for a content item when UmbracoContext.Current is null.");
|
|
|
|
|
|
if (UmbracoContext.Current.UrlProvider == null)
|
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
|
"Cannot resolve a Url for a content item when UmbracoContext.Current.UrlProvider is null.");
|
|
|
|
|
|
_url = UmbracoContext.Current.UrlProvider.GetUrl(Id);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case PublishedItemType.Media:
|
|
|
|
|
|
var prop = GetProperty(Constants.Conventions.Media.File);
|
2017-12-06 11:51:35 +01:00
|
|
|
|
if (prop == null || prop.GetValue() == null)
|
2017-07-20 11:21:28 +02:00
|
|
|
|
{
|
|
|
|
|
|
_url = string.Empty;
|
|
|
|
|
|
return _url;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var propType = ContentType.GetPropertyType(Constants.Conventions.Media.File);
|
|
|
|
|
|
|
2018-01-24 13:37:14 +01:00
|
|
|
|
// fixme this is horrible we need url providers for media too
|
2017-07-20 11:21:28 +02:00
|
|
|
|
//This is a hack - since we now have 2 properties that support a URL: upload and cropper, we need to detect this since we always
|
|
|
|
|
|
// want to return the normal URL and the cropper stores data as json
|
2018-01-10 12:48:51 +01:00
|
|
|
|
switch (propType.EditorAlias)
|
2017-07-20 11:21:28 +02:00
|
|
|
|
{
|
2018-01-20 12:09:15 +01:00
|
|
|
|
case Constants.PropertyEditors.Aliases.UploadField:
|
2017-12-06 11:51:35 +01:00
|
|
|
|
_url = prop.GetValue().ToString();
|
2017-07-20 11:21:28 +02:00
|
|
|
|
break;
|
2018-01-20 12:09:15 +01:00
|
|
|
|
case Constants.PropertyEditors.Aliases.ImageCropper:
|
2017-07-20 11:21:28 +02:00
|
|
|
|
//get the url from the json format
|
|
|
|
|
|
|
2018-01-24 13:37:14 +01:00
|
|
|
|
var stronglyTyped = prop.GetValue() as ImageCropperValue;
|
2017-07-20 11:21:28 +02:00
|
|
|
|
if (stronglyTyped != null)
|
|
|
|
|
|
{
|
2016-02-04 17:14:29 +01:00
|
|
|
|
_url = stronglyTyped.Src;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2018-01-24 13:37:14 +01:00
|
|
|
|
_url = prop.GetValue()?.ToString();
|
2017-07-20 11:21:28 +02:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _url;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public abstract PublishedItemType ItemType { get; }
|
|
|
|
|
|
public abstract int Id { get; }
|
2016-06-09 19:38:51 +02:00
|
|
|
|
public abstract Guid Key { get; }
|
|
|
|
|
|
public abstract int TemplateId { get; }
|
2017-07-20 11:21:28 +02:00
|
|
|
|
public abstract int SortOrder { get; }
|
2018-04-24 01:31:01 +10:00
|
|
|
|
public abstract string Name { get; }
|
|
|
|
|
|
//TODO: On the base ContentData instance this dictionary contains a CultureVariation, should we expose that model here or a different model?
|
|
|
|
|
|
public abstract IReadOnlyDictionary<string, PublishedCultureName> CultureNames { get; }
|
2017-07-20 11:21:28 +02:00
|
|
|
|
public abstract string UrlName { get; }
|
|
|
|
|
|
public abstract string DocumentTypeAlias { get; }
|
|
|
|
|
|
public abstract int DocumentTypeId { get; }
|
|
|
|
|
|
public abstract string WriterName { get; }
|
|
|
|
|
|
public abstract string CreatorName { get; }
|
|
|
|
|
|
public abstract int WriterId { get; }
|
|
|
|
|
|
public abstract int CreatorId { get; }
|
|
|
|
|
|
public abstract string Path { get; }
|
|
|
|
|
|
public abstract DateTime CreateDate { get; }
|
|
|
|
|
|
public abstract DateTime UpdateDate { get; }
|
|
|
|
|
|
public abstract int Level { get; }
|
2012-12-09 03:22:11 +05:00
|
|
|
|
|
2013-09-05 17:47:13 +02:00
|
|
|
|
public abstract bool IsDraft { get; }
|
2016-06-09 19:38:51 +02:00
|
|
|
|
|
2013-09-05 17:47:13 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Tree
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the parent of the content.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract IPublishedContent Parent { get; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the children of the content.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>Children are sorted by their sortOrder.</remarks>
|
|
|
|
|
|
public abstract IEnumerable<IPublishedContent> Children { get; }
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region ContentType
|
|
|
|
|
|
|
|
|
|
|
|
public abstract PublishedContentType ContentType { get; }
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the properties of the content.
|
|
|
|
|
|
/// </summary>
|
2016-06-10 16:37:28 +02:00
|
|
|
|
public abstract IEnumerable<IPublishedProperty> Properties { get; }
|
2012-12-09 03:22:11 +05:00
|
|
|
|
|
2013-09-05 17:47:13 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a property identified by its alias.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="alias">The property alias.</param>
|
|
|
|
|
|
/// <returns>The property identified by the alias.</returns>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// <para>If no property with the specified alias exists, returns <c>null</c>.</para>
|
|
|
|
|
|
/// <para>The returned property may have no value (ie <c>HasValue</c> is <c>false</c>).</para>
|
|
|
|
|
|
/// <para>The alias is case-insensitive.</para>
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public abstract IPublishedProperty GetProperty(string alias);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a property identified by its alias.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="alias">The property alias.</param>
|
|
|
|
|
|
/// <param name="recurse">A value indicating whether to navigate the tree upwards until a property with a value is found.</param>
|
|
|
|
|
|
/// <returns>The property identified by the alias.</returns>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// <para>Navigate the tree upwards and look for a property with that alias and with a value (ie <c>HasValue</c> is <c>true</c>).
|
|
|
|
|
|
/// If found, return the property. If no property with that alias is found, having a value or not, return <c>null</c>. Otherwise
|
|
|
|
|
|
/// return the first property that was found with the alias but had no value (ie <c>HasValue</c> is <c>false</c>).</para>
|
|
|
|
|
|
/// <para>The alias is case-insensitive.</para>
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public virtual IPublishedProperty GetProperty(string alias, bool recurse)
|
|
|
|
|
|
{
|
|
|
|
|
|
var property = GetProperty(alias);
|
|
|
|
|
|
if (recurse == false) return property;
|
|
|
|
|
|
|
|
|
|
|
|
IPublishedContent content = this;
|
|
|
|
|
|
var firstNonNullProperty = property;
|
2017-12-06 11:51:35 +01:00
|
|
|
|
while (content != null && (property == null || property.HasValue() == false))
|
2013-09-05 17:47:13 +02:00
|
|
|
|
{
|
|
|
|
|
|
content = content.Parent;
|
2017-07-27 11:09:53 +02:00
|
|
|
|
property = content?.GetProperty(alias);
|
2013-09-05 17:47:13 +02:00
|
|
|
|
if (firstNonNullProperty == null && property != null) firstNonNullProperty = property;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if we find a content with the property with a value, return that property
|
|
|
|
|
|
// if we find no content with the property, return null
|
|
|
|
|
|
// if we find a content with the property without a value, return that property
|
|
|
|
|
|
// have to save that first property while we look further up, hence firstNonNullProperty
|
|
|
|
|
|
|
2017-12-06 11:51:35 +01:00
|
|
|
|
return property != null && property.HasValue() ? property : firstNonNullProperty;
|
2013-09-05 17:47:13 +02:00
|
|
|
|
}
|
2013-03-02 01:02:38 +06:00
|
|
|
|
|
2013-09-05 17:47:13 +02:00
|
|
|
|
#endregion
|
2013-03-02 01:02:38 +06:00
|
|
|
|
}
|
2012-12-09 03:22:11 +05:00
|
|
|
|
}
|