/// <summary> /// Saves an attribute value to the database. /// </summary> /// <param name="entity">The entity.</param> /// <param name="key">The attribute key.</param> /// <param name="rockContext">The rock context.</param> public static void SaveAttributeValue(this Rock.Attribute.IHasAttributes entity, string key, RockContext rockContext = null) { if (entity.AttributeValues.ContainsKey(key)) { Rock.Attribute.Helper.SaveAttributeValue(entity, entity.Attributes[key], entity.AttributeValues[key].Value, rockContext); } }
/// <summary> /// Initializes a new instance of the <see cref="ComponentDescription"/> class. /// </summary> /// <param name="id">The id.</param> /// <param name="service">The service.</param> public ComponentDescription(int id, Rock.Attribute.IHasAttributes service) { Id = id; Type type = service.GetType(); Name = type.Name; // Look for a DescriptionAttribute on the class and if found, use its value for the description // property of this class var descAttributes = type.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); if (descAttributes != null) { foreach (System.ComponentModel.DescriptionAttribute descAttribute in descAttributes) { Description = descAttribute.Description; } } // If the class has an PropertyAttribute with 'Active' as the key get it's value for the property // otherwise default to true if (service.AttributeValues.ContainsKey("Active")) { Active = bool.Parse(service.AttributeValues["Active"].Value[0].Value); } else { Active = true; } }
/// <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) { using (var rockContext = new RockContext()) { LoadAttributes(entity, rockContext); } }
/// <summary> /// Saves the specified attribute values to the database. /// </summary> /// <param name="entity">The entity.</param> /// <param name="keys">The attribute keys.</param> /// <param name="rockContext">The rock context.</param> public static void SaveAttributeValues(this Rock.Attribute.IHasAttributes entity, IEnumerable <string> keys, RockContext rockContext = null) { foreach (var key in keys) { if (entity.AttributeValues.ContainsKey(key)) { Rock.Attribute.Helper.SaveAttributeValue(entity, entity.Attributes[key], entity.AttributeValues[key].Value, rockContext); } } }
/// <summary> /// Sets the value of an attribute key in memory. Note, this will not persist value to database /// </summary> /// <param name="entity">The entity.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> public static void SetAttributeValue(this Rock.Attribute.IHasAttributes entity, string key, DateTime?value) { if (value.HasValue) { entity.SetAttributeValue(key, value.Value.ToString("o")); } else { entity.SetAttributeValue(key, string.Empty); } }
/// <summary> /// Handles the Click event of the btnSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { Rock.Attribute.IHasAttributes component = _container.Dictionary[Int32.Parse(hfComponentId.Value)].Value; Rock.Attribute.Helper.GetEditValues(dlProperties, component); Rock.Attribute.Helper.SaveAttributeValues(component, CurrentPersonId); hfComponentId.Value = string.Empty; BindGrid(); pnlDetails.Visible = false; }
public virtual HttpResponseMessage SetAttributeValue(int id, string attributeKey, string attributeValue) { T model; if (!Service.TryGet(id, out model)) { throw new HttpResponseException(HttpStatusCode.NotFound); } CheckCanEdit(model); Rock.Attribute.IHasAttributes modelWithAttributes = model as Rock.Attribute.IHasAttributes; if (modelWithAttributes != null) { using (var rockContext = new RockContext()) { modelWithAttributes.LoadAttributes(rockContext); Rock.Web.Cache.AttributeCache attributeCache = modelWithAttributes.Attributes.ContainsKey(attributeKey) ? modelWithAttributes.Attributes[attributeKey] : null; if (attributeCache != null) { if (!attributeCache.IsAuthorized(Rock.Security.Authorization.EDIT, this.GetPerson())) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden) { ReasonPhrase = string.Format("Not authorized to edit {0} on {1}", modelWithAttributes.GetType().GetFriendlyTypeName(), attributeKey) }); } Rock.Attribute.Helper.SaveAttributeValue(modelWithAttributes, attributeCache, attributeValue, rockContext); var response = ControllerContext.Request.CreateResponse(HttpStatusCode.Accepted, modelWithAttributes.Id); return(response); } else { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = string.Format("{0} does not have a {1} attribute", modelWithAttributes.GetType().GetFriendlyTypeName(), attributeKey) }); } } } else { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = "specified item does not have attributes" }); } }
/// <summary> /// Gets the authorized attributes. /// </summary> /// <param name="entity">The entity.</param> /// <param name="action">The action.</param> /// <param name="person">The person.</param> /// <returns></returns> public static Dictionary <string, AttributeCache> GetAuthorizedAttributes(this Rock.Attribute.IHasAttributes entity, string action, Person person) { var authorizedAttributes = new Dictionary <string, AttributeCache>(); if (entity != null) { foreach (var item in entity.Attributes) { if (item.Value.IsAuthorized(action, person)) { authorizedAttributes.Add(item.Key, item.Value); } } } return(authorizedAttributes); }
/// <summary> /// Shows the edit panel /// </summary> /// <param name="serviceId">The service id.</param> /// <param name="setValues">if set to <c>true</c> [set values].</param> protected void ShowEdit(int serviceId, bool setValues) { Rock.Attribute.IHasAttributes component = _container.Dictionary[serviceId].Value; hfComponentId.Value = serviceId.ToString(); dlProperties.Controls.Clear(); foreach (HtmlGenericControl li in Rock.Attribute.Helper.GetEditControls(component, setValues)) { if (li.Attributes["attribute-key"] != "Order") { dlProperties.Controls.Add(li); } } lProperties.Text = _container.Dictionary[serviceId].Key + " Properties"; pnlList.Visible = false; pnlDetails.Visible = true; }
/// <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; }
/// <summary> /// Loads the attribute controls. /// </summary> /// <param name="marketingCampaignAd">The marketing campaign ad.</param> /// <param name="createControls">if set to <c>true</c> [create controls].</param> /// <param name="setValues">if set to <c>true</c> [set values].</param> private void LoadAdAttributes(Rock.Attribute.IHasAttributes marketingCampaignAd, bool createControls, bool setValues) { if (string.IsNullOrWhiteSpace(ddlMarketingCampaignAdType.SelectedValue)) { return; } int marketingAdTypeId = int.Parse(ddlMarketingCampaignAdType.SelectedValue); MarketingCampaignAdType adType = MarketingCampaignAdType.Read(marketingAdTypeId); tbAdDateRangeEndDate.Visible = adType.DateRangeType.Equals(DateRangeTypeEnum.DateRange); List <Rock.Model.Attribute> attributesForAdType = GetAttributesForAdType(marketingAdTypeId); marketingCampaignAd.Attributes = marketingCampaignAd.Attributes ?? new Dictionary <string, Rock.Web.Cache.AttributeCache>(); marketingCampaignAd.AttributeCategories = marketingCampaignAd.AttributeCategories ?? new SortedDictionary <string, List <string> >(); marketingCampaignAd.AttributeValues = marketingCampaignAd.AttributeValues ?? new Dictionary <string, List <AttributeValueDto> >(); foreach (var attribute in attributesForAdType) { marketingCampaignAd.Attributes[attribute.Key] = Rock.Web.Cache.AttributeCache.Read(attribute); if (marketingCampaignAd.AttributeValues.Count(v => v.Key.Equals(attribute.Key)) == 0) { List <AttributeValueDto> attributeValues = new List <AttributeValueDto>(); attributeValues.Add(new AttributeValueDto { Value = attribute.DefaultValue }); marketingCampaignAd.AttributeValues.Add(attribute.Key, attributeValues); } } foreach (var category in attributesForAdType.Select(a => a.Category).Distinct()) { marketingCampaignAd.AttributeCategories[category] = attributesForAdType.Where(a => a.Category.Equals(category)).Select(a => a.Key).ToList(); } if (createControls) { phAttributes.Controls.Clear(); Rock.Attribute.Helper.AddEditControls(marketingCampaignAd, phAttributes, setValues); } }
/// <summary> /// Loads the attribute controls. /// </summary> /// <param name="marketingCampaignAd">The marketing campaign ad.</param> /// <param name="createControls">if set to <c>true</c> [create controls].</param> /// <param name="setValues">if set to <c>true</c> [set values].</param> private void LoadAdAttributes(Rock.Attribute.IHasAttributes marketingCampaignAd, bool createControls, bool setValues) { if (string.IsNullOrWhiteSpace(ddlMarketingCampaignAdType.SelectedValue)) { return; } int marketingAdTypeId = int.Parse(ddlMarketingCampaignAdType.SelectedValue); MarketingCampaignAdType marketingCampaignAdType = new MarketingCampaignAdTypeService(new RockContext()).Get(marketingAdTypeId); drpAdDateRange.Visible = marketingCampaignAdType.DateRangeType.Equals(DateRangeTypeEnum.DateRange); dpAdSingleDate.Visible = marketingCampaignAdType.DateRangeType.Equals(DateRangeTypeEnum.SingleDate); List <Rock.Web.Cache.AttributeCache> attributesForAdType = GetAttributesForAdType(marketingAdTypeId); marketingCampaignAd.Attributes = marketingCampaignAd.Attributes ?? new Dictionary <string, Rock.Web.Cache.AttributeCache>(); marketingCampaignAd.AttributeValues = marketingCampaignAd.AttributeValues ?? new Dictionary <string, List <AttributeValue> >(); foreach (var attribute in attributesForAdType) { marketingCampaignAd.Attributes[attribute.Key] = attribute; if (marketingCampaignAd.AttributeValues.Count(v => v.Key.Equals(attribute.Key)) == 0) { List <AttributeValue> attributeValues = new List <AttributeValue>(); attributeValues.Add(new AttributeValue { Value = attribute.DefaultValue }); marketingCampaignAd.AttributeValues.Add(attribute.Key, attributeValues); } } if (createControls) { phAttributes.Controls.Clear(); Rock.Attribute.Helper.AddEditControls(marketingCampaignAd, phAttributes, setValues); } }
/// <summary> /// Loads the attribute. /// </summary> /// <param name="entity">The entity.</param> public static void LoadAttributes(this Rock.Attribute.IHasAttributes entity) { Rock.Attribute.Helper.LoadAttributes(entity); }
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.AttributeCache attribute, int?currentPersonId) { _model = model; _attribute = attribute; _currentPersonId = currentPersonId; }
protected override void OnInit(EventArgs e) { base.OnInit(e); string entity = AttributeValue("Entity"); if (string.IsNullOrWhiteSpace(entity)) { entity = PageParameter("Entity"); } string entityQualifierColumn = AttributeValue("EntityQualifierColumn"); if (string.IsNullOrWhiteSpace(entityQualifierColumn)) { entityQualifierColumn = PageParameter("EntityQualifierColumn"); } string entityQualifierValue = AttributeValue("EntityQualifierValue"); if (string.IsNullOrWhiteSpace(entityQualifierValue)) { entityQualifierValue = PageParameter("EntityQualifierValue"); } _category = AttributeValue("AttributeCategory"); if (string.IsNullOrWhiteSpace(_category)) { _category = PageParameter("AttributeCategory"); } ObjectCache cache = MemoryCache.Default; string cacheKey = string.Format("Attributes:{0}:{1}:{2}", entity, entityQualifierColumn, entityQualifierValue); Dictionary <string, List <int> > cachedAttributes = cache[cacheKey] as Dictionary <string, List <int> >; if (cachedAttributes == null) { cachedAttributes = new Dictionary <string, List <int> >(); AttributeService attributeService = new AttributeService(); foreach (var item in attributeService.Queryable(). Where(a => a.Entity == entity && (a.EntityQualifierColumn ?? string.Empty) == entityQualifierColumn && (a.EntityQualifierValue ?? string.Empty) == entityQualifierValue). OrderBy(a => a.Category). ThenBy(a => a.Order). Select(a => new { a.Category, a.Id })) { if (!cachedAttributes.ContainsKey(item.Category)) { cachedAttributes.Add(item.Category, new List <int>()); } cachedAttributes[item.Category].Add(item.Id); } CacheItemPolicy cacheItemPolicy = null; cache.Set(cacheKey, cachedAttributes, cacheItemPolicy); } Rock.Attribute.IHasAttributes model = PageInstance.GetCurrentContext(entity) as Rock.Attribute.IHasAttributes; if (model != null) { if (cachedAttributes.ContainsKey(_category)) { foreach (var attributeId in cachedAttributes[_category]) { var attribute = Rock.Web.Cache.Attribute.Read(attributeId); if (attribute != null) { phAttributes.Controls.Add(( AttributeInstanceValues )this.LoadControl("~/Blocks/Core/AttributeInstanceValues.ascx", model, attribute, CurrentPersonId)); } } } } string script = @" Sys.Application.add_load(function () { $('div.context-attribute-values .delete').click(function(){ return confirm('Are you sure?'); }); }); "; Page.ClientScript.RegisterStartupScript(this.GetType(), "ConfirmDelete", script, true); }
/// <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; }
/// <summary> /// Copies the attributes. /// </summary> /// <param name="entity">The entity.</param> /// <param name="source">The source.</param> public static void CopyAttributesFrom(this Rock.Attribute.IHasAttributes entity, Rock.Attribute.IHasAttributes source) { Rock.Attribute.Helper.CopyAttributes(source, entity); }
/// <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> /// <param name="rockContext">The rock context.</param> public static void LoadAttributes(Rock.Attribute.IHasAttributes entity, RockContext rockContext) { if (entity != null) { Dictionary <string, PropertyInfo> properties = new Dictionary <string, PropertyInfo>(); Type entityType = entity.GetType(); if (entityType.Namespace == "System.Data.Entity.DynamicProxies") { entityType = entityType.BaseType; } rockContext = rockContext ?? new RockContext(); // Check for group type attributes var groupTypeIds = new List <int>(); if (entity is GroupMember || entity is Group || entity is GroupType) { // Can't use GroupTypeCache here since it loads attributes and would result in a recursive stack overflow situation var groupTypeService = new GroupTypeService(rockContext); GroupType groupType = null; if (entity is GroupMember) { var group = ((GroupMember)entity).Group ?? new GroupService(rockContext) .Queryable().AsNoTracking().FirstOrDefault(g => g.Id == ((GroupMember)entity).GroupId); if (group != null) { groupType = group.GroupType ?? groupTypeService .Queryable().AsNoTracking().FirstOrDefault(t => t.Id == group.GroupTypeId); } } else if (entity is Group) { groupType = ((Group)entity).GroupType ?? groupTypeService .Queryable().AsNoTracking().FirstOrDefault(t => t.Id == ((Group)entity).GroupTypeId); } else { groupType = ((GroupType)entity); } while (groupType != null) { groupTypeIds.Insert(0, groupType.Id); // Check for inherited group type id's if (groupType.InheritedGroupTypeId.HasValue) { groupType = groupType.InheritedGroupType ?? groupTypeService .Queryable().AsNoTracking().FirstOrDefault(t => t.Id == (groupType.InheritedGroupTypeId ?? 0)); } else { groupType = null; } } } foreach (PropertyInfo propertyInfo in entityType.GetProperties()) { properties.Add(propertyInfo.Name.ToLower(), propertyInfo); } Rock.Model.AttributeService attributeService = new Rock.Model.AttributeService(rockContext); Rock.Model.AttributeValueService attributeValueService = new Rock.Model.AttributeValueService(rockContext); var inheritedAttributes = new Dictionary <int, List <Rock.Web.Cache.AttributeCache> >(); if (groupTypeIds.Any()) { groupTypeIds.ForEach(g => inheritedAttributes.Add(g, new List <Rock.Web.Cache.AttributeCache>())); } else { inheritedAttributes.Add(0, new List <Rock.Web.Cache.AttributeCache>()); } var attributes = new List <Rock.Web.Cache.AttributeCache>(); // Get all the attributes that apply to this entity type and this entity's properties match any attribute qualifiers var entityTypeCache = Rock.Web.Cache.EntityTypeCache.Read(entityType); if (entityTypeCache != null) { int entityTypeId = entityTypeCache.Id; foreach (var attribute in attributeService.Queryable() .AsNoTracking() .Where(a => a.EntityTypeId == entityTypeCache.Id) .Select(a => new { a.Id, a.EntityTypeQualifierColumn, a.EntityTypeQualifierValue } )) { // group type ids exist (entity is either GroupMember, Group, or GroupType) and qualifier is for a group type id if (groupTypeIds.Any() && ( (entity is GroupMember && string.Compare(attribute.EntityTypeQualifierColumn, "GroupTypeId", true) == 0) || (entity is Group && string.Compare(attribute.EntityTypeQualifierColumn, "GroupTypeId", true) == 0) || (entity is GroupType && string.Compare(attribute.EntityTypeQualifierColumn, "Id", true) == 0))) { int groupTypeIdValue = int.MinValue; if (int.TryParse(attribute.EntityTypeQualifierValue, out groupTypeIdValue) && groupTypeIds.Contains(groupTypeIdValue)) { inheritedAttributes[groupTypeIdValue].Add(Rock.Web.Cache.AttributeCache.Read(attribute.Id)); } } else 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(Rock.Web.Cache.AttributeCache.Read(attribute.Id)); } } } var allAttributes = new List <Rock.Web.Cache.AttributeCache>(); foreach (var attributeGroup in inheritedAttributes) { foreach (var attribute in attributeGroup.Value) { allAttributes.Add(attribute); } } foreach (var attribute in attributes) { allAttributes.Add(attribute); } var attributeValues = new Dictionary <string, Rock.Model.AttributeValue>(); if (allAttributes.Any()) { foreach (var attribute in allAttributes) { // Add a placeholder for this item's value for each attribute attributeValues.Add(attribute.Key, null); } // If loading attributes for a saved item, read the item's value(s) for each attribute if (!entityTypeCache.IsEntity || entity.Id != 0) { List <int> attributeIds = allAttributes.Select(a => a.Id).ToList(); foreach (var attributeValue in attributeValueService.Queryable().AsNoTracking() .Where(v => v.EntityId == entity.Id && attributeIds.Contains(v.AttributeId))) { var attributeKey = AttributeCache.Read(attributeValue.AttributeId).Key; attributeValues[attributeKey] = attributeValue.Clone(false) as Rock.Model.AttributeValue; } } // Look for any attributes that don't have a value and create a default value entry foreach (var attribute in allAttributes) { if (attributeValues[attribute.Key] == null) { var attributeValue = new Rock.Model.AttributeValue(); attributeValue.AttributeId = attribute.Id; if (entity.AttributeValueDefaults != null && entity.AttributeValueDefaults.ContainsKey(attribute.Name)) { attributeValue.Value = entity.AttributeValueDefaults[attribute.Name]; } else { attributeValue.Value = attribute.DefaultValue; } attributeValues[attribute.Key] = attributeValue; } else { if (!String.IsNullOrWhiteSpace(attribute.DefaultValue) && String.IsNullOrWhiteSpace(attributeValues[attribute.Key].Value)) { attributeValues[attribute.Key].Value = attribute.DefaultValue; } } } } entity.Attributes = new Dictionary <string, Web.Cache.AttributeCache>(); allAttributes.ForEach(a => entity.Attributes.Add(a.Key, a)); entity.AttributeValues = attributeValues; } }
/// <summary> /// Sets the value of an attribute key in memory. Note, this will not persist value to database /// </summary> /// <param name="entity">The entity.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> public static void SetAttributeValue(this Rock.Attribute.IHasAttributes entity, string key, Guid?value) { entity.SetAttributeValue(key, value.ToString()); }
/// <summary> /// Saves the attribute values. /// </summary> /// <param name="entity">The entity.</param> /// <param name="rockContext">The rock context.</param> public static void SaveAttributeValues(this Rock.Attribute.IHasAttributes entity, RockContext rockContext = null) { Rock.Attribute.Helper.SaveAttributeValues(entity, rockContext); }
/// <summary> /// Loads the attributes. /// </summary> /// <param name="entity">The entity.</param> /// <param name="rockContext">The rock context.</param> public static void LoadAttributes(this Rock.Attribute.IHasAttributes entity, RockContext rockContext) { Rock.Attribute.Helper.LoadAttributes(entity, rockContext); }