mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 19:04:51 +08:00
Adding BlogArchives widget
--HG-- branch : dev
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
using Orchard.Blogs.Models;
|
||||
using Orchard.Blogs.Services;
|
||||
using Orchard.Blogs.ViewModels;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.Core.ContentsLocation.Models;
|
||||
|
||||
namespace Orchard.Blogs.Drivers {
|
||||
public class BlogArchivesPartDriver : ContentPartDriver<BlogArchivesPart> {
|
||||
private readonly IBlogService _blogService;
|
||||
private readonly IBlogPostService _blogPostService;
|
||||
|
||||
public BlogArchivesPartDriver(IBlogService blogService, IBlogPostService blogPostService) {
|
||||
_blogService = blogService;
|
||||
_blogPostService = blogPostService;
|
||||
}
|
||||
|
||||
protected override DriverResult Display(BlogArchivesPart part, string displayType, dynamic shapeHelper) {
|
||||
BlogPart blog = null;
|
||||
if (!string.IsNullOrWhiteSpace(part.ForBlog))
|
||||
blog = _blogService.Get(part.ForBlog);
|
||||
|
||||
if ( blog != null ) {
|
||||
var model = new BlogPostArchiveViewModel {BlogPart = blog, Archives = _blogPostService.GetArchives(blog)};
|
||||
return ContentPartTemplate(model, "Parts/Blogs.BlogArchives");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected override DriverResult Editor(BlogArchivesPart part, dynamic shapeHelper) {
|
||||
var location = part.GetLocation("Editor", "Primary", "5");
|
||||
return ContentPartTemplate(part, "Parts/Blogs.BlogArchives").Location(location);
|
||||
}
|
||||
|
||||
protected override DriverResult Editor(BlogArchivesPart part, IUpdateModel updater, dynamic shapeHelper) {
|
||||
updater.TryUpdateModel(part, Prefix, null, null);
|
||||
return Editor(part, shapeHelper);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,53 +0,0 @@
|
||||
using System.Web.Mvc;
|
||||
using Orchard.Blogs.Models;
|
||||
using Orchard.Blogs.Services;
|
||||
using Orchard.Blogs.ViewModels;
|
||||
using Orchard.DisplayManagement;
|
||||
using Orchard.Mvc.Filters;
|
||||
|
||||
namespace Orchard.Blogs.Filters {
|
||||
public class ArchivesFilter : FilterProvider, IResultFilter {
|
||||
private readonly IBlogPostService _blogPostService;
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
private readonly IShapeHelperFactory _shapeHelperFactory;
|
||||
|
||||
public ArchivesFilter(IBlogPostService blogPostService, IWorkContextAccessor workContextAccessor, IShapeHelperFactory shapeHelperFactory) {
|
||||
_blogPostService = blogPostService;
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_shapeHelperFactory = shapeHelperFactory;
|
||||
}
|
||||
|
||||
//todo: make work for real
|
||||
public void OnResultExecuting(ResultExecutingContext filterContext) {
|
||||
dynamic model = filterContext.Controller.ViewData.Model;
|
||||
|
||||
if (model == null || model.GetType().GetProperty("Metadata") == null || model.Metadata.GetType().GetProperty("Type") == null)
|
||||
return;
|
||||
|
||||
var modelType = model.Metadata.Type;
|
||||
if (string.IsNullOrEmpty(modelType))
|
||||
return;
|
||||
|
||||
var workContext = _workContextAccessor.GetContext(filterContext);
|
||||
var shape = _shapeHelperFactory.CreateHelper();
|
||||
|
||||
if (modelType == "Items_Content_Blog") {
|
||||
BlogPart blog = model.ContentItem.Get(typeof (BlogPart));
|
||||
var blogArchives = shape.BlogArchives()
|
||||
.Archives(new BlogPostArchiveViewModel { BlogPart = blog, Archives = _blogPostService.GetArchives(blog) });
|
||||
workContext.Layout.Sidebar.Add(blogArchives);
|
||||
return;
|
||||
}
|
||||
|
||||
if (modelType == "Items_Content_BlogPost" || modelType == "BlogPostArchive") {
|
||||
BlogPart blog = model.Blog.ContentItem.Get(typeof (BlogPart));
|
||||
var blogArchives = shape.BlogArchives()
|
||||
.Archives(new BlogPostArchiveViewModel { BlogPart = blog, Archives = _blogPostService.GetArchives(blog) });
|
||||
workContext.Layout.Sidebar.Add(blogArchives);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnResultExecuted(ResultExecutedContext filterContext) {}
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Blogs.Models;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Data;
|
||||
|
||||
namespace Orchard.Blogs.Handlers {
|
||||
[UsedImplicitly]
|
||||
public class BlogArchivesPartHandler : ContentHandler {
|
||||
public BlogArchivesPartHandler(IRepository<BlogArchivesPartRecord> repository) {
|
||||
Filters.Add(StorageFilter.For(repository));
|
||||
}
|
||||
}
|
||||
}
|
@@ -72,5 +72,22 @@ namespace Orchard.Blogs {
|
||||
);
|
||||
return 4;
|
||||
}
|
||||
|
||||
public int UpdateFrom4() {
|
||||
SchemaBuilder.CreateTable("BlogArchivesPartRecord", table => table
|
||||
.ContentPartRecord()
|
||||
.Column<string>("BlogSlug", c => c.WithLength(255))
|
||||
);
|
||||
|
||||
ContentDefinitionManager.AlterTypeDefinition("BlogArchives",
|
||||
cfg => cfg
|
||||
.WithPart("BlogArchivesPart")
|
||||
.WithPart("CommonPart")
|
||||
.WithPart("WidgetPart")
|
||||
.WithSetting("Stereotype", "Widget")
|
||||
);
|
||||
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
using Orchard.ContentManagement;
|
||||
|
||||
namespace Orchard.Blogs.Models {
|
||||
/// <summary>
|
||||
/// The content part used by the BlogArchives widget
|
||||
/// </summary>
|
||||
public class BlogArchivesPart : ContentPart<BlogArchivesPartRecord> {
|
||||
public string ForBlog {
|
||||
get { return Record.BlogSlug; }
|
||||
set { Record.BlogSlug = value; }
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Orchard.ContentManagement.Records;
|
||||
|
||||
namespace Orchard.Blogs.Models {
|
||||
/// <summary>
|
||||
/// The content part used by the BlogArchives widget
|
||||
/// </summary>
|
||||
public class BlogArchivesPartRecord : ContentPartRecord {
|
||||
public const ushort DefaultBlogSlugLength = 255;
|
||||
|
||||
[StringLength(DefaultBlogSlugLength)]
|
||||
[Required]
|
||||
public virtual string BlogSlug { get; set; }
|
||||
}
|
||||
}
|
@@ -67,8 +67,12 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AdminMenu.cs" />
|
||||
<Compile Include="Drivers\BlogArchivesPartDriver.cs" />
|
||||
<Compile Include="Drivers\RecentBlogPostsPartDriver.cs" />
|
||||
<Compile Include="Handlers\BlogArchivesPartHandler.cs" />
|
||||
<Compile Include="Handlers\RecentBlogPostsPartHandler.cs" />
|
||||
<Compile Include="Models\BlogArchivesPart.cs" />
|
||||
<Compile Include="Models\BlogArchivesPartRecord.cs" />
|
||||
<Compile Include="Models\RecentBlogPostsPart.cs" />
|
||||
<Compile Include="Models\RecentBlogPostsPartRecord.cs" />
|
||||
<Compile Include="ResourceManifest.cs" />
|
||||
@@ -81,7 +85,6 @@
|
||||
<Compile Include="Drivers\BlogPostPartDriver.cs" />
|
||||
<Compile Include="Extensions\FeedManagerExtensions.cs" />
|
||||
<Compile Include="Extensions\UrlHelperExtensions.cs" />
|
||||
<Compile Include="Filters\ArchivesFilter.cs" />
|
||||
<Compile Include="Models\ArchiveData.cs" />
|
||||
<Compile Include="Handlers\BlogPartArchiveHandler.cs" />
|
||||
<Compile Include="Models\BlogPartArchiveRecord.cs" />
|
||||
@@ -115,7 +118,6 @@
|
||||
<Content Include="Scripts\archives.js" />
|
||||
<Content Include="Styles\admin.css" />
|
||||
<Content Include="Styles\archives.css" />
|
||||
<Content Include="Views\BlogArchives.cshtml" />
|
||||
<Content Include="Views\BlogAdmin\Create.cshtml" />
|
||||
<Content Include="Views\BlogAdmin\Edit.cshtml" />
|
||||
<Content Include="Views\BlogAdmin\Item.cshtml" />
|
||||
@@ -187,7 +189,9 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App_Data\Localization\fr-FR\orchard.module.po" />
|
||||
<None Include="Views\DisplayTemplates\Parts\Blogs.BlogArchives.cshtml" />
|
||||
<None Include="Views\EditorTemplates\Parts\Blogs.RecentBlogPosts.cshtml" />
|
||||
<None Include="Views\EditorTemplates\Parts\Blogs.BlogArchives.cshtml" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
|
||||
|
@@ -1,7 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Orchard.UI.Resources;
|
||||
|
||||
namespace Orchard.Blogs {
|
||||
|
@@ -1,40 +1,44 @@
|
||||
@using Orchard.Blogs.Extensions;
|
||||
@using Orchard.Blogs.ViewModels;
|
||||
@model Orchard.Blogs.ViewModels.BlogPostArchiveViewModel
|
||||
@using Orchard.Blogs.Extensions;
|
||||
|
||||
@{
|
||||
BlogPostArchiveViewModel model = Model.Archives;
|
||||
Style.Require("BlogsArchives");
|
||||
Script.Require("BlogsArchives");
|
||||
}
|
||||
|
||||
<div class="archives">
|
||||
<h3>@T("Archives")</h3>
|
||||
@if (model.Archives.Count() > 0) {
|
||||
/* todo: need to fix up this syntax
|
||||
if (Model.Archives.Count() > 20) {
|
||||
@if (Model.Archives.Count() > 20) {
|
||||
<ul class="years">
|
||||
@{
|
||||
int lastYear = Model.Archives.First().Key.Year;
|
||||
int firstYear = Model.Archives.Last().Key.Year;
|
||||
|
||||
for (int year = lastYear; year >= firstYear; year--) {
|
||||
var yearMonths = Model.Archives.Where(m => m.Key.Year == year);
|
||||
|
||||
if (year == lastYear) {
|
||||
}
|
||||
|
||||
|
||||
@for (int year = lastYear; year >= firstYear; year--) {
|
||||
var yearMonths = Model.Archives.Where(m => m.Key.Year == year);
|
||||
|
||||
if (year == lastYear) {
|
||||
<li>
|
||||
<h4>@year</h4>
|
||||
} else {
|
||||
</li>
|
||||
}
|
||||
|
||||
if (year != lastYear) {
|
||||
<li class="previous">
|
||||
<h4>@year <span>(@yearMonths.Sum(ym => ym.Value))</span></h4>
|
||||
}
|
||||
@Html.UnorderedList(yearMonths, (t, i) => Html.Link(string.Format("{0:MMMM} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(Model.BlogPart.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList")
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
} else {
|
||||
*/
|
||||
@Html.UnorderedList(model.Archives, (t, i) => Html.Link(string.Format("{0:MMMM yyyy} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(model.BlogPart.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList")
|
||||
/*
|
||||
}
|
||||
*/
|
||||
} else {
|
||||
|
||||
</ul>
|
||||
}
|
||||
else if (Model.Archives.Count() > 0) {
|
||||
@Html.UnorderedList(Model.Archives, (t, i) => Html.Link(string.Format("{0:MMMM yyyy} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(Model.BlogPart.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList")
|
||||
}
|
||||
else {
|
||||
<div class="message info">@T("None found")</div>
|
||||
}
|
||||
</div>
|
@@ -0,0 +1,8 @@
|
||||
@model Orchard.Blogs.Models.BlogArchivesPart
|
||||
<fieldset>
|
||||
<div>
|
||||
@Html.LabelFor(m => m.ForBlog)
|
||||
@Html.TextBoxFor(m => m.ForBlog)
|
||||
<span class="hint">@T("The blog's slug to display the archives for.")</span>
|
||||
</div>
|
||||
</fieldset>
|
Reference in New Issue
Block a user