Exemplo n.º 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="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);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="newValue">The new value.</param>
        /// <param name="personId">The person id.</param>
        public static void SaveAttributeValue(IHasAttributes model, Rock.Web.Cache.AttributeCache attribute, string newValue, int?personId)
        {
            Model.AttributeValueService attributeValueService = new Model.AttributeValueService();

            var attributeValue = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, model.Id).FirstOrDefault();

            if (attributeValue == null)
            {
                if (newValue == null)
                {
                    return;
                }

                attributeValue             = new Rock.Model.AttributeValue();
                attributeValue.AttributeId = attribute.Id;
                attributeValue.EntityId    = model.Id;
                attributeValue.Order       = 0;
                attributeValueService.Add(attributeValue, personId);
            }

            attributeValue.Value = newValue;

            attributeValueService.Save(attributeValue, personId);

            model.AttributeValues[attribute.Key] = new List <Rock.Model.AttributeValue>()
            {
                attributeValue.Clone() as Rock.Model.AttributeValue
            };
        }
Exemplo n.º 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;
        }
Exemplo n.º 4
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="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 );
            }
        }