#20590: Adding a file name editor to Media Library items

#20590: Bugfix

#20590: Base Class Shape is Required

#20590: Formatting and file name comparison with OrdinalIgnoreCase

#20590: Removing base class Shape

#20590: Using Current instead of GetSetting

#20590: Updating comment

#20590: Migration Bugfix

#20590: Undoing rename

#20590: Removing extra whitespace in driver

#20590 Rename ShowFileNameEditor to FileNameEditor, Driver improvement, Updated Migration for MediaLibrary

Conflicts:
	src/Orchard.Web/Modules/Orchard.MediaLibrary/Migrations.cs

#20590 Bugfix

#20590 Improving File Name editors display

#20590: Notifier Message Fix

#20590: Functionality to rename a media item

Work Item: 20590

Conflicts:
	src/Orchard.Web/Modules/Orchard.MediaLibrary/Migrations.cs
	src/Orchard.Web/Modules/Orchard.MediaLibrary/Orchard.MediaLibrary.csproj

Conflicts:
	src/Orchard.Web/Modules/Orchard.MediaLibrary/Orchard.MediaLibrary.csproj
This commit is contained in:
Stanley Goldman 2014-03-31 19:14:19 -04:00 committed by Stanley Goldman
parent 1d0b574c5e
commit 21e269ef20
8 changed files with 218 additions and 3 deletions

View File

