diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Drivers/TermPartDriver.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Drivers/TermPartDriver.cs index cab6831d7..1f57334c7 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Drivers/TermPartDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Drivers/TermPartDriver.cs @@ -1,137 +1,143 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web.Routing; -using Orchard.ContentManagement; -using Orchard.ContentManagement.Drivers; -using Orchard.Taxonomies.Models; -using Orchard.Taxonomies.Services; -using Orchard.ContentManagement.Handlers; -using Orchard.Core.Feeds; -using Orchard.Localization; -using Orchard.Mvc; -using Orchard.Settings; -using Orchard.UI.Navigation; - -namespace Orchard.Taxonomies.Drivers { - public class TermPartDriver : ContentPartDriver { - private readonly ITaxonomyService _taxonomyService; - private readonly ISiteService _siteService; - private readonly IHttpContextAccessor _httpContextAccessor; - private readonly IFeedManager _feedManager; - private readonly IContentManager _contentManager; - - public TermPartDriver( - ITaxonomyService taxonomyService, - ISiteService siteService, - IHttpContextAccessor httpContextAccessor, - IFeedManager feedManager, - IContentManager contentManager) { - _taxonomyService = taxonomyService; - _siteService = siteService; - _httpContextAccessor = httpContextAccessor; - _feedManager = feedManager; - _contentManager = contentManager; - T = NullLocalizer.Instance; - } - - public Localizer T { get; set; } - protected override string Prefix { get { return "Term"; } } - - protected override DriverResult Display(TermPart part, string displayType, dynamic shapeHelper) { - return Combined( - ContentShape("Parts_TermPart_Feed", () => { - - // generates a link to the RSS feed for this term - _feedManager.Register(part.Name, "rss", new RouteValueDictionary { { "term", part.Id } }); - return null; - }), - ContentShape("Parts_TermPart", () => { - var pagerParameters = new PagerParameters(); - var httpContext = _httpContextAccessor.Current(); - if (httpContext != null) { - pagerParameters.Page = Convert.ToInt32(httpContext.Request.QueryString["page"]); - } - - var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters); - var taxonomy = _taxonomyService.GetTaxonomy(part.TaxonomyId); - var totalItemCount = _taxonomyService.GetContentItemsCount(part); - - // asign Taxonomy and Term to the content item shape (Content) in order to provide - // alternates when those content items are displayed when they are listed on a term - var termContentItems = _taxonomyService.GetContentItems(part, pager.GetStartIndex(), pager.PageSize) - .Select(c => _contentManager.BuildDisplay(c, "Summary").Taxonomy(taxonomy).Term(part)); - - var list = shapeHelper.List(); - - list.AddRange(termContentItems); - - var pagerShape = shapeHelper.Pager(pager) - .TotalItemCount(totalItemCount) - .Taxonomy(taxonomy) - .Term(part); - - return shapeHelper.Parts_TermPart(ContentItems: list, Taxonomy: taxonomy, Pager: pagerShape); - })); - } - - protected override DriverResult Editor(TermPart part, dynamic shapeHelper) { - return ContentShape("Parts_Taxonomies_Term_Fields", - () => shapeHelper.EditorTemplate(TemplateName: "Parts/Taxonomies.Term.Fields", Model: part, Prefix: Prefix)); - } - - protected override DriverResult Editor(TermPart termPart, IUpdateModel updater, dynamic shapeHelper) { - if (updater.TryUpdateModel(termPart, Prefix, null, null)) { - var existing = _taxonomyService.GetTermByName(termPart.TaxonomyId, termPart.Name); - if (existing != null && existing.Record != termPart.Record && existing.Container.ContentItem.Record == termPart.Container.ContentItem.Record) { - updater.AddModelError("Name", T("The term {0} already exists at this level", termPart.Name)); - } - } - - return Editor(termPart, shapeHelper); - } - - protected override void Exporting(TermPart part, ExportContentContext context) { - context.Element(part.PartDefinition.Name).SetAttributeValue("Count", part.Record.Count); - context.Element(part.PartDefinition.Name).SetAttributeValue("Selectable", part.Record.Selectable); - context.Element(part.PartDefinition.Name).SetAttributeValue("Weight", part.Record.Weight); - - var taxonomy = _contentManager.Get(part.Record.TaxonomyId); - var identity = _contentManager.GetItemMetadata(taxonomy).Identity.ToString(); - context.Element(part.PartDefinition.Name).SetAttributeValue("TaxonomyId", identity); - - var identityPaths = new List(); - foreach(var pathPart in part.Record.Path.Split('/')) { - if(String.IsNullOrEmpty(pathPart)) { - continue; - } - - var parent = _contentManager.Get(Int32.Parse(pathPart)); - identityPaths.Add(_contentManager.GetItemMetadata(parent).Identity.ToString()); - } - - context.Element(part.PartDefinition.Name).SetAttributeValue("Path", String.Join(",", identityPaths.ToArray())); - } - - protected override void Importing(TermPart part, ImportContentContext context) { - part.Record.Count = Int32.Parse(context.Attribute(part.PartDefinition.Name, "Count")); - part.Record.Selectable = Boolean.Parse(context.Attribute(part.PartDefinition.Name, "Selectable")); - part.Record.Weight = Int32.Parse(context.Attribute(part.PartDefinition.Name, "Weight")); - - var identity = context.Attribute(part.PartDefinition.Name, "TaxonomyId"); - var contentItem = context.GetItemFromSession(identity); - - if (contentItem == null) { - throw new OrchardException(T("Unknown taxonomy: {0}", identity)); - } - +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web.Routing; +using Orchard.ContentManagement; +using Orchard.ContentManagement.Drivers; +using Orchard.Taxonomies.Models; +using Orchard.Taxonomies.Services; +using Orchard.ContentManagement.Handlers; +using Orchard.Core.Feeds; +using Orchard.Localization; +using Orchard.Mvc; +using Orchard.Settings; +using Orchard.Taxonomies.Settings; +using Orchard.UI.Navigation; + +namespace Orchard.Taxonomies.Drivers { + public class TermPartDriver : ContentPartDriver { + private readonly ITaxonomyService _taxonomyService; + private readonly ISiteService _siteService; + private readonly IHttpContextAccessor _httpContextAccessor; + private readonly IFeedManager _feedManager; + private readonly IContentManager _contentManager; + + public TermPartDriver( + ITaxonomyService taxonomyService, + ISiteService siteService, + IHttpContextAccessor httpContextAccessor, + IFeedManager feedManager, + IContentManager contentManager) { + _taxonomyService = taxonomyService; + _siteService = siteService; + _httpContextAccessor = httpContextAccessor; + _feedManager = feedManager; + _contentManager = contentManager; + T = NullLocalizer.Instance; + } + + public Localizer T { get; set; } + protected override string Prefix { get { return "Term"; } } + + protected override DriverResult Display(TermPart part, string displayType, dynamic shapeHelper) { + return Combined( + ContentShape("Parts_TermPart_Feed", () => { + + // generates a link to the RSS feed for this term + _feedManager.Register(part.Name, "rss", new RouteValueDictionary { { "term", part.Id } }); + return null; + }), + ContentShape("Parts_TermPart", () => { + var pagerParameters = new PagerParameters(); + var httpContext = _httpContextAccessor.Current(); + if (httpContext != null) { + pagerParameters.Page = Convert.ToInt32(httpContext.Request.QueryString["page"]); + } + + var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters); + var taxonomy = _taxonomyService.GetTaxonomy(part.TaxonomyId); + var totalItemCount = _taxonomyService.GetContentItemsCount(part); + + var partSettings = part.Settings.GetModel(); + var childDisplayType = partSettings != null && + !String.IsNullOrWhiteSpace(partSettings.ChildDisplayType) + ? partSettings.ChildDisplayType + : "Summary"; + // asign Taxonomy and Term to the content item shape (Content) in order to provide + // alternates when those content items are displayed when they are listed on a term + var termContentItems = _taxonomyService.GetContentItems(part, pager.GetStartIndex(), pager.PageSize) + .Select(c => _contentManager.BuildDisplay(c, childDisplayType).Taxonomy(taxonomy).Term(part)); + + var list = shapeHelper.List(); + + list.AddRange(termContentItems); + + var pagerShape = shapeHelper.Pager(pager) + .TotalItemCount(totalItemCount) + .Taxonomy(taxonomy) + .Term(part); + + return shapeHelper.Parts_TermPart(ContentItems: list, Taxonomy: taxonomy, Pager: pagerShape); + })); + } + + protected override DriverResult Editor(TermPart part, dynamic shapeHelper) { + return ContentShape("Parts_Taxonomies_Term_Fields", + () => shapeHelper.EditorTemplate(TemplateName: "Parts/Taxonomies.Term.Fields", Model: part, Prefix: Prefix)); + } + + protected override DriverResult Editor(TermPart termPart, IUpdateModel updater, dynamic shapeHelper) { + if (updater.TryUpdateModel(termPart, Prefix, null, null)) { + var existing = _taxonomyService.GetTermByName(termPart.TaxonomyId, termPart.Name); + if (existing != null && existing.Record != termPart.Record && existing.Container.ContentItem.Record == termPart.Container.ContentItem.Record) { + updater.AddModelError("Name", T("The term {0} already exists at this level", termPart.Name)); + } + } + + return Editor(termPart, shapeHelper); + } + + protected override void Exporting(TermPart part, ExportContentContext context) { + context.Element(part.PartDefinition.Name).SetAttributeValue("Count", part.Record.Count); + context.Element(part.PartDefinition.Name).SetAttributeValue("Selectable", part.Record.Selectable); + context.Element(part.PartDefinition.Name).SetAttributeValue("Weight", part.Record.Weight); + + var taxonomy = _contentManager.Get(part.Record.TaxonomyId); + var identity = _contentManager.GetItemMetadata(taxonomy).Identity.ToString(); + context.Element(part.PartDefinition.Name).SetAttributeValue("TaxonomyId", identity); + + var identityPaths = new List(); + foreach(var pathPart in part.Record.Path.Split('/')) { + if(String.IsNullOrEmpty(pathPart)) { + continue; + } + + var parent = _contentManager.Get(Int32.Parse(pathPart)); + identityPaths.Add(_contentManager.GetItemMetadata(parent).Identity.ToString()); + } + + context.Element(part.PartDefinition.Name).SetAttributeValue("Path", String.Join(",", identityPaths.ToArray())); + } + + protected override void Importing(TermPart part, ImportContentContext context) { + part.Record.Count = Int32.Parse(context.Attribute(part.PartDefinition.Name, "Count")); + part.Record.Selectable = Boolean.Parse(context.Attribute(part.PartDefinition.Name, "Selectable")); + part.Record.Weight = Int32.Parse(context.Attribute(part.PartDefinition.Name, "Weight")); + + var identity = context.Attribute(part.PartDefinition.Name, "TaxonomyId"); + var contentItem = context.GetItemFromSession(identity); + + if (contentItem == null) { + throw new OrchardException(T("Unknown taxonomy: {0}", identity)); + } + part.Record.TaxonomyId = contentItem.Id; part.Record.Path = "/"; - - foreach(var identityPath in context.Attribute(part.PartDefinition.Name, "Path").Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries)) { - var pathContentItem = context.GetItemFromSession(identityPath); + + foreach(var identityPath in context.Attribute(part.PartDefinition.Name, "Path").Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries)) { + var pathContentItem = context.GetItemFromSession(identityPath); part.Record.Path += pathContentItem.Id + "/"; - } - } - } + } + } + } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Orchard.Taxonomies.csproj b/src/Orchard.Web/Modules/Orchard.Taxonomies/Orchard.Taxonomies.csproj index b4a017dc1..11a06cdcd 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Orchard.Taxonomies.csproj +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Orchard.Taxonomies.csproj @@ -1,240 +1,244 @@ - - - - - Debug - AnyCPU - - - 2.0 - {E649EA64-D213-461B-87F7-D67035801443} - {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} - Library - Properties - Orchard.Taxonomies - Orchard.Taxonomies - v4.0 - false - false - - - - - 4.0 - - - - - - - true - full - false - bin\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\ - TRACE - prompt - 4 - - - - - - 3.5 - - - 3.5 - - - False - ..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll - - - 3.5 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + Debug + AnyCPU + + + 2.0 + {E649EA64-D213-461B-87F7-D67035801443} + {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + Orchard.Taxonomies + Orchard.Taxonomies + v4.0 + false + false + + + + + 4.0 + + + + + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\ + TRACE + prompt + 4 + + + + + + 3.5 + + + 3.5 + + + False + ..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll + + + 3.5 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - Designer - - - Designer - - - - - - - - - - Designer - - - - - - - - - - - {2d1d92bb-4555-4cbe-8d0e-63563d6ce4c6} - Orchard.Framework - - - {9916839c-39fc-4ceb-a5af-89ca7e87119f} - Orchard.Core - - - {475b6c45-b27c-438b-8966-908b9d6d1077} - Orchard.Alias - - - {66fccd76-2761-47e3-8d11-b45d0001ddaa} - Orchard.Autoroute - - - {6f759635-13d7-4e94-bcc9-80445d63f117} - Orchard.Tokens - - - - - - - - - - - - Code - - - - - - - - - - - - 10.0 - $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - - - - - - - - - - - - - False - True - 2078 - / - - - False - True - http://orchard.codeplex.com - False - - - - + + Designer + + + Designer + + + + + + + + + + Designer + + + + + + + + + + + {2d1d92bb-4555-4cbe-8d0e-63563d6ce4c6} + Orchard.Framework + + + {9916839c-39fc-4ceb-a5af-89ca7e87119f} + Orchard.Core + + + {475b6c45-b27c-438b-8966-908b9d6d1077} + Orchard.Alias + + + {66fccd76-2761-47e3-8d11-b45d0001ddaa} + Orchard.Autoroute + + + {6f759635-13d7-4e94-bcc9-80445d63f117} + Orchard.Tokens + + + + + + + + + + + + Code + + + + + + + + + + + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + + + + + + + + + False + True + 2078 + / + + + False + True + http://orchard.codeplex.com + False + + + + \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Settings/TermPartEditorEvents.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Settings/TermPartEditorEvents.cs new file mode 100644 index 000000000..b3fdd3801 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Settings/TermPartEditorEvents.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using Orchard.ContentManagement; +using Orchard.ContentManagement.MetaData; +using Orchard.ContentManagement.MetaData.Builders; +using Orchard.ContentManagement.MetaData.Models; +using Orchard.ContentManagement.ViewModels; + +namespace Orchard.Taxonomies.Settings { + public class TermPartEditorEvents : ContentDefinitionEditorEventsBase { + public override IEnumerable TypePartEditor(ContentTypePartDefinition definition) { + if (definition.PartDefinition.Name == "TermPart") { + var model = definition.Settings.GetModel(); + yield return DefinitionTemplate(model); + } + } + + public override IEnumerable TypePartEditorUpdate(ContentTypePartDefinitionBuilder builder, IUpdateModel updateModel) { + if (builder.Name != "TermPart") { + yield break; + } + + var model = new TermPartSettings(); + + if (updateModel.TryUpdateModel(model, "TermPartSettings", null, null)) { + builder + .WithSetting("TermPartSettings.ChildDisplayType", model.ChildDisplayType); + } + + yield return DefinitionTemplate(model); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Settings/TermPartSettings.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Settings/TermPartSettings.cs new file mode 100644 index 000000000..f56d45a6d --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Settings/TermPartSettings.cs @@ -0,0 +1,8 @@ +namespace Orchard.Taxonomies.Settings { + public class TermPartSettings { + /// + /// The display type to use for the child items of the term. + /// + public string ChildDisplayType { get; set; } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Views/DefinitionTemplates/TermPartSettings.cshtml b/src/Orchard.Web/Modules/Orchard.Taxonomies/Views/DefinitionTemplates/TermPartSettings.cshtml new file mode 100644 index 000000000..85bf95200 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Views/DefinitionTemplates/TermPartSettings.cshtml @@ -0,0 +1,7 @@ +@model Orchard.Taxonomies.Settings.TermPartSettings +
+ + @Html.TextBoxFor(m => m.ChildDisplayType, new { @class = "text medium" }) + @T("The display type to apply to child items when displaying the details of this taxonomy term.") + @Html.ValidationMessageFor(m => m.ChildDisplayType) +
\ No newline at end of file