Skip to content

Commit ff25ed2

Browse files
author
Warren Buckley
committed
New Workflow Setting View (Based off Forms built in FieldMapper) to list/fetch against 200 or so properties in Hubspot
1 parent c6cb166 commit ff25ed2

File tree

7 files changed

+158
-8
lines changed

7 files changed

+158
-8
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function hubspotResource($http, umbRequestHelper) {
2+
3+
return {
4+
fetch: function (id) {
5+
return umbRequestHelper.resourcePromise(
6+
$http.get(
7+
umbRequestHelper.getApiUrl(
8+
"packageInstallApiBaseUrl",
9+
"Fetch",
10+
[{ packageGuid: id }])),
11+
'Failed to download package with guid ' + id);
12+
},
13+
getAllProperties: function (apiKey) {
14+
return umbRequestHelper.resourcePromise(
15+
$http.get(
16+
umbRequestHelper.getApiUrl(
17+
"umbracoFormsExtensionsHubspotBaseUrl",
18+
"GetAllProperties",
19+
[{ apiKey: apiKey }])),
20+
'Failed to get Hubspot Properties');
21+
},
22+
};
23+
}
24+
25+
angular.module('umbraco.resources').factory('hubspotResource', hubspotResource);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<div ng-controller="UmbracoFormsExtensions.Hubspot.Fields">
2+
3+
<pre>{{ setting | json }}</pre>
4+
<pre>{{ mappings | json }}</pre>
5+
<!--<pre>{{ fields | json }}</pre>-->
6+
<!--<pre>{{ hubspotFields | json }}</pre>-->
7+
8+
<div class="umb-forms-mappings" ng-if="mappings.length > 0">
9+
10+
<div class="umb-forms-mapping-header">
11+
<div class="umb-forms-mapping-field -no-margin-left">Form Field</div>
12+
<div class="umb-forms-mapping-field">Hubspot Field</div>
13+
<div class="umb-forms-mapping-remove -no-margin-right"></div>
14+
</div>
15+
16+
<div class="umb-forms-mapping" ng-repeat="mapping in mappings">
17+
18+
<div class="umb-forms-mapping-field">
19+
<select class="-full-width"
20+
ng-options="field.id as field.value for field in fields"
21+
ng-model="mapping.formField"
22+
ng-change="stringifyValue()">
23+
<option value="">Map form field</option>
24+
</select>
25+
</div>
26+
27+
<div class="umb-forms-mapping-field">
28+
<select class="-full-width"
29+
ng-options="field.name as field.label for field in hubspotFields"
30+
ng-model="mapping.hubspotField"
31+
ng-change="stringifyValue()">
32+
<option value="">Map hubspot field</option>
33+
</select>
34+
</div>
35+
36+
<div class="umb-forms-mapping-remove -no-margin-right">
37+
<a href="" ng-click="deleteMapping($index)"><i class="icon-trash"></i></a>
38+
</div>
39+
40+
</div>
41+
</div>
42+
43+
<umb-button type="button" action="addMapping()" label="Add mapping"></umb-button>
44+
45+
</div>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
angular.module("umbraco").controller("UmbracoFormsExtensions.Hubspot.Fields", function ($scope, $routeParams, pickerResource, hubspotResource) {
2+
3+
function init() {
4+
if (!$scope.setting.value) {
5+
$scope.mappings = [];
6+
} else {
7+
$scope.mappings = JSON.parse($scope.setting.value);
8+
}
9+
10+
var formId = $routeParams.id;
11+
12+
if (formId === -1 && $scope.model && $scope.model.fields) {
13+
14+
} else {
15+
16+
// Available Form Fields
17+
pickerResource.getAllFields($routeParams.id).then(function (response) {
18+
$scope.fields = response.data;
19+
});
20+
21+
// TODO: Remove hard coded API key & remember to revoke it later on
22+
// Need to get API key from other field
23+
// May need to do parent.parent scope traversal - YUK :S
24+
hubspotResource.getAllProperties('6a488b25-b7e7-489d-ad45-2da52a878ff9').then(function (response) {
25+
$scope.hubspotFields = response;
26+
});
27+
}
28+
}
29+
30+
$scope.addMapping = function () {
31+
// Add new empty object into array
32+
$scope.mappings.push({
33+
formField: "",
34+
hubspotField: ""
35+
});
36+
};
37+
38+
$scope.deleteMapping = function (index) {
39+
$scope.mappings.splice(index, 1);
40+
$scope.setting.value = JSON.stringify($scope.mappings);
41+
};
42+
43+
$scope.stringifyValue = function () {
44+
$scope.setting.value = JSON.stringify($scope.mappings);
45+
};
46+
47+
init();
48+
49+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"css": [ "" ],
3+
"javascript": [
4+
"~/App_Plugins/UmbracoFormsExtensions/Hubspot/hubspotfields.js",
5+
"~/App_Plugins/UmbracoFormsExtensions/Hubspot/hubspot.resource.js"
6+
]
7+
}

src/Umbraco.Forms.Extensions.Crm.Hubspot/HubspotComponent.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,20 @@ public void Initialize()
2020

2121
private void ServerVariablesParser_Parsing(object sender, Dictionary<string, object> e)
2222
{
23+
if (!e.ContainsKey("umbracoUrls"))
24+
throw new ArgumentException("Missing umbracoUrls.");
25+
26+
var umbracoUrlsObject = e["umbracoUrls"];
27+
if (umbracoUrlsObject == null)
28+
throw new ArgumentException("Null umbracoUrls");
29+
30+
if (!(umbracoUrlsObject is Dictionary<string, object> umbracoUrls))
31+
throw new ArgumentException("Invalid umbracoUrls");
32+
2333
if (HttpContext.Current == null) throw new InvalidOperationException("HttpContext is null");
2434
var urlHelper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData()));
25-
e.Add("Umbraco.Forms.Extensions.HubSpot", new Dictionary<string, object>
26-
{
27-
{"GetPropertiesBaseUrl", urlHelper.GetUmbracoApiServiceBaseUrl<HubspotController>(controller => controller.GetAllProperties(null))},
28-
});
35+
36+
umbracoUrls["umbracoFormsExtensionsHubspotBaseUrl"] = urlHelper.GetUmbracoApiServiceBaseUrl<HubspotController>(controller => controller.GetAllProperties(null));
2937
}
3038

