Beispiel #1
0
 /// <summary>
 /// Activates the specified <see cref="Rock.Model.WorkflowAction" />.
 /// </summary>
 /// <param name="actionTypeCache">The action type cache.</param>
 /// <param name="activity">The <see cref="Rock.Model.WorkflowActivity" /> that this WorkflowAction belongs to..</param>
 /// <returns>
 /// The <see cref="Rock.Model.WorkflowAction" />
 /// </returns>
 internal static WorkflowAction Activate(WorkflowActionTypeCache actionTypeCache, WorkflowActivity activity)
 {
     using (var rockContext = new RockContext())
     {
         return(Activate(actionTypeCache, activity, rockContext));
     }
 }
Beispiel #2
0
        /// <summary>
        /// Updates any Cache Objects that are associated with this entity
        /// </summary>
        /// <param name="entityState">State of the entity.</param>
        /// <param name="dbContext">The database context.</param>
        public void UpdateCache(System.Data.Entity.EntityState entityState, Rock.Data.DbContext dbContext)
        {
            var workflowTypeId = WorkflowActivityTypeCache.Get(this.ActivityTypeId, dbContext as RockContext)?.WorkflowTypeId;

            if (workflowTypeId.HasValue)
            {
                WorkflowTypeCache.UpdateCachedEntity(workflowTypeId.Value, System.Data.Entity.EntityState.Modified);
            }

            WorkflowActivityTypeCache.UpdateCachedEntity(this.ActivityTypeId, System.Data.Entity.EntityState.Modified);
            WorkflowActionTypeCache.UpdateCachedEntity(this.Id, entityState);
        }
Beispiel #3
0
        /// <summary>
        /// Activates the specified <see cref="Rock.Model.WorkflowAction" />.
        /// </summary>
        /// <param name="actionTypeCache">The action type cache.</param>
        /// <param name="activity">The <see cref="Rock.Model.WorkflowActivity" /> that this WorkflowAction belongs to..</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns>
        /// The <see cref="Rock.Model.WorkflowAction" />
        /// </returns>
        internal static WorkflowAction Activate(WorkflowActionTypeCache actionTypeCache, WorkflowActivity activity, RockContext rockContext)
        {
            var action = new WorkflowAction();

            action.Activity     = activity;
            action.ActionTypeId = actionTypeCache.Id;
            action.LoadAttributes(rockContext);

            action.AddLogEntry("Activated");

            return(action);
        }
Beispiel #4
0
        internal static WorkflowAction Activate(WorkflowActionType actionType, WorkflowActivity activity, RockContext rockContext)
        {
            if (actionType != null)
            {
                var actionTypeCache = WorkflowActionTypeCache.Get(actionType.Id);
                var action          = Activate(actionTypeCache, activity, rockContext);
                if (action != null)
                {
                    action.ActionType = actionType;
                }
                return(action);
            }

            return(null);
        }
Beispiel #5
0
 /// <summary>
 /// Gets the cache object associated with this Entity
 /// </summary>
 /// <returns></returns>
 public IEntityCache GetCacheObject()
 {
     return(WorkflowActionTypeCache.Get(this.Id));
 }
