Adding admin controller for Update module

--HG--
branch : autoroute
This commit is contained in:
Sebastien Ros
2012-02-03 12:46:41 -08:00
parent 8d2e44ee16
commit 5f215d0029
8 changed files with 208 additions and 19 deletions

View File

@@ -1,9 +1,9 @@
81cb672c85fd980dd3db0515544b79a918e5eb69 src/Orchard.Web/Modules/Orchard.Alias
38ee40302540edd0f4671b8110788b295573c345 src/Orchard.Web/Modules/Orchard.Autoroute
5e43c516856ae311e2a4d35bb3c02df59c42e3d4 src/Orchard.Web/Modules/Orchard.Autoroute
c54cb640d6bc14c51b9fb9bd78231bb0facec067 src/Orchard.Web/Modules/Orchard.Forms
2bf79a49eea7006847fc142891d1956882c5e285 src/Orchard.Web/Modules/Orchard.Projections
a1ef39ba4e2d0cd78b3c91d6150e841793acb34b src/Orchard.Web/Modules/Orchard.Routable
f2a3984789ebe5caf2822ccb9e1d2c953add9c35 src/Orchard.Web/Modules/Orchard.Rules
ce578373f907c0a55fd91229a344f0755f290174 src/Orchard.Web/Modules/Orchard.TaskLease
2373765b3f1f4171b09b1458f7dce9e8b83db0a2 src/Orchard.Web/Modules/Orchard.Tokens
1a604b185be41e4e6a72436b4b84dabec4916f80 src/Orchard.Web/Modules/Orchard.Tokens
d8a83a676cb3b9d0266ec83ac6a1e8562821d82b src/orchard.web/modules/Orchard.Fields

View File

@@ -22,14 +22,5 @@ namespace Orchard.Pages {
ContentDefinitionManager.AlterTypeDefinition("Page", cfg => cfg.WithPart("CommonPart", p => p.WithSetting("DateEditorSettings.ShowDateEditor", "true")));
return 2;
}
public int UpdateFrom2() {
// TODO: (PH:Autoroute) Copy routes/titles
ContentDefinitionManager.AlterTypeDefinition("Page", cfg => cfg
.RemovePart("RoutePart")
.WithPart("TitlePart")
.WithPart("AutoroutePart"));
return 3;
}
}
}

View File

@@ -0,0 +1,17 @@
using Orchard.Localization;
using Orchard.Security;
using Orchard.UI.Navigation;
namespace UpgrateTo14 {
public class AdminMenu : INavigationProvider {
public Localizer T { get; set; }
public string MenuName {
get { return "admin"; }
}
public void GetNavigation(NavigationBuilder builder) {
builder.Add(T("Migrate Routes"), "0", item => item.Action("Index", "Admin", new { area = "UpgrateTo14" }).Permission(StandardPermissions.SiteOwner));
}
}
}

View File

