mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Adding fields migration support
--HG-- branch : autoroute
This commit is contained in:
@@ -11,7 +11,8 @@ namespace UpgradeTo14 {
|
||||
}
|
||||
|
||||
public void GetNavigation(NavigationBuilder builder) {
|
||||
builder.Add(T("Migrate Routes"), "0", item => item.Action("Index", "Admin", new { area = "UpgradeTo14" }).Permission(StandardPermissions.SiteOwner));
|
||||
builder.Add(T("Migrate Routes"), "0", item => item.Action("Index", "Route", new { area = "UpgradeTo14" }).Permission(StandardPermissions.SiteOwner));
|
||||
builder.Add(T("Migrate Fields"), "0", item => item.Action("Index", "Field", new { area = "UpgradeTo14" }).Permission(StandardPermissions.SiteOwner));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,94 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
using Orchard;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.Data;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Security;
|
||||
using Orchard.UI.Admin;
|
||||
using Orchard.UI.Notify;
|
||||
using UpgradeTo14.ViewModels;
|
||||
|
||||
namespace UpgradeTo14.Controllers {
|
||||
[Admin]
|
||||
public class FieldController : Controller {
|
||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||
private readonly IOrchardServices _orchardServices;
|
||||
private readonly ISessionFactoryHolder _sessionFactoryHolder;
|
||||
|
||||
public FieldController(
|
||||
IContentDefinitionManager contentDefinitionManager,
|
||||
IOrchardServices orchardServices,
|
||||
ISessionFactoryHolder sessionFactoryHolder) {
|
||||
_contentDefinitionManager = contentDefinitionManager;
|
||||
_orchardServices = orchardServices;
|
||||
_sessionFactoryHolder = sessionFactoryHolder;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public ActionResult Index() {
|
||||
var viewModel = new MigrateViewModel { ContentTypes = new List<ContentTypeEntry>() };
|
||||
foreach (var contentType in _contentDefinitionManager.ListTypeDefinitions().OrderBy(c => c.Name)) {
|
||||
// only display parts with fields
|
||||
if (contentType.Parts.Any(x => x.PartDefinition.Fields.Any())) {
|
||||
viewModel.ContentTypes.Add(new ContentTypeEntry {ContentTypeName = contentType.Name});
|
||||
}
|
||||
}
|
||||
|
||||
if(!viewModel.ContentTypes.Any()) {
|
||||
_orchardServices.Notifier.Warning(T("There are no content types with custom fields"));
|
||||
}
|
||||
|
||||
return View(viewModel);
|
||||
}
|
||||
|
||||
[HttpPost, ActionName("Index")]
|
||||
public ActionResult IndexPOST() {
|
||||
if (!_orchardServices.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to migrate fields.")))
|
||||
return new HttpUnauthorizedResult();
|
||||
|
||||
var viewModel = new MigrateViewModel { ContentTypes = new List<ContentTypeEntry>() };
|
||||
|
||||
if(TryUpdateModel(viewModel)) {
|
||||
var contentTypesToMigrate = viewModel.ContentTypes.Where(c => c.IsChecked).Select(c => c.ContentTypeName);
|
||||
|
||||
foreach (var contentType in contentTypesToMigrate) {
|
||||
|
||||
_orchardServices.ContentManager.Flush();
|
||||
_orchardServices.ContentManager.Clear();
|
||||
|
||||
var count = 0;
|
||||
IEnumerable<ContentItem> contents;
|
||||
|
||||
do {
|
||||
contents = _orchardServices.ContentManager.HqlQuery().ForType(contentType).Slice(count, 100);
|
||||
|
||||
foreach (ContentItem content in contents) {
|
||||
|
||||
// copy data to current version record
|
||||
content.VersionRecord.Data = content.Record.Data;
|
||||
var draft = _orchardServices.ContentManager.Get(content.Id, VersionOptions.Draft);
|
||||
|
||||
if(draft != null) {
|
||||
draft.VersionRecord.Data = content.Record.Data;
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
_orchardServices.ContentManager.Flush();
|
||||
_orchardServices.ContentManager.Clear();
|
||||
|
||||
} while (contents.Any());
|
||||
|
||||
_orchardServices.Notifier.Information(T("{0} fields were migrated successfully", contentType));
|
||||
}
|
||||
}
|
||||
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Transactions;
|
||||
using System.Web.Mvc;
|
||||
using Orchard;
|
||||
@@ -15,18 +13,20 @@ using Orchard.Data;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Security;
|
||||
using Orchard.UI.Admin;
|
||||
using Orchard.UI.Notify;
|
||||
using UpgradeTo14.ViewModels;
|
||||
|
||||
namespace UpgradeTo14.Controllers {
|
||||
public class AdminController : Controller {
|
||||
[Admin]
|
||||
public class RouteController : Controller {
|
||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||
private readonly IOrchardServices _orchardServices;
|
||||
private readonly ISessionFactoryHolder _sessionFactoryHolder;
|
||||
private readonly ShellSettings _shellSettings;
|
||||
private readonly IAutorouteService _autorouteService;
|
||||
|
||||
public AdminController(
|
||||
public RouteController(
|
||||
IContentDefinitionManager contentDefinitionManager,
|
||||
IOrchardServices orchardServices,
|
||||
ISessionFactoryHolder sessionFactoryHolder,
|
@@ -89,14 +89,16 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AdminMenu.cs" />
|
||||
<Compile Include="Controllers\AdminController.cs" />
|
||||
<Compile Include="Controllers\FieldController.cs" />
|
||||
<Compile Include="Controllers\RouteController.cs" />
|
||||
<Compile Include="ViewModels\MigrateViewModel.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Views\Admin\Index.cshtml" />
|
||||
<None Include="Views\Field\Index.cshtml" />
|
||||
<None Include="Views\Route\Index.cshtml" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
|
||||
|
26
src/Orchard.Web/Modules/UpgradeTo14/Views/Field/Index.cshtml
Normal file
26
src/Orchard.Web/Modules/UpgradeTo14/Views/Field/Index.cshtml
Normal file
@@ -0,0 +1,26 @@
|
||||
@using Orchard.Utility.Extensions
|
||||
@model UpgradeTo14.ViewModels.MigrateViewModel
|
||||
|
||||
@{ Layout.Title = T("Migrate Fields").ToString(); }
|
||||
|
||||
@using (Html.BeginFormAntiForgeryPost()) {
|
||||
Html.ValidationSummary();
|
||||
<fieldset>
|
||||
<legend>@T("Choose the types to migrate:")</legend>
|
||||
<span class="hint">@T("The migration process fields data. You will still need to enable Orchard.Fields, and remove Contrib.DateTime and Contrib.MediaPicker if you have installed them.")</span>
|
||||
<ol>
|
||||
@{ var contentTypeIndex = 0; }
|
||||
@foreach (var contentTypeEntry in Model.ContentTypes) {
|
||||
<li>
|
||||
<input type="hidden" value="@Model.ContentTypes[contentTypeIndex].ContentTypeName" name="@Html.NameOf(m => m.ContentTypes[contentTypeIndex].ContentTypeName)"/>
|
||||
<input type="checkbox" value="true" name="@Html.NameOf(m => m.ContentTypes[contentTypeIndex].IsChecked)" id="@Html.NameOf(m => m.ContentTypes[contentTypeIndex].IsChecked)" />
|
||||
<label class="forcheckbox" for="@Html.NameOf(m => m.ContentTypes[contentTypeIndex].IsChecked)">@Model.ContentTypes[contentTypeIndex].ContentTypeName.CamelFriendly()</label>
|
||||
</li>
|
||||
contentTypeIndex = contentTypeIndex + 1;
|
||||
}
|
||||
</ol>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<button type="submit">@T("Migrate")</button>
|
||||
</fieldset>
|
||||
}
|
Reference in New Issue
Block a user