using System; using System.Linq; using System.Web.Mvc; using Orchard.ContentManagement; using Orchard.Core.Common.Models; using Orchard.Core.Containers.Extensions; using Orchard.Core.Containers.Models; using Orchard.Core.Routable.Models; using Orchard.DisplayManagement; using Orchard.Mvc; using Orchard.Themes; using Orchard.UI.Navigation; using Orchard.Settings; namespace Orchard.Core.Containers.Controllers { public class ItemController : Controller { private readonly IContentManager _contentManager; private readonly IContainersPathConstraint _containersPathConstraint; private readonly ISiteService _siteService; public ItemController( IContentManager contentManager, IContainersPathConstraint containersPathConstraint, IShapeFactory shapeFactory, ISiteService siteService) { _contentManager = contentManager; _containersPathConstraint = containersPathConstraint; _siteService = siteService; Shape = shapeFactory; } dynamic Shape { get; set; } [Themed] public ActionResult Display(string path, PagerParameters pagerParameters) { var matchedPath = _containersPathConstraint.FindPath(path); if (string.IsNullOrEmpty(matchedPath)) { throw new ApplicationException("404 - should not have passed path constraint"); } var hits = _contentManager .Query(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"); } var container = _contentManager.Get(hits.Single().Id); IContentQuery query = _contentManager .Query(VersionOptions.Published) .Join().Where(cr => cr.Container.Id == container.Id); var descendingOrder = container.As().Record.OrderByDirection == (int) OrderByDirection.Descending; query = query.OrderBy(container.As().Record.OrderByProperty, descendingOrder); Pager pager = new Pager(_siteService.GetSiteSettings(), pagerParameters); pager.PageSize = pagerParameters.PageSize != null && container.As().Record.Paginated ? pager.PageSize : container.As().Record.PageSize; var pagerShape = Shape.Pager(pager).TotalItemCount(query.Count()); var startIndex = container.As().Record.Paginated ? pager.GetStartIndex() : 0; var pageOfItems = query.Slice(startIndex, pager.PageSize).ToList(); var list = Shape.List(); list.AddRange(pageOfItems.Select(item => _contentManager.BuildDisplay(item, "Summary"))); list.Classes.Add("content-items"); list.Classes.Add("list-items"); var model = _contentManager.BuildDisplay(container, "Detail"); model.Content.Add(list, "5"); if (container.As().Record.Paginated) { model.Content.Add(pagerShape, "5.1"); } return new ShapeResult(this, model); } } }