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) {
|
|
|
|
|
switch (status) {
|
|
|
|
|
case PageStatus.All:
|
|
|
|
|
return _contentManager.Query<Page, PageRecord>(VersionOptions.Latest).List();
|
|
|
|
|
case PageStatus.Published:
|
|
|
|
|
return _contentManager.Query<Page, PageRecord>(VersionOptions.Published).List();
|
|
|
|
|
case PageStatus.Offline:
|
|
|
|
|
IEnumerable<Page> allPages = _contentManager.Query<Page, PageRecord>(VersionOptions.Latest).List();
|
|
|
|
|
List<Page> offlinePages = new List<Page>();
|
|
|
|
|
foreach (var page in allPages) {
|
|
|
|
|
if (page.ContentItem.VersionRecord.Published == false) {
|
|
|
|
|
offlinePages.Add(page);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return offlinePages;
|
|
|
|
|
default:
|
|
|
|
|
return new List<Page>();
|
|
|
|
|
}
|
2010-01-07 01:06:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Page Get(string slug) {
|
|
|
|
|
return _contentManager.Query<Page, PageRecord>()
|
|
|
|
|
.Join<RoutableRecord>().Where(rr => rr.Slug == slug)
|
|
|
|
|
.List().FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
return _contentManager.Query<Page, PageRecord>(VersionOptions.Latest)
|
2010-01-07 23:12:46 +00:00
|
|
|
|
.Join<RoutableRecord>().Where(rr => rr.Slug == slug)
|
2010-01-10 11:18:28 +00:00
|
|
|
|
.Slice(0, 1).FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Page GetPageOrDraft(string slug) {
|
|
|
|
|
Page page = GetLatest(slug);
|
2010-01-07 23:12:46 +00:00
|
|
|
|
return _contentManager.GetDraftRequired<Page>(page.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Page New() {
|
|
|
|
|
return _contentManager.New<Page>("page");
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-12 01:04:11 +00:00
|
|
|
|
public Page Create(bool publishNow, DateTime? publishDate) {
|
|
|
|
|
//TODO: (erikpo) Evaluate if publish options should be moved into create or out of create to keep it clean
|
|
|
|
|
return _contentManager.Create<Page>("page", publishNow ? VersionOptions.Published : VersionOptions.Draft,
|
|
|
|
|
bp => {
|
|
|
|
|
if (!publishNow && publishDate != null)
|
|
|
|
|
bp.Published = publishDate.Value;
|
|
|
|
|
});
|
2010-01-07 23:12:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|