Skip to content

Commit 33e15b8

Browse files
authored
Merge pull request #14 from umbraco/feature/zapier-form-submitted
Feature/zapier form submitted
2 parents 3a2713b + 0cb188e commit 33e15b8

26 files changed

+613
-707
lines changed

azure-pipeline - Automation.Zapier.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
trigger:
2-
- feature/zapier-integration
2+
- feature/zapier-form-submitted
33

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

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ 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

108
public static class ZapierAppConfiguration
@@ -18,5 +16,21 @@ public static class Configuration
1816
{
1917
public const string Settings = "Umbraco:Forms:Integrations:Automation:Zapier:Settings";
2018
}
19+
20+
public static class FormProperties
21+
{
22+
public const string Id = "formId";
23+
24+
public const string Name = "formName";
25+
26+
public const string SubmissionDate = "submissionDate";
27+
28+
public const string PageUrl = "pageUrl";
29+
}
30+
31+
public static class EntityType
32+
{
33+
public const int Form = 2;
34+
}
2135
}
2236
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
using Umbraco.Forms.Integrations.Automation.Zapier.Models.Dtos;
5+
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
6+
7+
#if NETCOREAPP
8+
using Microsoft.Extensions.Options;
9+
10+
using Umbraco.Forms.Integrations.Automation.Zapier.Configuration;
11+
#endif
12+
13+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
14+
{
15+
/// <summary>
16+
/// When a Zapier user creates a new "New Form Submitted" trigger, the API is used to provide them with the list of forms.
17+
/// </summary>
18+
public class FormController : ZapierFormAuthorizedApiController
19+
{
20+
private readonly ZapierFormService _zapierFormService;
21+
22+
#if NETCOREAPP
23+
public FormController(IOptions<ZapierSettings> options, IUserValidationService userValidationService, ZapierFormService zapierFormService)
24+
: base(options, userValidationService)
25+
#else
26+
public FormController(ZapierFormService zapierFormService, IUserValidationService userValidationService)
27+
: base(userValidationService)
28+
#endif
29+
{
30+
_zapierFormService = zapierFormService;
31+
}
32+
33+
public IEnumerable<FormDto> GetForms()
34+
{
35+
if (!IsUserValid()) return Enumerable.Empty<FormDto>();
36+
37+
return _zapierFormService.GetAll();
38+
}
39+
40+
}
41+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
using Umbraco.Forms.Integrations.Automation.Zapier.Models.Dtos;
6+
using Umbraco.Forms.Integrations.Automation.Zapier.Extensions;
7+
using Umbraco.Forms.Integrations.Automation.Zapier.Services;
8+
9+
#if NETCOREAPP
10+
using Microsoft.Extensions.Options;
11+
12+
using Umbraco.Forms.Integrations.Automation.Zapier.Configuration;
13+
#endif
14+
15+
namespace Umbraco.Forms.Integrations.Automation.Zapier.Controllers
16+
{
17+
/// <summary>
18+
/// When a Zapier user creates a "New Form Submitted" trigger, they are authenticated, they select a form, and the API provides an output json with the
19+
/// structure of the selected form.
20+
/// </summary>
21+
public class FormPollingController : ZapierFormAuthorizedApiController
22+
{
23+
private readonly ZapierFormService _zapierFormService;
24+
25+
#if NETCOREAPP
26+
public FormPollingController(IOptions<ZapierSettings> options, ZapierFormService zapierFormService, IUserValidationService userValidationService)
27+
: base(options, userValidationService)
28+
#else
29+
public FormPollingController(ZapierFormService zapierFormService, IUserValidationService userValidationService)
30+
: base(userValidationService)
31+
#endif
32+
{
33+
_zapierFormService = zapierFormService;
34+
}
35+
36+
[Obsolete("Used only for Umbraco Zapier app v1.0.0. For updated versions use GetFormById")]
37+
public IEnumerable<FormDto> GetSampleForm()
38+
{
39+
if (!IsUserValid()) return null;
40+
41+
return _zapierFormService.GetAll().Take(1);
42+
}
43+
44+
public List<Dictionary<string, string>> GetFormPropertiesById(string id)
45+
{
46+
if (!IsUserValid()) return new List<Dictionary<string, string>>();
47+
48+
var form = _zapierFormService.GetById(id);
49+
return form != null
50+
? new List<Dictionary<string, string>> { form.ToFormDictionary() }
51+
: new List<Dictionary<string, string>>();
52+
}
53+
}
54+
}

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

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

0 commit comments

Comments
 (0)