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-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-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-11-11 06:53:02 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IUserRepository, IUser, int>(null);
|
|
|
|
|
|
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-11-11 06:53:02 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IUserRepository, IUser, int>(null);
|
|
|
|
|
|
return repository.GetProfileById(content.WriterId);
|
2012-11-06 15:17:58 -01:00
|
|
|
|
}
|
2012-10-05 11:03:08 -02:00
|
|
|
|
}
|
|
|
|
|
|
}
|