2010-11-08 17:44:43 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
using Orchard.Core.Common.Models;
|
2010-11-12 15:36:57 -08:00
|
|
|
|
using Orchard.Core.Containers.Extensions;
|
2010-11-11 16:15:49 -08:00
|
|
|
|
using Orchard.Core.Containers.Models;
|
2010-11-08 17:44:43 -08:00
|
|
|
|
using Orchard.Core.Routable.Models;
|
|
|
|
|
using Orchard.DisplayManagement;
|
2010-11-08 15:33:56 -08:00
|
|
|
|
using Orchard.Themes;
|
2010-11-10 16:35:17 -08:00
|
|
|
|
using Orchard.UI.Navigation;
|
2010-11-08 15:33:56 -08:00
|
|
|
|
|
|
|
|
|
namespace Orchard.Core.Containers.Controllers {
|
2010-11-08 17:44:43 -08:00
|
|
|
|
public class ItemController : Controller {
|
|
|
|
|
private readonly IContentManager _contentManager;
|
|
|
|
|
private readonly IContainersPathConstraint _containersPathConstraint;
|
|
|
|
|
|
|
|
|
|
public ItemController(IContentManager contentManager, IContainersPathConstraint containersPathConstraint, IShapeFactory shapeFactory) {
|
|
|
|
|
_contentManager = contentManager;
|
|
|
|
|
_containersPathConstraint = containersPathConstraint;
|
2010-11-11 16:15:49 -08:00
|
|
|
|
Shape = shapeFactory;
|
2010-11-08 17:44:43 -08:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-11 16:15:49 -08:00
|
|
|
|
dynamic Shape { get; set; }
|
2010-11-08 17:44:43 -08:00
|
|
|
|
|
2010-11-08 15:33:56 -08:00
|
|
|
|
[Themed]
|
2010-11-10 16:35:17 -08:00
|
|
|
|
public ActionResult Display(string path, Pager pager) {
|
2010-11-08 17:44:43 -08:00
|
|
|
|
var matchedPath = _containersPathConstraint.FindPath(path);
|
|
|
|
|
if (string.IsNullOrEmpty(matchedPath)) {
|
|
|
|
|
throw new ApplicationException("404 - should not have passed path constraint");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hits = _contentManager
|
|
|
|
|
.Query<RoutePart, RoutePartRecord>(VersionOptions.Published)
|
|
|
|
|
.Where(r => r.Path == matchedPath)
|
|
|
|
|
.Slice(0, 2);
|
|
|
|
|
|
|
|
|
|
if (hits.Count() == 0) {
|
|
|
|
|
throw new ApplicationException("404 - should not have passed path constraint");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hits.Count() != 1) {
|
|
|
|
|
throw new ApplicationException("Ambiguous content");
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-11 16:15:49 -08:00
|
|
|
|
var container = _contentManager.Get(hits.Single().Id);
|
|
|
|
|
IContentQuery<ContentItem> query = _contentManager
|
|
|
|
|
.Query(VersionOptions.Published)
|
|
|
|
|
.Join<CommonPartRecord>().Where(cr => cr.Container.Id == container.Id);
|
2010-11-08 17:44:43 -08:00
|
|
|
|
|
2010-11-11 16:15:49 -08:00
|
|
|
|
var descendingOrder = container.As<ContainerPart>().Record.OrderByDirection == (int) OrderByDirection.Descending;
|
2010-11-12 15:36:57 -08:00
|
|
|
|
query = query.OrderBy(container.As<ContainerPart>().Record.OrderByProperty, descendingOrder);
|
2010-11-11 16:15:49 -08:00
|
|
|
|
|
2010-11-12 15:36:57 -08:00
|
|
|
|
pager.PageSize = pager.PageSize != Pager.PageSizeDefault && container.As<ContainerPart>().Record.Paginated
|
|
|
|
|
? pager.PageSize
|
|
|
|
|
: container.As<ContainerPart>().Record.PageSize;
|
2010-11-11 16:15:49 -08:00
|
|
|
|
var pagerShape = Shape.Pager(pager).TotalItemCount(query.Count());
|
2010-11-12 15:36:57 -08:00
|
|
|
|
|
|
|
|
|
var startIndex = container.As<ContainerPart>().Record.Paginated ? pager.GetStartIndex() : 0;
|
|
|
|
|
var pageOfItems = query.Slice(startIndex, pager.PageSize).ToList();
|
2010-11-11 16:15:49 -08:00
|
|
|
|
|
|
|
|
|
var list = Shape.List();
|
|
|
|
|
list.AddRange(pageOfItems.Select(item => _contentManager.BuildDisplay(item, "Summary")));
|
|
|
|
|
|
2010-11-18 11:21:43 -08:00
|
|
|
|
dynamic viewModel = Shape.ViewModel()
|
2010-11-11 16:15:49 -08:00
|
|
|
|
.ContentItems(list)
|
2010-11-12 15:36:57 -08:00
|
|
|
|
.Pager(pagerShape)
|
|
|
|
|
.ShowPager(container.As<ContainerPart>().Record.Paginated);
|
2010-11-08 17:44:43 -08:00
|
|
|
|
|
2010-11-18 11:21:43 -08:00
|
|
|
|
return View((object)viewModel);
|
2010-11-08 15:33:56 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|