public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages) { AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext); AttributeMatrixItemService attributeMatrixItemService = new AttributeMatrixItemService(rockContext); errorMessages = new List <string>(); // Get all the attribute values var attributeMatrixGuid = action.GetWorklowAttributeValue(GetActionAttributeValue(action, "AttributeMatrix").AsGuid()).AsGuidOrNull(); var itemGuid = GetActionAttributeValue(action, "ItemGuid").ResolveMergeFields(GetMergeFields(action)).AsGuidOrNull(); if (attributeMatrixGuid.HasValue && itemGuid.HasValue) { // Load the matrix AttributeMatrix matrix = attributeMatrixService.Get(attributeMatrixGuid.Value); AttributeMatrixItem item = matrix.AttributeMatrixItems.Where(i => i.Guid == itemGuid.Value).FirstOrDefault(); if (item != null) { matrix.AttributeMatrixItems.Remove(item); attributeMatrixItemService.Delete(item); } } return(true); }
public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages) { AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext); errorMessages = new List <string>(); // Get all the attribute values var attributeMatrixGuid = action.GetWorklowAttributeValue(GetActionAttributeValue(action, "AttributeMatrix").AsGuid()).AsGuidOrNull(); var itemAttributes = GetActionAttributeValue(action, "ItemAttributes").AsDictionaryOrNull(); if (attributeMatrixGuid.HasValue && itemAttributes != null) { // Load the matrix AttributeMatrix matrix = attributeMatrixService.Get(attributeMatrixGuid.Value); // Create the new item AttributeMatrixItem item = new AttributeMatrixItem(); item.AttributeMatrix = matrix; item.LoadAttributes(); foreach (var attribute in item.Attributes) { if (itemAttributes.ContainsKey(attribute.Key)) { item.SetAttributeValue(attribute.Key, itemAttributes[attribute.Key].ResolveMergeFields(GetMergeFields(action))); } } matrix.AttributeMatrixItems.Add(item); rockContext.SaveChanges(); item.SaveAttributeValues(rockContext); } return(true); }
/// <summary> /// Sets the value. /// </summary> /// <param name="control">The control.</param> /// <param name="configurationValues">The configuration values.</param> /// <param name="value">The value.</param> public override void SetEditValue(Control control, Dictionary <string, ConfigurationValue> configurationValues, string value) { AttributeMatrixEditor attributeMatrixEditor = control as AttributeMatrixEditor; if (attributeMatrixEditor != null) { var rockContext = new RockContext(); AttributeMatrixTemplate attributeMatrixTemplate = null; if (attributeMatrixEditor.AttributeMatrixTemplateId.HasValue) { attributeMatrixTemplate = new AttributeMatrixTemplateService(rockContext).Get(attributeMatrixEditor.AttributeMatrixTemplateId.Value); } if (attributeMatrixTemplate != null) { var attributeMatrixService = new AttributeMatrixService(rockContext); AttributeMatrix attributeMatrix = null; Guid? attributeMatrixGuid = value.AsGuidOrNull(); if (attributeMatrixGuid.HasValue) { attributeMatrix = attributeMatrixService.Get(attributeMatrixGuid.Value); } if (attributeMatrix == null) { // Create the AttributeMatrix now and save it even though they haven't hit save yet. We'll need the AttributeMatrix record to exist so that we can add AttributeMatrixItems to it // If this ends up creating an orphan, we can clean up it up later attributeMatrix = new AttributeMatrix { Guid = Guid.NewGuid() }; attributeMatrix.AttributeMatrixTemplateId = attributeMatrixEditor.AttributeMatrixTemplateId.Value; attributeMatrix.AttributeMatrixItems = new List <AttributeMatrixItem>(); attributeMatrixService.Add(attributeMatrix); rockContext.SaveChanges(); } // If the AttributeMatrixTemplateId jwas changed since the last time the attributeMatrix was saved, change it and wipe out the items if (attributeMatrix.AttributeMatrixTemplateId != attributeMatrixEditor.AttributeMatrixTemplateId.Value) { attributeMatrix.AttributeMatrixTemplateId = attributeMatrixEditor.AttributeMatrixTemplateId.Value; var attributeMatrixItemService = new AttributeMatrixItemService(rockContext); // If the AttributeMatrixTemplateId changed, all the values in the AttributeMatrixItems // are referring to attributes from the old template, so wipe them out. All of them. foreach (var attributeMatrixItem in attributeMatrix.AttributeMatrixItems.ToList()) { attributeMatrixItemService.Delete(attributeMatrixItem); } attributeMatrix.AttributeMatrixItems.Clear(); rockContext.SaveChanges(); } attributeMatrixEditor.AttributeMatrixGuid = attributeMatrix.Guid; } } }
public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages) { AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext); errorMessages = new List <string>(); // Get all the attribute values var attributeGuid = GetActionAttributeValue(action, "AttributeMatrix").AsGuidOrNull(); var attributeMatrixGuid = action.GetWorkflowAttributeValue(attributeGuid.HasValue ? attributeGuid.Value : Guid.Empty).AsGuidOrNull(); var itemAttributes = GetActionAttributeValue(action, "ItemAttributes").AsDictionaryOrNull(); if (attributeGuid.HasValue && itemAttributes != null) { // Load the matrix AttributeMatrix matrix = attributeMatrixService.Get(attributeMatrixGuid.HasValue ? attributeMatrixGuid.Value : Guid.Empty); // If the matrix is null, create it first if (matrix == null) { var attribute = AttributeCache.Get(GetActionAttributeValue(action, "AttributeMatrix").AsGuid()); matrix = new AttributeMatrix(); matrix.AttributeMatrixItems = new List <AttributeMatrixItem>(); matrix.AttributeMatrixTemplateId = attribute.QualifierValues["attributematrixtemplate"]?.Value?.AsInteger() ?? 0; attributeMatrixService.Add(matrix); // Persist it and make sure it gets saved rockContext.SaveChanges(); if (attribute.EntityTypeId == new Rock.Model.Workflow().TypeId) { action.Activity.Workflow.SetAttributeValue(attribute.Key, matrix.Guid.ToString()); } else if (attribute.EntityTypeId == new WorkflowActivity().TypeId) { action.Activity.SetAttributeValue(attribute.Key, matrix.Guid.ToString()); } } // Create the new item AttributeMatrixItem item = new AttributeMatrixItem(); item.AttributeMatrix = matrix; item.LoadAttributes(); foreach (var attribute in item.Attributes) { if (itemAttributes.ContainsKey(attribute.Key)) { item.SetAttributeValue(attribute.Key, itemAttributes[attribute.Key].ResolveMergeFields(GetMergeFields(action))); } } matrix.AttributeMatrixItems.Add(item); rockContext.SaveChanges(); item.SaveAttributeValues(rockContext); } return(true); }
/// <summary> /// Returns the field's current value(s) /// </summary> /// <param name="parentControl">The parent control.</param> /// <param name="value">Information about the value</param> /// <param name="configurationValues">The configuration values.</param> /// <param name="condensed">Flag indicating if the value should be condensed (i.e. for use in a grid column)</param> /// <returns></returns> public override string FormatValue(Control parentControl, string value, Dictionary <string, ConfigurationValue> configurationValues, bool condensed) { using (var rockContext = new RockContext()) { var attributeMatrixService = new AttributeMatrixService(rockContext); AttributeMatrix attributeMatrix = null; Guid? attributeMatrixGuid = value.AsGuidOrNull(); if (attributeMatrixGuid.HasValue) { attributeMatrix = attributeMatrixService.GetNoTracking(attributeMatrixGuid.Value); } if (attributeMatrix != null) { if (configurationValues.ContainsKey(ATTRIBUTE_MATRIX_TEMPLATE)) { // set the AttributeMatrixTemplateId just in case it was changed since the last time the attributeMatrix was saved int attributeMatrixTemplateId = configurationValues[ATTRIBUTE_MATRIX_TEMPLATE].Value.AsInteger(); if (attributeMatrix.AttributeMatrixTemplateId != attributeMatrixTemplateId) { attributeMatrix.AttributeMatrixTemplateId = attributeMatrixTemplateId; attributeMatrix.AttributeMatrixTemplate = new AttributeMatrixTemplateService(rockContext).GetNoTracking(attributeMatrix.AttributeMatrixTemplateId); // If the AttributeMatrixTemplateId changed, all the values in the AttributeMatrixItems // are referring to attributes from the old template, so wipe them out. All of them. attributeMatrix.AttributeMatrixItems.Clear(); } } // make a temp attributeMatrixItem to see what Attributes they have AttributeMatrixItem tempAttributeMatrixItem = new AttributeMatrixItem(); tempAttributeMatrixItem.AttributeMatrix = attributeMatrix; tempAttributeMatrixItem.LoadAttributes(); var lavaTemplate = attributeMatrix.AttributeMatrixTemplate.FormattedLava; Dictionary <string, object> mergeFields = Lava.LavaHelper.GetCommonMergeFields(parentControl?.RockBlock()?.RockPage, null, new Lava.CommonMergeFieldsOptions { GetLegacyGlobalMergeFields = false }); mergeFields.Add("AttributeMatrix", attributeMatrix); mergeFields.Add("ItemAttributes", tempAttributeMatrixItem.Attributes.Select(a => a.Value).OrderBy(a => a.Order).ThenBy(a => a.Name)); mergeFields.Add("AttributeMatrixItems", attributeMatrix.AttributeMatrixItems.OrderBy(a => a.Order)); return(lavaTemplate.ResolveMergeFields(mergeFields)); } } return(base.FormatValue(parentControl, value, configurationValues, condensed)); }
public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages) { AttributeMatrixService attributeMatrixService = new AttributeMatrixService(rockContext); errorMessages = new List <string>(); // Get all the attribute values var sourceMatrixAttributeGuid = action.GetWorkflowAttributeValue(GetActionAttributeValue(action, "SourceAttributeMatrix").AsGuid()).AsGuidOrNull(); var targetMatrixAttributeGuid = action.GetWorkflowAttributeValue(GetActionAttributeValue(action, "TargetAttributeMatrix").AsGuid()).AsGuidOrNull(); if (sourceMatrixAttributeGuid.HasValue) { // Load the source matrix AttributeMatrix sourceMatrix = attributeMatrixService.Get(sourceMatrixAttributeGuid.Value); AttributeMatrix targetMatrix = null; if (targetMatrixAttributeGuid.HasValue) { // Just delete all the existing items and add new items from the source attribute targetMatrix = attributeMatrixService.Get(targetMatrixAttributeGuid.Value); targetMatrix.AttributeMatrixItems.Clear(); } else { targetMatrix = new AttributeMatrix(); targetMatrix.AttributeMatrixItems = new List <AttributeMatrixItem>(); targetMatrix.AttributeMatrixTemplateId = sourceMatrix.AttributeMatrixTemplateId; attributeMatrixService.Add(targetMatrix); } // Now copy all the items from the source to the target foreach (var sourceItem in sourceMatrix.AttributeMatrixItems) { var targetItem = new AttributeMatrixItem(); sourceItem.LoadAttributes(); targetItem.AttributeMatrix = targetMatrix; targetItem.LoadAttributes(); foreach (var attribute in sourceItem.AttributeValues) { targetItem.SetAttributeValue(attribute.Key, attribute.Value.Value); } targetMatrix.AttributeMatrixItems.Add(targetItem); rockContext.SaveChanges(true); targetItem.SaveAttributeValues(rockContext); } // Now store the target attribute var targetAttribute = AttributeCache.Get(GetActionAttributeValue(action, "TargetAttributeMatrix").AsGuid(), rockContext); if (targetAttribute.EntityTypeId == new Rock.Model.Workflow().TypeId) { action.Activity.Workflow.SetAttributeValue(targetAttribute.Key, targetMatrix.Guid.ToString()); } else if (targetAttribute.EntityTypeId == new WorkflowActivity().TypeId) { action.Activity.SetAttributeValue(targetAttribute.Key, targetMatrix.Guid.ToString()); } } return(true); }