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 { /// /// Set property values by alias with an annonymous object /// 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); } } } /// /// Checks whether an item has any published versions /// /// /// True if the content has any published versiom otherwise False public static bool HasPublishedVersion(this IContent content) { if (content.HasIdentity == false) return false; return ServiceContext.Current.ContentService.HasPublishedVersion(content.Id); } /// /// Gets the for the Creator of this content. /// public static IProfile GetCreatorProfile(this IContent content) { var repository = RepositoryResolver.Current.Factory.CreateUserRepository( new PetaPocoUnitOfWork()); return repository.GetProfileById(content.CreatorId); } /// /// Gets the for the Writer of this content. /// public static IProfile GetWriterProfile(this IContent content) { var repository = RepositoryResolver.Current.Factory.CreateUserRepository( new PetaPocoUnitOfWork()); return repository.GetProfileById(content.WriterId); } } }