Beispiel #6
0
        private void CompleteFormAction(string formAction)
        {
            if (!string.IsNullOrWhiteSpace(formAction) &&
                _workflow != null &&
                _actionType != null &&
                _actionType.WorkflowForm != null &&
                _activity != null &&
                _action != null)
            {
                var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);
                mergeFields.Add("Action", _action);
                mergeFields.Add("Activity", _activity);
                mergeFields.Add("Workflow", _workflow);

                Guid   activityTypeGuid = Guid.Empty;
                string responseText     = "Your information has been submitted successfully.";

                foreach (var action in _actionType.WorkflowForm.Actions.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var actionDetails = action.Split(new char[] { '^' });
                    if (actionDetails.Length > 0 && actionDetails[0] == formAction)
                    {
                        if (actionDetails.Length > 2)
                        {
                            activityTypeGuid = actionDetails[2].AsGuid();
                        }

                        if (actionDetails.Length > 3 && !string.IsNullOrWhiteSpace(actionDetails[3]))
                        {
                            responseText = actionDetails[3].ResolveMergeFields(mergeFields);
                        }
                        break;
                    }
                }

                _action.MarkComplete();
                _action.FormAction = formAction;
                _action.AddLogEntry("Form Action Selected: " + _action.FormAction);

                if (_action.ActionTypeCache.IsActivityCompletedOnSuccess)
                {
                    _action.Activity.MarkComplete();
                }

                if (_actionType.WorkflowForm.ActionAttributeGuid.HasValue)
                {
                    var attribute = AttributeCache.Get(_actionType.WorkflowForm.ActionAttributeGuid.Value);
                    if (attribute != null)
                    {
                        Rock.Attribute.IHasAttributes item = null;
                        if (attribute.EntityTypeId == _workflow.TypeId)
                        {
                            item = _workflow;
                        }
                        else if (attribute.EntityTypeId == _activity.TypeId)
                        {
                            item = _activity;
                        }

                        if (item != null)
                        {
                            item.SetAttributeValue(attribute.Key, formAction);
                        }
                    }
                }

                if (!activityTypeGuid.IsEmpty())
                {
                    var activityType = _workflowType.ActivityTypes.Where(a => a.Guid.Equals(activityTypeGuid)).FirstOrDefault();
                    if (activityType != null)
                    {
                        WorkflowActivity.Activate(activityType, _workflow);
                    }
                }

                List <string> errorMessages;
                if (_workflowService.Process(_workflow, out errorMessages))
                {
                    Guid?previousActionGuid = null;

                    if (_action != null)
                    {
                        // Compare GUIDs since the IDs are DB generated and will be 0 if the workflow is not persisted.
                        previousActionGuid = _action.Guid;
                    }

                    ActionTypeId = null;
                    _action      = null;
                    _actionType  = null;
                    _activity    = null;
                    bool hydrateObjectsResult = HydrateObjects();

                    if (hydrateObjectsResult && _action != null && _action.Guid != previousActionGuid)
                    {
                        // The block reloads the page with the workflow IDs as a parameter. At this point the workflow must be persisted regardless of user settings in order for the workflow to work.
                        _workflowService.PersistImmediately(_action);

                        // If we are already being directed (presumably from the Redirect Action), don't redirect again.
                        if (!Response.IsRequestBeingRedirected)
                        {
                            var cb = CurrentPageReference;
                            cb.Parameters.AddOrReplace("WorkflowId", _workflow.Id.ToString());
                            foreach (var key in cb.QueryString.AllKeys.Where(k => !k.Equals("Command", StringComparison.OrdinalIgnoreCase)))
                            {
                                cb.Parameters.AddOrIgnore(key, cb.QueryString[key]);
                            }
                            cb.QueryString = new System.Collections.Specialized.NameValueCollection();
                            Response.Redirect(cb.BuildUrl(), false);
                            Context.ApplicationInstance.CompleteRequest();
                        }
                    }
                    else
                    {
                        if (lSummary.Text.IsNullOrWhiteSpace())
                        {
                            ShowMessage(NotificationBoxType.Success, string.Empty, responseText, (_action == null || _action.Guid != previousActionGuid));
                        }
                        else
                        {
                            pnlForm.Visible = false;
                        }
                    }
                }
                else
                {
                    ShowMessage(NotificationBoxType.Danger, "Workflow Processing Error(s):",
                                "<ul><li>" + errorMessages.AsDelimited("</li><li>", null, true) + "</li></ul>");
                }
                if (_workflow.Id != 0)
                {
                    WorkflowId = _workflow.Id;
                }
            }
        }
