Skip to content

Commit 12de2ab

Browse files
committed
V8 new form submitted handler
1 parent 7df8991 commit 12de2ab

File tree

5 files changed

+176
-11
lines changed

5 files changed

+176
-11
lines changed

src/Umbraco.Cms.Integrations.Automation.Zapier/Components/NewContentPublishedNotification.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
#if NETCOREAPP
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Threading.Tasks;
5-
62
using Microsoft.Extensions.Logging;
73

84
using Umbraco.Cms.Core.Events;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#if NETFRAMEWORK
2+
using System;
3+
using System.Collections;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Web;
7+
using Umbraco.Cms.Integrations.Automation.Zapier.Helpers;
8+
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
9+
using Umbraco.Core;
10+
using Umbraco.Core.Composing;
11+
using Umbraco.Core.Logging;
12+
using Umbraco.Core.Models.PublishedContent;
13+
using Umbraco.Web;
14+
15+
namespace Umbraco.Cms.Integrations.Automation.Zapier.Components
16+
{
17+
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
18+
public class NewFormSubmittedComposer : ComponentComposer<NewFormSubmittedComponent>
19+
{ }
20+
21+
public class NewFormSubmittedComponent : IComponent
22+
{
23+
private readonly ZapierFormSubscriptionHookService _zapierFormSubscriptionHookService;
24+
25+
private readonly ZapierFormService _zapierFormService;
26+
27+
private readonly ZapierService _zapierService;
28+
29+
private readonly ILogger _logger;
30+
31+
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
32+
33+
public NewFormSubmittedComponent(ZapierFormSubscriptionHookService zapierFormSubscriptionHookService,
34+
ZapierFormService zapierFormService,
35+
ZapierService zapierService,
36+
ILogger logger,
37+
IUmbracoContextAccessor umbracoContextAccessor)
38+
{
39+
_zapierFormSubscriptionHookService = zapierFormSubscriptionHookService;
40+
41+
_zapierFormService = zapierFormService;
42+
43+
_zapierService = zapierService;
44+
45+
_logger = logger;
46+
47+
_umbracoContextAccessor = umbracoContextAccessor;
48+
}
49+
50+
public void Initialize()
51+
{
52+
if (FormsHelper.IsFormsExtensionInstalled && _zapierFormService.TryResolveRecordStorage(out var recordStorage))
53+
{
54+
_zapierFormService.AddEventHandler(recordStorage, RecordInserting);
55+
}
56+
}
57+
58+
public void Terminate()
59+
{
60+
}
61+
62+
private void RecordInserting(object sender, object args)
63+
{
64+
var record = args.GetType().GetProperty("Record");
65+
var form = args.GetType().GetProperty("Form");
66+
67+
if (form == null) throw new ArgumentNullException("Invalid form reference");
68+
69+
var formObjValue = form.GetValue(args);
70+
var recordObjValue = record.GetValue(args);
71+
72+
var formId = formObjValue.GetType().GetProperty("Id").GetValue(formObjValue).ToString();
73+
var formName = formObjValue.GetType().GetProperty("Name").GetValue(formObjValue).ToString();
74+
var umbracoPageId = recordObjValue.GetType().GetProperty("UmbracoPageId").GetValue(recordObjValue).ToString();
75+
76+
var triggerHelper = new TriggerHelper(_zapierService);
77+
78+
if (_zapierFormSubscriptionHookService.TryGetByName(formName, out var zapFormConfigList))
79+
{
80+
UmbracoContext umbracoContext = _umbracoContextAccessor.UmbracoContext;
81+
var pageUrl = umbracoContext.UrlProvider.GetUrl(int.Parse(umbracoPageId), UrlMode.Absolute);
82+
83+
foreach (var zapFormConfig in zapFormConfigList)
84+
{
85+
86+
var result = triggerHelper.FormExecute(zapFormConfig.HookUrl, formId, formName,
87+
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority));
88+
89+
if(!string.IsNullOrEmpty(result))
90+
_logger.Error<NewFormSubmittedComponent>(result);
91+
}
92+
}
93+
}
94+
}
95+
}
96+
97+
#endif

src/Umbraco.Cms.Integrations.Automation.Zapier/Constants.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,22 @@ public static class Configuration
2727

