From c0b90bbe185352520b8ddd577a3f220ce2d90ce3 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Mon, 28 Jul 2014 12:32:28 -0700 Subject: [PATCH] Adding Listable metadata --- .../Contents/Controllers/AdminController.cs | 19 +++++++++++--- .../Contents/Extensions/MetaDataExtensions.cs | 4 +++ .../Contents/Settings/ContentTypeSettings.cs | 4 +++ .../Orchard.ContentTypes/Migrations.cs | 26 +++++++++++++++++++ .../Orchard.ContentTypes.csproj | 1 + .../Settings/EditorEvents.cs | 2 ++ .../ContentTypeSettingsViewModel.cs | 3 ++- .../ContentTypeSettingsViewModel.cshtml | 7 +++++ 8 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.ContentTypes/Migrations.cs diff --git a/src/Orchard.Web/Core/Contents/Controllers/AdminController.cs b/src/Orchard.Web/Core/Contents/Controllers/AdminController.cs index 86191480c..84e5dcc5d 100644 --- a/src/Orchard.Web/Core/Contents/Controllers/AdminController.cs +++ b/src/Orchard.Web/Core/Contents/Controllers/AdminController.cs @@ -73,7 +73,7 @@ namespace Orchard.Core.Contents.Controllers { break; } - var query = _contentManager.Query(versionOptions, GetCreatableTypes(false).Select(ctd => ctd.Name).ToArray()); + var query = _contentManager.Query(versionOptions, GetListableTypes(false).Select(ctd => ctd.Name).ToArray()); if (!string.IsNullOrEmpty(model.TypeName)) { var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(model.TypeName); @@ -101,7 +101,7 @@ namespace Orchard.Core.Contents.Controllers { } model.Options.SelectedFilter = model.TypeName; - model.Options.FilterOptions = GetCreatableTypes(false) + model.Options.FilterOptions = GetListableTypes(false) .Select(ctd => new KeyValuePair(ctd.Name, ctd.DisplayName)) .ToList().OrderBy(kvp => kvp.Value); @@ -128,6 +128,13 @@ namespace Orchard.Core.Contents.Controllers { (!andContainable || ctd.Parts.Any(p => p.PartDefinition.Name == "ContainablePart"))); } + private IEnumerable GetListableTypes(bool andContainable) { + return _contentDefinitionManager.ListTypeDefinitions().Where(ctd => + Services.Authorizer.Authorize(Permissions.EditContent, _contentManager.New(ctd.Name)) && + ctd.Settings.GetModel().Listable && + (!andContainable || ctd.Parts.Any(p => p.PartDefinition.Name == "ContainablePart"))); + } + [HttpPost, ActionName("List")] [Mvc.FormValueRequired("submit.Filter")] public ActionResult ListFilterPOST(ContentOptions options) { @@ -135,7 +142,7 @@ namespace Orchard.Core.Contents.Controllers { if (options != null) { routeValues["Options.OrderBy"] = options.OrderBy; //todo: don't hard-code the key routeValues["Options.ContentsStatus"] = options.ContentsStatus; //todo: don't hard-code the key - if (GetCreatableTypes(false).Any(ctd => string.Equals(ctd.Name, options.SelectedFilter, StringComparison.OrdinalIgnoreCase))) { + if (GetListableTypes(false).Any(ctd => string.Equals(ctd.Name, options.SelectedFilter, StringComparison.OrdinalIgnoreCase))) { routeValues["id"] = options.SelectedFilter; } else { @@ -201,6 +208,12 @@ namespace Orchard.Core.Contents.Controllers { return View("CreatableTypeList", viewModel); } + ActionResult ListableTypeList(int? containerId) { + var viewModel = Shape.ViewModel(ContentTypes: GetListableTypes(containerId.HasValue), ContainerId: containerId); + + return View("ListableTypeList", viewModel); + } + public ActionResult Create(string id, int? containerId) { if (string.IsNullOrEmpty(id)) return CreatableTypeList(containerId); diff --git a/src/Orchard.Web/Core/Contents/Extensions/MetaDataExtensions.cs b/src/Orchard.Web/Core/Contents/Extensions/MetaDataExtensions.cs index b67891fa9..42c5c03ca 100644 --- a/src/Orchard.Web/Core/Contents/Extensions/MetaDataExtensions.cs +++ b/src/Orchard.Web/Core/Contents/Extensions/MetaDataExtensions.cs @@ -7,6 +7,10 @@ namespace Orchard.Core.Contents.Extensions { return builder.WithSetting("ContentTypeSettings.Creatable", creatable.ToString()); } + public static ContentTypeDefinitionBuilder Listable(this ContentTypeDefinitionBuilder builder, bool listable = true) { + return builder.WithSetting("ContentTypeSettings.Listable", listable.ToString()); + } + public static ContentTypeDefinitionBuilder Draftable(this ContentTypeDefinitionBuilder builder, bool draftable = true) { return builder.WithSetting("ContentTypeSettings.Draftable", draftable.ToString()); } diff --git a/src/Orchard.Web/Core/Contents/Settings/ContentTypeSettings.cs b/src/Orchard.Web/Core/Contents/Settings/ContentTypeSettings.cs index db94956a6..25567cc29 100644 --- a/src/Orchard.Web/Core/Contents/Settings/ContentTypeSettings.cs +++ b/src/Orchard.Web/Core/Contents/Settings/ContentTypeSettings.cs @@ -5,6 +5,10 @@ /// public bool Creatable { get; set; } /// + /// Used to determine if an instance of this content type can be listed in the contents page + /// + public bool Listable { get; set; } + /// /// Used to determine if this content type supports draft versions /// public bool Draftable { get; set; } diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Migrations.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/Migrations.cs new file mode 100644 index 000000000..a16f9c2ae --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Migrations.cs @@ -0,0 +1,26 @@ +using System; +using System.Globalization; +using Orchard.ContentManagement.MetaData; +using Orchard.Data.Migration; + +namespace Orchard.ContentTypes { + public class Migrations : DataMigrationImpl { + private readonly IContentDefinitionManager _contentDefinitionManager; + + public Migrations(IContentDefinitionManager contentDefinitionManager) { + _contentDefinitionManager = contentDefinitionManager; + } + + public int Create() { + foreach (var typeDefinition in _contentDefinitionManager.ListTypeDefinitions()) { + if (typeDefinition.Settings.ContainsKey("ContentTypeSettings.Creatable") && Convert.ToBoolean(typeDefinition.Settings["ContentTypeSettings.Creatable"], CultureInfo.InvariantCulture)) { + typeDefinition.Settings["ContentTypeSettings.Listable"] = "True"; + _contentDefinitionManager.StoreTypeDefinition(typeDefinition); + } + } + + return 1; + } + + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj b/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj index f5d713ec7..bd2fd29fe 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Orchard.ContentTypes.csproj @@ -79,6 +79,7 @@ + diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Settings/EditorEvents.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/Settings/EditorEvents.cs index d445ef9ab..ae9c7c2a3 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Settings/EditorEvents.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Settings/EditorEvents.cs @@ -16,6 +16,7 @@ namespace Orchard.ContentTypes.Settings { var settings = definition.Settings.GetModel(); var model = new ContentTypeSettingsViewModel { Creatable = settings.Creatable, + Listable = settings.Listable, Draftable = settings.Draftable, }; @@ -31,6 +32,7 @@ namespace Orchard.ContentTypes.Settings { updateModel.TryUpdateModel(model, "ContentTypeSettingsViewModel", null, null); builder.Creatable(model.Creatable); + builder.Listable(model.Listable); builder.Draftable(model.Draftable); builder.WithSetting("Stereotype", model.Stereotype); diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/ContentTypeSettingsViewModel.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/ContentTypeSettingsViewModel.cs index 3b1b173c4..a5cf3a2c1 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/ContentTypeSettingsViewModel.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/ViewModels/ContentTypeSettingsViewModel.cs @@ -1,7 +1,8 @@ namespace Orchard.ContentTypes.ViewModels { public class ContentTypeSettingsViewModel { public bool Creatable { get; set; } - public bool Draftable{ get; set; } + public bool Listable { get; set; } + public bool Draftable { get; set; } public string Stereotype { get; set; } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/DefinitionTemplates/ContentTypeSettingsViewModel.cshtml b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/DefinitionTemplates/ContentTypeSettingsViewModel.cshtml index b206585ee..5afe640f5 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/DefinitionTemplates/ContentTypeSettingsViewModel.cshtml +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Views/DefinitionTemplates/ContentTypeSettingsViewModel.cshtml @@ -7,6 +7,13 @@ @T("Determines if an instance of this content type can be created through the UI.") +
+ @Html.EditorFor(m => m.Listable) + + @Html.ValidationMessageFor(m => m.Listable) + @T("Determines if an instance of this content type can be listed through the UI.") +
+
@Html.EditorFor(m => m.Draftable)