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); }
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); }