Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
});
});
function ($scope, uioMaticExportResource) {
$scope.loading = true;
uioMaticExportResource.getExport($scope.model.dialogData.typeAlias).then(function (response) {
$scope.file = response.data;
$scope.loading = false;
});
});
10 changes: 5 additions & 5 deletions src/UIOMaticAddons.Export/App_Plugins/UIOMaticAddons/export.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

<div class="umb-panel umb-modal">
<div class="umb-panel-body no-header with-footer">

<div class="umb-pane " style="">
<h5 class="umb-pane-title">Export your data</h5>
<div class="control-group umb-control-group" style="">

<div class="umb-el-wrap ">

<div class="controls controls-row">

<p ng-show="loading">Generating Export...</p>
<a href="{{file}}" ng-hide="loading" target="_blank">Download export here</a>

</div>
</div>
</div>
Expand All @@ -24,13 +24,13 @@ <h5 class="umb-pane-title">Export your data</h5>

<div class="umb-panel-footer">
<div class="btn-toolbar umb-btn-toolbar">
<a id="cancelButton" ng-click="close();" class="btn btn-link">
<a id="cancelButton" ng-click="model.close();" class="btn btn-link">
Cancel
</a>
</div>
</div>

</div>

</div>

112 changes: 62 additions & 50 deletions src/UIOMaticAddons.Export/Controllers/ExportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Object>;

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<string, object>)item)
//{
// csv.WriteField(v.Value);
//}
csv.NextRecord();
//csv.WriteRecord(x);
}

}
}

return new { data = "../App_Plugins/UIOMaticAddons/Exports/" + guid.ToString() + ".csv" };
}
}
}

//foreach (var v in (IDictionary<string, object>)item)
//{
// csv.WriteField(v.Value);
//}
csv.NextRecord();
//csv.WriteRecord(x);
}

}
}

return new { data = "../App_Plugins/UIOMaticAddons/Exports/" + guid.ToString() + ".csv" };
}
}
}
91 changes: 45 additions & 46 deletions src/UIOMaticAddons.Export/ServerVariableParserEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerVariableParserEvent>();
}
}
namespace UIOMaticAddons.Export {

public class ServerVariableParserEvent : IComponent
{
public ServerVariableParserEvent()
{
public class ServerVariableParserEventComposer : IComposer {
public void Compose(IUmbracoBuilder builder) {
builder.AddNotificationHandler<ServerVariablesParsingNotification, ServerVariableParserEvent>();

}
}
}

public void Initialize()
{
ServerVariablesParser.Parsing += this.ServerVariablesParser_Parsing;
}
public class ServerVariableParserEvent : INotificationHandler<ServerVariablesParsingNotification> {
private readonly UmbracoApiControllerTypeCollection _umbracoApiControllerTypeCollection;
private readonly IActionContextAccessor _actionContextAccessor;
private readonly IUrlHelperFactory _urlFactory;

void ServerVariablesParser_Parsing(object sender, Dictionary<string, object> 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<string, object>
{
{
"ecBaseUrl",
urlHelper.GetUmbracoApiServiceBaseUrl<ExportController>(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<string, object>
{
{
"ecBaseUrl",
urlHelper.GetUmbracoApiServiceBaseUrl<ExportController>(_umbracoApiControllerTypeCollection, controller => controller.ToString())
}
};


}
if (!notification.ServerVariables.ContainsKey("uioMaticAddons")) {
notification.ServerVariables.Add("uioMaticAddons", mainDictionary);
}
}
}


}