Beispiel #7
0
        private bool HydrateObjects()
        {
            LoadWorkflowType();

            // Set the note type if this is first request
            if (!Page.IsPostBack)
            {
                var entityType = EntityTypeCache.Get(typeof(Rock.Model.Workflow));
                var noteTypes  = NoteTypeCache.GetByEntity(entityType.Id, string.Empty, string.Empty);
                ncWorkflowNotes.NoteOptions.SetNoteTypes(noteTypes);
            }

            if (_workflowType == null)
            {
                ShowNotes(false);
                ShowMessage(NotificationBoxType.Danger, "Configuration Error", "Workflow type was not configured or specified correctly.");
                return(false);
            }

            if (!_workflowType.IsAuthorized(Authorization.VIEW, CurrentPerson))
            {
                ShowNotes(false);
                ShowMessage(NotificationBoxType.Warning, "Sorry", "You are not authorized to view this type of workflow.");
                return(false);
            }

            if (!(_workflowType.IsActive ?? true))
            {
                ShowNotes(false);
                ShowMessage(NotificationBoxType.Warning, "Sorry", "This type of workflow is not active.");
                return(false);
            }

            // If operating against an existing workflow, get the workflow and load attributes
            if (!WorkflowId.HasValue)
            {
                WorkflowId = PageParameter("WorkflowId").AsIntegerOrNull();
                if (!WorkflowId.HasValue)
                {
                    Guid guid = PageParameter("WorkflowGuid").AsGuid();
                    if (!guid.IsEmpty())
                    {
                        _workflow = _workflowService.Queryable()
                                    .Where(w => w.Guid.Equals(guid) && w.WorkflowTypeId == _workflowType.Id)
                                    .FirstOrDefault();
                        if (_workflow != null)
                        {
                            WorkflowId = _workflow.Id;
                        }
                    }
                }
            }

            if (WorkflowId.HasValue)
            {
                if (_workflow == null)
                {
                    _workflow = _workflowService.Queryable()
                                .Where(w => w.Id == WorkflowId.Value && w.WorkflowTypeId == _workflowType.Id)
                                .FirstOrDefault();
                }
                if (_workflow != null)
                {
                    hlblWorkflowId.Text = _workflow.WorkflowId;

                    _workflow.LoadAttributes();
                    foreach (var activity in _workflow.Activities)
                    {
                        activity.LoadAttributes();
                    }
                }
            }

            // If an existing workflow was not specified, activate a new instance of workflow and start processing
            if (_workflow == null)
            {
                string workflowName = PageParameter("WorkflowName");
                if (string.IsNullOrWhiteSpace(workflowName))
                {
                    workflowName = "New " + _workflowType.WorkTerm;
                }

                _workflow = Rock.Model.Workflow.Activate(_workflowType, workflowName);
                if (_workflow != null)
                {
                    // If a PersonId or GroupId parameter was included, load the corresponding
                    // object and pass that to the actions for processing
                    object entity   = null;
                    int?   personId = PageParameter("PersonId").AsIntegerOrNull();
                    if (personId.HasValue)
                    {
                        entity = new PersonService(_rockContext).Get(personId.Value);
                    }
                    else
                    {
                        int?groupId = PageParameter("GroupId").AsIntegerOrNull();
                        if (groupId.HasValue)
                        {
                            entity = new GroupService(_rockContext).Get(groupId.Value);
                        }
                    }

                    // Loop through all the query string parameters and try to set any workflow
                    // attributes that might have the same key
                    foreach (var param in RockPage.PageParameters())
                    {
                        if (param.Value != null && param.Value.ToString().IsNotNullOrWhiteSpace())
                        {
                            _workflow.SetAttributeValue(param.Key, param.Value.ToString());
                        }
                    }

                    List <string> errorMessages;
                    if (!_workflowService.Process(_workflow, entity, out errorMessages))
                    {
                        ShowNotes(false);
                        ShowMessage(NotificationBoxType.Danger, "Workflow Processing Error(s):",
                                    "<ul><li>" + errorMessages.AsDelimited("</li><li>") + "</li></ul>");
                        return(false);
                    }
                    if (_workflow.Id != 0)
                    {
                        WorkflowId = _workflow.Id;
                    }
                }
            }

            if (_workflow == null)
            {
                ShowNotes(false);
                ShowMessage(NotificationBoxType.Danger, "Workflow Activation Error", "Workflow could not be activated.");
                return(false);
            }

            var canEdit = UserCanEdit || _workflow.IsAuthorized(Authorization.EDIT, CurrentPerson);

            if (_workflow.IsActive)
            {
                if (ActionTypeId.HasValue)
                {
                    foreach (var activity in _workflow.ActiveActivities)
                    {
                        _action = activity.ActiveActions.Where(a => a.ActionTypeId == ActionTypeId.Value).FirstOrDefault();
                        if (_action != null)
                        {
                            _activity = activity;
                            _activity.LoadAttributes();

                            _actionType  = _action.ActionTypeCache;
                            ActionTypeId = _actionType.Id;
                            return(true);
                        }
                    }
                }

                // Find first active action form
                int personId = CurrentPerson != null ? CurrentPerson.Id : 0;
                int?actionId = PageParameter("ActionId").AsIntegerOrNull();
                foreach (var activity in _workflow.Activities
                         .Where(a =>
                                a.IsActive &&
                                (!actionId.HasValue || a.Actions.Any(ac => ac.Id == actionId.Value)) &&
                                (
                                    (canEdit) ||
                                    (!a.AssignedGroupId.HasValue && !a.AssignedPersonAliasId.HasValue) ||
                                    (a.AssignedPersonAlias != null && a.AssignedPersonAlias.PersonId == personId) ||
                                    (a.AssignedGroup != null && a.AssignedGroup.Members.Any(m => m.PersonId == personId))
                                )
                                )
                         .ToList()
                         .OrderBy(a => a.ActivityTypeCache.Order))
                {
                    if (canEdit || (activity.ActivityTypeCache.IsAuthorized(Authorization.VIEW, CurrentPerson)))
                    {
                        foreach (var action in activity.ActiveActions
                                 .Where(a => (!actionId.HasValue || a.Id == actionId.Value)))
                        {
                            if (action.ActionTypeCache.WorkflowForm != null && action.IsCriteriaValid)
                            {
                                _activity = activity;
                                _activity.LoadAttributes();

                                _action      = action;
                                _actionType  = _action.ActionTypeCache;
                                ActionTypeId = _actionType.Id;
                                return(true);
                            }
                        }
                    }
                }

                lSummary.Text = string.Empty;
            }
            else
            {
                if (GetAttributeValue("ShowSummaryView").AsBoolean() && !string.IsNullOrWhiteSpace(_workflowType.SummaryViewText))
                {
                    var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);
                    mergeFields.Add("Action", _action);
                    mergeFields.Add("Activity", _activity);
                    mergeFields.Add("Workflow", _workflow);

                    lSummary.Text    = _workflowType.SummaryViewText.ResolveMergeFields(mergeFields, CurrentPerson);
                    lSummary.Visible = true;
                }
            }

            if (lSummary.Text.IsNullOrWhiteSpace())
            {
                if (_workflowType.NoActionMessage.IsNullOrWhiteSpace())
                {
                    ShowMessage(NotificationBoxType.Warning, string.Empty, "The selected workflow is not in a state that requires you to enter information.");
                }
                else
                {
                    var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);
                    mergeFields.Add("Action", _action);
                    mergeFields.Add("Activity", _activity);
                    mergeFields.Add("Workflow", _workflow);
                    ShowMessage(NotificationBoxType.Warning, string.Empty, _workflowType.NoActionMessage.ResolveMergeFields(mergeFields, CurrentPerson));
                }
            }

            ShowNotes(false);
            return(false);
        }
