2010-01-12 01:04:11 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2010-01-07 01:06:17 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Orchard.Pages.Models;
|
|
|
|
|
using Orchard.Core.Common.Records;
|
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Pages.Services {
|
|
|
|
|
public class PageService : IPageService {
|
|
|
|
|
private readonly IContentManager _contentManager;
|
|
|
|
|
|
2010-01-07 23:12:46 +00:00
|
|
|
|
public PageService(IContentManager contentManager) {
|
2010-01-07 01:06:17 +00:00
|
|
|
|
_contentManager = contentManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Page> Get() {
|
2010-01-08 21:07:35 +00:00
|
|
|
|
return Get(PageStatus.All);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Page> Get(PageStatus status) {
|
2010-01-13 23:51:07 +00:00
|
|
|
|
IEnumerable<ContentItem> contentItems;
|
|
|
|
|
|
2010-01-08 21:07:35 +00:00
|
|
|
|
switch (status) {
|
|
|
|
|
case PageStatus.All:
|
2010-01-13 23:51:07 +00:00
|
|
|
|
contentItems = _contentManager.Query(VersionOptions.Latest, "page").List();
|
|
|
|
|
break;
|
2010-01-08 21:07:35 +00:00
|
|
|
|
case PageStatus.Published:
|
2010-01-13 23:51:07 +00:00
|
|
|
|
contentItems = _contentManager.Query(VersionOptions.Published, "page").List();
|
|
|
|
|
break;
|
2010-01-08 21:07:35 +00:00
|
|
|
|
case PageStatus.Offline:
|
2010-01-13 23:51:07 +00:00
|
|
|
|
contentItems = _contentManager.Query(VersionOptions.Latest, "page").List().Where(ci => !ci.VersionRecord.Published);
|
|
|
|
|
break;
|
2010-01-08 21:07:35 +00:00
|
|
|
|
default:
|
2010-01-13 23:51:07 +00:00
|
|
|
|
contentItems = new List<Page>().Cast<ContentItem>();
|
|
|
|
|
break;
|
2010-01-08 21:07:35 +00:00
|
|
|
|
}
|
2010-01-13 23:51:07 +00:00
|
|
|
|
|
|
|
|
|
return contentItems.Select(ci => ci.As<Page>());
|
2010-01-07 01:06:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Page Get(string slug) {
|
2010-01-13 23:51:07 +00:00
|
|
|
|
return
|
|
|
|
|
_contentManager.Query("page").Join<RoutableRecord>().Where(rr => rr.Slug == slug).List().FirstOrDefault
|
|
|
|
|
().As<Page>();
|
2010-01-07 01:06:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-08 21:07:35 +00:00
|
|
|
|
public Page GetLatest(int id) {
|
|
|
|
|
return _contentManager.Get<Page>(id, VersionOptions.Latest);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-10 11:18:28 +00:00
|
|
|
|
public Page GetLatest(string slug) {
|
2010-01-13 23:51:07 +00:00
|
|
|
|
return
|
|
|
|
|
_contentManager.Query(VersionOptions.Latest, "page").Join<RoutableRecord>().Where(rr => rr.Slug == slug)
|
|
|
|
|
.Slice(0, 1).FirstOrDefault().As<Page>();
|
2010-01-10 11:18:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Page GetPageOrDraft(string slug) {
|
|
|
|
|
Page page = GetLatest(slug);
|
2010-01-07 23:12:46 +00:00
|
|
|
|
return _contentManager.GetDraftRequired<Page>(page.Id);
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 01:06:17 +00:00
|
|
|
|
public void Delete(Page page) {
|
2010-01-07 23:12:46 +00:00
|
|
|
|
_contentManager.Remove(page.ContentItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Publish(Page page) {
|
|
|
|
|
_contentManager.Publish(page.ContentItem);
|
2010-01-12 01:04:11 +00:00
|
|
|
|
//TODO: (erikpo) Not sure if this is needed or not
|
|
|
|
|
page.Published = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Publish(Page page, DateTime publishDate) {
|
|
|
|
|
//TODO: (erikpo) This logic should move out of blogs and pages and into content manager
|
|
|
|
|
if (page.Published != null && page.Published.Value >= DateTime.UtcNow)
|
|
|
|
|
_contentManager.Unpublish(page.ContentItem);
|
|
|
|
|
page.Published = publishDate;
|
2010-01-07 01:06:17 +00:00
|
|
|
|
}
|
2010-01-08 22:49:16 +00:00
|
|
|
|
|
|
|
|
|
public void Unpublish(Page page) {
|
2010-01-12 01:04:11 +00:00
|
|
|
|
_contentManager.Unpublish(page.ContentItem);
|
|
|
|
|
//TODO: (erikpo) Not sure if this is needed or not
|
|
|
|
|
page.Published = null;
|
2010-01-08 22:49:16 +00:00
|
|
|
|
}
|
2010-01-07 01:06:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|