Files
Umbraco-CMS/src/Umbraco.Core/Models/ContentExtensions.cs

82 lines
3.3 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using Umbraco.Core.Models.Membership;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Services;
namespace Umbraco.Core.Models
{
public static class ContentExtensions
{
/// <summary>
/// Set property values by alias with an annonymous object
/// </summary>
public static void PropertyValues(this IContent content, object value)
{
if (value == null)
throw new Exception("No properties has been passed in");
var propertyInfos = value.GetType().GetProperties();
foreach (var propertyInfo in propertyInfos)
{
//Check if a PropertyType with alias exists thus being a valid property
var propertyType = content.PropertyTypes.FirstOrDefault(x => x.Alias == propertyInfo.Name);
if (propertyType == null)
throw new Exception(
string.Format(
"The property alias {0} is not valid, because no PropertyType with this alias exists",
propertyInfo.Name));
//Check if a Property with the alias already exists in the collection thus being updated or inserted
var item = content.Properties.FirstOrDefault(x => x.Alias == propertyInfo.Name);
if (item != null)
{
item.Value = propertyInfo.GetValue(value, null);
//Update item with newly added value
content.Properties.Add(item);
}
else
{
//Create new Property to add to collection
var property = propertyType.CreatePropertyFromValue(propertyInfo.GetValue(value, null));
content.Properties.Add(property);
}
}
}
/// <summary>
/// Checks whether an <see cref="IContent"/> item has any published versions
/// </summary>
/// <param name="content"></param>
/// <returns>True if the content has any published versiom otherwise False</returns>
public static bool HasPublishedVersion(this IContent content)
{
if (content.HasIdentity == false)
return false;
return ServiceContext.Current.ContentService.HasPublishedVersion(content.Id);
}
/// <summary>
/// Gets the <see cref="IProfile"/> for the Creator of this content.
/// </summary>
public static IProfile GetCreatorProfile(this IContent content)
{
var repository = RepositoryResolver.Current.Factory.CreateUserRepository(
new PetaPocoUnitOfWork());
return repository.GetProfileById(content.CreatorId);
}
/// <summary>
/// Gets the <see cref="IProfile"/> for the Writer of this content.
/// </summary>
public static IProfile GetWriterProfile(this IContent content)
{
var repository = RepositoryResolver.Current.Factory.CreateUserRepository(
new PetaPocoUnitOfWork());
return repository.GetProfileById(content.WriterId);
}
}
}