using ProjectName.Website.Core.FormExtensions.Constants;
using ProjectName.Website.Core.FormExtensions.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Umbraco.Cms.Core.Web;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Attributes;
using Umbraco.Forms.Core.Enums;
namespace ProjectName.Website.Core.FormExtensions.CustomWorkflows
{
public class ConditionalGoToPageWorkflow : WorkflowType
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<ConditionalGoToPageWorkflow> _logger;
public ConditionalGoToPageWorkflow(IUmbracoContextAccessor umbracoContextAccessor,
IHttpContextAccessor httpContextAccessor,
ILogger<ConditionalGoToPageWorkflow> logger)
{
Name = "Go to page based on conditions";
Id = new Guid(FormWorkflowIds.ConditionalGoToPageWorkflowId);
Description = "This will go to a page based on a condition in the form";
Icon = "icon-directions-alt";
_umbracoContextAccessor = umbracoContextAccessor;
_httpContextAccessor = httpContextAccessor;
_logger = logger;
}
[Setting("Routes",
Description = "Page to go to on submission, depending on selected fields",
View = "FieldConditions")]
public string Routes { get; set; } = string.Empty;
public override Task<WorkflowExecutionStatus> ExecuteAsync(WorkflowExecutionContext context)
{
try
{
List<PageRouteMapping> pageRoutes = JsonConvert.DeserializeObject<List<PageRouteMapping>>(Routes);
foreach (var route in pageRoutes)
{
Guid.TryParse(route.Field, out var fieldGuid);
if (fieldGuid != Guid.Empty &&
context.Record.RecordFields.ContainsKey(fieldGuid) &&
context.Record.RecordFields[fieldGuid].HasValue())
{
var fieldValue = context.Record.RecordFields[fieldGuid].ValuesAsString();
if (!string.IsNullOrWhiteSpace(fieldValue) &&
fieldValue.Equals(route.FieldValue))
{
if (_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext) &&
_httpContextAccessor.HttpContext != null &&
!string.IsNullOrEmpty(route?.PageUdi) &&
route.PageUdi.StartsWith("umb://document/"))
{
// Extract the GUID from the UDI string
var guidString = route.PageUdi.Replace("umb://document/", "");
if (Guid.TryParse(guidString, out var guid))
{
var goToPage = umbracoContext.Content?.GetById(guid);
if (goToPage != null)
{
// This won't work!
//_httpContextAccessor.HttpContext
//.Response.Redirect(goToPage.Url());
// This will redirect the user to the next page
context.Form.GoToPageOnSubmit = goToPage.Id;
return Task.FromResult(WorkflowExecutionStatus.Completed);
}
}
}
}
}
}
return Task.FromResult(WorkflowExecutionStatus.Completed);
}
catch (Exception ex)
{
_logger.LogError(ex,
$"There was a problem going to page from Workflow for
Form {context.Form.Name} with
id {context.Form.Id} for
Record with unique id {context.Record.UniqueId}");
return Task.FromResult(WorkflowExecutionStatus.Failed);
}
}
public override List<Exception> ValidateSettings()
{
List<Exception> exceptionList = new List<Exception>();
// Removed for brevity
return exceptionList;
}
}
}
Comments
Post a Comment