/// <summary> /// Saves an attribute value. /// </summary> /// <param name="model">The model.</param> /// <param name="attribute">The attribute.</param> /// <param name="newValues">The new values.</param> /// <param name="personId">The person id.</param> public static void SaveAttributeValues(IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, List <Rock.Model.AttributeValueDto> newValues, int?personId) { Model.AttributeValueService attributeValueService = new Model.AttributeValueService(); var attributeValues = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, model.Id).ToList(); int i = 0; while (i < attributeValues.Count || i < newValues.Count) { Rock.Model.AttributeValue attributeValue; if (i < attributeValues.Count) { attributeValue = attributeValues[i]; } else { attributeValue = new Rock.Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = model.Id; attributeValue.Order = i; attributeValueService.Add(attributeValue, personId); } if (i >= newValues.Count) { attributeValueService.Delete(attributeValue, personId); } else { if (attributeValue.Value != newValues[i].Value) { attributeValue.Value = newValues[i].Value; } newValues[i] = new Rock.Model.AttributeValueDto(attributeValue); } attributeValueService.Save(attributeValue, personId); i++; } model.AttributeValues[attribute.Key] = newValues; }
/// <summary> /// Gets the edit values. /// </summary> /// <param name="parentControl">The parent control.</param> /// <param name="item">The item.</param> public static void GetEditValues(Control parentControl, IHasAttributes item) { if (item.Attributes != null) { foreach (var attribute in item.Attributes) { Control control = parentControl.FindControl(string.Format("attribute_field_{0}", attribute.Value.Id.ToString())); if (control != null) { var value = new Rock.Model.AttributeValueDto(); value.Value = attribute.Value.FieldType.Field.GetEditValue(control, attribute.Value.QualifierValues); item.AttributeValues[attribute.Key] = new List <Rock.Model.AttributeValueDto>() { value }; } } } }
/// <summary> /// Loads the <see cref="P:IHasAttributes.Attributes"/> and <see cref="P:IHasAttributes.AttributeValues"/> of any <see cref="IHasAttributes"/> object /// </summary> /// <param name="entity">The item.</param> public static void LoadAttributes(Rock.Attribute.IHasAttributes entity) { var attributes = new Dictionary <int, Rock.Web.Cache.AttributeCache>(); Dictionary <string, PropertyInfo> properties = new Dictionary <string, PropertyInfo>(); Type entityType = entity.GetType(); if (entityType.Namespace == "System.Data.Entity.DynamicProxies") { entityType = entityType.BaseType; } foreach (PropertyInfo propertyInfo in entityType.GetProperties()) { properties.Add(propertyInfo.Name.ToLower(), propertyInfo); } Rock.Model.AttributeService attributeService = new Rock.Model.AttributeService(); Rock.Model.AttributeValueService attributeValueService = new Rock.Model.AttributeValueService(); // Get all the attributes that apply to this entity type and this entity's properties match any attribute qualifiers int?entityTypeId = Rock.Web.Cache.EntityTypeCache.Read(entityType.FullName).Id; foreach (Rock.Model.Attribute attribute in attributeService.GetByEntityTypeId(entityTypeId)) { if (string.IsNullOrEmpty(attribute.EntityTypeQualifierColumn) || (properties.ContainsKey(attribute.EntityTypeQualifierColumn.ToLower()) && (string.IsNullOrEmpty(attribute.EntityTypeQualifierValue) || properties[attribute.EntityTypeQualifierColumn.ToLower()].GetValue(entity, null).ToString() == attribute.EntityTypeQualifierValue))) { attributes.Add(attribute.Id, Rock.Web.Cache.AttributeCache.Read(attribute)); } } var attributeCategories = new SortedDictionary <string, List <string> >(); var attributeValues = new Dictionary <string, List <Rock.Model.AttributeValueDto> >(); foreach (var attribute in attributes) { // Categorize the attributes if (!attributeCategories.ContainsKey(attribute.Value.Category)) { attributeCategories.Add(attribute.Value.Category, new List <string>()); } attributeCategories[attribute.Value.Category].Add(attribute.Value.Key); // Add a placeholder for this item's value for each attribute attributeValues.Add(attribute.Value.Key, new List <Rock.Model.AttributeValueDto>()); } // Read this item's value(s) for each attribute foreach (dynamic item in attributeValueService.Queryable() .Where(v => v.EntityId == entity.Id && attributes.Keys.Contains(v.AttributeId)) .Select(v => new { Value = v, Key = v.Attribute.Key })) { attributeValues[item.Key].Add(new Rock.Model.AttributeValueDto(item.Value)); } // Look for any attributes that don't have a value and create a default value entry foreach (var attributeEntry in attributes) { if (attributeValues[attributeEntry.Value.Key].Count == 0) { var attributeValue = new Rock.Model.AttributeValueDto(); attributeValue.AttributeId = attributeEntry.Value.Id; attributeValue.Value = attributeEntry.Value.DefaultValue; attributeValues[attributeEntry.Value.Key].Add(attributeValue); } else { if (!String.IsNullOrWhiteSpace(attributeEntry.Value.DefaultValue)) { foreach (var value in attributeValues[attributeEntry.Value.Key]) { if (String.IsNullOrWhiteSpace(value.Value)) { value.Value = attributeEntry.Value.DefaultValue; } } } } } entity.AttributeCategories = attributeCategories; entity.Attributes = new Dictionary <string, Web.Cache.AttributeCache>(); attributes.Values.ToList().ForEach(a => entity.Attributes.Add(a.Key, a)); entity.AttributeValues = attributeValues; }