Beispiel #8
0
        public override MobileBlockResponse HandleRequest(string request, Dictionary <string, string> Body)
        {
            if (Body.ContainsKey("__WorkflowId__"))
            {
                int id = 0;
                int.TryParse(Body["__WorkflowId__"], out id);
                if (id > 0)
                {
                    WorkflowId = id;
                }
            }

            if (Body.ContainsKey("__ActionTypeId__"))
            {
                int id = 0;
                int.TryParse(Body["__ActionTypeId__"], out id);
                if (id > 0)
                {
                    ActionTypeId = id;
                }
            }

            HydrateObjects();

            var response = new FormResponse();

            SetFormValues(Body);

            if (!string.IsNullOrWhiteSpace(request) &&
                _workflow != null &&
                _actionType != null &&
                _actionType.WorkflowForm != null &&
                _activity != null &&
                _action != null)
            {
                var mergeFields = AvalancheUtilities.GetMergeFields(this.CurrentPerson);
                mergeFields.Add("Action", _action);
                mergeFields.Add("Activity", _activity);
                mergeFields.Add("Workflow", _workflow);

                Guid   activityTypeGuid = Guid.Empty;
                string responseText     = "Your information has been submitted successfully.";

                foreach (var action in _actionType.WorkflowForm.Actions.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var actionDetails = action.Split(new char[] { '^' });
                    if (actionDetails.Length > 0 && actionDetails[0] == request)
                    {
                        if (actionDetails.Length > 2)
                        {
                            activityTypeGuid = actionDetails[2].AsGuid();
                        }

                        if (actionDetails.Length > 3 && !string.IsNullOrWhiteSpace(actionDetails[3]))
                        {
                            responseText = actionDetails[3].ResolveMergeFields(mergeFields);
                        }
                        break;
                    }
                }

                _action.MarkComplete();
                _action.FormAction = request;
                _action.AddLogEntry("Form Action Selected: " + _action.FormAction);

                if (_action.ActionTypeCache.IsActivityCompletedOnSuccess)
                {
                    _action.Activity.MarkComplete();
                }

                if (_actionType.WorkflowForm.ActionAttributeGuid.HasValue)
                {
                    var attribute = AttributeCache.Read(_actionType.WorkflowForm.ActionAttributeGuid.Value);
                    if (attribute != null)
                    {
                        IHasAttributes item = null;
                        if (attribute.EntityTypeId == _workflow.TypeId)
                        {
                            item = _workflow;
                        }
                        else if (attribute.EntityTypeId == _activity.TypeId)
                        {
                            item = _activity;
                        }

                        if (item != null)
                        {
                            item.SetAttributeValue(attribute.Key, request);
                        }
                    }
                }

                if (!activityTypeGuid.IsEmpty())
                {
                    var activityType = _workflowType.ActivityTypes.Where(a => a.Guid.Equals(activityTypeGuid)).FirstOrDefault();
                    if (activityType != null)
                    {
                        WorkflowActivity.Activate(activityType, _workflow);
                    }
                }

                List <string> errorMessages;
                if (_workflowService.Process(_workflow, out errorMessages))
                {
                    int?previousActionId = null;

                    if (_action != null)
                    {
                        previousActionId = _action.Id;
                    }

                    ActionTypeId = null;
                    _action      = null;
                    _actionType  = null;
                    _activity    = null;

                    if (HttpContext.Current.Response.Headers.GetValues("ActionType") != null && HttpContext.Current.Response.Headers.GetValues("ActionType").Any())
                    {
                        response.Success    = true;
                        response.ActionType = HttpContext.Current.Response.Headers.GetValues("ActionType").FirstOrDefault();
                        if (HttpContext.Current.Response.Headers.GetValues("Resource") != null)
                        {
                            response.Resource = HttpContext.Current.Response.Headers.GetValues("Resource").FirstOrDefault();
                        }
                        if (HttpContext.Current.Response.Headers.GetValues("Parameter") != null)
                        {
                            response.Parameter = HttpContext.Current.Response.Headers.GetValues("Parameter").FirstOrDefault();
                        }
                    }
                    else
                    {
                        if (HydrateObjects() && _action != null && _action.Id != previousActionId)
                        {
                            response.FormElementItems = BuildForm(true);
                            response.Success          = true;
                        }
                        else
                        {
                            response.Message = responseText;
                            response.Success = true;
                        }
                    }
                }
                else
                {
                    response.Message  = "Workflow Processing Error(s): \n";
                    response.Message += errorMessages.AsDelimited("\n", null, false);
                }
            }


            return(new MobileBlockResponse()
            {
                Request = request,
                Response = JsonConvert.SerializeObject(response),
                TTL = 0
            });
        }