2828
public static class Content
2929
{
30-
public const string Id = "Id";
30+
public const string Id = "id";
3131

32-
public const string Name = "Name";
32+
public const string Name = "name";
3333

34-
public const string PublishDate = "PublishDate";
34+
public const string PublishDate = "publishDate";
35+
}
36+
37+
public static class Form
38+
{
39+
public const string Id = "formId";
40+
41+
public const string Name = "formName";
42+
43+
public const string SubmissionDate = "submissionDate";
44+
45+
public const string PageUrl = "pageUrl";
3546
}
3647
}
3748
}

src/Umbraco.Cms.Integrations.Automation.Zapier/Helpers/TriggerHelper.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
4-
4+
using Umbraco.Cms.Integrations.Automation.Zapier.Models.Dtos;
55
using Umbraco.Cms.Integrations.Automation.Zapier.Services;
66

77
namespace Umbraco.Cms.Integrations.Automation.Zapier.Helpers
@@ -29,5 +29,21 @@ public string Execute(string hookUrl, string contentId, string contentName)
2929

3030
return t.Result;
3131
}
32+
33+
public string FormExecute(string hookUrl, string formId, string formName, string pageUrl)
34+
{
35+
var content = new Dictionary<string, string>
36+
{
37+
{Constants.Form.Id, formId},
38+
{Constants.Form.Name, formName},
39+
{Constants.Form.SubmissionDate, DateTime.UtcNow.ToString()},
40+
{Constants.Form.PageUrl, pageUrl}
41+
};
42+
43+
var t = Task.Run(
44+
async () => await _zapierService.TriggerAsync(hookUrl, content));
45+
46+
return t.Result;
47+
}
3248
}
3349
}

src/Umbraco.Cms.Integrations.Automation.Zapier/Services/ZapierFormService.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
4+
using System.Reflection;
55
using Umbraco.Cms.Integrations.Automation.Zapier.Helpers;
66

77
#if NETCOREAPP
@@ -28,15 +28,16 @@ public ZapierFormService(IServiceProvider serviceProvider)
2828
private Type FormStorageType =>
2929
Type.GetType("Umbraco.Forms.Core.Data.Storage.IFormStorage, Umbraco.Forms.Core");
3030

31+
private Type RecordStorageType =>
32+
Type.GetType("Umbraco.Forms.Core.Data.Storage.IRecordStorage, Umbraco.Forms.Core");
33+
3134
public bool TryResolveFormService(out object formService)
3235
{
3336
#if NETCOREAPP
3437
formService = _serviceProvider.GetService(FormServiceType);
3538
#else
36-
3739
formService = DependencyResolver.Current.GetService(FormServiceType);
3840
#endif
39-
4041
return formService != null;
4142
}
4243

@@ -87,5 +88,49 @@ public IEnumerable<FormDto> GetAll()
8788

8889
}
8990
#endif
91+
92+
public bool TryResolveRecordStorage(out object recordStorage)
93+
{
94+
#if NETCOREAPP
95+
recordStorage = _serviceProvider.GetService(RecordStorageType);
96+
#else
97+
recordStorage = DependencyResolver.Current.GetService(RecordStorageType);
98+
#endif
99+
100+
return recordStorage != null;
101+
}
102+
103+
public void AddEventHandler(object target, Action<object, object> handler)
104+
{
105+
var eventInfo = RecordStorageType.GetEvent("RecordInserting");
106+
107+
if (eventInfo == null && RecordStorageType.IsInterface)
108+
{
109+
eventInfo = GetAllEventsForInterface(RecordStorageType)
110+
.FirstOrDefault(x => x.Name == "RecordInserting");
111+
}
112+
113+
if (eventInfo == null)
114+
{
115+
throw new ArgumentOutOfRangeException("RecordInserting",
116+
$"Couldn't find event RecordInserting in type {RecordStorageType.FullName}");
117+
}
118+
119+
Delegate convertedHandler = ConvertDelegate(handler, eventInfo.EventHandlerType);
120+
eventInfo.AddEventHandler(target, convertedHandler);
121+
}
122+
123+
private IEnumerable<EventInfo> GetAllEventsForInterface(Type interfaceType) =>
124+
(new Type[] {interfaceType})
125+
.Concat(interfaceType.GetInterfaces())
126+
.SelectMany(i => i.GetEvents());
127+
128+
private Delegate ConvertDelegate(Delegate originalDelegate, Type targetDelegateType)
129+
{
130+
return Delegate.CreateDelegate(
131+
targetDelegateType,
132+
originalDelegate.Target,
133+
originalDelegate.Method);
134+
}
90135
}
91136
}

0 commit comments

Comments
 (0)