diff --git a/src/UIOMaticAddons.Export/App_Plugins/UIOMaticAddons/export.controller.js b/src/UIOMaticAddons.Export/App_Plugins/UIOMaticAddons/export.controller.js index 71281a3..6fb3ee0 100644 --- a/src/UIOMaticAddons.Export/App_Plugins/UIOMaticAddons/export.controller.js +++ b/src/UIOMaticAddons.Export/App_Plugins/UIOMaticAddons/export.controller.js @@ -1,8 +1,8 @@ angular.module("umbraco").controller("uioMaticAddons.ExportController", - function ($scope, uioMaticExportResource) { - $scope.loading = true; - uioMaticExportResource.getExport($scope.dialogData.typeAlias).then(function (response) { - $scope.file = response.data; - $scope.loading = false; - }); -}); \ No newline at end of file + function ($scope, uioMaticExportResource) { + $scope.loading = true; + uioMaticExportResource.getExport($scope.model.dialogData.typeAlias).then(function (response) { + $scope.file = response.data; + $scope.loading = false; + }); + }); \ No newline at end of file diff --git a/src/UIOMaticAddons.Export/App_Plugins/UIOMaticAddons/export.html b/src/UIOMaticAddons.Export/App_Plugins/UIOMaticAddons/export.html index 2bbf0fd..36613ea 100644 --- a/src/UIOMaticAddons.Export/App_Plugins/UIOMaticAddons/export.html +++ b/src/UIOMaticAddons.Export/App_Plugins/UIOMaticAddons/export.html @@ -2,18 +2,18 @@
diff --git a/src/UIOMaticAddons.Export/Controllers/ExportController.cs b/src/UIOMaticAddons.Export/Controllers/ExportController.cs index 4b01c02..d95c3d2 100644 --- a/src/UIOMaticAddons.Export/Controllers/ExportController.cs +++ b/src/UIOMaticAddons.Export/Controllers/ExportController.cs @@ -4,66 +4,78 @@ using System.Dynamic; using System.IO; using System.Reflection; -using Umbraco.Core.IO; -using Umbraco.Web.Editors; using System.Text; +using System.Formats.Asn1; +using Umbraco.Cms.Core.IO; +using Umbraco.Cms.Web.BackOffice.Controllers; +using System.Globalization; +using Umbraco.Cms.Core.Cache; +using UIOMatic; +using UIOMatic.Interfaces; +using UIOMatic.Services; +using Umbraco.Cms.Core.Hosting; +using System.Linq; -namespace UIOMaticAddons.Export.Controllers -{ - public class ExportController: UmbracoAuthorizedJsonController - { - public object GetExport(string typeAlias) - { - var guid = Guid.NewGuid(); +namespace UIOMaticAddons.Export.Controllers { + public class ExportController : UmbracoAuthorizedJsonController { + private readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment; + private readonly IHostingEnvironment _umbHostingEnvironment; + private readonly AppCaches _appCaches; + private readonly IUIOMaticHelper _uIOMaticHelper; + private readonly UIOMaticObjectService _uIOMaticObjectService; - using (var textWriter = new StreamWriter( - File.Open(IOHelper.MapPath(@"~\App_Plugins\UIOMaticAddons\Exports\" + guid + ".csv"), FileMode.CreateNew), - Encoding.GetEncoding("iso-8859-1"))) - { - textWriter.NewLine = "\n"; - using (var csv = new CsvWriter(textWriter)) - { - var os = new UIOMatic.Services.NPocoObjectService(); + public ExportController(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment, Umbraco.Cms.Core.Hosting.IHostingEnvironment umbHostingEnvironment, AppCaches appCaches, IUIOMaticHelper uIOMaticHelper, UIOMaticObjectService uIOMaticObjectService) { + _hostingEnvironment = hostingEnvironment; + _umbHostingEnvironment = umbHostingEnvironment; + _appCaches = appCaches; + _uIOMaticHelper = uIOMaticHelper; + _uIOMaticObjectService = uIOMaticObjectService; + } - var data = os.GetAll(UIOMatic.Helper.GetUIOMaticTypeByAlias(typeAlias)); + public object GetExport(string typeAlias) { + var guid = Guid.NewGuid(); - csv.Configuration.Delimiter = ";"; - //csv.Configuration.QuoteAllFields = true; - csv.Configuration.HasHeaderRecord = true; + //Create the path if it doesn't exist + var path = Path.Combine(_hostingEnvironment.WebRootPath, "App_Plugins/UIOMaticAddons/Exports/"); + System.IO.Directory.CreateDirectory(path); + using (var textWriter = new StreamWriter(Path.Combine(path, guid + ".csv"))) { + textWriter.NewLine = "\n"; + using (var csv = new CsvWriter(textWriter, CultureInfo.InvariantCulture)) { + var os = new UIOMatic.Services.NPocoObjectService(_appCaches, _umbHostingEnvironment, _uIOMaticHelper, (UIOMaticObjectService)_uIOMaticObjectService); - //csv.WriteRecords(data); + var data = os.GetAll(_uIOMaticHelper.GetUIOMaticTypeByAlias(typeAlias)); - - + - foreach (var item in data) - { - Type myObjOriginalType = item.GetType(); - PropertyInfo[] myProps = myObjOriginalType.GetProperties(); + csv.WriteHeader(data.FirstOrDefault().GetType()); //Write the header record + csv.NextRecord();//WriteHeader will not advance you to the next row, so this line is needed - //var x = new ExpandoObject() as IDictionary; - + foreach (var item in data) + { + Type myObjOriginalType = item.GetType(); + PropertyInfo[] myProps = myObjOriginalType.GetProperties(); - foreach (var prop in myProps) - { - //x.Add(prop.Name, prop.GetValue(item) == null ? string.Empty: prop.GetValue(item).ToString().Replace(System.Environment.NewLine," ")); - csv.WriteField(prop.GetValue(item) == null ? string.Empty : prop.GetValue(item).ToString().Replace(System.Environment.NewLine, " ")); - } + foreach (var prop in myProps) + { + //x.Add(prop.Name, prop.GetValue(item) == null ? string.Empty: prop.GetValue(item).ToString().Replace(System.Environment.NewLine," ")); + csv.WriteField(prop.GetValue(item) == null ? string.Empty : prop.GetValue(item).ToString().Replace(System.Environment.NewLine, " ")); + } - //foreach (var v in (IDictionary)item) - //{ - // csv.WriteField(v.Value); - //} - csv.NextRecord(); - //csv.WriteRecord(x); - } - - } - } - - return new { data = "../App_Plugins/UIOMaticAddons/Exports/" + guid.ToString() + ".csv" }; - } - } -} \ No newline at end of file + + //foreach (var v in (IDictionary)item) + //{ + // csv.WriteField(v.Value); + //} + csv.NextRecord(); + //csv.WriteRecord(x); + } + + } + } + + return new { data = "../App_Plugins/UIOMaticAddons/Exports/" + guid.ToString() + ".csv" }; + } + } +} diff --git a/src/UIOMaticAddons.Export/ServerVariableParserEvent.cs b/src/UIOMaticAddons.Export/ServerVariableParserEvent.cs index a359018..0231480 100644 --- a/src/UIOMaticAddons.Export/ServerVariableParserEvent.cs +++ b/src/UIOMaticAddons.Export/ServerVariableParserEvent.cs @@ -2,63 +2,62 @@ using System.Collections.Generic; using System.Linq; using System.Web; -using System.Web.Mvc; -using System.Web.Routing; +using Azure; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.Mvc.Routing; using UIOMaticAddons.Export.Controllers; using Umbraco; -using Umbraco.Core; -using Umbraco.Core.Composing; -using Umbraco.Core.Persistence; +using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Composing; +using Umbraco.Cms.Core.DependencyInjection; +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Notifications; +using Umbraco.Cms.Infrastructure.WebAssets; +using Umbraco.Extensions; using Umbraco.Web; -using Umbraco.Web.JavaScript; -namespace UIOMaticAddons.Export -{ - - public class ServerVariableParserEventComposer : IUserComposer - { - public void Compose(Composition composition) - { - composition.Components().Append(); - } - } +namespace UIOMaticAddons.Export { - public class ServerVariableParserEvent : IComponent - { - public ServerVariableParserEvent() - { + public class ServerVariableParserEventComposer : IComposer { + public void Compose(IUmbracoBuilder builder) { + + builder.AddNotificationHandler(); - } + } + } - public void Initialize() - { - ServerVariablesParser.Parsing += this.ServerVariablesParser_Parsing; - } + public class ServerVariableParserEvent : INotificationHandler { + private readonly UmbracoApiControllerTypeCollection _umbracoApiControllerTypeCollection; + private readonly IActionContextAccessor _actionContextAccessor; + private readonly IUrlHelperFactory _urlFactory; - void ServerVariablesParser_Parsing(object sender, Dictionary e) - { - if (HttpContext.Current == null) return; - var urlHelper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData())); + public ServerVariableParserEvent(UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, IActionContextAccessor actionContext, IUrlHelperFactory urlFactory) { + _umbracoApiControllerTypeCollection = umbracoApiControllerTypeCollection; + _actionContextAccessor = actionContext; + _urlFactory = urlFactory; + } - var mainDictionary = new Dictionary - { - { - "ecBaseUrl", - urlHelper.GetUmbracoApiServiceBaseUrl(controller => controller.ToString()) - } - }; + public void Terminate() { } - if (!e.Keys.Contains("uioMaticAddons")) - { - e.Add("uioMaticAddons", mainDictionary); - } - } + public void Handle(ServerVariablesParsingNotification notification) + var urlHelper = _urlFactory.GetUrlHelper(_actionContextAccessor.ActionContext); - public void Terminate() - { } - } + var mainDictionary = new Dictionary + { + { + "ecBaseUrl", + urlHelper.GetUmbracoApiServiceBaseUrl(_umbracoApiControllerTypeCollection, controller => controller.ToString()) + } + }; - - } + if (!notification.ServerVariables.ContainsKey("uioMaticAddons")) { + notification.ServerVariables.Add("uioMaticAddons", mainDictionary); + } + } + } + + +}