3139
public void Terminate()

src/Umbraco.Forms.Extensions.Crm.Hubspot/HubspotController.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using System;
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
24
using System.Collections.Generic;
35
using System.Linq;
46
using System.Net.Http;
7+
using System.Net.Http.Json;
58
using Umbraco.Core.Composing;
69
using Umbraco.Core.Logging;
710
using Umbraco.Web.Editors;
@@ -35,17 +38,27 @@ public List<Property> GetAllProperties(string apiKey)
3538

3639
// Map Properties back to our simplier object
3740
// Don't need all the fields in the response
38-
39-
41+
var rawResult = propertiesResponse.Content.ReadAsStringAsync().Result;
42+
var json = JsonConvert.DeserializeObject<PropertyResult>(rawResult);
43+
properties.AddRange(json.Results);
4044
return properties;
4145
}
4246

47+
public class PropertyResult
48+
{
49+
[JsonProperty(PropertyName = "results")]
50+
public List<Property> Results { get; set; }
51+
}
52+
4353
public class Property
4454
{
55+
[JsonProperty(PropertyName = "name")]
4556
public string Name { get; set; }
4657

58+
[JsonProperty(PropertyName = "label")]
4759
public string Label { get; set; }
4860

61+
[JsonProperty(PropertyName = "description")]
4962
public string Description { get; set; }
5063
}
5164
}

src/Umbraco.Forms.Extensions.Crm.Hubspot/HubspotWorkflow.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public HubspotWorkflow()
2626
[Setting("Hubspot API Key", Description = "Enter the API Key from your HubSpot account", View = "TextField")]
2727
public string HubspotApiKey { get; set; }
2828

29-
private Uri HubspotApiUrl
29+
[Setting("Field Mappings", Description = "Map Umbraco Form fields to HubSpot contact fields", View = "~/App_Plugins/UmbracoFormsExtensions/Hubspot/hubspotfields.html")]
30+
public string FieldMappings { get; set; }
31+
32+
private Uri HubspotContactApiUrl
3033
{
3134
get
3235
{

0 commit comments

Comments
 (0)