Exemple #1
0
        /// <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);
            }
        }
Exemple #3
0
        /// <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>
        /// 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 );
            }
        }
Exemple #5
0
        /// <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="rockContext">The rock context.</param>
        /// <remarks>
        /// If a rockContext value is included, this method will save any previous changes made to the context
        /// </remarks>
        public static void SaveAttributeValues( IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, List<Rock.Model.AttributeValue> newValues, RockContext rockContext = null )
        {
            rockContext = rockContext ?? new RockContext();
            var attributeValueService = new Model.AttributeValueService( rockContext );

            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 );
                }

                if ( i >= newValues.Count )
                    attributeValueService.Delete( attributeValue );
                else
                {
                    if ( attributeValue.Value != newValues[i].Value )
                        attributeValue.Value = newValues[i].Value;
                    newValues[i] = attributeValue.Clone( false ) as Rock.Model.AttributeValue;
                }

                i++;
            }

            rockContext.SaveChanges();

            model.AttributeValues[attribute.Key] = newValues;
        }
        /// <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();
                    }
                }
            }
        }
        /// <summary>
        /// Saves a <see cref="Rock.Model.Person">Person's</see> user preference setting by key and SavesChanges()
        /// </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>
        /// Deletes a <see cref="Rock.Model.Person">Person's</see> user preference setting by key and SavesChanges()
        /// </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>
        public static void DeleteUserPreference( Person person, string key )
        {
            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 attributeValueService = new Model.AttributeValueService( rockContext );
                    var attributeValue = attributeValueService.GetByAttributeIdAndEntityId( attribute.Id, person.Id );
                    if ( attributeValue != null )
                    {
                        attributeValueService.Delete( attributeValue );
                    }

                    attributeService.Delete( attribute );
                    rockContext.SaveChanges();
                }
            }
        }