mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Adding upgrade steps for MediaPickerField to MediaLibraryField
--HG-- branch : 1.x
This commit is contained in:
@@ -4,18 +4,19 @@ using System.Linq;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.FieldStorage;
|
||||
using Orchard.ContentManagement.Utilities;
|
||||
using Orchard.MediaLibrary.Models;
|
||||
|
||||
namespace Orchard.MediaLibrary.Fields {
|
||||
public class MediaLibraryPickerField : ContentField {
|
||||
private static readonly char[] separator = new [] {'{', '}', ','};
|
||||
internal LazyField<IEnumerable<ContentItem>> _contentItems = new LazyField<IEnumerable<ContentItem>>();
|
||||
internal LazyField<IEnumerable<MediaPart>> _contentItems = new LazyField<IEnumerable<MediaPart>>();
|
||||
|
||||
public int[] Ids {
|
||||
get { return DecodeIds(Storage.Get<string>()); }
|
||||
set { Storage.Set(EncodeIds(value)); }
|
||||
}
|
||||
|
||||
public IEnumerable<ContentItem> ContentItems {
|
||||
public IEnumerable<MediaPart> MediaParts {
|
||||
get {
|
||||
return _contentItems.Value;
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.MediaLibrary.Fields;
|
||||
using Orchard.MediaLibrary.Models;
|
||||
|
||||
namespace Orchard.MediaLibrary.Handlers {
|
||||
public class MediaLibraryPickerFieldHandler : ContentHandler {
|
||||
@@ -30,7 +31,7 @@ namespace Orchard.MediaLibrary.Handlers {
|
||||
|
||||
foreach (var field in fields) {
|
||||
var localField = field;
|
||||
field._contentItems.Loader(x => _contentManager.GetMany<ContentItem>(localField.Ids, VersionOptions.Published, QueryHints.Empty));
|
||||
field._contentItems.Loader(x => _contentManager.GetMany<MediaPart>(localField.Ids, VersionOptions.Published, QueryHints.Empty));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -5,15 +5,15 @@
|
||||
@{
|
||||
var field = (MediaLibraryPickerField) Model.ContentField;
|
||||
string name = field.DisplayName;
|
||||
var contentItems = field.ContentItems;
|
||||
var mediaParts = field.MediaParts;
|
||||
|
||||
var returnUrl = ViewContext.RequestContext.HttpContext.Request.ToUrlString();
|
||||
}
|
||||
<span class="name">@name:</span>
|
||||
<p class="media-library-picker-field media-library-picker-field-@name.HtmlClassify()">
|
||||
|
||||
@if(contentItems.Any()) {
|
||||
foreach(var contentItem in contentItems) {
|
||||
@if (mediaParts.Any()) {
|
||||
foreach (var contentItem in mediaParts) {
|
||||
<span>
|
||||
@if (Authorizer.Authorize(Permissions.EditContent, contentItem)) {
|
||||
<a href="@Url.ItemEditUrl(contentItem, new {returnUrl})">
|
||||
|
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Orchard.FileSystems.WebSite;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
@@ -11,10 +10,8 @@ using Orchard.Utility.Extensions;
|
||||
|
||||
namespace Orchard.MediaPicker.Drivers {
|
||||
public class MediaPickerFieldDriver : ContentFieldDriver<MediaPickerField> {
|
||||
private readonly IWebSiteFolder _webSiteFolder;
|
||||
|
||||
public MediaPickerFieldDriver(IWebSiteFolder webSiteFolder) {
|
||||
_webSiteFolder = webSiteFolder;
|
||||
public MediaPickerFieldDriver() {
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Orchard;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.FieldStorage.InfosetStorage;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.Environment.Features;
|
||||
using Orchard.FileSystems.Media;
|
||||
using Orchard.Localization;
|
||||
@@ -21,6 +25,7 @@ namespace Upgrade.Controllers {
|
||||
private readonly IFeatureManager _featureManager;
|
||||
private readonly IEnumerable<IMediaLibraryService> _mediaLibraryServices;
|
||||
private readonly IMimeTypeProvider _mimeTypeProvider;
|
||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||
private IMediaLibraryService _mediaLibraryService { get { return _mediaLibraryServices.FirstOrDefault(); }}
|
||||
private int BATCH = 100;
|
||||
|
||||
@@ -28,11 +33,13 @@ namespace Upgrade.Controllers {
|
||||
IOrchardServices orchardServices,
|
||||
IFeatureManager featureManager,
|
||||
IEnumerable<IMediaLibraryService> mediaLibraryServices,
|
||||
IMimeTypeProvider mimeTypeProvider) {
|
||||
IMimeTypeProvider mimeTypeProvider,
|
||||
IContentDefinitionManager contentDefinitionManager) {
|
||||
_orchardServices = orchardServices;
|
||||
_featureManager = featureManager;
|
||||
_mediaLibraryServices = mediaLibraryServices;
|
||||
_mimeTypeProvider = mimeTypeProvider;
|
||||
_contentDefinitionManager = contentDefinitionManager;
|
||||
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
@@ -69,6 +76,28 @@ namespace Upgrade.Controllers {
|
||||
}
|
||||
}
|
||||
|
||||
// ensuring all media items have been migrated
|
||||
var hasMore = false;
|
||||
|
||||
if (ViewBag.CanMigrate) {
|
||||
|
||||
// crawl media file, ignore recipes and profiles
|
||||
IEnumerable<MediaFolder> mediaFolders = _mediaLibraryService.GetMediaFolders(null);
|
||||
foreach (var mediaFolder in mediaFolders) {
|
||||
ImportMediaFolder(mediaFolder, x => {
|
||||
var media = _orchardServices.ContentManager.Query().ForPart<MediaPart>().Where<MediaPartRecord>(m => m.FolderPath == x.FolderName && m.FileName == x.Name).Slice(0, 1).FirstOrDefault();
|
||||
if (media == null) {
|
||||
hasMore = true;
|
||||
}
|
||||
});
|
||||
if (hasMore) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ViewBag.CanMigrateFields = ViewBag.CanMigrate && !hasMore;
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
@@ -141,5 +170,52 @@ namespace Upgrade.Controllers {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult MediaPicker() {
|
||||
|
||||
var matches = _contentDefinitionManager
|
||||
.ListTypeDefinitions()
|
||||
.SelectMany(ct => ct.Parts.SelectMany(p => p.PartDefinition.Fields.Select(y => new {Type = ct, Part = p, Field = y})))
|
||||
.Where(x => x.Field.FieldDefinition.Name == "MediaPickerField");
|
||||
|
||||
foreach (var match in matches) {
|
||||
foreach (var contentItem in _orchardServices.ContentManager.Query().ForType(match.Type.Name).List()) {
|
||||
var contentPart = contentItem.Parts.FirstOrDefault(x => x.PartDefinition.Name == match.Part.PartDefinition.Name);
|
||||
if (contentPart != null) {
|
||||
dynamic contentField = contentPart.Fields.FirstOrDefault(x => x.Name == match.Field.Name);
|
||||
if (contentField != null && contentField.Url != null) {
|
||||
string url = Convert.ToString(contentField.Url);
|
||||
var filename = Path.GetFileName(url);
|
||||
var media = _orchardServices.ContentManager.Query().ForPart<MediaPart>().Where<MediaPartRecord>(x => filename == x.FileName).Slice(0, 1).FirstOrDefault();
|
||||
if (media != null) {
|
||||
contentField.Url = "{" + media.Id + "}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var match in matches) {
|
||||
|
||||
string hint, required;
|
||||
match.Field.Settings.TryGetValue("MediaPickerFieldSettings.Hint", out hint);
|
||||
match.Field.Settings.TryGetValue("MediaPickerFieldSettings.Required", out required);
|
||||
|
||||
_contentDefinitionManager.AlterPartDefinition(match.Part.PartDefinition.Name,
|
||||
cfg => cfg.RemoveField(match.Field.Name));
|
||||
|
||||
_contentDefinitionManager.AlterPartDefinition(match.Part.PartDefinition.Name, cfg => cfg
|
||||
.WithField(match.Field.Name, builder => builder
|
||||
.OfType("MediaLibraryPickerField")
|
||||
.WithDisplayName(match.Field.DisplayName)
|
||||
.WithSetting("MediaLibraryPickerFieldSettings.Hint", hint)
|
||||
.WithSetting("MediaLibraryPickerFieldSettings.Required", required)
|
||||
.WithSetting("MediaLibraryPickerFieldSettings.Multiple", false.ToString(CultureInfo.InvariantCulture))
|
||||
.WithSetting("MediaLibraryPickerFieldSettings.DisplayedContentTypes", String.Empty)
|
||||
));
|
||||
}
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -16,4 +16,20 @@
|
||||
<button type="submit">@T("Migrate")</button>
|
||||
}
|
||||
</fieldset>
|
||||
}
|
||||
|
||||
@using (Html.BeginFormAntiForgeryPost(Url.Action("MediaPicker", "Media"))) {
|
||||
<fieldset>
|
||||
<legend>@T("Migrating Media Picker Fields:")</legend>
|
||||
<span class="hint">@T("This migration step will migrate your Media Picker and Media Gallery Picker fields to the new Media Library Field.")</span>
|
||||
@if (!ViewBag.CanMigrateFields) {
|
||||
<span class="hint">@T("Before migrating fields, please migrate all the Media files.")</span>
|
||||
}
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
@if (ViewBag.CanMigrateFields) {
|
||||
<button type="submit">@T("Migrate")</button>
|
||||
}
|
||||
</fieldset>
|
||||
|
||||
}
|
Reference in New Issue
Block a user