/// <summary> /// Saves a <see cref="Rock.Model.Person">Person's</see> user preference setting by key. /// </summary> /// <param name="person">The <see cref="Rock.Model.Person"/> who the preference value belongs to.</param> /// <param name="key">A <see cref="System.String"/> representing the key (name) of the preference setting.</param> /// <param name="value">The value.</param> public static void SaveUserPreference(Person person, string key, string value) { int?PersonEntityTypeId = Rock.Web.Cache.EntityTypeCache.Read(Person.USER_VALUE_ENTITY).Id; using (var rockContext = new RockContext()) { var attributeService = new Model.AttributeService(rockContext); var attribute = attributeService.Get(PersonEntityTypeId, string.Empty, string.Empty, key); if (attribute == null) { var fieldTypeService = new Model.FieldTypeService(rockContext); var fieldType = FieldTypeCache.Read(Rock.SystemGuid.FieldType.TEXT.AsGuid()); attribute = new Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = PersonEntityTypeId; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = key; attribute.Name = key; attribute.IconCssClass = string.Empty; attribute.DefaultValue = string.Empty; attribute.IsMultiValue = false; attribute.IsRequired = false; attribute.Description = string.Empty; attribute.FieldTypeId = fieldType.Id; attribute.Order = 0; attributeService.Add(attribute); rockContext.SaveChanges(); } var attributeValueService = new Model.AttributeValueService(rockContext); var attributeValue = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, person.Id); if (string.IsNullOrWhiteSpace(value)) { // Delete existing value if no existing value if (attributeValue != null) { attributeValueService.Delete(attributeValue); } } else { if (attributeValue == null) { attributeValue = new Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = person.Id; attributeValueService.Add(attributeValue); } attributeValue.Value = value; } rockContext.SaveChanges(); } }
/// <summary> /// Saves a <see cref="Rock.Model.Person">Person's</see> user preference setting by key. /// </summary> /// <param name="person">The <see cref="Rock.Model.Person"/> who the preference value belongs to.</param> /// <param name="key">A <see cref="System.String"/> representing the key (name) of the preference setting. </param> /// <param name="values">A list of <see cref="System.String"/> values representing the value of the preference setting.</param> /// <param name="personId">A <see cref="System.Int32"/> representing the Id of the <see cref="Rock.Model.Person"/> saving the setting.</param> public void SaveUserPreference(Person person, string key, List <string> values, int?personId) { int?PersonEntityTypeId = Rock.Web.Cache.EntityTypeCache.Read(Person.USER_VALUE_ENTITY).Id; var attributeService = new Model.AttributeService(); var attribute = attributeService.Get(PersonEntityTypeId, string.Empty, string.Empty, key); if (attribute == null) { var fieldTypeService = new Model.FieldTypeService(); var fieldType = fieldTypeService.GetByGuid(new Guid(Rock.SystemGuid.FieldType.TEXT)); attribute = new Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = PersonEntityTypeId; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = key; attribute.Name = key; attribute.DefaultValue = string.Empty; attribute.IsMultiValue = false; attribute.IsRequired = false; attribute.Description = string.Empty; attribute.FieldTypeId = fieldType.Id; attribute.Order = 0; attributeService.Add(attribute, personId); attributeService.Save(attribute, personId); } var attributeValueService = new Model.AttributeValueService(); // Delete existing values var attributeValues = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, person.Id).ToList(); foreach (var attributeValue in attributeValues) { attributeValueService.Delete(attributeValue, personId); attributeValueService.Save(attributeValue, personId); } // Save new values foreach (var value in values.Where(v => !string.IsNullOrWhiteSpace(v))) { var attributeValue = new Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = person.Id; attributeValue.Value = value; attributeValueService.Add(attributeValue, personId); attributeValueService.Save(attributeValue, personId); } }
/// <summary> /// Copies the attributes from one entity to another /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> public static void CopyAttributes(IHasAttributes source, IHasAttributes target) { if (source != null && target != null) { // Copy Attributes if (source.Attributes != null) { target.Attributes = new Dictionary <string, Web.Cache.AttributeCache>(); foreach (var item in source.Attributes) { target.Attributes.Add(item.Key, item.Value); } } else { target.Attributes = null; } // Copy Attribute Values if (source.AttributeValues != null) { target.AttributeValues = new Dictionary <string, Model.AttributeValue>(); foreach (var item in source.AttributeValues) { var value = item.Value; if (value != null) { var attributeValue = new Model.AttributeValue(); attributeValue.IsSystem = value.IsSystem; attributeValue.AttributeId = value.AttributeId; attributeValue.EntityId = value.EntityId; attributeValue.Value = value.Value; attributeValue.Id = value.Id; attributeValue.Guid = value.Guid; target.AttributeValues.Add(item.Key, attributeValue); } else { target.AttributeValues.Add(item.Key, null); } } } else { target.AttributeValues = null; } } }
/// <summary> /// Copies the attributes from one entity to another /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> public static void CopyAttributes(IHasAttributes source, IHasAttributes target) { // Copy Attributes if (source.Attributes != null) { target.Attributes = new Dictionary <string, Web.Cache.AttributeCache>(); foreach (var item in source.Attributes) { target.Attributes.Add(item.Key, item.Value); } } else { target.Attributes = null; } // Copy Attribute Values if (source.AttributeValues != null) { target.AttributeValues = new Dictionary <string, List <Model.AttributeValue> >(); foreach (var item in source.AttributeValues) { var list = new List <Model.AttributeValue>(); foreach (var value in item.Value) { var attributeValue = new Model.AttributeValue(); attributeValue.IsSystem = value.IsSystem; attributeValue.AttributeId = value.AttributeId; attributeValue.EntityId = value.EntityId; attributeValue.Order = value.Order; attributeValue.Value = value.Value; attributeValue.Id = value.Id; attributeValue.Guid = value.Guid; list.Add(attributeValue); } target.AttributeValues.Add(item.Key, list); } } else { target.AttributeValues = null; } }
/// <summary> /// Copies the attributes from one entity to another /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> public static void CopyAttributes( IHasAttributes source, IHasAttributes target ) { if ( source != null && target != null ) { // Copy Attributes if ( source.Attributes != null ) { target.Attributes = new Dictionary<string, Web.Cache.AttributeCache>(); foreach ( var item in source.Attributes ) { target.Attributes.Add( item.Key, item.Value ); } } else { target.Attributes = null; } // Copy Attribute Values if ( source.AttributeValues != null ) { target.AttributeValues = new Dictionary<string, Model.AttributeValue>(); foreach ( var item in source.AttributeValues ) { var value = item.Value; if ( value != null ) { var attributeValue = new Model.AttributeValue(); attributeValue.IsSystem = value.IsSystem; attributeValue.AttributeId = value.AttributeId; attributeValue.EntityId = value.EntityId; attributeValue.Value = value.Value; attributeValue.Id = value.Id; attributeValue.Guid = value.Guid; target.AttributeValues.Add( item.Key, attributeValue ); } else { target.AttributeValues.Add( item.Key, null ); } } } else { target.AttributeValues = null; } } }
/// <summary> /// Saves a <see cref="Rock.Model.Person">Person's</see> user preference setting by key. /// </summary> /// <param name="person">The <see cref="Rock.Model.Person"/> who the preference value belongs to.</param> /// <param name="key">A <see cref="System.String"/> representing the key (name) of the preference setting. </param> /// <param name="values">A list of <see cref="System.String"/> values representing the value of the preference setting.</param> /// <param name="personId">A <see cref="System.Int32"/> representing the Id of the <see cref="Rock.Model.Person"/> saving the setting.</param> public void SaveUserPreference(Person person, string key, List<string> values, int? personId) { int? PersonEntityTypeId = Rock.Web.Cache.EntityTypeCache.Read( Person.USER_VALUE_ENTITY ).Id; var attributeService = new Model.AttributeService(); var attribute = attributeService.Get( PersonEntityTypeId, string.Empty, string.Empty, key ); if ( attribute == null ) { var fieldTypeService = new Model.FieldTypeService(); var fieldType = fieldTypeService.GetByGuid( new Guid( Rock.SystemGuid.FieldType.TEXT ) ); attribute = new Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = PersonEntityTypeId; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = key; attribute.Name = key; attribute.DefaultValue = string.Empty; attribute.IsMultiValue = false; attribute.IsRequired = false; attribute.Description = string.Empty; attribute.FieldTypeId = fieldType.Id; attribute.Order = 0; attributeService.Add( attribute, personId ); attributeService.Save( attribute, personId ); } var attributeValueService = new Model.AttributeValueService(); // Delete existing values var attributeValues = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, person.Id ).ToList(); foreach ( var attributeValue in attributeValues ) { attributeValueService.Delete( attributeValue, personId ); attributeValueService.Save( attributeValue, personId ); } // Save new values foreach ( var value in values.Where( v => !string.IsNullOrWhiteSpace( v ) ) ) { var attributeValue = new Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = person.Id; attributeValue.Value = value; attributeValueService.Add( attributeValue, personId ); attributeValueService.Save( attributeValue, personId ); } }
/// <summary> /// Saves a <see cref="Rock.Model.Person">Person's</see> user preference setting by key. /// </summary> /// <param name="person">The <see cref="Rock.Model.Person"/> who the preference value belongs to.</param> /// <param name="key">A <see cref="System.String"/> representing the key (name) of the preference setting.</param> /// <param name="value">The value.</param> public static void SaveUserPreference( Person person, string key, string value ) { int? PersonEntityTypeId = Rock.Web.Cache.EntityTypeCache.Read( Person.USER_VALUE_ENTITY ).Id; using ( var rockContext = new RockContext() ) { var attributeService = new Model.AttributeService( rockContext ); var attribute = attributeService.Get( PersonEntityTypeId, string.Empty, string.Empty, key ); if ( attribute == null ) { var fieldTypeService = new Model.FieldTypeService( rockContext ); var fieldType = FieldTypeCache.Read( Rock.SystemGuid.FieldType.TEXT.AsGuid() ); attribute = new Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = PersonEntityTypeId; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = key; attribute.Name = key; attribute.IconCssClass = string.Empty; attribute.DefaultValue = string.Empty; attribute.IsMultiValue = false; attribute.IsRequired = false; attribute.Description = string.Empty; attribute.FieldTypeId = fieldType.Id; attribute.Order = 0; attributeService.Add( attribute ); rockContext.SaveChanges(); } var attributeValueService = new Model.AttributeValueService( rockContext ); var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, person.Id ); if ( string.IsNullOrWhiteSpace( value ) ) { // Delete existing value if no existing value if ( attributeValue != null ) { attributeValueService.Delete( attributeValue ); } } else { if ( attributeValue == null ) { attributeValue = new Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = person.Id; attributeValueService.Add( attributeValue ); } attributeValue.Value = value; } rockContext.SaveChanges(); } }
/// <summary> /// Copies the attributes from one entity to another /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> public static void CopyAttributes( IHasAttributes source, IHasAttributes target ) { // Copy Attributes if ( source.Attributes != null ) { target.Attributes = new Dictionary<string, Web.Cache.AttributeCache>(); foreach ( var item in source.Attributes ) { target.Attributes.Add( item.Key, item.Value ); } } else { target.Attributes = null; } // Copy Attribute Values if ( source.AttributeValues != null ) { target.AttributeValues = new Dictionary<string, List<Model.AttributeValue>>(); foreach ( var item in source.AttributeValues ) { var list = new List<Model.AttributeValue>(); foreach ( var value in item.Value ) { var attributeValue = new Model.AttributeValue(); attributeValue.IsSystem = value.IsSystem; attributeValue.AttributeId = value.AttributeId; attributeValue.EntityId = value.EntityId; attributeValue.Order = value.Order; attributeValue.Value = value.Value; attributeValue.Id = value.Id; attributeValue.Guid = value.Guid; list.Add( attributeValue ); } target.AttributeValues.Add( item.Key, list ); } } else { target.AttributeValues = null; } }
/// <summary> /// Saves the user preferences. /// </summary> /// <param name="person">The person.</param> /// <param name="preferences">The preferences.</param> public static void SaveUserPreferences( Person person, Dictionary<string, string> preferences ) { if ( preferences != null ) { int? personEntityTypeId = Rock.Web.Cache.EntityTypeCache.Read( Person.USER_VALUE_ENTITY ).Id; using ( var rockContext = new RockContext() ) { var attributeService = new Model.AttributeService( rockContext ); var attributes = attributeService .Get( personEntityTypeId, string.Empty, string.Empty ) .Where( a => preferences.Keys.Contains( a.Key ) ) .ToList(); bool wasUpdated = false; foreach ( var attributeKeyValue in preferences ) { var attribute = attributes.FirstOrDefault( a => a.Key == attributeKeyValue.Key ); if ( attribute == null ) { var fieldTypeService = new Model.FieldTypeService( rockContext ); var fieldType = FieldTypeCache.Read( Rock.SystemGuid.FieldType.TEXT.AsGuid() ); attribute = new Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = personEntityTypeId; attribute.EntityTypeQualifierColumn = string.Empty; attribute.EntityTypeQualifierValue = string.Empty; attribute.Key = attributeKeyValue.Key; attribute.Name = attributeKeyValue.Key; attribute.IconCssClass = string.Empty; attribute.DefaultValue = string.Empty; attribute.IsMultiValue = false; attribute.IsRequired = false; attribute.Description = string.Empty; attribute.FieldTypeId = fieldType.Id; attribute.Order = 0; attributeService.Add( attribute ); wasUpdated = true; } } if ( wasUpdated ) { // Save any new attributes rockContext.SaveChanges(); // Requery attributes ( so they all have ids ) attributes = attributeService .Get( personEntityTypeId, string.Empty, string.Empty ) .Where( a => preferences.Keys.Contains( a.Key ) ) .ToList(); } var attributeIds = attributes.Select( a => a.Id ).ToList(); var attributeValueService = new Model.AttributeValueService( rockContext ); var attributeValues = attributeValueService.Queryable( "Attribute" ) .Where( v => attributeIds.Contains( v.AttributeId ) && v.EntityId.HasValue && v.EntityId.Value == person.Id ) .ToList(); wasUpdated = false; foreach ( var attributeKeyValue in preferences ) { if ( string.IsNullOrWhiteSpace( attributeKeyValue.Value ) ) { foreach ( var attributeValue in attributeValues .Where( v => v.Attribute != null && v.Attribute.Key == attributeKeyValue.Key ) .ToList() ) { attributeValueService.Delete( attributeValue ); attributeValues.Remove( attributeValue ); wasUpdated = true; } } else { var attributeValue = attributeValues .Where( v => v.Attribute != null && v.Attribute.Key == attributeKeyValue.Key ) .FirstOrDefault(); if ( attributeValue == null ) { var attribute = attributes .Where( a => a.Key == attributeKeyValue.Key ) .FirstOrDefault(); if ( attribute != null ) { attributeValue = new Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; attributeValue.EntityId = person.Id; attributeValueService.Add( attributeValue ); } } wasUpdated = wasUpdated || ( attributeValue.Value != attributeKeyValue.Value ); attributeValue.Value = attributeKeyValue.Value; } } if ( wasUpdated ) { rockContext.SaveChanges(); } } } }