Beispiel #9
0
        private bool HydrateObjects()
        {
            LoadWorkflowType();

            if (_workflowType == null)
            {
                nbError.NotificationBoxType = NotificationBoxType.Danger;
                nbError.Text = "Configuration Error, Workflow type was not configured or specified correctly.";
                return(false);
            }

            if (!_workflowType.IsAuthorized(Authorization.VIEW, CurrentPerson))
            {
                return(false);
            }

            if (!(_workflowType.IsActive ?? true))
            {
                nbError.NotificationBoxType = NotificationBoxType.Danger;
                nbError.Text = "This type of workflow is not active.";

                return(false);
            }

            // If operating against an existing workflow, get the workflow and load attributes
            if (!WorkflowId.HasValue)
            {
                WorkflowId = parameter.AsIntegerOrNull();
                if (!WorkflowId.HasValue)
                {
                    Guid guid = parameter.AsGuid();
                    if (!guid.IsEmpty())
                    {
                        _workflow = _workflowService.Queryable()
                                    .Where(w => w.Guid.Equals(guid) && w.WorkflowTypeId == _workflowType.Id)
                                    .FirstOrDefault();
                        if (_workflow != null)
                        {
                            WorkflowId = _workflow.Id;
                        }
                    }
                }
            }

            if (WorkflowId.HasValue)
            {
                if (_workflow == null)
                {
                    _workflow = _workflowService.Queryable()
                                .Where(w => w.Id == WorkflowId.Value && w.WorkflowTypeId == _workflowType.Id)
                                .FirstOrDefault();
                }
                if (_workflow != null)
                {
                    _workflow.LoadAttributes();
                    foreach (var activity in _workflow.Activities)
                    {
                        activity.LoadAttributes();
                    }
                }
            }

            // If an existing workflow was not specified, activate a new instance of workflow and start processing
            if (_workflow == null)
            {
                var workflowName = "New " + _workflowType.WorkTerm;

                _workflow = Rock.Model.Workflow.Activate(_workflowType, workflowName);
                if (_workflow != null)
                {
                    // If a PersonId or GroupId parameter was included, load the corresponding
                    // object and pass that to the actions for processing
                    object entity = null;

                    List <string> errorMessages;
                    if (!_workflowService.Process(_workflow, entity, out errorMessages))
                    {
                        //ShowMessage( NotificationBoxType.Danger, "Workflow Processing Error(s):",
                        //    "<ul><li>" + errorMessages.AsDelimited( "</li><li>" ) + "</li></ul>" );
                        return(false);
                    }
                    if (_workflow.Id != 0)
                    {
                        WorkflowId = _workflow.Id;
                    }
                }
            }

            if (_workflow == null)
            {
                //ShowNotes( false );
                //ShowMessage( NotificationBoxType.Danger, "Workflow Activation Error", "Workflow could not be activated." );
                return(false);
            }

            var canEdit = UserCanEdit || _workflow.IsAuthorized(Authorization.EDIT, CurrentPerson);

            if (_workflow.IsActive)
            {
                if (ActionTypeId.HasValue)
                {
                    foreach (var activity in _workflow.ActiveActivities)
                    {
                        _action = activity.ActiveActions.Where(a => a.ActionTypeId == ActionTypeId.Value).FirstOrDefault();
                        if (_action != null)
                        {
                            _activity = activity;
                            _activity.LoadAttributes();

                            _actionType  = _action.ActionTypeCache;
                            ActionTypeId = _actionType.Id;
                            return(true);
                        }
                    }
                }

                // Find first active action form
                int personId = CurrentPerson != null ? CurrentPerson.Id : 0;
                int?actionId = null;
                foreach (var activity in _workflow.Activities
                         .Where(a =>
                                a.IsActive &&
                                (!actionId.HasValue || a.Actions.Any(ac => ac.Id == actionId.Value)) &&
                                (
                                    (canEdit) ||
                                    (!a.AssignedGroupId.HasValue && !a.AssignedPersonAliasId.HasValue) ||
                                    (a.AssignedPersonAlias != null && a.AssignedPersonAlias.PersonId == personId) ||
                                    (a.AssignedGroup != null && a.AssignedGroup.Members.Any(m => m.PersonId == personId))
                                )
                                )
                         .ToList()
                         .OrderBy(a => a.ActivityTypeCache.Order))
                {
                    if (canEdit || (activity.ActivityTypeCache.IsAuthorized(Authorization.VIEW, CurrentPerson)))
                    {
                        foreach (var action in activity.ActiveActions
                                 .Where(a => (!actionId.HasValue || a.Id == actionId.Value)))
                        {
                            if (action.ActionTypeCache.WorkflowForm != null && action.IsCriteriaValid)
                            {
                                _activity = activity;
                                _activity.LoadAttributes();

                                _action      = action;
                                _actionType  = _action.ActionTypeCache;
                                ActionTypeId = _actionType.Id;
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Beispiel #10
0
        private void CompleteFormAction(string formAction)
        {
            if (!string.IsNullOrWhiteSpace(formAction) &&
                _workflow != null &&
                _actionType != null &&
                _actionType.WorkflowForm != null &&
                _activity != null &&
                _action != null)
            {
                var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson);
                mergeFields.Add("Action", _action);
                mergeFields.Add("Activity", _activity);
                mergeFields.Add("Workflow", _workflow);

                Guid   activityTypeGuid = Guid.Empty;
                string responseText     = "Your information has been submitted successfully.";

                foreach (var action in _actionType.WorkflowForm.Actions.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var actionDetails = action.Split(new char[] { '^' });
                    if (actionDetails.Length > 0 && actionDetails[0] == formAction)
                    {
                        if (actionDetails.Length > 2)
                        {
                            activityTypeGuid = actionDetails[2].AsGuid();
                        }

                        if (actionDetails.Length > 3 && !string.IsNullOrWhiteSpace(actionDetails[3]))
                        {
                            responseText = actionDetails[3].ResolveMergeFields(mergeFields);
                        }
                        break;
                    }
                }

                _action.MarkComplete();
                _action.FormAction = formAction;
                _action.AddLogEntry("Form Action Selected: " + _action.FormAction);

                if (_action.ActionTypeCache.IsActivityCompletedOnSuccess)
                {
                    _action.Activity.MarkComplete();
                }

                if (_actionType.WorkflowForm.ActionAttributeGuid.HasValue)
                {
                    var attribute = AttributeCache.Read(_actionType.WorkflowForm.ActionAttributeGuid.Value);
                    if (attribute != null)
                    {
                        IHasAttributes item = null;
                        if (attribute.EntityTypeId == _workflow.TypeId)
                        {
                            item = _workflow;
                        }
                        else if (attribute.EntityTypeId == _activity.TypeId)
                        {
                            item = _activity;
                        }

                        if (item != null)
                        {
                            item.SetAttributeValue(attribute.Key, formAction);
                        }
                    }
                }

                if (!activityTypeGuid.IsEmpty())
                {
                    var activityType = _workflowType.ActivityTypes.Where(a => a.Guid.Equals(activityTypeGuid)).FirstOrDefault();
                    if (activityType != null)
                    {
                        WorkflowActivity.Activate(activityType, _workflow);
                    }
                }

                List <string> errorMessages;
                if (_workflowService.Process(_workflow, out errorMessages))
                {
                    int?previousActionId = null;

                    if (_action != null)
                    {
                        previousActionId = _action.Id;
                    }

                    ActionTypeId = null;
                    _action      = null;
                    _actionType  = null;
                    _activity    = null;

                    if (HydrateObjects() && _action != null && _action.Id != previousActionId)
                    {
                        var cb = CurrentPageReference;
                        cb.Parameters.AddOrReplace("WorkflowId", _workflow.Id.ToString());
                        Response.Redirect(cb.BuildUrl(), false);
                        Context.ApplicationInstance.CompleteRequest();
                    }
                    else
                    {
                        if (lSummary.Text.IsNullOrWhiteSpace())
                        {
                            ShowMessage(NotificationBoxType.Success, string.Empty, responseText, (_action == null || _action.Id != previousActionId));
                        }
                        else
                        {
                            pnlForm.Visible = false;
                        }
                    }
                }
                else
                {
                    ShowMessage(NotificationBoxType.Danger, "Workflow Processing Error(s):",
                                "<ul><li>" + errorMessages.AsDelimited("</li><li>", null, true) + "</li></ul>");
                }
                if (_workflow.Id != 0)
                {
                    WorkflowId = _workflow.Id;
                }
            }
        }