2012-10-03 12:51:32 -02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2012-10-29 14:28:16 -01:00
|
|
|
|
using Umbraco.Core.Logging;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
using Umbraco.Core.Models;
|
2012-10-31 15:15:02 -01:00
|
|
|
|
using Umbraco.Core.Models.EntityBase;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
using Umbraco.Core.Persistence;
|
2012-10-10 12:13:23 -02:00
|
|
|
|
using Umbraco.Core.Persistence.Querying;
|
|
|
|
|
|
using Umbraco.Core.Persistence.Repositories;
|
|
|
|
|
|
using Umbraco.Core.Persistence.UnitOfWork;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
using Umbraco.Web.Publishing;
|
2012-10-10 12:13:23 -02:00
|
|
|
|
using Content = Umbraco.Core.Models.Content;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Services
|
|
|
|
|
|
{
|
2012-10-24 08:36:45 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents the Content Service, which is an easy access to operations involving <see cref="IContent"/>
|
|
|
|
|
|
/// </summary>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public class ContentService : IContentService
|
|
|
|
|
|
{
|
2012-10-10 12:13:23 -02:00
|
|
|
|
private readonly IUnitOfWorkProvider _provider;
|
|
|
|
|
|
private readonly IPublishingStrategy _publishingStrategy;
|
2012-10-30 20:01:09 -01:00
|
|
|
|
private readonly IUnitOfWork _unitOfWork;
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
public ContentService() : this(new PetaPocoUnitOfWorkProvider())
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ContentService(IUnitOfWorkProvider provider) : this(provider, new PublishingStrategy())
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ContentService(IUnitOfWorkProvider provider, IPublishingStrategy publishingStrategy)
|
2012-10-03 12:51:32 -02:00
|
|
|
|
{
|
2012-10-10 12:13:23 -02:00
|
|
|
|
_provider = provider;
|
|
|
|
|
|
_publishingStrategy = publishingStrategy;
|
2012-10-30 20:01:09 -01:00
|
|
|
|
_unitOfWork = provider.GetUnitOfWork();
|
2012-10-10 12:13:23 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates an <see cref="IContent"/> object using the alias of the <see cref="IContentType"/>
|
|
|
|
|
|
/// that this Content is based on.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="parentId">Id of Parent for content</param>
|
|
|
|
|
|
/// <param name="contentTypeAlias">Alias of the <see cref="IContentType"/></param>
|
|
|
|
|
|
/// <returns><see cref="IContent"/></returns>
|
|
|
|
|
|
public IContent CreateContent(int parentId, string contentTypeAlias)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentTypeRepository, IContentType, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
var query = Query<IContentType>.Builder.Where(x => x.Alias == contentTypeAlias);
|
|
|
|
|
|
var contentTypes = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
|
if (!contentTypes.Any())
|
2012-10-25 18:38:23 -02:00
|
|
|
|
throw new Exception(string.Format("No ContentType matching the passed in Alias: '{0}' was found", contentTypeAlias));
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
var contentType = contentTypes.First();
|
|
|
|
|
|
|
|
|
|
|
|
if (contentType == null)
|
2012-10-31 08:20:45 -01:00
|
|
|
|
throw new Exception(string.Format("ContentType matching the passed in Alias: '{0}' was null", contentTypeAlias));
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
return new Content(parentId, contentType);
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets an <see cref="IContent"/> object by Id
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">Id of the Content to retrieve</param>
|
|
|
|
|
|
/// <returns><see cref="IContent"/></returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public IContent GetById(int id)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
return repository.Get(id);
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-24 08:36:45 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a collection of <see cref="IContent"/> objects by the Id of the <see cref="IContentType"/>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">Id of the <see cref="IContentType"/></param>
|
|
|
|
|
|
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
|
|
|
|
|
|
public IEnumerable<IContent> GetContentOfContentType(int id)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-24 08:36:45 -02:00
|
|
|
|
|
|
|
|
|
|
var query = Query<IContent>.Builder.Where(x => x.ContentTypeId == id);
|
|
|
|
|
|
var contents = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
|
return contents;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a collection of <see cref="IContent"/> objects by Level
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="level">The level to retrieve Content from</param>
|
|
|
|
|
|
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public IEnumerable<IContent> GetByLevel(int level)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
var query = Query<IContent>.Builder.Where(x => x.Level == level);
|
|
|
|
|
|
var contents = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
|
return contents;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a collection of <see cref="IContent"/> objects by Parent Id
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">Id of the Parent to retrieve Children from</param>
|
|
|
|
|
|
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public IEnumerable<IContent> GetChildren(int id)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
var query = Query<IContent>.Builder.Where(x => x.ParentId == id);
|
|
|
|
|
|
var contents = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
|
return contents;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a collection of an <see cref="IContent"/> objects versions by Id
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
|
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public IEnumerable<IContent> GetVersions(int id)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
var versions = repository.GetAllVersions(id);
|
|
|
|
|
|
return versions;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a collection of <see cref="IContent"/> objects, which reside at the first level / root
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public IEnumerable<IContent> GetRootContent()
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
var query = Query<IContent>.Builder.Where(x => x.ParentId == -1);
|
|
|
|
|
|
var contents = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
|
return contents;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
2012-10-31 15:15:02 -01:00
|
|
|
|
/// Gets a collection of <see cref="IContent"/> objects, which has an expiration date less than or equal to today.
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public IEnumerable<IContent> GetContentForExpiration()
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-10-31 15:15:02 -01:00
|
|
|
|
var query = Query<IContent>.Builder.Where(x => x.Published == true && x.ExpireDate <= DateTime.UtcNow);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
var contents = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
|
return contents;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
2012-10-31 15:15:02 -01:00
|
|
|
|
/// Gets a collection of <see cref="IContent"/> objects, which has a release date less than or equal to today.
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public IEnumerable<IContent> GetContentForRelease()
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-10-31 15:15:02 -01:00
|
|
|
|
var query = Query<IContent>.Builder.Where(x => x.Published == false && x.ReleaseDate <= DateTime.UtcNow);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
var contents = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
|
return contents;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a collection of an <see cref="IContent"/> objects, which resides in the Recycle Bin
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public IEnumerable<IContent> GetContentInRecycleBin()
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
var query = Query<IContent>.Builder.Where(x => x.ParentId == -20);
|
|
|
|
|
|
var contents = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
|
return contents;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Re-Publishes all Content
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userId">Id of the User issueing the publishing</param>
|
|
|
|
|
|
/// <returns>True if publishing succeeded, otherwise False</returns>
|
|
|
|
|
|
public bool RePublishAll(int userId)
|
2012-10-03 12:51:32 -02:00
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
var list = new List<IContent>();
|
|
|
|
|
|
|
2012-10-29 14:28:16 -01:00
|
|
|
|
//Consider creating a Path query instead of recursive method:
|
2012-10-10 13:18:14 -02:00
|
|
|
|
//var query = Query<IContent>.Builder.Where(x => x.Path.StartsWith("-1"));
|
2012-10-29 14:28:16 -01:00
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
var rootContent = GetRootContent();
|
|
|
|
|
|
foreach (var content in rootContent)
|
|
|
|
|
|
{
|
2012-10-29 14:28:16 -01:00
|
|
|
|
if(content.IsValid())
|
|
|
|
|
|
{
|
2012-10-31 15:15:02 -01:00
|
|
|
|
list.Add(content);
|
2012-10-29 14:28:16 -01:00
|
|
|
|
list.AddRange(GetChildrenDeep(content.Id));
|
|
|
|
|
|
}
|
2012-10-10 12:13:23 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-29 14:28:16 -01:00
|
|
|
|
//Publish and then update the database with new status
|
|
|
|
|
|
var published = _publishingStrategy.PublishWithChildren(list, userId);
|
|
|
|
|
|
if (published)
|
2012-10-10 12:13:23 -02:00
|
|
|
|
{
|
2012-10-31 15:15:02 -01:00
|
|
|
|
//Only loop through content where the Published property has been updated
|
|
|
|
|
|
foreach (var item in list.Where(x => ((ICanBeDirty)x).IsPropertyDirty("Published")))
|
2012-10-10 13:18:14 -02:00
|
|
|
|
{
|
|
|
|
|
|
repository.AddOrUpdate(item);
|
|
|
|
|
|
}
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-10-29 14:28:16 -01:00
|
|
|
|
//TODO Change this so we can avoid a depencency to the horrible library method / umbraco.content (singleton) class.
|
2012-10-31 15:15:02 -01:00
|
|
|
|
//global::umbraco.library.RefreshContent();
|
2012-10-29 14:28:16 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return published;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Publishes a single <see cref="IContent"/> object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content">The <see cref="IContent"/> to publish</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User issueing the publishing</param>
|
|
|
|
|
|
/// <returns>True if publishing succeeded, otherwise False</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public bool Publish(IContent content, int userId)
|
|
|
|
|
|
{
|
2012-10-10 12:13:23 -02:00
|
|
|
|
return SaveAndPublish(content, userId);
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Publishes a <see cref="IContent"/> object and all its children
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content">The <see cref="IContent"/> to publish along with its children</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User issueing the publishing</param>
|
|
|
|
|
|
/// <returns>True if publishing succeeded, otherwise False</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public bool PublishWithChildren(IContent content, int userId)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-11-01 10:59:32 -01:00
|
|
|
|
//Check if parent is published (although not if its a root node) - if parent isn't published this Content cannot be published
|
2012-11-01 13:48:03 -01:00
|
|
|
|
if (content.ParentId != -1 && content.ParentId != -20 && !GetById(content.ParentId).Published)
|
2012-10-29 14:28:16 -01:00
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Info<ContentService>(
|
|
|
|
|
|
string.Format("Content '{0}' with Id '{1}' could not be published because its parent is not published.",
|
|
|
|
|
|
content.Name, content.Id));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Content contains invalid property values and can therefore not be published - fire event?
|
|
|
|
|
|
if (!content.IsValid())
|
|
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Info<ContentService>(
|
|
|
|
|
|
string.Format("Content '{0}' with Id '{1}' could not be published because of invalid properties.",
|
|
|
|
|
|
content.Name, content.Id));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Consider creating a Path query instead of recursive method:
|
2012-10-10 13:18:14 -02:00
|
|
|
|
//var query = Query<IContent>.Builder.Where(x => x.Path.StartsWith(content.Path));
|
2012-10-29 14:28:16 -01:00
|
|
|
|
|
2012-11-01 10:59:32 -01:00
|
|
|
|
var list = new List<IContent>();
|
2012-10-10 12:13:23 -02:00
|
|
|
|
list.Add(content);
|
2012-11-01 10:59:32 -01:00
|
|
|
|
list.AddRange(GetChildrenDeep(content.Id));
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-10-29 14:28:16 -01:00
|
|
|
|
//Publish and then update the database with new status
|
|
|
|
|
|
var published = _publishingStrategy.PublishWithChildren(list, userId);
|
|
|
|
|
|
if (published)
|
2012-10-10 12:13:23 -02:00
|
|
|
|
{
|
2012-10-31 15:15:02 -01:00
|
|
|
|
//Only loop through content where the Published property has been updated
|
|
|
|
|
|
foreach (var item in list.Where(x => ((ICanBeDirty)x).IsPropertyDirty("Published")))
|
2012-10-10 13:18:14 -02:00
|
|
|
|
{
|
|
|
|
|
|
repository.AddOrUpdate(item);
|
|
|
|
|
|
}
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-10-29 14:28:16 -01:00
|
|
|
|
//TODO Change this so we can avoid a depencency to the horrible library method / umbraco.content (singleton) class.
|
|
|
|
|
|
//TODO Need to investigate if it will also update the cache for children of the Content object
|
2012-10-31 15:15:02 -01:00
|
|
|
|
//global::umbraco.library.UpdateDocumentCache(content.Id);
|
2012-10-29 14:28:16 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return published;
|
2012-10-10 12:13:23 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// UnPublishes a single <see cref="IContent"/> object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content">The <see cref="IContent"/> to publish</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User issueing the publishing</param>
|
|
|
|
|
|
/// <returns>True if unpublishing succeeded, otherwise False</returns>
|
|
|
|
|
|
public bool UnPublish(IContent content, int userId)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-11-01 10:59:32 -01:00
|
|
|
|
//Look for children and unpublish them if any exists, otherwise just unpublish the passed in Content.
|
|
|
|
|
|
var children = GetChildrenDeep(content.Id);
|
|
|
|
|
|
var hasChildren = children.Any();
|
|
|
|
|
|
|
|
|
|
|
|
if(hasChildren)
|
|
|
|
|
|
children.Add(content);
|
|
|
|
|
|
|
|
|
|
|
|
var unpublished = hasChildren
|
|
|
|
|
|
? _publishingStrategy.UnPublish(children, userId)
|
|
|
|
|
|
: _publishingStrategy.UnPublish(content, userId);
|
2012-10-29 14:28:16 -01:00
|
|
|
|
|
|
|
|
|
|
if (unpublished)
|
|
|
|
|
|
{
|
|
|
|
|
|
repository.AddOrUpdate(content);
|
2012-11-01 10:59:32 -01:00
|
|
|
|
|
|
|
|
|
|
if (hasChildren)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var child in children)
|
|
|
|
|
|
{
|
|
|
|
|
|
repository.AddOrUpdate(child);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-29 14:28:16 -01:00
|
|
|
|
|
|
|
|
|
|
//TODO Change this so we can avoid a depencency to the horrible library method / umbraco.content class.
|
2012-10-31 15:15:02 -01:00
|
|
|
|
//global::umbraco.library.UnPublishSingleNode(content.Id);
|
2012-10-29 14:28:16 -01:00
|
|
|
|
}
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-10-29 14:28:16 -01:00
|
|
|
|
return unpublished;
|
2012-10-10 12:13:23 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets a flat list of decendents of content from parent id
|
|
|
|
|
|
/// </summary>
|
2012-10-29 14:28:16 -01:00
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// Only contains valid <see cref="IContent"/> objects, which means
|
|
|
|
|
|
/// that everything in the returned list can be published.
|
|
|
|
|
|
/// If an invalid <see cref="IContent"/> object is found it will not
|
|
|
|
|
|
/// be added to the list neither will its children.
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
/// <param name="parentId">Id of the parent to retrieve children from</param>
|
|
|
|
|
|
/// <returns>A list of valid <see cref="IContent"/> that can be published</returns>
|
2012-10-10 12:13:23 -02:00
|
|
|
|
private List<IContent> GetChildrenDeep(int parentId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var list = new List<IContent>();
|
|
|
|
|
|
var children = GetChildren(parentId);
|
|
|
|
|
|
foreach (var child in children)
|
|
|
|
|
|
{
|
2012-10-29 14:28:16 -01:00
|
|
|
|
if (child.IsValid())
|
|
|
|
|
|
{
|
|
|
|
|
|
list.Add(child);
|
|
|
|
|
|
list.AddRange(GetChildrenDeep(child.Id));
|
|
|
|
|
|
}
|
2012-10-10 12:13:23 -02:00
|
|
|
|
}
|
|
|
|
|
|
return list;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Saves and Publishes a single <see cref="IContent"/> object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content">The <see cref="IContent"/> to save and publish</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User issueing the publishing</param>
|
|
|
|
|
|
/// <returns>True if publishing succeeded, otherwise False</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public bool SaveAndPublish(IContent content, int userId)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 13:18:14 -02:00
|
|
|
|
|
2012-11-01 10:59:32 -01:00
|
|
|
|
//Check if parent is published (although not if its a root node) - if parent isn't published this Content cannot be published
|
2012-11-01 13:48:03 -01:00
|
|
|
|
if (content.ParentId != -1 && content.ParentId != -20 && GetById(content.ParentId).Published == false)
|
2012-10-29 14:28:16 -01:00
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Info<ContentService>(
|
|
|
|
|
|
string.Format("Content '{0}' with Id '{1}' could not be published because its parent is not published.",
|
|
|
|
|
|
content.Name, content.Id));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2012-10-24 06:42:04 -02:00
|
|
|
|
|
2012-10-29 14:28:16 -01:00
|
|
|
|
//Content contains invalid property values and can therefore not be published - fire event?
|
2012-10-10 13:18:14 -02:00
|
|
|
|
if (!content.IsValid())
|
2012-10-29 14:28:16 -01:00
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Info<ContentService>(
|
|
|
|
|
|
string.Format("Content '{0}' with Id '{1}' could not be published because of invalid properties.",
|
|
|
|
|
|
content.Name, content.Id));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2012-10-10 13:18:14 -02:00
|
|
|
|
|
2012-10-29 14:28:16 -01:00
|
|
|
|
//Publish and then update the database with new status
|
|
|
|
|
|
bool published = _publishingStrategy.Publish(content, userId);
|
|
|
|
|
|
if (published)
|
|
|
|
|
|
{
|
|
|
|
|
|
repository.AddOrUpdate(content);
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-29 14:28:16 -01:00
|
|
|
|
|
|
|
|
|
|
//TODO Change this so we can avoid a depencency to the horrible library method / umbraco.content (singleton) class.
|
2012-10-31 15:15:02 -01:00
|
|
|
|
//global::umbraco.library.UpdateDocumentCache(content.Id);
|
2012-10-29 14:28:16 -01:00
|
|
|
|
}
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-10-29 14:28:16 -01:00
|
|
|
|
return published;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Saves a single <see cref="IContent"/> object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content">The <see cref="IContent"/> to save</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User saving the Content</param>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public void Save(IContent content, int userId)
|
|
|
|
|
|
{
|
2012-10-30 20:01:09 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
repository.AddOrUpdate(content);
|
2012-10-30 20:01:09 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Saves a collection of <see cref="IContent"/> objects
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="contents">Collection of <see cref="IContent"/> to save</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User saving the Content</param>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public void Save(IEnumerable<IContent> contents, int userId)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
foreach (var content in contents)
|
|
|
|
|
|
{
|
|
|
|
|
|
repository.AddOrUpdate(content);
|
|
|
|
|
|
}
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Deletes all content of specified type. All children of deleted content is moved to Recycle Bin.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>This needs extra care and attention as its potentially a dangerous and extensive operation</remarks>
|
|
|
|
|
|
/// <param name="contentTypeId">Id of the <see cref="IContentType"/></param>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public void DeleteContentOfType(int contentTypeId)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
//NOTE What about content that has the contenttype as part of its composition?
|
|
|
|
|
|
var query = Query<IContent>.Builder.Where(x => x.ContentTypeId == contentTypeId);
|
|
|
|
|
|
var contents = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var content in contents)
|
|
|
|
|
|
{
|
|
|
|
|
|
((Content)content).ChangeTrashedState(true);
|
|
|
|
|
|
repository.AddOrUpdate(content);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Permanently deletes an <see cref="IContent"/> object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>Please note that this method will completely remove the Content from the database</remarks>
|
|
|
|
|
|
/// <param name="content">The <see cref="IContent"/> to delete</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User deleting the Content</param>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public void Delete(IContent content, int userId)
|
|
|
|
|
|
{
|
2012-10-24 08:36:45 -02:00
|
|
|
|
//TODO Ensure that content is unpublished when deleted
|
2012-10-10 12:13:23 -02:00
|
|
|
|
//TODO This method should handle/react to errors when there is a constraint issue with the content being deleted
|
2012-10-24 08:36:45 -02:00
|
|
|
|
//TODO Children should either be deleted or moved to the recycle bin
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
repository.Delete(content);
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Deletes an <see cref="IContent"/> object by moving it to the Recycle Bin
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>Move an item to the Recycle Bin will result in the item being unpublished</remarks>
|
|
|
|
|
|
/// <param name="content">The <see cref="IContent"/> to delete</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User deleting the Content</param>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public void MoveToRecycleBin(IContent content, int userId)
|
|
|
|
|
|
{
|
2012-10-24 08:36:45 -02:00
|
|
|
|
//TODO If content item has children those should also be moved to the recycle bin
|
|
|
|
|
|
//TODO Unpublish deleted content + children
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-31 15:15:02 -01:00
|
|
|
|
content.ChangeTrashedState(true);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
repository.AddOrUpdate(content);
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
2012-10-31 08:20:45 -01:00
|
|
|
|
/// Moves an <see cref="IContent"/> object to a new location by changing its parent id.
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// </summary>
|
2012-10-31 08:20:45 -01:00
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// If the <see cref="IContent"/> object is already published it will be
|
|
|
|
|
|
/// published after being moved to its new location. Otherwise it'll just
|
|
|
|
|
|
/// be saved with a new parent id.
|
|
|
|
|
|
/// </remarks>
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <param name="content">The <see cref="IContent"/> to move</param>
|
|
|
|
|
|
/// <param name="parentId">Id of the Content's new Parent</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User moving the Content</param>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public void Move(IContent content, int parentId, int userId)
|
|
|
|
|
|
{
|
2012-10-31 15:15:02 -01:00
|
|
|
|
//If Content is being moved away from Recycle Bin, its state should be un-trashed
|
|
|
|
|
|
if(content.Trashed && parentId != -20)
|
|
|
|
|
|
{
|
|
|
|
|
|
content.ChangeTrashedState(false, parentId);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
content.ParentId = parentId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//If Content is published, it should be (re)published from its new location
|
2012-10-31 08:20:45 -01:00
|
|
|
|
if(content.Published)
|
|
|
|
|
|
{
|
|
|
|
|
|
SaveAndPublish(content, userId);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Save(content, userId);
|
|
|
|
|
|
}
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-24 08:36:45 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Empties the Recycle Bin by deleting all <see cref="IContent"/> that resides in the bin
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void EmptyRecycleBin()
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-24 08:36:45 -02:00
|
|
|
|
|
|
|
|
|
|
var query = Query<IContent>.Builder.Where(x => x.ParentId == -20);
|
|
|
|
|
|
var contents = repository.GetByQuery(query);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var content in contents)
|
|
|
|
|
|
{
|
|
|
|
|
|
repository.Delete(content);
|
|
|
|
|
|
}
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-24 08:36:45 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Copies an <see cref="IContent"/> object by creating a new Content object of the same type and copies all data from the current
|
|
|
|
|
|
/// to the new copy which is returned.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content">The <see cref="IContent"/> to copy</param>
|
|
|
|
|
|
/// <param name="parentId">Id of the Content's new Parent</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User copying the Content</param>
|
|
|
|
|
|
/// <returns>The newly created <see cref="IContent"/> object</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public IContent Copy(IContent content, int parentId, int userId)
|
|
|
|
|
|
{
|
2012-10-10 12:13:23 -02:00
|
|
|
|
var copy = ((Content) content).Clone();
|
|
|
|
|
|
copy.ParentId = parentId;
|
2012-10-31 15:15:02 -01:00
|
|
|
|
copy.Name = copy.Name + " (1)";
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
repository.AddOrUpdate(copy);
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
return copy;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sends an <see cref="IContent"/> to Publication, which executes handlers and events for the 'Send to Publication' action.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="content">The <see cref="IContent"/> to send to publication</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User issueing the send to publication</param>
|
|
|
|
|
|
/// <returns>True if sending publication was succesfull otherwise false</returns>
|
|
|
|
|
|
public bool SendToPublication(IContent content, int userId)
|
2012-10-03 12:51:32 -02:00
|
|
|
|
{
|
2012-10-10 12:13:23 -02:00
|
|
|
|
//TODO Implement something similar to this
|
|
|
|
|
|
/*SendToPublishEventArgs e = new SendToPublishEventArgs();
|
|
|
|
|
|
FireBeforeSendToPublish(e);
|
|
|
|
|
|
if (!e.Cancel)
|
|
|
|
|
|
{
|
|
|
|
|
|
global::umbraco.BusinessLogic.Actions.Action.RunActionHandlers(content, ActionToPublish.Instance);
|
|
|
|
|
|
|
|
|
|
|
|
FireAfterSendToPublish(e);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;*/
|
|
|
|
|
|
return false;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Rollback an <see cref="IContent"/> object to a previous version.
|
|
|
|
|
|
/// This will create a new version, which is a copy of all the old data.
|
|
|
|
|
|
/// </summary>
|
2012-10-31 15:15:02 -01:00
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// The way data is stored actually only allows us to rollback on properties
|
|
|
|
|
|
/// and not data like Name and Alias of the Content.
|
|
|
|
|
|
/// </remarks>
|
2012-10-10 12:13:23 -02:00
|
|
|
|
/// <param name="id">Id of the <see cref="IContent"/>being rolled back</param>
|
|
|
|
|
|
/// <param name="versionId">Id of the version to rollback to</param>
|
|
|
|
|
|
/// <param name="userId">Id of the User issueing the rollback of the Content</param>
|
|
|
|
|
|
/// <returns>The newly created <see cref="IContent"/> object</returns>
|
2012-10-03 12:51:32 -02:00
|
|
|
|
public IContent Rollback(int id, Guid versionId, int userId)
|
|
|
|
|
|
{
|
2012-10-31 08:20:45 -01:00
|
|
|
|
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
|
2012-10-10 12:13:23 -02:00
|
|
|
|
var content = repository.GetByVersion(id, versionId);
|
|
|
|
|
|
|
|
|
|
|
|
repository.AddOrUpdate(content);
|
2012-10-31 08:20:45 -01:00
|
|
|
|
_unitOfWork.Commit();
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
return content;
|
2012-10-03 12:51:32 -02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|