diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/Controllers/AdminController.cs index 2dc619707..20d678544 100644 --- a/src/Orchard.Web/Modules/Orchard.ImportExport/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Controllers/AdminController.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; +using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using Orchard.ContentManagement; @@ -8,25 +6,27 @@ using Orchard.ImportExport.Services; using Orchard.ImportExport.ViewModels; using Orchard.Localization; using Orchard.Recipes.Services; -using Orchard.UI.Notify; namespace Orchard.ImportExport.Controllers { public class AdminController : Controller, IUpdateModel { private readonly IImportExportService _importExportService; private readonly IRecipeResultAccessor _recipeResultAccessor; private readonly IEnumerable _exportActions; + private readonly IEnumerable _importActions; private readonly IRecipeParser _recipeParser; public AdminController( IOrchardServices services, IImportExportService importExportService, IRecipeResultAccessor recipeResultAccessor, - IEnumerable exportActions, + IEnumerable exportActions, + IEnumerable importActions, IRecipeParser recipeParser) { _importExportService = importExportService; _recipeResultAccessor = recipeResultAccessor; _exportActions = exportActions; + _importActions = importActions; _recipeParser = recipeParser; Services = services; T = NullLocalizer.Instance; @@ -36,7 +36,13 @@ namespace Orchard.ImportExport.Controllers { public Localizer T { get; set; } public ActionResult Import() { - var viewModel = new ImportViewModel(); + var actions = _importActions.OrderBy(x => x.Priority).Select(x => new ImportActionViewModel { + Editor = x.BuildEditor(Services.New) + }).Where(x => x != null).ToList(); + + var viewModel = new ImportViewModel { + Actions = actions + }; return View(viewModel); } @@ -46,17 +52,23 @@ namespace Orchard.ImportExport.Controllers { if (!Services.Authorizer.Authorize(Permissions.Import, T("Not allowed to import."))) return new HttpUnauthorizedResult(); - if (String.IsNullOrEmpty(Request.Files["RecipeFile"].FileName)) { - ModelState.AddModelError("RecipeFile", T("Please choose a recipe file to import.").Text); - Services.Notifier.Error(T("Please choose a recipe file to import.")); + var actions = _importActions.OrderBy(x => x.Priority).ToList(); + var viewModel = new ImportViewModel { + Actions = actions.Select(x => new ImportActionViewModel { + Editor = x.UpdateEditor(Services.New, this) + }).Where(x => x != null).ToList() + }; + + if (!ModelState.IsValid) { + return View(viewModel); } - if (ModelState.IsValid) { - var executionId = _importExportService.Import(new StreamReader(Request.Files["RecipeFile"].InputStream).ReadToEnd()); - return RedirectToAction("ImportResult", new { executionId = executionId }); + var context = new ImportActionContext { ActionResult = RedirectToAction("Import") }; + foreach(var action in actions) { + action.Execute(context); } - - return View(new ImportViewModel()); + + return context.ActionResult; } public ActionResult ImportResult(string executionId) { diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Orchard.ImportExport.csproj b/src/Orchard.Web/Modules/Orchard.ImportExport/Orchard.ImportExport.csproj index 810442b6c..228f1aab2 100644 --- a/src/Orchard.Web/Modules/Orchard.ImportExport/Orchard.ImportExport.csproj +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Orchard.ImportExport.csproj @@ -72,10 +72,14 @@ + + + + @@ -83,6 +87,7 @@ + @@ -130,7 +135,7 @@ - + diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Providers/ImportActions/UploadRecipe.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/Providers/ImportActions/UploadRecipe.cs new file mode 100644 index 000000000..b77336a80 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Providers/ImportActions/UploadRecipe.cs @@ -0,0 +1,49 @@ +using System; +using System.IO; +using System.Web; +using System.Web.Mvc; +using System.Web.Routing; +using Orchard.ContentManagement; +using Orchard.ImportExport.Services; +using Orchard.UI.Notify; + +namespace Orchard.ImportExport.Providers.ImportActions { + public class UploadRecipe : ImportAction { + private readonly IOrchardServices _orchardServices; + private readonly IImportExportService _importExportService; + + public UploadRecipe(IOrchardServices orchardServices, IImportExportService importExportService) { + _orchardServices = orchardServices; + _importExportService = importExportService; + } + + public override string Name { get { return "UploadRecipe"; } } + + public HttpPostedFileBase File { get; set; } + + public override dynamic BuildEditor(dynamic shapeFactory) { + return UpdateEditor(shapeFactory, null); + } + + public override dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater) { + + if (updater != null) { + var request = _orchardServices.WorkContext.HttpContext.Request; + var file = request.Files["RecipeFile"]; + if (String.IsNullOrEmpty(file.FileName)) { + updater.AddModelError("RecipeFile", T("Please choose a recipe file to import.")); + _orchardServices.Notifier.Error(T("Please choose a recipe file to import.")); + } + else + File = file; + } + + return shapeFactory.EditorTemplate(TemplateName: "ImportActions/UploadRecipe", Prefix: Prefix); + } + + public override void Execute(ImportActionContext context) { + var executionId = _importExportService.Import(new StreamReader(File.InputStream).ReadToEnd()); + context.ActionResult = new RedirectToRouteResult(new RouteValueDictionary(new { action = "ImportResult", controller = "Admin", area = "Orchard.ImportExport", executionId = executionId })); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Services/ExportAction.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/Services/ExportAction.cs index a4fe2b27e..254514113 100644 --- a/src/Orchard.Web/Modules/Orchard.ImportExport/Services/ExportAction.cs +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Services/ExportAction.cs @@ -1,7 +1,7 @@ using Orchard.ContentManagement; namespace Orchard.ImportExport.Services { - public abstract class ExportAction : IExportAction { + public abstract class ExportAction : Component, IExportAction { public virtual int Priority { get { return 0; } } public abstract string Name { get; } @@ -17,6 +17,6 @@ namespace Orchard.ImportExport.Services { return null; } - public abstract void Execute(ExportActionContext exportActionContext); + public abstract void Execute(ExportActionContext context); } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Services/IExportAction.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/Services/IExportAction.cs index 6bb6415fe..0aa1b26c6 100644 --- a/src/Orchard.Web/Modules/Orchard.ImportExport/Services/IExportAction.cs +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Services/IExportAction.cs @@ -7,6 +7,6 @@ namespace Orchard.ImportExport.Services { dynamic BuildEditor(dynamic shapeFactory); dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater); - void Execute(ExportActionContext exportActionContext); + void Execute(ExportActionContext context); } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Services/IImportAction.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/Services/IImportAction.cs new file mode 100644 index 000000000..74e8313e0 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Services/IImportAction.cs @@ -0,0 +1,12 @@ +using Orchard.ContentManagement; + +namespace Orchard.ImportExport.Services { + public interface IImportAction : IDependency { + int Priority { get; } + string Name { get; } + + dynamic BuildEditor(dynamic shapeFactory); + dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater); + void Execute(ImportActionContext context); + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Services/ImportAction.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/Services/ImportAction.cs new file mode 100644 index 000000000..30d9a00c8 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Services/ImportAction.cs @@ -0,0 +1,22 @@ +using Orchard.ContentManagement; + +namespace Orchard.ImportExport.Services { + public abstract class ImportAction : Component, IImportAction { + public virtual int Priority { get { return 0; } } + public abstract string Name { get; } + + protected virtual string Prefix { + get { return GetType().Name; } + } + + public virtual dynamic BuildEditor(dynamic shapeFactory) { + return null; + } + + public virtual dynamic UpdateEditor(dynamic shapeFactory, IUpdateModel updater) { + return null; + } + + public abstract void Execute(ImportActionContext context); + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Services/ImportActionContext.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/Services/ImportActionContext.cs new file mode 100644 index 000000000..429857778 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Services/ImportActionContext.cs @@ -0,0 +1,7 @@ +using System.Web.Mvc; + +namespace Orchard.ImportExport.Services { + public class ImportActionContext { + public ActionResult ActionResult { get; set; } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ExportViewModel.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ExportViewModel.cs index 6a1d9f8c3..9d485703d 100644 --- a/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ExportViewModel.cs +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ExportViewModel.cs @@ -2,6 +2,10 @@ namespace Orchard.ImportExport.ViewModels { public class ExportViewModel { + public ExportViewModel() { + Actions = new List(); + } + public IList Actions { get; set; } } } diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ImportActionViewModel.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ImportActionViewModel.cs new file mode 100644 index 000000000..9a4dcd400 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ImportActionViewModel.cs @@ -0,0 +1,5 @@ +namespace Orchard.ImportExport.ViewModels { + public class ImportActionViewModel { + public dynamic Editor { get; set; } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ImportViewModel.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ImportViewModel.cs index ff350a79b..94683e39e 100644 --- a/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ImportViewModel.cs +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/ViewModels/ImportViewModel.cs @@ -1,4 +1,11 @@ -namespace Orchard.ImportExport.ViewModels { +using System.Collections.Generic; + +namespace Orchard.ImportExport.ViewModels { public class ImportViewModel { + public ImportViewModel() { + Actions = new List(); + } + + public IList Actions { get; set; } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Views/EditorTemplates/ImportActions/UploadRecipe.cshtml b/src/Orchard.Web/Modules/Orchard.ImportExport/Views/EditorTemplates/ImportActions/UploadRecipe.cshtml new file mode 100644 index 000000000..260d7d5c6 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Views/EditorTemplates/ImportActions/UploadRecipe.cshtml @@ -0,0 +1,7 @@ +

+ @T("Choose a recipe file to import. Please consider {0} or backing up your data first.", @Html.Link(T("exporting").Text, Url.Action("Export", "Admin", new { area = "Orchard.ImportExport" }))) +

+
+ + +
\ No newline at end of file