2012-10-05 11:03:08 -02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
2012-11-11 06:53:02 -01:00
|
|
|
|
using Umbraco.Core.Models.Membership;
|
|
|
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
|
using Umbraco.Core.Persistence.Repositories;
|
2012-12-10 02:58:23 +05:00
|
|
|
|
using Umbraco.Core.Persistence.UnitOfWork;
|
2012-12-07 08:48:46 -01:00
|
|
|
|
using Umbraco.Core.Services;
|
2012-10-05 11:03:08 -02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Models
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class ContentExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Set property values by alias with an annonymous object
|
|
|
|
|
|
/// </summary>
|
2012-11-02 09:11:23 -01:00
|
|
|
|
public static void PropertyValues(this IContent content, object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (value == null)
|
|
|
|
|
|
throw new Exception("No properties has been passed in");
|
2012-10-05 11:03:08 -02:00
|
|
|
|
|
2012-11-02 09:11:23 -01:00
|
|
|
|
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));
|
2012-10-05 11:03:08 -02:00
|
|
|
|
|
2012-11-02 09:11:23 -01:00
|
|
|
|
//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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-12-07 08:48:46 -01:00
|
|
|
|
/// <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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-06 10:47:14 -01:00
|
|
|
|
/// <summary>
|
2012-11-11 06:53:02 -01:00
|
|
|
|
/// Gets the <see cref="IProfile"/> for the Creator of this content.
|
2012-11-06 10:47:14 -01:00
|
|
|
|
/// </summary>
|
2012-11-11 06:53:02 -01:00
|
|
|
|
public static IProfile GetCreatorProfile(this IContent content)
|
2012-11-06 10:47:14 -01:00
|
|
|
|
{
|
2012-12-10 02:58:23 +05:00
|
|
|
|
var repository = RepositoryResolver.Current.Factory.CreateUserRepository(
|
|
|
|
|
|
new PetaPocoUnitOfWork());
|
2012-11-11 06:53:02 -01:00
|
|
|
|
return repository.GetProfileById(content.CreatorId);
|
2012-11-06 10:47:14 -01:00
|
|
|
|
}
|
2012-11-06 15:17:58 -01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2012-11-11 06:53:02 -01:00
|
|
|
|
/// Gets the <see cref="IProfile"/> for the Writer of this content.
|
2012-11-06 15:17:58 -01:00
|
|
|
|
/// </summary>
|
2012-11-11 06:53:02 -01:00
|
|
|
|
public static IProfile GetWriterProfile(this IContent content)
|
2012-11-06 15:17:58 -01:00
|
|
|
|
{
|
2012-12-10 02:58:23 +05:00
|
|
|
|
var repository = RepositoryResolver.Current.Factory.CreateUserRepository(
|
|
|
|
|
|
new PetaPocoUnitOfWork());
|
2012-11-11 06:53:02 -01:00
|
|
|
|
return repository.GetProfileById(content.WriterId);
|
2012-11-06 15:17:58 -01:00
|
|
|
|
}
|
2012-10-05 11:03:08 -02:00
|
|
|
|
}
|
|
|
|
|
|
}
|