Example #1
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="value">The value.</param>
        /// <param name="personId">The person id.</param>
        public static void SaveAttributeValue(IHasAttributes model, Rock.Web.Cache.Attribute attribute, string newValue, int?personId)
        {
            Core.AttributeValueService attributeValueService = new Core.AttributeValueService();

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

            if (attributeValue == null)
            {
                attributeValue             = new Rock.Core.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 KeyValuePair <string, List <Rock.Core.DTO.AttributeValue> >(
                    attribute.Name,
                    new List <Rock.Core.DTO.AttributeValue>()
            {
                attributeValue.DataTransferObject
            });
        }
Example #2
0
        /// <summary>
        /// Saves an attribute value.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="value">The value.</param>
        /// <param name="personId">The person id.</param>
        public static void SaveAttributeValues(IHasAttributes model, Rock.Web.Cache.Attribute attribute, List <Rock.Core.DTO.AttributeValue> newValues, int?personId)
        {
            Core.AttributeValueService attributeValueService = new Core.AttributeValueService();

            var attributeValues = attributeValueService.GetByAttributeIdAndEntityId(attribute.Id, model.Id).ToList();
            int i = 0;

            while (i < attributeValues.Count || i < newValues.Count)
            {
                Rock.Core.AttributeValue attributeValue;

                if (i < attributeValues.Count)
                {
                    attributeValue = attributeValues[i];
                }
                else
                {
                    attributeValue             = new Rock.Core.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] = attributeValue.DataTransferObject;
                }

                attributeValueService.Save(attributeValue, personId);


                i++;
            }

            model.AttributeValues[attribute.Key] =
                new KeyValuePair <string, List <Rock.Core.DTO.AttributeValue> >(attribute.Name, newValues);
        }
Example #3
0
        /// <summary>
        /// Loads the <see cref="P:IHasAttributes.Attributes"/> and <see cref="P:IHasAttributes.AttributeValues"/> of any <see cref="IHasAttributes"/> object
        /// </summary>
        /// <param name="item">The item.</param>
        public static void LoadAttributes(Rock.Attribute.IHasAttributes item)
        {
            var attributes      = new SortedDictionary <string, List <Web.Cache.Attribute> >();
            var attributeValues = new Dictionary <string, KeyValuePair <string, List <Rock.Core.DTO.AttributeValue> > >();

            Dictionary <string, PropertyInfo> properties = new Dictionary <string, PropertyInfo>();

            Type entityType = item.GetType();

            if (entityType.Namespace == "System.Data.Entity.DynamicProxies")
            {
                entityType = entityType.BaseType;
            }

            foreach (PropertyInfo propertyInfo in entityType.GetProperties())
            {
                properties.Add(propertyInfo.Name.ToLower(), propertyInfo);
            }

            Rock.Core.AttributeService attributeService = new Rock.Core.AttributeService();

            foreach (Rock.Core.Attribute attribute in attributeService.GetByEntity(entityType.FullName))
            {
                if (string.IsNullOrEmpty(attribute.EntityQualifierColumn) ||
                    (properties.ContainsKey(attribute.EntityQualifierColumn.ToLower()) &&
                     (string.IsNullOrEmpty(attribute.EntityQualifierValue) ||
                      properties[attribute.EntityQualifierColumn.ToLower()].GetValue(item, null).ToString() == attribute.EntityQualifierValue)))
                {
                    Rock.Web.Cache.Attribute cachedAttribute = Rock.Web.Cache.Attribute.Read(attribute);

                    if (!attributes.ContainsKey(cachedAttribute.Category))
                    {
                        attributes.Add(cachedAttribute.Category, new List <Web.Cache.Attribute>());
                    }

                    attributes[cachedAttribute.Category].Add(cachedAttribute);
                    attributeValues.Add(attribute.Key, new KeyValuePair <string, List <Rock.Core.DTO.AttributeValue> >(attribute.Name, attribute.GetValues(item.Id)));
                }
            }

            item.Attributes      = attributes;
            item.AttributeValues = attributeValues;
        }
 public AttributeInstanceValues( Rock.Attribute.IHasAttributes model, Rock.Web.Cache.Attribute attribute, int? currentPersonId )
 {
     _model = model;
     _attribute = attribute;
     _currentPersonId = currentPersonId;
 }
 public AttributeInstanceValues(Rock.Attribute.IHasAttributes model, Rock.Web.Cache.Attribute attribute, int?currentPersonId)
 {
     _model           = model;
     _attribute       = attribute;
     _currentPersonId = currentPersonId;
 }