2010-06-08 10:50:08 -07:00
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
using Orchard.ContentManagement.Aspects;
|
|
|
|
|
using Orchard.Core.Common.Models;
|
|
|
|
|
using Orchard.Core.Routable.Models;
|
|
|
|
|
using Orchard.Core.Routable.ViewModels;
|
|
|
|
|
using Orchard.Mvc.ViewModels;
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Core.Routable.Controllers {
|
|
|
|
|
[ValidateInput(false)]
|
|
|
|
|
public class ItemController : Controller {
|
|
|
|
|
private readonly IContentManager _contentManager;
|
2010-06-08 11:01:39 -07:00
|
|
|
private readonly IRoutablePathConstraint _routablePathConstraint;
|
2010-06-08 10:50:08 -07:00
|
|
|
|
2010-06-08 11:01:39 -07:00
|
|
|
public ItemController(IContentManager contentManager, IRoutablePathConstraint routablePathConstraint) {
|
2010-06-08 10:50:08 -07:00
|
|
|
_contentManager = contentManager;
|
2010-06-08 11:01:39 -07:00
|
|
|
_routablePathConstraint = routablePathConstraint;
|
2010-06-08 10:50:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Display(string path) {
|
2010-06-08 11:01:39 -07:00
|
|
|
var matchedPath = _routablePathConstraint.FindPath(path);
|
|
|
|
|
if (string.IsNullOrEmpty(matchedPath)) {
|
|
|
|
|
throw new ApplicationException("404 - should not have passed path constraint");
|
|
|
|
|
}
|
2010-06-08 12:05:29 -07:00
|
|
|
|
2010-06-08 10:50:08 -07:00
|
|
|
var hits = _contentManager
|
|
|
|
|
.Query<IsRoutable, RoutableRecord>(VersionOptions.Published)
|
2010-06-08 11:01:39 -07:00
|
|
|
.Where(r => r.Path == matchedPath)
|
2010-06-08 10:50:08 -07:00
|
|
|
.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 model = new RoutableDisplayViewModel {
|
|
|
|
|
Routable = _contentManager.BuildDisplayModel<IRoutableAspect>(hits.Single(), "Detail")
|
|
|
|
|
};
|
|
|
|
|
PrepareDisplayViewModel(model.Routable);
|
|
|
|
|
return View("Display", model);
|
|
|
|
|
}
|
2010-06-08 11:01:39 -07:00
|
|
|
|
2010-06-08 10:50:08 -07:00
|
|
|
private void PrepareDisplayViewModel(ContentItemViewModel<IRoutableAspect> itemViewModel) {
|
|
|
|
|
if (string.IsNullOrEmpty(itemViewModel.TemplateName)) {
|
|
|
|
|
itemViewModel.TemplateName = "Items/Contents.Item";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|