예제 #1
0
        /// <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);
        }
예제 #2
0
        /// <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();
        }
예제 #3
0
        /// <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();
        }
예제 #4
0
        /// <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();
        }