Esempio n. 1
0
        private void GetFormValues()
        {
            if (_workflow != null && _actionType != null)
            {
                var form = _actionType.WorkflowForm;

                var values = new Dictionary <int, string>();
                foreach (var formAttribute in form.FormAttributes.OrderBy(a => a.Order))
                {
                    if (formAttribute.IsVisible && !formAttribute.IsReadOnly)
                    {
                        var attribute = AttributeCache.Get(formAttribute.AttributeId);
                        var control   = phAttributes.FindControl(string.Format("attribute_field_{0}", formAttribute.AttributeId));

                        if (attribute != null && control != 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, attribute.FieldType.Field.GetEditValue(attribute.GetControl(control), attribute.QualifierValues));
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
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;
                }
            }
        }