@ -0,0 +1,77 @@
using System;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Localization;
using Orchard.MediaLibrary.Models;
using Orchard.MediaLibrary.Services;
using Orchard.Security;
using Orchard.UI.Notify;
namespace Orchard.MediaLibrary.MediaFileName
{
public class MediaFileNameDriver : ContentPartDriver<MediaPart> {
private readonly IAuthenticationService _authenticationService;
private readonly IAuthorizationService _authorizationService;
private readonly IMediaLibraryService _mediaLibraryService;
private readonly INotifier _notifier;
public MediaFileNameDriver(IAuthorizationService authorizationService, IAuthenticationService authenticationService, IMediaLibraryService mediaLibraryService, INotifier notifier) {
_authorizationService = authorizationService;
_authenticationService = authenticationService;
_mediaLibraryService = mediaLibraryService;
_notifier = notifier;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
protected override string Prefix {
get { return "MediaFileName"; }
}
protected override DriverResult Editor(MediaPart part, dynamic shapeHelper) {
return Editor(part, null, shapeHelper);
}
protected override DriverResult Editor(MediaPart part, IUpdateModel updater, dynamic shapeHelper) {
return ContentShape(
"Parts_Media_Edit_FileName",
() => {
var currentUser = _authenticationService.GetAuthenticatedUser();
if (!_authorizationService.TryCheckAccess(Permissions.ManageMediaContent, currentUser, part)) {
return null;
}
var settings = part.TypeDefinition.Settings.GetModel<MediaFileNameEditorSettings>();
if (!settings.ShowFileNameEditor) {
return null;
}
MediaFileNameEditorViewModel model = shapeHelper.Parts_Media_Edit_FileName(typeof(MediaFileNameEditorViewModel));
if (part.FileName != null) {
model.FileName = part.FileName;
}
if (updater != null) {
var priorFileName = model.FileName;
if (updater.TryUpdateModel(model, Prefix, null, null)) {
if (model.FileName != null && !model.FileName.Equals(priorFileName, StringComparison.OrdinalIgnoreCase)) {
try {
_mediaLibraryService.RenameFile(part.FolderPath, priorFileName, model.FileName);
part.FileName = model.FileName;
_notifier.Add(NotifyType.Information, T("File '{0}' was renamed to '{1}'", priorFileName, model.FileName));
}
catch (Exception) {
updater.AddModelError("MediaFileNameEditorSettings.FileName", T("Unable to rename file"));
}
}
}
}
return model;
});
}
}
}

View File

@ -0,0 +1,37 @@
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.MediaLibrary.MediaFileName {
public class MediaFileNameEditorSettings {
public MediaFileNameEditorSettings() {
// file name editor should not be displayed by default
ShowFileNameEditor = false;
}
public bool ShowFileNameEditor { get; set; }
}
public class MediaFileNameEditorSettingsEvents : ContentDefinitionEditorEventsBase {
public override IEnumerable<TemplateViewModel> TypeEditor(ContentTypeDefinition definition) {
if (definition.Settings.ContainsKey("Stereotype") && definition.Settings["Stereotype"] == "Media") {
var model = definition.Settings.GetModel<MediaFileNameEditorSettings>();
yield return DefinitionTemplate(model);
}
}
public override IEnumerable<TemplateViewModel> TypeEditorUpdate(ContentTypeDefinitionBuilder builder, IUpdateModel updateModel) {
if (builder.Current.Settings["Stereotype"] == "Media") {
var model = new MediaFileNameEditorSettings();
if (updateModel.TryUpdateModel(model, "MediaFileNameEditorSettings", null, null)) {
builder.WithSetting("MediaFileNameEditorSettings.ShowFileNameEditor", model.ShowFileNameEditor.ToString());
}
}
return base.TypeEditorUpdate(builder, updateModel);
}
}
}

View File

@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;
using Orchard.DisplayManagement.Shapes;
namespace Orchard.MediaLibrary.MediaFileName
{
public class MediaFileNameEditorViewModel : Shape {
[Required]
public string FileName { get; set; }
}
}

View File

@ -14,11 +14,47 @@ namespace Orchard.MediaLibrary {
.Column<string>("AlternateText", c => c.Unlimited())
.Column<string>("FolderPath", c => c.WithLength(2048))
.Column<string>("FileName", c => c.WithLength(2048))
);
);
SchemaBuilder.AlterTable("MediaPartRecord", t => t
.CreateIndex("IDX_MediaPartRecord_FolderPath", "FolderPath")
);
ContentDefinitionManager.AlterPartDefinition("MediaPart", part => part
.Attachable()
.WithDescription("Turns a content type into a Media. Note: you need to set the stereotype to \"Media\" as well.")
);
ContentDefinitionManager.AlterPartDefinition("ImagePart", part => part
.Attachable()
.WithDescription("Provides common metadata for an Image Media.")
);
ContentDefinitionManager.AlterPartDefinition("VideoPart", part => part
.Attachable()
.WithDescription("Provides common metadata for a Video Media.")
);
ContentDefinitionManager.AlterPartDefinition("AudioPart", part => part
.Attachable()
.WithDescription("Provides common metadata for an Audio Media.")
);
ContentDefinitionManager.AlterPartDefinition("DocumentPart", part => part
.Attachable()
.WithDescription("Provides common metadata for a Document Media.")
);
ContentDefinitionManager.AlterPartDefinition("OEmbedPart", part => part
.Attachable()
.WithDescription("Provides common metadata for an OEmbed Media.")
);
ContentDefinitionManager.AlterTypeDefinition("Image", td => td
.DisplayedAs("Image")
.WithSetting("Stereotype", "Media")
.WithSetting("MediaFileNameEditorSettings.ShowFileNameEditor", "True")
.WithPart("IdentityPart")
.WithPart("CommonPart")
.WithPart("MediaPart")
.WithPart("ImagePart")
@ -28,6 +64,8 @@ namespace Orchard.MediaLibrary {
ContentDefinitionManager.AlterTypeDefinition("Video", td => td
.DisplayedAs("Video")
.WithSetting("Stereotype", "Media")
.WithSetting("MediaFileNameEditorSettings.ShowFileNameEditor", "True")
.WithPart("IdentityPart")
.WithPart("CommonPart")
.WithPart("MediaPart")
.WithPart("VideoPart")
@ -37,6 +75,8 @@ namespace Orchard.MediaLibrary {
ContentDefinitionManager.AlterTypeDefinition("Audio", td => td
.DisplayedAs("Audio")
.WithSetting("Stereotype", "Media")
.WithSetting("MediaFileNameEditorSettings.ShowFileNameEditor", "True")
.WithPart("IdentityPart")
.WithPart("CommonPart")
.WithPart("MediaPart")
.WithPart("AudioPart")
@ -46,6 +86,8 @@ namespace Orchard.MediaLibrary {
ContentDefinitionManager.AlterTypeDefinition("Document", td => td
.DisplayedAs("Document")
.WithSetting("Stereotype", "Media")
.WithSetting("MediaFileNameEditorSettings.ShowFileNameEditor", "True")
.WithPart("IdentityPart")
.WithPart("CommonPart")
.WithPart("MediaPart")
.WithPart("DocumentPart")
@ -55,13 +97,14 @@ namespace Orchard.MediaLibrary {
ContentDefinitionManager.AlterTypeDefinition("OEmbed", td => td
.DisplayedAs("External Media")
.WithSetting("Stereotype", "Media")
.WithPart("IdentityPart")
.WithPart("CommonPart")
.WithPart("MediaPart")
.WithPart("OEmbedPart")
.WithPart("TitlePart")
);
return 2;
return 6;
}
public int UpdateFrom2() {
@ -121,7 +164,7 @@ namespace Orchard.MediaLibrary {
return 4;
}
public int UpdateFrom4() {
SchemaBuilder.AlterTable("MediaPartRecord", t => t
@ -130,5 +173,25 @@ namespace Orchard.MediaLibrary {
return 5;
}
public int UpdateFrom5() {
ContentDefinitionManager.AlterTypeDefinition("Image", td => td
.WithSetting("MediaFileNameEditorSettings.ShowFileNameEditor", "True")
);
ContentDefinitionManager.AlterTypeDefinition("Video", td => td
.WithSetting("MediaFileNameEditorSettings.ShowFileNameEditor", "True")
);
ContentDefinitionManager.AlterTypeDefinition("Audio", td => td
.WithSetting("MediaFileNameEditorSettings.ShowFileNameEditor", "True")
);
ContentDefinitionManager.AlterTypeDefinition("Document", td => td
.WithSetting("MediaFileNameEditorSettings.ShowFileNameEditor", "True")
);
return 6;
}
}
}

View File

@ -105,6 +105,9 @@
<Content Include="Scripts\Web.config" />
<Content Include="Styles\Web.config" />
<Compile Include="Models\IMediaFolder.cs" />
<Compile Include="MediaFileName\MediaFileNameDriver.cs" />
<Compile Include="MediaFileName\MediaFileNameEditorSettings.cs" />
<Compile Include="MediaFileName\MediaFileNameEditorViewModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Content Include="Module.txt" />
</ItemGroup>
@ -351,6 +354,12 @@
<ItemGroup>
<Content Include="Views\MediaLibraryPicker.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\DefinitionTemplates\MediaFileNameEditorSettings.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Parts\Media.Edit.FileName.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@ -1,5 +1,6 @@
<Placement>
<Place Parts_Media_Edit="Content:6"/>
<Place Parts_Media_Edit_FileName="Content:6"/>
<!-- Raw Media -->
<Match DisplayType="Raw">

View File

@ -0,0 +1,9 @@
@model Orchard.MediaLibrary.MediaFileName.MediaFileNameEditorSettings
<fieldset>
<div>
@Html.EditorFor(m=>m.ShowFileNameEditor)
<label for="@Html.FieldIdFor(m => m.ShowFileNameEditor)" class="forcheckbox">@T("Show editor for filename")</label>
@Html.ValidationMessageFor(m => m.ShowFileNameEditor)
</div>
</fieldset>

View File

@ -0,0 +1,9 @@
@model Orchard.MediaLibrary.MediaFileName.MediaFileNameEditorViewModel
@{
var MediaFileName = Model;
}
<fieldset>
@Html.LabelFor(m => MediaFileName.FileName, T("File Name"))
@Html.TextBoxFor(m => MediaFileName.FileName, new { @class = "text large" })
@Html.ValidationMessageFor(m => MediaFileName.FileName)
</fieldset>