Skip to content

Commit c5d97e0

Browse files
committed
Zapier forms extension for handling submissions
1 parent 3a2713b commit c5d97e0

20 files changed

+340
-779
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#if NETFRAMEWORK
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
using Umbraco.Core;
7+
using Umbraco.Core.Composing;
8+
using Umbraco.Core.Logging;
9+
using Umbraco.Core.Models.PublishedContent;
10+
using Umbraco.Forms.Core.Data.Storage;
11+
using Umbraco.Forms.Integrations.Automation.Zapier.Helpers;
12+
using Umbraco.Forms.Integrations.Automation.Zapier.Models.Dtos;
13+
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
14+
using Umbraco.Web;
15+
16+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Components
17+
{
18+
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
19+
public class NewFormSubmittedComposer : ComponentComposer<NewFormSubmittedComponent>
20+
{ }
21+
22+
public class NewFormSubmittedComponent : IComponent
23+
{
24+
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
25+
26+
private readonly IRecordStorage _recordStorage;
27+
28+
private readonly ZapierService _zapierService;
29+
30+
private readonly ZapierFormSubscriptionHookService _zapierFormSubscriptionHookService;
31+
32+
private readonly ILogger _logger;
33+
34+
public NewFormSubmittedComponent(IUmbracoContextAccessor umbracoContextAccessor, IRecordStorage recordStorage,
35+
ZapierService zapierService,
36+
ZapierFormSubscriptionHookService zapierFormSubscriptionHookService,
37+
ILogger logger)
38+
{
39+
_umbracoContextAccessor = umbracoContextAccessor;
40+
41+
_recordStorage = recordStorage;
42+
43+
_zapierService = zapierService;
44+
45+
_zapierFormSubscriptionHookService = zapierFormSubscriptionHookService;
46+
47+
_logger = logger;
48+
}
49+
50+
public void Initialize()
51+
{
52+
_recordStorage.RecordInserting += RecordStorage_RecordInserting;
53+
}
54+
55+
public void Terminate()
56+
{
57+
}
58+
59+
private void RecordStorage_RecordInserting(object sender, Core.RecordEventArgs e)
60+
{
61+
var triggerHelper = new TriggerHelper(_zapierService);
62+
63+
UmbracoContext umbracoContext = _umbracoContextAccessor.UmbracoContext;
64+
var umbracoPageId = e.Record.UmbracoPageId;
65+
var pageUrl = umbracoContext.UrlProvider.GetUrl(umbracoPageId, UrlMode.Absolute);
66+
67+
if (_zapierFormSubscriptionHookService.TryGetByName(e.Form.Name, out var zapFormConfigList))
68+
{
69+
var content = new Dictionary<string, string>
70+
{
71+
{ Constants.Form.Id, e.Form.Id.ToString() },
72+
{ Constants.Form.Name, e.Form.Name },
73+
{ Constants.Form.SubmissionDate, DateTime.UtcNow.ToString("s") },
74+
{ Constants.Form.PageUrl, pageUrl }
75+
};
76+
77+
foreach (var recordField in e.Record.RecordFields)
78+
{
79+
content.Add(recordField.Value.Alias, recordField.Value.ValuesAsString());
80+
}
81+
82+
foreach (var zapFormConfig in zapFormConfigList)
83+
{
84+
var result = triggerHelper.FormExecute(zapFormConfig.HookUrl, content);
85+
86+
if (!string.IsNullOrEmpty(result))
87+
_logger.Error<NewFormSubmittedComponent>(result);
88+
}
89+
}
90+
}
91+
}
92+
}
93+
#endif
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#if NETCOREAPP
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
using Microsoft.Extensions.Logging;
7+
8+
using Umbraco.Cms.Core.Events;
9+
using Umbraco.Cms.Core.Models.PublishedContent;
10+
using Umbraco.Cms.Core.Routing;
11+
using Umbraco.Cms.Web.Common;
12+
using Umbraco.Extensions;
13+
using Umbraco.Forms.Core.Services;
14+
using Umbraco.Forms.Core.Services.Notifications;
15+
using Umbraco.Forms.Integrations.Automation.Zapier.Helpers;
16+
using Umbraco.Forms.Integrations.Automation.Zapier.Models.Dtos;
17+
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
18+
19+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Components
20+
{
21+
public class NewFormSubmittedNotification : INotificationHandler<RecordCreatingNotification>
22+
{
23+
private readonly IUmbracoHelperAccessor _umbracoHelperAccessor;
24+
25+
private readonly IPublishedUrlProvider _publishedUrlProvider;
26+
27+
private readonly IFormService _formService;
28+
29+
private readonly ZapierService _zapierService;
30+
31+
private readonly ZapierFormSubscriptionHookService _zapierFormSubscriptionHookService;
32+
33+
private readonly ILogger<NewFormSubmittedNotification> _logger;
34+
35+
public NewFormSubmittedNotification(
36+
IUmbracoHelperAccessor umbracoHelperAccessor,
37+
IPublishedUrlProvider publishedUrlProvider,
38+
IFormService formService,
39+
ZapierService zapierService, ZapierFormSubscriptionHookService zapierFormSubscriptionHookService,
40+
ILogger<NewFormSubmittedNotification> logger)
41+
{
42+
_umbracoHelperAccessor = umbracoHelperAccessor;
43+
44+
_publishedUrlProvider = publishedUrlProvider;
45+
46+
_formService = formService;
47+
48+
_zapierService = zapierService;
49+
50+
_zapierFormSubscriptionHookService = zapierFormSubscriptionHookService;
51+
52+
_logger = logger;
53+
}
54+
55+
public void Handle(RecordCreatingNotification notification)
56+
{
57+
var triggerHelper = new TriggerHelper(_zapierService);
58+
59+
foreach (var notificationSavedEntity in notification.SavedEntities)
60+
{
61+
var form = _formService.Get(notificationSavedEntity.Form);
62+
63+
if (_zapierFormSubscriptionHookService.TryGetByName(form.Name, out var zapFormConfigList))
64+
{
65+
string pageUrl = string.Empty;
66+
if (_umbracoHelperAccessor.TryGetUmbracoHelper(out UmbracoHelper umbracoHelper))
67+
{
68+
IPublishedContent publishedContent = umbracoHelper.Content(notificationSavedEntity.UmbracoPageId);
69+
if (publishedContent != null)
70+
{
71+
pageUrl = publishedContent.Url(_publishedUrlProvider, mode: UrlMode.Absolute);
72+
}
73+
}
74+
75+
var content = new Dictionary<string, string>
76+
{
77+
{ Constants.Form.Id, form.Id.ToString() },
78+
{ Constants.Form.Name, form.Name },
79+
{ Constants.Form.SubmissionDate, DateTime.UtcNow.ToString("s") },
80+
{ Constants.Form.PageUrl, pageUrl }
81+
};
82+
83+
foreach (var recordField in notificationSavedEntity.RecordFields)
84+
{
85+
content.Add(recordField.Value.Alias, recordField.Value.ValuesAsString());
86+
}
87+
88+
foreach (var formConfigDto in zapFormConfigList)
89+
{
90+
var result =
91+
triggerHelper.FormExecute(formConfigDto.HookUrl, content);
92+
93+
if(!string.IsNullOrEmpty(result))
94+
_logger.LogError(result);
95+
}
96+
}
97+
}
98+
}
99+
}
100+
}
101+
#endif

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ namespace Umbraco.Forms.Integrations.Automation.Zapier
33
{
44
public class Constants
55
{
6-
public const string ZapierWorkflowTypeId = "d05b95e5-86f8-4c31-99b8-4ec7fc62a787";
7-
86
public const string UmbracoFormsIntegrationsAutomationZapierUserGroup = "Umbraco.Forms.Integrations.Automation.Zapier.UserGroup";
97

10-
public static class ZapierAppConfiguration
8+
public static class Configuration
119
{
12-
public const string UsernameHeaderKey = "X-USERNAME";
13-
14-
public const string PasswordHeaderKey = "X-PASSWORD";
10+
public const string Settings = "Umbraco:Forms:Integrations:Automation:Zapier:Settings";
1511
}
1612

17-
public static class Configuration
13+
public static class Form
1814
{
19-
public const string Settings = "Umbraco:Forms:Integrations:Automation:Zapier:Settings";
15+
public const string Id = "formId";
16+
17+
public const string Name = "formName";
18+
19+
public const string SubmissionDate = "submissionDate";
20+
21+
public const string PageUrl = "pageUrl";
2022
}
2123
}
2224
}

src/Umbraco.Forms.Integrations.Automation.Zapier/Controllers/FormsAuthController.cs

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)