/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { using (new UnitOfWorkScope()) { RockTransactionScope.WrapTransaction(() => { var attributeService = new AttributeService(); Rock.Model.Attribute attribute = null; // remove old qualifier values in case they changed if (edtAttribute.AttributeId.HasValue) { AttributeQualifierService attributeQualifierService = new AttributeQualifierService(); foreach (var oldQualifier in attributeQualifierService.GetByAttributeId(edtAttribute.AttributeId.Value).ToList()) { attributeQualifierService.Delete(oldQualifier, CurrentPersonId); attributeQualifierService.Save(oldQualifier, CurrentPersonId); } attribute = attributeService.Get(edtAttribute.AttributeId.Value); } if (attribute == null) { attribute = new Rock.Model.Attribute(); attributeService.Add(attribute, CurrentPersonId); } if (_configuredType) { attribute.EntityTypeId = _entityTypeId; attribute.EntityTypeQualifierColumn = _entityQualifierColumn; attribute.EntityTypeQualifierValue = _entityQualifierValue; } else { attribute.EntityTypeId = ddlAttrEntityType.SelectedValueAsInt(); attribute.EntityTypeQualifierColumn = tbAttrQualifierField.Text; attribute.EntityTypeQualifierValue = tbAttrQualifierValue.Text; } edtAttribute.GetAttributeProperties(attribute); // Controls will show warnings if (!attribute.IsValid) { return; } Rock.Web.Cache.AttributeCache.Flush(attribute.Id); attributeService.Save(attribute, CurrentPersonId); }); } BindGrid(); pnlDetails.Visible = false; pnlList.Visible = true; }
/// <summary> /// Saves any attribute edits made using an Attribute Editor control /// </summary> /// <param name="edtAttribute">The edt attribute.</param> /// <param name="entityTypeId">The entity type identifier.</param> /// <param name="entityTypeQualifierColumn">The entity type qualifier column.</param> /// <param name="entityTypeQualifierValue">The entity type qualifier value.</param> /// <param name="currentPersonId">The current person identifier.</param> /// <returns></returns> public static Rock.Model.Attribute SaveAttributeEdits(AttributeEditor edtAttribute, int?entityTypeId, string entityTypeQualifierColumn, string entityTypeQualifierValue, int?currentPersonId) { Rock.Model.Attribute attribute = null; using (new Rock.Data.UnitOfWorkScope()) { var attributeService = new AttributeService(); var attributeQualifierService = new AttributeQualifierService(); var categoryService = new CategoryService(); Rock.Data.RockTransactionScope.WrapTransaction(() => { attribute = SaveAttributeEdits(edtAttribute, attributeService, attributeQualifierService, categoryService, entityTypeId, entityTypeQualifierColumn, entityTypeQualifierValue, currentPersonId); }); } return(attribute); }
/// <summary> /// Handles the Click event of the btnSaveAttribute control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param> protected void btnSaveAttribute_Click(object sender, EventArgs e) { using (new Rock.Data.UnitOfWorkScope()) { var attributeService = new AttributeService(); var attributeQualifierService = new AttributeQualifierService(); Rock.Model.Attribute attribute; int attributeId = ((hfIdAttribute.Value) != null && hfIdAttribute.Value != String.Empty) ? Int32.Parse(hfIdAttribute.Value) : 0; if (attributeId == 0) { attribute = new Rock.Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = _entityTypeId; attribute.EntityTypeQualifierColumn = _entityQualifier; attribute.EntityTypeQualifierValue = hfIdType.Value; attributeService.Add(attribute, CurrentPersonId); } else { Rock.Web.Cache.AttributeCache.Flush(attributeId); attribute = attributeService.Get(attributeId); } attribute.Key = tbAttributeKey.Text; attribute.Name = tbAttributeName.Text; attribute.Category = tbAttributeCategory.Text; attribute.Description = tbAttributeDescription.Text; attribute.FieldTypeId = Int32.Parse(ddlAttributeFieldType.SelectedValue); attribute.DefaultValue = tbAttributeDefaultValue.Text; attribute.IsGridColumn = cbAttributeGridColumn.Checked; attribute.IsRequired = cbAttributeRequired.Checked; attributeService.Save(attribute, CurrentPersonId); } rGridAttribute_Bind(hfIdType.Value); modalAttributes.Hide(); }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { if (!Page.IsValid) { return; } GroupType groupType = null; var rockContext = new RockContext(); GroupTypeService groupTypeService = new GroupTypeService(rockContext); AttributeService attributeService = new AttributeService(rockContext); AttributeQualifierService attributeQualifierService = new AttributeQualifierService(rockContext); int?groupTypeId = hfGroupTypeId.ValueAsInt(); if (groupTypeId.HasValue && groupTypeId.Value > 0) { groupType = groupTypeService.Get(groupTypeId.Value); } bool newGroupType = false; if (groupType == null) { groupType = new GroupType(); groupTypeService.Add(groupType); var templatePurpose = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_TEMPLATE.AsGuid()); if (templatePurpose != null) { groupType.GroupTypePurposeValueId = templatePurpose.Id; } newGroupType = true; } if (groupType != null) { groupType.Name = tbName.Text; groupType.Description = tbDescription.Text; groupType.LoadAttributes(rockContext); Rock.Attribute.Helper.GetEditValues(phAttributeEdits, groupType); groupType.SetAttributeValue("core_checkin_AgeRequired", cbAgeRequired.Checked.ToString()); groupType.SetAttributeValue("core_checkin_GradeRequired", cbGradeRequired.Checked.ToString()); groupType.SetAttributeValue("core_checkin_HidePhotos", cbHidePhotos.Checked.ToString()); groupType.SetAttributeValue("core_checkin_PreventDuplicateCheckin", cbPreventDuplicateCheckin.Checked.ToString()); groupType.SetAttributeValue("core_checkin_PreventInactivePeople", cbPreventInactivePeople.Checked.ToString()); groupType.SetAttributeValue("core_checkin_CheckInType", ddlType.SelectedValue); groupType.SetAttributeValue("core_checkin_DisplayLocationCount", cbDisplayLocCount.Checked.ToString()); groupType.SetAttributeValue("core_checkin_EnableManagerOption", cbEnableManager.Checked.ToString()); groupType.SetAttributeValue("core_checkin_EnableOverride", cbEnableOverride.Checked.ToString()); groupType.SetAttributeValue("core_checkin_MaximumPhoneSearchLength", nbMaxPhoneLength.Text); groupType.SetAttributeValue("core_checkin_MaxSearchResults", nbMaxResults.Text); groupType.SetAttributeValue("core_checkin_MinimumPhoneSearchLength", nbMinPhoneLength.Text); groupType.SetAttributeValue("core_checkin_UseSameOptions", cbUseSameOptions.Checked.ToString()); groupType.SetAttributeValue("core_checkin_PhoneSearchType", ddlPhoneSearchType.SelectedValue); groupType.SetAttributeValue("core_checkin_RefreshInterval", nbRefreshInterval.Text); groupType.SetAttributeValue("core_checkin_RegularExpressionFilter", tbSearchRegex.Text); groupType.SetAttributeValue("core_checkin_ReuseSameCode", cbReuseCode.Checked.ToString()); var searchType = DefinedValueCache.Read(ddlSearchType.SelectedValueAsInt() ?? 0); if (searchType != null) { groupType.SetAttributeValue("core_checkin_SearchType", searchType.Guid.ToString()); } else { groupType.SetAttributeValue("core_checkin_SearchType", Rock.SystemGuid.DefinedValue.CHECKIN_SEARCH_TYPE_PHONE_NUMBER); } groupType.SetAttributeValue("core_checkin_SecurityCodeLength", nbSecurityCodeLength.Text); groupType.SetAttributeValue("core_checkin_AutoSelectDaysBack", nbAutoSelectDaysBack.Text); rockContext.WrapTransaction(() => { rockContext.SaveChanges(); groupType.SaveAttributeValues(rockContext); }); if (newGroupType) { var pageRef = new PageReference(CurrentPageReference.PageId, CurrentPageReference.RouteId); pageRef.Parameters.Add("CheckinTypeId", groupType.Id.ToString()); NavigateToPage(pageRef); } else { groupType = groupTypeService.Get(groupType.Id); ShowReadonlyDetails(groupType); } GroupTypeCache.Flush(groupType.Id); Rock.CheckIn.KioskDevice.FlushAll(); } }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { using (new UnitOfWorkScope()) { MarketingCampaignAdType marketingCampaignAdType; MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService(); int marketingCampaignAdTypeId = int.Parse(hfMarketingCampaignAdTypeId.Value); if (marketingCampaignAdTypeId == 0) { marketingCampaignAdType = new MarketingCampaignAdType(); marketingCampaignAdTypeService.Add(marketingCampaignAdType, CurrentPersonId); } else { marketingCampaignAdType = marketingCampaignAdTypeService.Get(marketingCampaignAdTypeId); } marketingCampaignAdType.Name = tbName.Text; marketingCampaignAdType.DateRangeType = (DateRangeTypeEnum)int.Parse(ddlDateRangeType.SelectedValue); if (!marketingCampaignAdType.IsValid) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction(() => { AttributeService attributeService = new AttributeService(); AttributeQualifierService attributeQualifierService = new AttributeQualifierService(); CategoryService categoryService = new CategoryService(); marketingCampaignAdTypeService.Save(marketingCampaignAdType, CurrentPersonId); // get it back to make sure we have a good Id for it for the Attributes marketingCampaignAdType = marketingCampaignAdTypeService.Get(marketingCampaignAdType.Guid); var entityTypeId = EntityTypeCache.Read(typeof(MarketingCampaignAd)).Id; string qualifierColumn = "MarketingCampaignAdTypeId"; string qualifierValue = marketingCampaignAdType.Id.ToString(); // Get the existing attributes for this entity type and qualifier value var attributes = attributeService.Get(entityTypeId, qualifierColumn, qualifierValue); // Delete any of those attributes that were removed in the UI var selectedAttributeGuids = AttributesState.Select(a => a.Guid); foreach (var attr in attributes.Where(a => !selectedAttributeGuids.Contains(a.Guid))) { Rock.Web.Cache.AttributeCache.Flush(attr.Id); attributeService.Delete(attr, CurrentPersonId); attributeService.Save(attr, CurrentPersonId); } // Update the Attributes that were assigned in the UI foreach (var attributeState in AttributesState) { Rock.Attribute.Helper.SaveAttributeEdits(attributeState, attributeService, attributeQualifierService, categoryService, entityTypeId, qualifierColumn, qualifierValue, CurrentPersonId); } }); } NavigateToParentPage(); }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { BinaryFileType binaryFileType; var rockContext = new RockContext(); BinaryFileTypeService binaryFileTypeService = new BinaryFileTypeService(rockContext); AttributeService attributeService = new AttributeService(rockContext); AttributeQualifierService attributeQualifierService = new AttributeQualifierService(rockContext); CategoryService categoryService = new CategoryService(rockContext); int binaryFileTypeId = int.Parse(hfBinaryFileTypeId.Value); if (binaryFileTypeId == 0) { binaryFileType = new BinaryFileType(); binaryFileTypeService.Add(binaryFileType); } else { binaryFileType = binaryFileTypeService.Get(binaryFileTypeId); } binaryFileType.Name = tbName.Text; binaryFileType.Description = tbDescription.Text; binaryFileType.IconCssClass = tbIconCssClass.Text; binaryFileType.AllowCaching = cbAllowCaching.Checked; binaryFileType.RequiresSecurity = cbRequiresSecurity.Checked; if (!string.IsNullOrWhiteSpace(cpStorageType.SelectedValue)) { var entityTypeService = new EntityTypeService(rockContext); var storageEntityType = entityTypeService.Get(new Guid(cpStorageType.SelectedValue)); if (storageEntityType != null) { binaryFileType.StorageEntityTypeId = storageEntityType.Id; } } binaryFileType.LoadAttributes(rockContext); Rock.Attribute.Helper.GetEditValues(phAttributes, binaryFileType); if (!binaryFileType.IsValid) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction(() => { rockContext.SaveChanges(); // get it back to make sure we have a good Id for it for the Attributes binaryFileType = binaryFileTypeService.Get(binaryFileType.Guid); /* Take care of Binary File Attributes */ var entityTypeId = Rock.Web.Cache.EntityTypeCache.Read(typeof(BinaryFile)).Id; // delete BinaryFileAttributes that are no longer configured in the UI var attributes = attributeService.Get(entityTypeId, "BinaryFileTypeId", binaryFileType.Id.ToString()); var selectedAttributeGuids = BinaryFileAttributesState.Select(a => a.Guid); foreach (var attr in attributes.Where(a => !selectedAttributeGuids.Contains(a.Guid))) { Rock.Web.Cache.AttributeCache.Flush(attr.Id); attributeService.Delete(attr); } rockContext.SaveChanges(); // add/update the BinaryFileAttributes that are assigned in the UI foreach (var attributeState in BinaryFileAttributesState) { Rock.Attribute.Helper.SaveAttributeEdits(attributeState, entityTypeId, "BinaryFileTypeId", binaryFileType.Id.ToString(), rockContext); } // SaveAttributeValues for the BinaryFileType binaryFileType.SaveAttributeValues(rockContext); }); NavigateToParentPage(); }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { using (new UnitOfWorkScope()) { BinaryFileType binaryFileType; BinaryFileTypeService binaryFileTypeService = new BinaryFileTypeService(); AttributeService attributeService = new AttributeService(); int binaryFileTypeId = int.Parse(hfBinaryFileTypeId.Value); if (binaryFileTypeId == 0) { binaryFileType = new BinaryFileType(); binaryFileTypeService.Add(binaryFileType, CurrentPersonId); } else { binaryFileType = binaryFileTypeService.Get(binaryFileTypeId); } binaryFileType.Name = tbName.Text; binaryFileType.Description = tbDescription.Text; binaryFileType.IconCssClass = tbIconCssClass.Text; binaryFileType.IconSmallFileId = imgIconSmall.ImageId; binaryFileType.IconLargeFileId = imgIconLarge.ImageId; if (!string.IsNullOrWhiteSpace(cpStorageType.SelectedValue)) { var entityTypeService = new EntityTypeService(); var storageEntityType = entityTypeService.Get(new Guid(cpStorageType.SelectedValue)); if (storageEntityType != null) { binaryFileType.StorageEntityTypeId = storageEntityType.Id; } } if (!binaryFileType.IsValid) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction(() => { binaryFileTypeService.Save(binaryFileType, CurrentPersonId); // get it back to make sure we have a good Id for it for the Attributes binaryFileType = binaryFileTypeService.Get(binaryFileType.Guid); /* Take care of Binary File Attributes */ // delete BinaryFileAttributes that are no longer configured in the UI string qualifierValue = binaryFileType.Id.ToString(); var BinaryFileAttributesQry = attributeService.GetByEntityTypeId(new BinaryFile().TypeId).AsQueryable() .Where(a => a.EntityTypeQualifierColumn.Equals("BinaryFileTypeId", StringComparison.OrdinalIgnoreCase) && a.EntityTypeQualifierValue.Equals(qualifierValue)); var deletedBinaryFileAttributes = from attr in BinaryFileAttributesQry where !(from d in BinaryFileAttributesState select d.Guid).Contains(attr.Guid) select attr; deletedBinaryFileAttributes.ToList().ForEach(a => { var attr = attributeService.Get(a.Guid); Rock.Web.Cache.AttributeCache.Flush(attr.Id); attributeService.Delete(attr, CurrentPersonId); attributeService.Save(attr, CurrentPersonId); }); // add/update the BinaryFileAttributes that are assigned in the UI foreach (var attributeState in BinaryFileAttributesState) { // remove old qualifiers in case they changed var qualifierService = new AttributeQualifierService(); foreach (var oldQualifier in qualifierService.GetByAttributeId(attributeState.Id).ToList()) { qualifierService.Delete(oldQualifier, CurrentPersonId); qualifierService.Save(oldQualifier, CurrentPersonId); } Attribute attribute = BinaryFileAttributesQry.FirstOrDefault(a => a.Guid.Equals(attributeState.Guid)); if (attribute == null) { attribute = attributeState.Clone() as Rock.Model.Attribute; attributeService.Add(attribute, CurrentPersonId); } else { attributeState.Id = attribute.Id; attribute.FromDictionary(attributeState.ToDictionary()); foreach (var qualifier in attributeState.AttributeQualifiers) { attribute.AttributeQualifiers.Add(qualifier.Clone() as AttributeQualifier); } } attribute.EntityTypeQualifierColumn = "BinaryFileTypeId"; attribute.EntityTypeQualifierValue = binaryFileType.Id.ToString(); attribute.EntityTypeId = Rock.Web.Cache.EntityTypeCache.Read(typeof(BinaryFile)).Id; Rock.Web.Cache.AttributeCache.Flush(attribute.Id); attributeService.Save(attribute, CurrentPersonId); } }); } NavigateToParentPage(); }
/// <summary> /// Saves any attribute edits made using an Attribute Editor control /// </summary> /// <param name="edtAttribute">The edt attribute.</param> /// <param name="attributeService">The attribute service.</param> /// <param name="attributeQualifierService">The attribute qualifier service.</param> /// <param name="categoryService">The category service.</param> /// <param name="entityTypeId">The entity type identifier.</param> /// <param name="entityTypeQualifierColumn">The entity type qualifier column.</param> /// <param name="entityTypeQualifierValue">The entity type qualifier value.</param> /// <param name="currentPersonId">The current person identifier.</param> /// <returns></returns> public static Rock.Model.Attribute SaveAttributeEdits(AttributeEditor edtAttribute, AttributeService attributeService, AttributeQualifierService attributeQualifierService, CategoryService categoryService, int?entityTypeId, string entityTypeQualifierColumn, string entityTypeQualifierValue, int?currentPersonId) { // Create and update a new attribute object with new values var newAttribute = new Rock.Model.Attribute(); edtAttribute.GetAttributeProperties(newAttribute); return(SaveAttributeEdits(newAttribute, attributeService, attributeQualifierService, categoryService, entityTypeId, entityTypeQualifierColumn, entityTypeQualifierValue, currentPersonId)); }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { Group group; bool wasSecurityRole = false; using (new UnitOfWorkScope()) { GroupService groupService = new GroupService(); AttributeService attributeService = new AttributeService(); int groupId = int.Parse(hfGroupId.Value); if (groupId == 0) { group = new Group(); group.IsSystem = false; group.Name = string.Empty; } else { group = groupService.Get(groupId); wasSecurityRole = group.IsSecurityRole; } if ((ddlGroupType.SelectedValueAsInt() ?? 0) == 0) { ddlGroupType.ShowErrorMessage(Rock.Constants.WarningMessage.CannotBeBlank(GroupType.FriendlyTypeName)); return; } group.Name = tbName.Text; group.Description = tbDescription.Text; group.CampusId = ddlCampus.SelectedValue.Equals(None.IdValue) ? (int?)null : int.Parse(ddlCampus.SelectedValue); group.GroupTypeId = int.Parse(ddlGroupType.SelectedValue); group.ParentGroupId = gpParentGroup.SelectedValue.Equals(None.IdValue) ? (int?)null : int.Parse(gpParentGroup.SelectedValue); group.IsSecurityRole = cbIsSecurityRole.Checked; group.IsActive = cbIsActive.Checked; if (group.ParentGroupId == group.Id) { gpParentGroup.ShowErrorMessage("Group cannot be a Parent Group of itself."); return; } group.LoadAttributes(); Rock.Attribute.Helper.GetEditValues(phGroupAttributes, group); if (!Page.IsValid) { return; } if (!group.IsValid) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction(() => { if (group.Id.Equals(0)) { groupService.Add(group, CurrentPersonId); } groupService.Save(group, CurrentPersonId); Rock.Attribute.Helper.SaveAttributeValues(group, CurrentPersonId); /* Take care of Group Member Attributes */ // delete GroupMemberAttributes that are no longer configured in the UI string qualifierValue = group.Id.ToString(); var groupMemberAttributesQry = attributeService.GetByEntityTypeId(new GroupMember().TypeId).AsQueryable() .Where(a => a.EntityTypeQualifierColumn.Equals("GroupId", StringComparison.OrdinalIgnoreCase) && a.EntityTypeQualifierValue.Equals(qualifierValue)); var deletedGroupMemberAttributes = from attr in groupMemberAttributesQry where !(from d in GroupMemberAttributesState select d.Guid).Contains(attr.Guid) select attr; deletedGroupMemberAttributes.ToList().ForEach(a => { var attr = attributeService.Get(a.Guid); Rock.Web.Cache.AttributeCache.Flush(attr.Id); attributeService.Delete(attr, CurrentPersonId); attributeService.Save(attr, CurrentPersonId); }); // add/update the GroupMemberAttributes that are assigned in the UI foreach (var attributeState in GroupMemberAttributesState) { // remove old qualifiers in case they changed var qualifierService = new AttributeQualifierService(); foreach (var oldQualifier in qualifierService.GetByAttributeId(attributeState.Id).ToList()) { qualifierService.Delete(oldQualifier, CurrentPersonId); qualifierService.Save(oldQualifier, CurrentPersonId); } Attribute attribute = groupMemberAttributesQry.FirstOrDefault(a => a.Guid.Equals(attributeState.Guid)); if (attribute == null) { attribute = attributeState.Clone() as Rock.Model.Attribute; attributeService.Add(attribute, CurrentPersonId); } else { attributeState.Id = attribute.Id; attribute.FromDictionary(attributeState.ToDictionary()); foreach (var qualifier in attributeState.AttributeQualifiers) { attribute.AttributeQualifiers.Add(qualifier.Clone() as AttributeQualifier); } } attribute.EntityTypeQualifierColumn = "GroupId"; attribute.EntityTypeQualifierValue = group.Id.ToString(); attribute.EntityTypeId = Rock.Web.Cache.EntityTypeCache.Read(typeof(GroupMember)).Id; Rock.Web.Cache.AttributeCache.Flush(attribute.Id); attributeService.Save(attribute, CurrentPersonId); } }); } if (group != null && wasSecurityRole) { if (!group.IsSecurityRole) { // if this group was a SecurityRole, but no longer is, flush Rock.Security.Role.Flush(group.Id); Rock.Security.Authorization.Flush(); } } else { if (group.IsSecurityRole) { // new security role, flush Rock.Security.Authorization.Flush(); } } var qryParams = new Dictionary <string, string>(); qryParams["groupId"] = group.Id.ToString(); NavigateToPage(this.CurrentPage.Guid, qryParams); }
/// <summary> /// Creates a new entity in the database from the encoded information. The entity /// is saved before being returned. /// </summary> /// <param name="encodedEntity">The encoded entity information to create the new entity from.</param> /// <returns>A reference to the new entity.</returns> protected IEntity CreateNewEntity(EncodedEntity encodedEntity) { Type entityType = Reflection.FindType(typeof(IEntity), encodedEntity.EntityType); var service = Reflection.GetServiceForEntityType(entityType, RockContext); if (service != null) { var addMethod = service.GetType().GetMethod("Add", new Type[] { entityType }); if (addMethod != null) { IEntity entity = ( IEntity )Activator.CreateInstance(entityType); RestoreEntityProperties(entity, encodedEntity); entity.Guid = FindMappedGuid(encodedEntity.Guid); // // Do custom pre-save processing. // foreach (var processor in FindEntityProcessors(entityType)) { processor.ProcessImportedEntity(entity, encodedEntity, encodedEntity.GetTransformData(processor.Identifier.ToString()), this); } // // Special handling of AttributeQualifier because Guids may not be the same // across installations and the AttributeId+Key columns make up a unique key. // if (encodedEntity.EntityType == "Rock.Model.AttributeQualifier") { var reference = encodedEntity.References.Where(r => r.Property == "AttributeId").First(); var attribute = GetExistingEntity("Rock.Model.Attribute", FindMappedGuid(new Guid(( string )reference.Data))); string key = ( string )encodedEntity.Properties["Key"]; var existingEntity = new AttributeQualifierService(RockContext) .GetByAttributeId(attribute.Id) .Where(a => a.Key == key) .FirstOrDefault(); if (existingEntity != null) { if (entity.Guid != encodedEntity.Guid) { throw new Exception("AttributeQualifier marked for new Guid but conflicting value already exists."); } GuidMap.AddOrReplace(encodedEntity.Guid, existingEntity.Guid); return(existingEntity); } } // // Special handling of Attribute's. The guid's might be different but if the entity type, // entity qualifiers and key are the same, assume it's the same. // else if (encodedEntity.EntityType == "Rock.Model.Attribute") { var attribute = (Rock.Model.Attribute)entity; var existingEntity = new AttributeService(RockContext) .GetByEntityTypeId(attribute.EntityTypeId) .Where(a => a.EntityTypeQualifierColumn == attribute.EntityTypeQualifierColumn && a.EntityTypeQualifierValue == attribute.EntityTypeQualifierValue && a.Key == attribute.Key) .FirstOrDefault(); if (existingEntity != null) { if (entity.Guid != encodedEntity.Guid) { throw new Exception("Attribute marked for new Guid but conflicting value already exists."); } GuidMap.AddOrReplace(encodedEntity.Guid, existingEntity.Guid); return(existingEntity); } } // // Special handling of AttributeValue's. The guid's might be different but if the attribute Id // and entity Id are the same, assume it's the same. // else if (encodedEntity.EntityType == "Rock.Model.AttributeValue") { var attributeReference = encodedEntity.References.Where(r => r.Property == "AttributeId").First(); var attribute = GetExistingEntity("Rock.Model.Attribute", FindMappedGuid(new Guid(( string )attributeReference.Data))); var entityReference = encodedEntity.References.Where(r => r.Property == "EntityId").First(); var entityRef = GetExistingEntity(entityReference.EntityType, FindMappedGuid(new Guid(( string )entityReference.Data))); var existingEntity = new AttributeValueService(RockContext) .Queryable().Where(a => a.AttributeId == attribute.Id && a.EntityId == entityRef.Id) .FirstOrDefault(); if (existingEntity != null) { if (entity.Guid != encodedEntity.Guid) { throw new Exception("AttributeValue marked for new Guid but conflicting value already exists."); } GuidMap.AddOrReplace(encodedEntity.Guid, existingEntity.Guid); return(existingEntity); } } addMethod.Invoke(service, new object[] { entity }); RockContext.SaveChanges(true); return(entity); } } throw new Exception(string.Format("Failed to create new database entity for {0}_{1}", encodedEntity.EntityType, encodedEntity.Guid)); }
/// <summary> /// Saves any attribute edits made to an attribute /// </summary> /// <param name="newAttribute">The new attribute.</param> /// <param name="entityTypeId">The entity type identifier.</param> /// <param name="entityTypeQualifierColumn">The entity type qualifier column.</param> /// <param name="entityTypeQualifierValue">The entity type qualifier value.</param> /// <param name="rockContext">The rock context.</param> /// <returns></returns> /// <remarks> /// If a rockContext value is included, this method will save any previous changes made to the context /// </remarks> public static Rock.Model.Attribute SaveAttributeEdits(Rock.Model.Attribute newAttribute, int?entityTypeId, string entityTypeQualifierColumn, string entityTypeQualifierValue, RockContext rockContext = null) { rockContext = rockContext ?? new RockContext(); var internalAttributeService = new AttributeService(rockContext); var attributeQualifierService = new AttributeQualifierService(rockContext); var categoryService = new CategoryService(rockContext); // If attribute is not valid, return null if (!newAttribute.IsValid) { return(null); } // Create a attribute model that will be saved Rock.Model.Attribute attribute = null; // Check to see if this was an existing or new attribute if (newAttribute.Id > 0) { // If editing an existing attribute, remove all the old qualifiers in case they were changed foreach (var oldQualifier in attributeQualifierService.GetByAttributeId(newAttribute.Id).ToList()) { attributeQualifierService.Delete(oldQualifier); } rockContext.SaveChanges(); // Then re-load the existing attribute attribute = internalAttributeService.Get(newAttribute.Id); } if (attribute == null) { // If the attribute didn't exist, create it attribute = new Rock.Model.Attribute(); internalAttributeService.Add(attribute); } else { // If it did exist, set the new attribute ID and GUID since we're copying all properties in the next step newAttribute.Id = attribute.Id; newAttribute.Guid = attribute.Guid; } // Copy all the properties from the new attribute to the attribute model attribute.CopyPropertiesFrom(newAttribute); // Add any qualifiers foreach (var qualifier in newAttribute.AttributeQualifiers) { attribute.AttributeQualifiers.Add(new AttributeQualifier { Key = qualifier.Key, Value = qualifier.Value, IsSystem = qualifier.IsSystem }); } // Add any categories attribute.Categories.Clear(); foreach (var category in newAttribute.Categories) { attribute.Categories.Add(categoryService.Get(category.Id)); } attribute.EntityTypeId = entityTypeId; attribute.EntityTypeQualifierColumn = entityTypeQualifierColumn; attribute.EntityTypeQualifierValue = entityTypeQualifierValue; rockContext.SaveChanges(); if (attribute != null) { Rock.Web.Cache.AttributeCache.Flush(attribute.Id); // If this is a global attribute, flush all global attributes if (!entityTypeId.HasValue && entityTypeQualifierColumn == string.Empty && entityTypeQualifierValue == string.Empty) { Rock.Web.Cache.GlobalAttributesCache.Flush(); } } return(attribute); }
/// <summary> /// Adds or Updates a <see cref="Rock.Model.Attribute" /> item for the attribute. /// </summary> /// <param name="property">The property.</param> /// <param name="entityTypeId">The entity type id.</param> /// <param name="entityQualifierColumn">The entity qualifier column.</param> /// <param name="entityQualifierValue">The entity qualifier value.</param> /// <param name="rockContext">The rock context.</param> /// <returns></returns> /// <remarks> /// If a rockContext value is included, this method will save any previous changes made to the context /// </remarks> private static bool UpdateAttribute(FieldAttribute property, int?entityTypeId, string entityQualifierColumn, string entityQualifierValue, RockContext rockContext = null) { bool updated = false; rockContext = rockContext ?? new RockContext(); var attributeService = new AttributeService(rockContext); var attributeQualifierService = new AttributeQualifierService(rockContext); var fieldTypeService = new FieldTypeService(rockContext); var categoryService = new CategoryService(rockContext); var propertyCategories = property.Category.SplitDelimitedValues(false).ToList(); // Look for an existing attribute record based on the entity, entityQualifierColumn and entityQualifierValue Model.Attribute attribute = attributeService.Get(entityTypeId, entityQualifierColumn, entityQualifierValue, property.Key); if (attribute == null) { // If an existing attribute record doesn't exist, create a new one updated = true; attribute = new Model.Attribute(); attribute.EntityTypeId = entityTypeId; attribute.EntityTypeQualifierColumn = entityQualifierColumn; attribute.EntityTypeQualifierValue = entityQualifierValue; attribute.Key = property.Key; attribute.IconCssClass = string.Empty; attribute.IsGridColumn = false; } else { // Check to see if the existing attribute record needs to be updated if (attribute.Name != property.Name || attribute.DefaultValue != property.DefaultValue || attribute.Description != property.Description || attribute.Order != property.Order || attribute.FieldType.Assembly != property.FieldTypeAssembly || attribute.FieldType.Class != property.FieldTypeClass || attribute.IsRequired != property.IsRequired) { updated = true; } // Check category else if (attribute.Categories.Select(c => c.Name).Except(propertyCategories).Any() || propertyCategories.Except(attribute.Categories.Select(c => c.Name)).Any()) { updated = true; } // Check the qualifier values else if (attribute.AttributeQualifiers.Select(q => q.Key).Except(property.FieldConfigurationValues.Select(c => c.Key)).Any() || property.FieldConfigurationValues.Select(c => c.Key).Except(attribute.AttributeQualifiers.Select(q => q.Key)).Any()) { updated = true; } else { foreach (var attributeQualifier in attribute.AttributeQualifiers) { if (!property.FieldConfigurationValues.ContainsKey(attributeQualifier.Key) || property.FieldConfigurationValues[attributeQualifier.Key].Value != attributeQualifier.Value) { updated = true; break; } } } } if (updated) { // Update the attribute attribute.Name = property.Name; attribute.Description = property.Description; attribute.DefaultValue = property.DefaultValue; attribute.Order = property.Order; attribute.IsRequired = property.IsRequired; attribute.Categories.Clear(); if (propertyCategories.Any()) { foreach (string propertyCategory in propertyCategories) { int attributeEntityTypeId = EntityTypeCache.Read(typeof(Rock.Model.Attribute)).Id; var category = categoryService.Get(propertyCategory, attributeEntityTypeId, "EntityTypeId", entityTypeId.ToString()).FirstOrDefault(); if (category == null) { category = new Category(); category.Name = propertyCategory; category.EntityTypeId = attributeEntityTypeId; category.EntityTypeQualifierColumn = "EntityTypeId"; category.EntityTypeQualifierValue = entityTypeId.ToString(); category.Order = 0; } attribute.Categories.Add(category); } } foreach (var qualifier in attribute.AttributeQualifiers.ToList()) { attributeQualifierService.Delete(qualifier); } attribute.AttributeQualifiers.Clear(); foreach (var configValue in property.FieldConfigurationValues) { var qualifier = new Model.AttributeQualifier(); qualifier.Key = configValue.Key; qualifier.Value = configValue.Value.Value; attribute.AttributeQualifiers.Add(qualifier); } // Try to set the field type by searching for an existing field type with the same assembly and class name if (attribute.FieldType == null || attribute.FieldType.Assembly != property.FieldTypeAssembly || attribute.FieldType.Class != property.FieldTypeClass) { attribute.FieldType = fieldTypeService.Queryable().FirstOrDefault(f => f.Assembly == property.FieldTypeAssembly && f.Class == property.FieldTypeClass); } // If this is a new attribute, add it, otherwise remove the exiting one from the cache if (attribute.Id == 0) { attributeService.Add(attribute); } else { AttributeCache.Flush(attribute.Id); } rockContext.SaveChanges(); return(true); } else { return(false); } }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { Location location; var rockContext = new RockContext(); LocationService locationService = new LocationService(rockContext); AttributeService attributeService = new AttributeService(rockContext); AttributeQualifierService attributeQualifierService = new AttributeQualifierService(rockContext); int locationId = int.Parse(hfLocationId.Value); if (locationId == 0) { location = new Location(); location.Name = string.Empty; } else { location = locationService.Get(locationId); } location.Name = tbName.Text; location.IsActive = cbIsActive.Checked; location.LocationTypeValueId = ddlLocationType.SelectedValueAsId(); if (gpParentLocation != null && gpParentLocation.Location != null) { location.ParentLocationId = gpParentLocation.Location.Id; } else { location.ParentLocationId = null; } location.PrinterDeviceId = ddlPrinter.SelectedValueAsInt(); var addrLocation = locapAddress.Location; if (addrLocation != null) { location.Street1 = addrLocation.Street1; location.Street2 = addrLocation.Street2; location.City = addrLocation.City; location.State = addrLocation.State; location.Zip = addrLocation.Zip; } location.GeoPoint = geopPoint.SelectedValue; if (geopPoint.SelectedValue != null) { location.IsGeoPointLocked = true; } location.GeoFence = geopFence.SelectedValue; location.IsGeoPointLocked = cbGeoPointLocked.Checked; location.LoadAttributes(rockContext); Rock.Attribute.Helper.GetEditValues(phAttributeEdits, location); if (!Page.IsValid) { return; } if (!location.IsValid) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction(() => { if (location.Id.Equals(0)) { locationService.Add(location); } rockContext.SaveChanges(); location.SaveAttributeValues(rockContext); }); var qryParams = new Dictionary <string, string>(); qryParams["LocationId"] = location.Id.ToString(); NavigateToPage(RockPage.Guid, qryParams); }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { using (new UnitOfWorkScope()) { MarketingCampaignAdType marketingCampaignAdType; MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService(); int marketingCampaignAdTypeId = int.Parse(hfMarketingCampaignAdTypeId.Value); if (marketingCampaignAdTypeId == 0) { marketingCampaignAdType = new MarketingCampaignAdType(); marketingCampaignAdTypeService.Add(marketingCampaignAdType, CurrentPersonId); } else { marketingCampaignAdType = marketingCampaignAdTypeService.Get(marketingCampaignAdTypeId); } marketingCampaignAdType.Name = tbName.Text; marketingCampaignAdType.DateRangeType = (DateRangeTypeEnum)int.Parse(ddlDateRangeType.SelectedValue); if (!marketingCampaignAdType.IsValid) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction(() => { marketingCampaignAdTypeService.Save(marketingCampaignAdType, CurrentPersonId); // get it back to make sure we have a good Id for it for the Attributes marketingCampaignAdType = marketingCampaignAdTypeService.Get(marketingCampaignAdType.Guid); // delete AdTypeAttributes that are no longer configured in the UI AttributeService attributeService = new AttributeService(); string qualifierValue = marketingCampaignAdType.Id.ToString(); int typeId = new MarketingCampaignAd().TypeId; var qry = attributeService.GetByEntityTypeId(typeId).AsQueryable() .Where(a => a.EntityTypeQualifierColumn == "MarketingCampaignAdTypeId" && a.EntityTypeQualifierValue.Equals(qualifierValue)); var deletedAttributes = from attr in qry.ToList() where !(from d in AttributesState select d.Guid).Contains(attr.Guid) select attr; deletedAttributes.ToList().ForEach(a => { var attr = attributeService.Get(a.Guid); if (attr != null) { Rock.Web.Cache.AttributeCache.Flush(attr.Id); attributeService.Delete(attr, CurrentPersonId); attributeService.Save(attr, CurrentPersonId); } }); // add/update the AdTypes that are assigned in the UI foreach (var attributeState in AttributesState) { // remove old qualifiers in case they changed var qualifierService = new AttributeQualifierService(); foreach (var oldQualifier in qualifierService.GetByAttributeId(attributeState.Id).ToList()) { qualifierService.Delete(oldQualifier, CurrentPersonId); qualifierService.Save(oldQualifier, CurrentPersonId); } Attribute attribute = qry.FirstOrDefault(a => a.Guid.Equals(attributeState.Guid)); if (attribute == null) { attribute = attributeState.Clone() as Rock.Model.Attribute; attributeService.Add(attribute, CurrentPersonId); } else { attributeState.Id = attribute.Id; attribute.FromDictionary(attributeState.ToDictionary()); foreach (var qualifier in attributeState.AttributeQualifiers) { attribute.AttributeQualifiers.Add(qualifier.Clone() as AttributeQualifier); } } attribute.EntityTypeQualifierColumn = "MarketingCampaignAdTypeId"; attribute.EntityTypeQualifierValue = marketingCampaignAdType.Id.ToString(); attribute.EntityTypeId = Rock.Web.Cache.EntityTypeCache.Read(typeof(MarketingCampaignAd)).Id; Rock.Web.Cache.AttributeCache.Flush(attribute.Id); attributeService.Save(attribute, CurrentPersonId); } }); } NavigateToParentPage(); }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { Location location; var rockContext = new RockContext(); LocationService locationService = new LocationService(rockContext); AttributeService attributeService = new AttributeService(rockContext); AttributeQualifierService attributeQualifierService = new AttributeQualifierService(rockContext); int locationId = int.Parse(hfLocationId.Value); if (locationId == 0) { location = new Location(); location.Name = string.Empty; } else { location = locationService.Get(locationId); FlushCampus(locationId); } int?orphanedImageId = null; if (location.ImageId != imgImage.BinaryFileId) { orphanedImageId = location.ImageId; location.ImageId = imgImage.BinaryFileId; } location.Name = tbName.Text; location.IsActive = cbIsActive.Checked; location.LocationTypeValueId = ddlLocationType.SelectedValueAsId(); if (gpParentLocation != null && gpParentLocation.Location != null) { location.ParentLocationId = gpParentLocation.Location.Id; } else { location.ParentLocationId = null; } location.PrinterDeviceId = ddlPrinter.SelectedValueAsInt(); acAddress.GetValues(location); location.GeoPoint = geopPoint.SelectedValue; if (geopPoint.SelectedValue != null) { location.IsGeoPointLocked = true; } location.GeoFence = geopFence.SelectedValue; location.IsGeoPointLocked = cbGeoPointLocked.Checked; location.LoadAttributes(rockContext); Rock.Attribute.Helper.GetEditValues(phAttributeEdits, location); if (!Page.IsValid) { return; } if (!location.IsValid) { // Controls will render the error messages return; } rockContext.WrapTransaction(() => { if (location.Id.Equals(0)) { locationService.Add(location); } rockContext.SaveChanges(); if (orphanedImageId.HasValue) { BinaryFileService binaryFileService = new BinaryFileService(rockContext); var binaryFile = binaryFileService.Get(orphanedImageId.Value); if (binaryFile != null) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; rockContext.SaveChanges(); } } location.SaveAttributeValues(rockContext); }); var qryParams = new Dictionary <string, string>(); qryParams["LocationId"] = location.Id.ToString(); qryParams["ExpandedIds"] = PageParameter("ExpandedIds"); NavigateToPage(RockPage.Guid, qryParams); }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { if (!Page.IsValid) { return; } GroupType groupType = null; var rockContext = new RockContext(); GroupTypeService groupTypeService = new GroupTypeService(rockContext); AttributeService attributeService = new AttributeService(rockContext); AttributeQualifierService attributeQualifierService = new AttributeQualifierService(rockContext); int?groupTypeId = hfGroupTypeId.ValueAsInt(); if (groupTypeId.HasValue && groupTypeId.Value > 0) { groupType = groupTypeService.Get(groupTypeId.Value); } bool newGroupType = false; if (groupType == null) { groupType = new GroupType(); groupTypeService.Add(groupType); var templatePurpose = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_TEMPLATE.AsGuid()); if (templatePurpose != null) { groupType.GroupTypePurposeValueId = templatePurpose.Id; } newGroupType = true; } if (groupType != null) { groupType.Name = tbName.Text; groupType.Description = tbDescription.Text; groupType.IconCssClass = tbIconCssClass.Text; groupType.LoadAttributes(rockContext); Rock.Attribute.Helper.GetEditValues(phAttributeEdits, groupType); groupType.SetAttributeValue("core_checkin_AgeRequired", cbAgeRequired.Checked.ToString()); groupType.SetAttributeValue("core_checkin_GradeRequired", cbGradeRequired.Checked.ToString()); groupType.SetAttributeValue("core_checkin_HidePhotos", cbHidePhotos.Checked.ToString()); groupType.SetAttributeValue("core_checkin_PreventDuplicateCheckin", cbPreventDuplicateCheckin.Checked.ToString()); groupType.SetAttributeValue("core_checkin_PreventInactivePeople", cbPreventInactivePeople.Checked.ToString()); groupType.SetAttributeValue("core_checkin_CheckInType", ddlType.SelectedValue); groupType.SetAttributeValue("core_checkin_DisplayLocationCount", cbDisplayLocCount.Checked.ToString()); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_GROUPTYPE_ABILITY_LEVEL_DETERMINATION, rblAbilityLevelDetermination.SelectedValue); groupType.SetAttributeValue("core_checkin_EnableManagerOption", cbEnableManager.Checked.ToString()); groupType.SetAttributeValue("core_checkin_EnableOverride", cbEnableOverride.Checked.ToString()); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_GROUPTYPE_ACHIEVEMENT_TYPES, listboxAchievementTypes.SelectedValues.AsDelimited(",")); groupType.SetAttributeValue("core_checkin_MaximumPhoneSearchLength", nbMaxPhoneLength.Text); groupType.SetAttributeValue("core_checkin_MaxSearchResults", nbMaxResults.Text); groupType.SetAttributeValue("core_checkin_MinimumPhoneSearchLength", nbMinPhoneLength.Text); groupType.SetAttributeValue("core_checkin_UseSameOptions", cbUseSameOptions.Checked.ToString()); groupType.SetAttributeValue("core_checkin_PhoneSearchType", ddlPhoneSearchType.SelectedValue); groupType.SetAttributeValue("core_checkin_RefreshInterval", nbRefreshInterval.Text); groupType.SetAttributeValue("core_checkin_RegularExpressionFilter", tbSearchRegex.Text); groupType.SetAttributeValue("core_checkin_ReuseSameCode", cbReuseCode.Checked.ToString()); var searchType = DefinedValueCache.Get(ddlSearchType.SelectedValueAsInt() ?? 0); if (searchType != null) { groupType.SetAttributeValue("core_checkin_SearchType", searchType.Guid.ToString()); } else { groupType.SetAttributeValue("core_checkin_SearchType", Rock.SystemGuid.DefinedValue.CHECKIN_SEARCH_TYPE_PHONE_NUMBER); } groupType.SetAttributeValue("core_checkin_SecurityCodeLength", nbCodeAlphaNumericLength.Text); groupType.SetAttributeValue("core_checkin_SecurityCodeAlphaLength", nbCodeAlphaLength.Text); groupType.SetAttributeValue("core_checkin_SecurityCodeNumericLength", nbCodeNumericLength.Text); groupType.SetAttributeValue("core_checkin_SecurityCodeNumericRandom", cbCodeRandom.Checked.ToString()); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_GROUPTYPE_ALLOW_CHECKOUT, cbAllowCheckout.Checked.ToString()); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_GROUPTYPE_ENABLE_PRESENCE, cbEnablePresence.Checked.ToString()); groupType.SetAttributeValue("core_checkin_AutoSelectDaysBack", nbAutoSelectDaysBack.Text); groupType.SetAttributeValue("core_checkin_AutoSelectOptions", ddlAutoSelectOptions.SelectedValueAsInt()); // Registration Settings groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_DISPLAYALTERNATEIDFIELDFORADULTS, cbRegistrationDisplayAlternateIdFieldForAdults.Checked.ToTrueFalse()); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_DISPLAYALTERNATEIDFIELDFORCHILDREN, cbRegistrationDisplayAlternateIdFieldForChildren.Checked.ToTrueFalse()); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_DISPLAYSMSBUTTON, cbRegistrationDisplaySmsEnabled.Checked.ToTrueFalse()); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_DEFAULTSMSENABLED, cbRegistrationSmsEnabledByDefault.Checked.ToTrueFalse()); groupType.SetAttributeValue( Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_REQUIREDATTRIBUTESFORADULTS, lbRegistrationRequiredAttributesForAdults.SelectedValues.AsDelimited(",")); groupType.SetAttributeValue( Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_OPTIONALATTRIBUTESFORADULTS, lbRegistrationOptionalAttributesForAdults.SelectedValues.AsDelimited(",")); groupType.SetAttributeValue( Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_REQUIREDATTRIBUTESFORCHILDREN, lbRegistrationRequiredAttributesForChildren.SelectedValues.AsDelimited(",")); groupType.SetAttributeValue( Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_OPTIONALATTRIBUTESFORCHILDREN, lbRegistrationOptionalAttributesForChildren.SelectedValues.AsDelimited(",")); groupType.SetAttributeValue( Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_REQUIREDATTRIBUTESFORFAMILIES, lbRegistrationRequiredAttributesForFamilies.SelectedValues.AsDelimited(",")); groupType.SetAttributeValue( Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_OPTIONALATTRIBUTESFORFAMILIES, lbRegistrationOptionalAttributesForFamilies.SelectedValues.AsDelimited(",")); Guid?defaultPersonConnectionStatusValueGuid = null; var defaultPersonConnectionStatusValueId = dvpRegistrationDefaultPersonConnectionStatus.SelectedValue.AsIntegerOrNull(); if (defaultPersonConnectionStatusValueId.HasValue) { var defaultPersonConnectionStatusValue = DefinedValueCache.Get(defaultPersonConnectionStatusValueId.Value); if (defaultPersonConnectionStatusValue != null) { defaultPersonConnectionStatusValueGuid = defaultPersonConnectionStatusValue.Guid; } } groupType.SetAttributeValue( Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_DEFAULTPERSONCONNECTIONSTATUS, defaultPersonConnectionStatusValueGuid.ToString()); var workflowTypeService = new WorkflowTypeService(rockContext); groupType.SetAttributeValue( Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_ADDFAMILYWORKFLOWTYPES, workflowTypeService.GetByIds(wftpRegistrationAddFamilyWorkflowTypes.SelectedValuesAsInt().ToList()).Select(a => a.Guid).ToList().AsDelimited(",")); groupType.SetAttributeValue( Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_ADDPERSONWORKFLOWTYPES, workflowTypeService.GetByIds(wftpRegistrationAddPersonWorkflowTypes.SelectedValuesAsInt().ToList()).Select(a => a.Guid).ToList().AsDelimited(",")); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_ENABLECHECKINAFTERREGISTRATION, cbEnableCheckInAfterRegistration.Checked.ToTrueFalse()); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_KNOWNRELATIONSHIPTYPES, lbKnownRelationshipTypes.SelectedValues.AsDelimited(",")); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_SAMEFAMILYKNOWNRELATIONSHIPTYPES, lbSameFamilyKnownRelationshipTypes.SelectedValues.AsDelimited(",")); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_REGISTRATION_CANCHECKINKNOWNRELATIONSHIPTYPES, lbCanCheckInKnownRelationshipTypes.SelectedValues.AsDelimited(",")); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_ACTION_SELECT_HEADER_LAVA_TEMPLATE, ceActionSelectHeaderTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_CHECKOUT_PERSON_SELECT_HEADER_LAVA_TEMPLATE, ceCheckoutPersonSelectHeaderTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_PERSON_SELECT_HEADER_LAVA_TEMPLATE, cePersonSelectHeaderTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_MULTI_PERSON_SELECT_HEADER_LAVA_TEMPLATE, ceMultiPersonSelectHeaderTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_GROUP_TYPE_SELECT_HEADER_LAVA_TEMPLATE, ceGroupTypeSelectHeaderTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_TIME_SELECT_HEADER_LAVA_TEMPLATE, ceTimeSelectHeaderTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_ABILITY_LEVEL_SELECT_HEADER_LAVA_TEMPLATE, ceAbilityLevelSelectHeaderTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_LOCATION_SELECT_HEADER_LAVA_TEMPLATE, ceLocationSelectHeaderTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_GROUP_SELECT_HEADER_LAVA_TEMPLATE, ceGroupSelectHeaderTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_START_LAVA_TEMPLATE, ceStartTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_FAMILYSELECT_LAVA_TEMPLATE, ceFamilySelectTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_PERSON_SELECT_ADDITIONAL_INFORMATION_LAVA_TEMPLATE, cePersonSelectTemplate.Text); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_SUCCESS_LAVA_TEMPLATE_OVERRIDE_DISPLAY_MODE, ddlSuccessTemplateOverrideDisplayMode.SelectedValue); groupType.SetAttributeValue(Rock.SystemKey.GroupTypeAttributeKey.CHECKIN_SUCCESS_LAVA_TEMPLATE, ceSuccessTemplate.Text); // Save group type and attributes rockContext.WrapTransaction(() => { rockContext.SaveChanges(); groupType.SaveAttributeValues(rockContext); }); if (newGroupType) { var pageRef = new PageReference(CurrentPageReference.PageId, CurrentPageReference.RouteId); pageRef.Parameters.Add("CheckinTypeId", groupType.Id.ToString()); NavigateToPage(pageRef); } else { groupType = groupTypeService.Get(groupType.Id); ShowReadonlyDetails(groupType); } Rock.CheckIn.KioskDevice.Clear(); } }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { using (new UnitOfWorkScope()) { var attributeService = new AttributeService(); var attributeQualifierService = new AttributeQualifierService(); Rock.Model.Attribute attribute; int attributeId = 0; if (hfId.Value != string.Empty && !int.TryParse(hfId.Value, out attributeId)) { attributeId = 0; } if (attributeId == 0) { attribute = new Rock.Model.Attribute(); attribute.IsSystem = false; attribute.EntityTypeId = _entityTypeId; attribute.EntityTypeQualifierColumn = _entityQualifierColumn; attribute.EntityTypeQualifierValue = _entityQualifierValue; attributeService.Add(attribute, CurrentPersonId); } else { AttributeCache.Flush(attributeId); attribute = attributeService.Get(attributeId); } attribute.Key = tbKey.Text; attribute.Name = tbName.Text; attribute.Category = tbCategory.Text; attribute.Description = tbDescription.Text; attribute.FieldTypeId = int.Parse(ddlFieldType.SelectedValue); var fieldType = FieldTypeCache.Read(attribute.FieldTypeId); foreach (var oldQualifier in attribute.AttributeQualifiers.ToList()) { attributeQualifierService.Delete(oldQualifier, CurrentPersonId); } attribute.AttributeQualifiers.Clear(); List <Control> configControls = new List <Control>(); foreach (var key in fieldType.Field.ConfigurationKeys()) { configControls.Add(phFieldTypeQualifiers.FindControl("configControl_" + key)); } foreach (var configValue in fieldType.Field.ConfigurationValues(configControls)) { AttributeQualifier qualifier = new AttributeQualifier(); qualifier.IsSystem = false; qualifier.Key = configValue.Key; qualifier.Value = configValue.Value.Value ?? string.Empty; attribute.AttributeQualifiers.Add(qualifier); } attribute.DefaultValue = tbDefaultValue.Text; attribute.IsMultiValue = cbMultiValue.Checked; attribute.IsRequired = cbRequired.Checked; attributeService.Save(attribute, CurrentPersonId); } BindGrid(); pnlDetails.Visible = false; pnlList.Visible = true; }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { BinaryFileType binaryFileType; var rockContext = new RockContext(); BinaryFileTypeService binaryFileTypeService = new BinaryFileTypeService(rockContext); AttributeService attributeService = new AttributeService(rockContext); AttributeQualifierService attributeQualifierService = new AttributeQualifierService(rockContext); CategoryService categoryService = new CategoryService(rockContext); int binaryFileTypeId = int.Parse(hfBinaryFileTypeId.Value); if (binaryFileTypeId == 0) { binaryFileType = new BinaryFileType(); binaryFileTypeService.Add(binaryFileType); } else { binaryFileType = binaryFileTypeService.Get(binaryFileTypeId); } binaryFileType.Name = tbName.Text; binaryFileType.Description = tbDescription.Text; binaryFileType.IconCssClass = tbIconCssClass.Text; binaryFileType.CacheToServerFileSystem = cbCacheToServerFileSystem.Checked; binaryFileType.CacheControlHeaderSettings = cpCacheSettings.CurrentCacheablity.ToJson(); binaryFileType.RequiresViewSecurity = cbRequiresViewSecurity.Checked; binaryFileType.MaxWidth = nbMaxWidth.Text.AsInteger(); binaryFileType.MaxHeight = nbMaxHeight.Text.AsInteger(); binaryFileType.PreferredFormat = ddlPreferredFormat.SelectedValueAsEnum <Format>(); binaryFileType.PreferredResolution = ddlPreferredResolution.SelectedValueAsEnum <Resolution>(); binaryFileType.PreferredColorDepth = ddlPreferredColorDepth.SelectedValueAsEnum <ColorDepth>(); binaryFileType.PreferredRequired = cbPreferredRequired.Checked; if (!string.IsNullOrWhiteSpace(cpStorageType.SelectedValue)) { var entityTypeService = new EntityTypeService(rockContext); var storageEntityType = entityTypeService.Get(new Guid(cpStorageType.SelectedValue)); if (storageEntityType != null) { binaryFileType.StorageEntityTypeId = storageEntityType.Id; } } binaryFileType.LoadAttributes(rockContext); Rock.Attribute.Helper.GetEditValues(phAttributes, binaryFileType); if (!binaryFileType.IsValid) { // Controls will render the error messages return; } rockContext.WrapTransaction(() => { rockContext.SaveChanges(); // get it back to make sure we have a good Id for it for the Attributes binaryFileType = binaryFileTypeService.Get(binaryFileType.Guid); /* Take care of Binary File Attributes */ var entityTypeId = EntityTypeCache.Get(typeof(BinaryFile)).Id; // delete BinaryFileAttributes that are no longer configured in the UI var attributes = attributeService.GetByEntityTypeQualifier(entityTypeId, "BinaryFileTypeId", binaryFileType.Id.ToString(), true); var selectedAttributeGuids = BinaryFileAttributesState.Select(a => a.Guid); foreach (var attr in attributes.Where(a => !selectedAttributeGuids.Contains(a.Guid))) { attributeService.Delete(attr); } rockContext.SaveChanges(); // add/update the BinaryFileAttributes that are assigned in the UI foreach (var attributeState in BinaryFileAttributesState) { Rock.Attribute.Helper.SaveAttributeEdits(attributeState, entityTypeId, "BinaryFileTypeId", binaryFileType.Id.ToString(), rockContext); } // SaveAttributeValues for the BinaryFileType binaryFileType.SaveAttributeValues(rockContext); }); NavigateToParentPage(); }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { EventCalendar eventCalendar; using (var rockContext = new RockContext()) { EventCalendarService eventCalendarService = new EventCalendarService(rockContext); EventCalendarContentChannelService eventCalendarContentChannelService = new EventCalendarContentChannelService(rockContext); ContentChannelService contentChannelService = new ContentChannelService(rockContext); AttributeService attributeService = new AttributeService(rockContext); AttributeQualifierService qualifierService = new AttributeQualifierService(rockContext); int eventCalendarId = int.Parse(hfEventCalendarId.Value); if (eventCalendarId == 0) { eventCalendar = new EventCalendar(); eventCalendarService.Add(eventCalendar); } else { eventCalendar = eventCalendarService.Get(eventCalendarId); } eventCalendar.IsActive = cbActive.Checked; eventCalendar.Name = tbName.Text; eventCalendar.Description = tbDescription.Text; eventCalendar.IconCssClass = tbIconCssClass.Text; eventCalendar.LoadAttributes(); Rock.Attribute.Helper.GetEditValues(phAttributes, eventCalendar); if (!eventCalendar.IsValid) { // Controls will render the error messages return; } // need WrapTransaction due to Attribute saves rockContext.WrapTransaction(() => { rockContext.SaveChanges(); eventCalendar.SaveAttributeValues(rockContext); var dbChannelGuids = eventCalendarContentChannelService.Queryable() .Where(c => c.EventCalendarId == eventCalendar.Id) .Select(c => c.Guid) .ToList(); var uiChannelGuids = ContentChannelsState.Select(c => c.Key).ToList(); var toDelete = eventCalendarContentChannelService .Queryable() .Where(c => dbChannelGuids.Contains(c.Guid) && !uiChannelGuids.Contains(c.Guid)); eventCalendarContentChannelService.DeleteRange(toDelete); contentChannelService.Queryable() .Where(c => uiChannelGuids.Contains(c.Guid) && !dbChannelGuids.Contains(c.Guid)) .ToList() .ForEach(c => { var eventCalendarContentChannel = new EventCalendarContentChannel(); eventCalendarContentChannel.EventCalendarId = eventCalendar.Id; eventCalendarContentChannel.ContentChannelId = c.Id; eventCalendarContentChannelService.Add(eventCalendarContentChannel); }); rockContext.SaveChanges(); /* Save Event Attributes */ string qualifierValue = eventCalendar.Id.ToString(); SaveAttributes(new EventCalendarItem().TypeId, "EventCalendarId", qualifierValue, EventAttributesState, rockContext); // Reload calendar and make sure that the person who may have just added a calendar has security to view/edit/administrate the calendar eventCalendar = eventCalendarService.Get(eventCalendar.Id); if (eventCalendar != null) { if (!eventCalendar.IsAuthorized(Authorization.VIEW, CurrentPerson)) { eventCalendar.AllowPerson(Authorization.VIEW, CurrentPerson, rockContext); } if (!eventCalendar.IsAuthorized(Authorization.EDIT, CurrentPerson)) { eventCalendar.AllowPerson(Authorization.EDIT, CurrentPerson, rockContext); } if (!eventCalendar.IsAuthorized(Authorization.ADMINISTRATE, CurrentPerson)) { eventCalendar.AllowPerson(Authorization.ADMINISTRATE, CurrentPerson, rockContext); } } }); } // Redirect back to same page so that item grid will show any attributes that were selected to show on grid var qryParams = new Dictionary <string, string>(); qryParams["EventCalendarId"] = eventCalendar.Id.ToString(); NavigateToPage(RockPage.Guid, qryParams); }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { Location location = null; var rockContext = new RockContext(); LocationService locationService = new LocationService(rockContext); AttributeService attributeService = new AttributeService(rockContext); AttributeQualifierService attributeQualifierService = new AttributeQualifierService(rockContext); int locationId = int.Parse(hfLocationId.Value); if (locationId != 0) { location = locationService.Get(locationId); } if (location == null) { location = new Location(); location.Name = string.Empty; } string previousName = location.Name; int?orphanedImageId = null; if (location.ImageId != imgImage.BinaryFileId) { orphanedImageId = location.ImageId; location.ImageId = imgImage.BinaryFileId; } location.Name = tbName.Text; location.IsActive = cbIsActive.Checked; location.LocationTypeValueId = dvpLocationType.SelectedValueAsId(); if (gpParentLocation != null && gpParentLocation.Location != null) { location.ParentLocationId = gpParentLocation.Location.Id; } else { location.ParentLocationId = null; } location.PrinterDeviceId = ddlPrinter.SelectedValueAsInt(); acAddress.GetValues(location); location.GeoPoint = geopPoint.SelectedValue; if (geopPoint.SelectedValue != null) { location.IsGeoPointLocked = true; } location.GeoFence = geopFence.SelectedValue; location.IsGeoPointLocked = cbGeoPointLocked.Checked; location.SoftRoomThreshold = nbSoftThreshold.Text.AsIntegerOrNull(); location.FirmRoomThreshold = nbFirmThreshold.Text.AsIntegerOrNull(); location.LoadAttributes(rockContext); Rock.Attribute.Helper.GetEditValues(phAttributeEdits, location); if (!Page.IsValid) { return; } // if the location IsValid is false, and the UI controls didn't report any errors, it is probably because the custom rules of location didn't pass. // So, make sure a message is displayed in the validation summary cvLocation.IsValid = location.IsValid; if (!cvLocation.IsValid) { cvLocation.ErrorMessage = location.ValidationResults.Select(a => a.ErrorMessage).ToList().AsDelimited("<br />"); return; } rockContext.WrapTransaction(() => { if (location.Id.Equals(0)) { locationService.Add(location); } rockContext.SaveChanges(); if (orphanedImageId.HasValue) { BinaryFileService binaryFileService = new BinaryFileService(rockContext); var binaryFile = binaryFileService.Get(orphanedImageId.Value); if (binaryFile != null) { // marked the old images as IsTemporary so they will get cleaned up later binaryFile.IsTemporary = true; rockContext.SaveChanges(); } } location.SaveAttributeValues(rockContext); }); // If this is a names location (or was previously) if (!string.IsNullOrWhiteSpace(location.Name) || (previousName ?? string.Empty) != (location.Name ?? string.Empty)) { // flush the checkin config Rock.CheckIn.KioskDevice.Clear(); } if (_personId.HasValue) { NavigateToParentPage(new Dictionary <string, string> { { "PersonId", _personId.Value.ToString() } }); } else { Rock.CheckIn.KioskDevice.Clear(); var qryParams = new Dictionary <string, string>(); qryParams["LocationId"] = location.Id.ToString(); qryParams["ExpandedIds"] = PageParameter("ExpandedIds"); NavigateToPage(RockPage.Guid, qryParams); } }
/// <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="EventArgs" /> instance containing the event data.</param> protected void btnSave_Click(object sender, EventArgs e) { using (new UnitOfWorkScope()) { GroupType groupType; GroupTypeService groupTypeService = new GroupTypeService(); AttributeService attributeService = new AttributeService(); int groupTypeId = int.Parse(hfGroupTypeId.Value); if (groupTypeId == 0) { groupType = new GroupType(); groupTypeService.Add(groupType, CurrentPersonId); } else { groupType = groupTypeService.Get(groupTypeId); } groupType.Name = tbName.Text; groupType.Description = tbDescription.Text; groupType.GroupTerm = tbGroupTerm.Text; groupType.GroupMemberTerm = tbGroupMemberTerm.Text; groupType.DefaultGroupRoleId = ddlDefaultGroupRole.SelectedValueAsInt(); groupType.ShowInGroupList = cbShowInGroupList.Checked; groupType.ShowInNavigation = cbShowInNavigation.Checked; groupType.IconCssClass = tbIconCssClass.Text; groupType.IconSmallFileId = imgIconSmall.BinaryFileId; groupType.IconLargeFileId = imgIconLarge.BinaryFileId; groupType.TakesAttendance = cbTakesAttendance.Checked; groupType.AttendanceRule = ddlAttendanceRule.SelectedValueAsEnum <AttendanceRule>(); groupType.AttendancePrintTo = ddlAttendancePrintTo.SelectedValueAsEnum <PrintTo>(); groupType.LocationSelectionMode = ddlLocationSelectionMode.SelectedValueAsEnum <LocationPickerMode>(); groupType.GroupTypePurposeValueId = ddlGroupTypePurpose.SelectedValueAsInt(); groupType.AllowMultipleLocations = cbAllowMultipleLocations.Checked; groupType.InheritedGroupTypeId = gtpInheritedGroupType.SelectedGroupTypeId; groupType.ChildGroupTypes = new List <GroupType>(); groupType.ChildGroupTypes.Clear(); foreach (var item in ChildGroupTypesDictionary) { var childGroupType = groupTypeService.Get(item.Key); if (childGroupType != null) { groupType.ChildGroupTypes.Add(childGroupType); } } DefinedValueService definedValueService = new DefinedValueService(); groupType.LocationTypes = new List <GroupTypeLocationType>(); groupType.LocationTypes.Clear(); foreach (var item in LocationTypesDictionary) { var locationType = definedValueService.Get(item.Key); if (locationType != null) { groupType.LocationTypes.Add(new GroupTypeLocationType { LocationTypeValueId = locationType.Id }); } } if (!groupType.IsValid) { // Controls will render the error messages return; } RockTransactionScope.WrapTransaction(() => { groupTypeService.Save(groupType, CurrentPersonId); // get it back to make sure we have a good Id for it for the Attributes groupType = groupTypeService.Get(groupType.Guid); /* Take care of Group Type Attributes */ // delete GroupTypeAttributes that are no longer configured in the UI var groupTypeAttributesQry = attributeService.Get(new GroupType().TypeId, "Id", groupType.Id.ToString()); var selectedAttributes = GroupTypeAttributesState.Select(a => a.Guid); foreach (var attr in groupTypeAttributesQry.Where(a => !selectedAttributes.Contains(a.Guid))) { Rock.Web.Cache.AttributeCache.Flush(attr.Id); attributeService.Delete(attr, CurrentPersonId); attributeService.Save(attr, CurrentPersonId); } string qualifierValue = groupType.Id.ToString(); // add/update the GroupTypeAttributes that are assigned in the UI foreach (var attributeState in GroupTypeAttributesState .Where(a => a.EntityTypeQualifierValue == null || a.EntityTypeQualifierValue.Trim() == string.Empty || a.EntityTypeQualifierValue.Equals(qualifierValue))) { // remove old qualifiers in case they changed var qualifierService = new AttributeQualifierService(); foreach (var oldQualifier in qualifierService.GetByAttributeId(attributeState.Id).ToList()) { qualifierService.Delete(oldQualifier, CurrentPersonId); qualifierService.Save(oldQualifier, CurrentPersonId); } Attribute attribute = groupTypeAttributesQry.FirstOrDefault(a => a.Guid.Equals(attributeState.Guid)); if (attribute == null) { attribute = attributeState.Clone() as Rock.Model.Attribute; attributeService.Add(attribute, CurrentPersonId); } else { attributeState.Id = attribute.Id; attribute.FromDictionary(attributeState.ToDictionary()); foreach (var qualifier in attributeState.AttributeQualifiers) { attribute.AttributeQualifiers.Add(qualifier.Clone() as AttributeQualifier); } } attribute.EntityTypeQualifierColumn = "Id"; attribute.EntityTypeQualifierValue = qualifierValue; attribute.EntityTypeId = Rock.Web.Cache.EntityTypeCache.Read(typeof(GroupType)).Id; Rock.Web.Cache.AttributeCache.Flush(attribute.Id); attributeService.Save(attribute, CurrentPersonId); } /* Take care of Group Attributes */ // delete GroupAttributes that are no longer configured in the UI var groupAttributesQry = attributeService.Get(new Group().TypeId, "GroupTypeId", groupType.Id.ToString()); selectedAttributes = GroupAttributesState.Select(a => a.Guid); foreach (var attr in groupAttributesQry.Where(a => !selectedAttributes.Contains(a.Guid))) { Rock.Web.Cache.AttributeCache.Flush(attr.Id); attributeService.Delete(attr, CurrentPersonId); attributeService.Save(attr, CurrentPersonId); } // add/update the GroupAttributes that are assigned in the UI foreach (var attributeState in GroupAttributesState .Where(a => a.EntityTypeQualifierValue == null || a.EntityTypeQualifierValue.Trim() == string.Empty || a.EntityTypeQualifierValue.Equals(qualifierValue))) { // remove old qualifiers in case they changed var qualifierService = new AttributeQualifierService(); foreach (var oldQualifier in qualifierService.GetByAttributeId(attributeState.Id).ToList()) { qualifierService.Delete(oldQualifier, CurrentPersonId); qualifierService.Save(oldQualifier, CurrentPersonId); } Attribute attribute = groupAttributesQry.FirstOrDefault(a => a.Guid.Equals(attributeState.Guid)); if (attribute == null) { attribute = attributeState.Clone() as Rock.Model.Attribute; attributeService.Add(attribute, CurrentPersonId); } else { attributeState.Id = attribute.Id; attribute.FromDictionary(attributeState.ToDictionary()); foreach (var qualifier in attributeState.AttributeQualifiers) { attribute.AttributeQualifiers.Add(qualifier.Clone() as AttributeQualifier); } } attribute.EntityTypeQualifierColumn = "GroupTypeId"; attribute.EntityTypeQualifierValue = qualifierValue; attribute.EntityTypeId = Rock.Web.Cache.EntityTypeCache.Read(typeof(Group)).Id; Rock.Web.Cache.AttributeCache.Flush(attribute.Id); attributeService.Save(attribute, CurrentPersonId); } }); } NavigateToParentPage(); }