@@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using System.Web.Mvc;
using Orchard;
using Orchard.Autoroute.Models;
using Orchard.Autoroute.Services;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Common.Models;
using Orchard.Core.Title.Models;
using Orchard.Data;
using Orchard.Environment.Configuration;
using Orchard.Localization;
using Orchard.Security;
using Orchard.UI.Notify;
using UpgrateTo14.ViewModels;
namespace UpgrateTo14.Controllers {
public class AdminController : Controller {
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly IOrchardServices _orchardServices;
private readonly ISessionFactoryHolder _sessionFactoryHolder;
private readonly ShellSettings _shellSettings;
private readonly IAutorouteService _autorouteService;
public AdminController(
IContentDefinitionManager contentDefinitionManager,
IOrchardServices orchardServices,
ISessionFactoryHolder sessionFactoryHolder,
ShellSettings shellSettings,
IAutorouteService autorouteService) {
_contentDefinitionManager = contentDefinitionManager;
_orchardServices = orchardServices;
_sessionFactoryHolder = sessionFactoryHolder;
_shellSettings = shellSettings;
_autorouteService = autorouteService;
}
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 routeparts
if (contentType.Parts.Any(x => x.PartDefinition.Name == "RoutePart")) {
viewModel.ContentTypes.Add(new ContentTypeEntry {ContentTypeName = contentType.Name});
}
}
if(!viewModel.ContentTypes.Any()) {
_orchardServices.Notifier.Warning(T("There are no content types with RoutePart"));
}
return View(viewModel);
}
[HttpPost, ActionName("Index")]
public ActionResult IndexPOST() {
if (!_orchardServices.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not allowed to migrate routes.")))
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);
var sessionFactory = _sessionFactoryHolder.GetSessionFactory();
var session = sessionFactory.OpenSession();
foreach (var contentType in contentTypesToMigrate) {
// migrating pages
_contentDefinitionManager.AlterTypeDefinition(contentType,
builder => builder
.WithPart("AutoroutePart")
.WithPart("TitlePart"));
var count = 0;
var isContainable = false;
IEnumerable<ContentItem> contents;
do {
contents = _orchardServices.ContentManager.HqlQuery().ForType(contentType).Slice(count, 100);
foreach (dynamic content in contents) {
var autoroutePart = ((ContentItem) content).As<AutoroutePart>();
var titlePart = ((ContentItem) content).As<TitlePart>();
var commonPart = ((ContentItem) content).As<CommonPart>();
if(commonPart != null && commonPart.Container != null) {
isContainable = true;
}
using (new TransactionScope(TransactionScopeOption.Suppress)) {
var command = session.Connection.CreateCommand();
command.CommandText = string.Format("SELECT Title, Path FROM {0} WHERE ContentItemRecord_Id = {1}", GetPrefixedTableName("Routable_RoutePartRecord"), autoroutePart.ContentItem.Id);
var reader = command.ExecuteReader();
reader.Read();
var title = reader.GetString(0);
var path = reader.GetString(1);
reader.Close();
autoroutePart.DisplayAlias = path;
titlePart.Title = title;
}
_autorouteService.PublishAlias(autoroutePart);
count++;
}
_orchardServices.ContentManager.Flush();
// todo: _orchardServices.ContentManager.Clear();
} while (contents.Any());
_contentDefinitionManager.AlterTypeDefinition(contentType, builder => builder.RemovePart("RoutePart"));
var typeDefinition = _contentDefinitionManager.GetTypeDefinition(contentType);
if (isContainable || typeDefinition.Parts.Any(x => x.PartDefinition.Name == "ContainablePart")) {
_autorouteService.CreatePattern(contentType, "Container and Title", "{Content.Container.Path}/{Content.Slug}", "my-container/a-sample-title", true);
}
else {
_autorouteService.CreatePattern(contentType, "Title", "{Content.Slug}", "my-sample-title", true);
}
_orchardServices.Notifier.Information(T("{0} was migrated successfully", contentType));
}
}
return RedirectToAction("Index");
}
private string GetPrefixedTableName(string tableName) {
if (string.IsNullOrWhiteSpace(_shellSettings.DataTablePrefix)) {
return tableName;
}
return _shellSettings.DataTablePrefix + "_" + tableName;
}
}
}

View File

@@ -1,38 +1,33 @@
using System.Transactions;
using Orchard.Alias;
using Orchard.Autoroute.Models;
using Orchard.Autoroute.Services;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Common.Models;
using Orchard.Core.Title.Models;
using Orchard.Data;
using Orchard.Data.Migration;
using Orchard.Data.Migration.Schema;
using Orchard.Environment.Configuration;
namespace UpgrateTo14 {
public class UpdateTo14DataMigration : DataMigrationImpl {
private readonly IContentManager _contentManager;
private readonly IAutorouteService _autorouteService;
private readonly IAliasService _aliasService;
private readonly ISessionFactoryHolder _sessionFactoryHolder;
private readonly ShellSettings _shellSettings;
public UpdateTo14DataMigration(
IContentManager contentManager,
IAutorouteService autorouteService,
IAliasService aliasService,
IAutorouteService autorouteService,
ISessionFactoryHolder sessionFactoryHolder,
ShellSettings shellSettings) {
_contentManager = contentManager;
_autorouteService = autorouteService;
_aliasService = aliasService;
_sessionFactoryHolder = sessionFactoryHolder;
_shellSettings = shellSettings;
}
public int Create() {
return 1;
var sessionFactory = _sessionFactoryHolder.GetSessionFactory();
var session = sessionFactory.OpenSession();

View File

@@ -88,12 +88,17 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="AdminMenu.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Migrations.cs" />
<Compile Include="ViewModels\MigrateViewModel.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers\" />
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup>
<None Include="Views\Admin\Index.cshtml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -0,0 +1,12 @@
using System.Collections.Generic;
namespace UpgrateTo14.ViewModels {
public class MigrateViewModel {
public IList<ContentTypeEntry> ContentTypes { get; set; }
}
public class ContentTypeEntry {
public string ContentTypeName { get; set; }
public bool IsChecked { get; set; }
}
}

View File

@@ -0,0 +1,26 @@
@using Orchard.Utility.Extensions
@model UpgrateTo14.ViewModels.MigrateViewModel
@{ Layout.Title = T("Migrate Routes").ToString(); }
@using (Html.BeginFormAntiForgeryPost()) {
Html.ValidationSummary();
<fieldset>
<legend>@T("Choose the types to migrate:")</legend>
<span class="hint">@T("The migration process will move the Route and Title properties by adding an Autoroute and a Title part to all the content items for the selected content types.")</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>
}