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.Read(formAttribute.AttributeId); var control = phAttributes.FindControl(string.Format("attribute_field_{0}", formAttribute.AttributeId)); if (attribute != null && control != 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, attribute.FieldType.Field.GetEditValue(attribute.GetControl(control), attribute.QualifierValues)); } } } } } }
/// <summary> /// Sets attribute values that have been provided by a remote client. /// </summary> /// <remarks> /// This should be used to handle values the client sent back after /// calling the <see cref="GetClientEditableAttributeValues(IHasAttributes, Person, bool)"/> /// method. It handles conversion from custom data formats into the /// proper values to be stored in the database. /// </remarks> /// <param name="entity">The entity.</param> /// <param name="attributeValues">The attribute values.</param> /// <param name="currentPerson">The current person.</param> /// <param name="enforceSecurity">if set to <c>true</c> then security will be enforced.</param> public static void SetClientAttributeValues(this IHasAttributes entity, Dictionary <string, string> attributeValues, Person currentPerson, bool enforceSecurity = true) { if (entity == null || entity.Attributes == null || entity.AttributeValues == null) { return; } foreach (var kvp in attributeValues) { if (!entity.Attributes.ContainsKey(kvp.Key) || !entity.AttributeValues.ContainsKey(kvp.Key)) { continue; } var attribute = entity.Attributes[kvp.Key]; if (enforceSecurity && !attribute.IsAuthorized(Rock.Security.Authorization.EDIT, currentPerson)) { continue; } var value = ClientAttributeHelper.GetValueFromClient(attribute, kvp.Value); entity.SetAttributeValue(kvp.Key, value); } }
private void SetFormValues(Dictionary <string, string> body) { 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.Read(formAttribute.AttributeId); if (attribute != null && body.ContainsKey(attribute.Key)) { IHasAttributes item = null; if (attribute.EntityTypeId == _workflow.TypeId) { item = _workflow; } else if (attribute.EntityTypeId == _activity.TypeId) { item = _activity; } if (item != null) { var convertedValue = attribute.FieldType.Field.DecodeAttributeValue(attribute, body[attribute.Key]); item.SetAttributeValue(attribute.Key, convertedValue); } } } } } }
/// <summary> /// Update the entity with values from the custom UI. /// </summary> /// <param name="attributeEntity">The attribute entity.</param> /// <param name="rockContext">The rock context to use when accessing the database.</param> /// <remarks> /// Do not save the entity, it will be automatically saved later. This call will be made inside /// a SQL transaction for the passed rockContext. If you need to make changes to the database /// do so on this context so they can be rolled back if something fails during the final save. /// </remarks> public void WriteSettingsToEntity(IHasAttributes attributeEntity, RockContext rockContext) { attributeEntity.SetAttributeValue(AttributeKeys.FieldSettings, this.FieldSettings.ToJson()); attributeEntity.SetAttributeValue(AttributeKeys.ContentChannel, ddlContentChannel.SelectedValue); attributeEntity.SetAttributeValue(AttributeKeys.PageSize, nbPageSize.Text); attributeEntity.SetAttributeValue(AttributeKeys.IncludeFollowing, cbIncludeFollowing.Checked.ToString()); attributeEntity.SetAttributeValue(AttributeKeys.QueryParameterFiltering, cbQueryParamFiltering.Checked.ToString()); attributeEntity.SetAttributeValue(AttributeKeys.ShowChildrenOfParent, cbShowChildrenOfParent.Checked.ToString()); attributeEntity.SetAttributeValue(AttributeKeys.CheckItemSecurity, cbCheckItemSecurity.Checked.ToString()); attributeEntity.SetAttributeValue(AttributeKeys.Order, kvlOrder.Value); string detailPage = string.Empty; if (ppDetailPage.SelectedValueAsId().HasValue) { detailPage = PageCache.Get(ppDetailPage.SelectedValueAsId().Value).Guid.ToString(); } attributeEntity.SetAttributeValue(AttributeKeys.DetailPage, detailPage); attributeEntity.SetAttributeValue(AttributeKeys.FilterId, SaveDataViewFilter(rockContext).Id.ToString()); }
/// <summary> /// Sets a single attribute values that have been provided by a remote client. /// </summary> /// <remarks> /// This should be used to handle values the client sent back after /// calling the <see cref="GetClientEditableAttributeValues(IHasAttributes, Person, bool)"/> /// method. It handles conversion from custom data formats into the /// proper values to be stored in the database. /// </remarks> /// <param name="entity">The entity.</param> /// <param name="key">The attribute key to set.</param> /// <param name="value">The value provided by the remote client.</param> /// <param name="currentPerson">The current person.</param> /// <param name="enforceSecurity">if set to <c>true</c> then security will be enforced.</param> public static void SetClientAttributeValue(this IHasAttributes entity, string key, string value, Person currentPerson, bool enforceSecurity = true) { if (entity == null || entity.Attributes == null || entity.AttributeValues == null) { return; } if (!entity.Attributes.ContainsKey(key) || !entity.AttributeValues.ContainsKey(key)) { return; } var attribute = entity.Attributes[key]; if (enforceSecurity && !attribute.IsAuthorized(Rock.Security.Authorization.EDIT, currentPerson)) { return; } var databaseValue = ClientAttributeHelper.GetValueFromClient(attribute, value); entity.SetAttributeValue(key, databaseValue); }
/// <summary> /// Updates the editable attributes in the given entity. This method should be called /// to update the attributes with the postback data received from a prevoius /// GetEditAttributeXaml() call. /// </summary> /// <param name="entity">The entity whose attribute values will be updated.</param> /// <param name="postbackData">The postback data.</param> /// <param name="attributes">If not null, updating will be limited to these attributes.</param> /// <param name="person">If not null then security will be enforced for this person.</param> public static void UpdateEditAttributeValues(IHasAttributes entity, Dictionary <string, object> postbackData, List <AttributeCache> attributes = null, Person person = null) { if (entity.Attributes == null) { entity.LoadAttributes(); } attributes = attributes ?? entity.Attributes.Values.ToList(); foreach (var attribute in attributes) { if (person != null && !attribute.IsAuthorized(Authorization.EDIT, person)) { continue; } var keyName = $"attribute_{attribute.Id}"; if (postbackData.ContainsKey(keyName)) { entity.SetAttributeValue(attribute.Key, postbackData[keyName].ToStringSafe()); } } }
/// <summary> /// Sets the form values. /// </summary> /// <param name="action">The action.</param> /// <param name="formFields">The form fields.</param> private void SetFormValues(WorkflowAction action, List <MobileField> formFields) { var activity = action.Activity; var workflow = activity.Workflow; var form = action.ActionTypeCache.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 formField = formFields.FirstOrDefault(f => f.AttributeGuid == formAttribute.Attribute.Guid); if (attribute != null && formField != 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, formField.Value); } } } } }
private void CompleteFormAction(string formAction) { if (!string.IsNullOrWhiteSpace(formAction) && _workflow != null && _actionType != null && _actionType.WorkflowForm != null && _activity != null && _action != null) { var mergeFields = Rock.Web.Cache.GlobalAttributesCache.GetMergeFields(null); mergeFields.Add("Action", _action); mergeFields.Add("Activity", _activity); mergeFields.Add("Workflow", _workflow); if (CurrentPerson != null) { mergeFields.Add("CurrentPerson", CurrentPerson); } Guid activityTypeGuid = Guid.Empty; string responseText = "Your information has been submitted succesfully."; 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.ActionType.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 (_workflow.Process(_rockContext, out errorMessages)) { if (_workflow.IsPersisted || _workflowType.IsPersisted) { if (_workflow.Id == 0) { _workflowService.Add(_workflow); } _rockContext.WrapTransaction(() => { _rockContext.SaveChanges(); _workflow.SaveAttributeValues(_rockContext); foreach (var activity in _workflow.Activities) { activity.SaveAttributeValues(_rockContext); } }); WorkflowId = _workflow.Id; } int?previousActionId = null; if (_action != null) { previousActionId = _action.Id; } ActionTypeId = null; _action = null; _actionType = null; _activity = null; if (HydrateObjects() && _action != null && _action.Id != previousActionId) { BuildForm(true); } else { ShowMessage(NotificationBoxType.Success, string.Empty, responseText, (_action == null || _action.Id != previousActionId)); } } else { ShowMessage(NotificationBoxType.Danger, "Workflow Processing Error(s):", "<ul><li>" + errorMessages.AsDelimited("</li><li>") + "</li></ul>"); } } }
/// <summary> /// Completes the form action based on the action selected by the user. /// </summary> /// <param name="action">The action.</param> /// <param name="formAction">The form action.</param> /// <param name="currentPerson">The current person.</param> /// <param name="rockContext">The rock context.</param> /// <returns></returns> private string CompleteFormAction(WorkflowAction action, string formAction, Person currentPerson, RockContext rockContext) { var workflowService = new WorkflowService(rockContext); var activity = action.Activity; var workflow = activity.Workflow; var mergeFields = RequestContext.GetCommonMergeFields(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."; // // Get the target activity type guid and response text from the // submitted form action. // foreach (var act in action.ActionTypeCache.WorkflowForm.Actions.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)) { var actionDetails = act.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(); } // // Set the attribute that should contain the submitted form action. // if (action.ActionTypeCache.WorkflowForm.ActionAttributeGuid.HasValue) { var attribute = AttributeCache.Get(action.ActionTypeCache.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); } } } // // Activate the requested activity if there was one. // if (!activityTypeGuid.IsEmpty()) { var activityType = workflow.WorkflowTypeCache.ActivityTypes.Where(a => a.Guid.Equals(activityTypeGuid)).FirstOrDefault(); if (activityType != null) { WorkflowActivity.Activate(activityType, workflow); } } return(responseText); }
/// <summary> /// Sets the value of an attribute key in memory. Note, this will not persist value to database /// </summary> /// <param name="entity">The entity.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> public static void SetAttributeValue(this IHasAttributes entity, string key, DateTime?value) { entity.SetAttributeValue(key, value?.ToString("o") ?? string.Empty); }
/// <summary> /// Sets the value of an attribute key in memory. Note, this will not persist value to database /// </summary> /// <param name="entity">The entity.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> public static void SetAttributeValue(this IHasAttributes entity, string key, Guid?value) { entity.SetAttributeValue(key, value.ToString()); }
/// <summary> /// Update the entity with values from the custom UI. /// </summary> /// <param name="attributeEntity">The attribute entity.</param> /// <param name="control">The control returned by the GetCustomSettingsControl() method.</param> /// <param name="rockContext">The rock context to use when accessing the database.</param> public override void WriteSettingsToEntity(IHasAttributes attributeEntity, Control control, RockContext rockContext) { var jfBuilder = ( JsonFieldsBuilder )control.Controls[0]; attributeEntity.SetAttributeValue(AttributeKeys.AdditionalFields, jfBuilder.FieldSettings.ToJson()); }
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) { // 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.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; } } }
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 }); }
/// <inheritdoc/> public void WriteSettingsToEntity(IHasAttributes attributeEntity, RockContext rockContext) { attributeEntity.SetAttributeValue(AttributeKey.GroupTypes, rlbGroupTypes.SelectedValues.AsDelimited(",")); attributeEntity.SetAttributeValue(AttributeKey.GroupTypesLocationType, GroupTypeLocations.ToJson()); attributeEntity.SetAttributeValue(AttributeKey.AttributeFilters, cblAttributeFilters.SelectedValues.AsDelimited(",")); }