/// <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; } } }
/// <summary> /// Creates the control(s) necessary for prompting user for a new value /// </summary> /// <param name="configurationValues">The configuration values.</param> /// <param name="id">The id.</param> /// <returns> /// The control /// </returns> public override Control EditControl(Dictionary <string, ConfigurationValue> configurationValues, string id) { if (!configurationValues.ContainsKey(ATTRIBUTE_MATRIX_TEMPLATE)) { return(null); } AttributeMatrixEditor attributeMatrixEditor = new AttributeMatrixEditor { ID = id }; attributeMatrixEditor.AttributeMatrixTemplateId = configurationValues[ATTRIBUTE_MATRIX_TEMPLATE].Value.AsInteger(); return(attributeMatrixEditor); }
/// <summary> /// Reads new values entered by the user for the field /// </summary> /// <param name="control">Parent control that controls were added to in the CreateEditControl() method</param> /// <param name="configurationValues">The configuration values.</param> /// <returns></returns> public override string GetEditValue(Control control, Dictionary <string, ConfigurationValue> configurationValues) { AttributeMatrixEditor attributeMatrixEditor = control as AttributeMatrixEditor; if (attributeMatrixEditor != null && configurationValues.ContainsKey(ATTRIBUTE_MATRIX_TEMPLATE)) { int?attributeMatrixTemplateId = configurationValues[ATTRIBUTE_MATRIX_TEMPLATE]?.Value.AsIntegerOrNull(); if (attributeMatrixTemplateId.HasValue) { if (attributeMatrixEditor.AttributeMatrixGuid.HasValue) { var rockContext = new RockContext(); var attributeMatrix = new AttributeMatrixService(rockContext).Get(attributeMatrixEditor.AttributeMatrixGuid.Value); return(attributeMatrix.Guid.ToString()); } } } return(null); }