예제 #1
0
        /// <summary>
        /// Handles the Click event of the btnCopy 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 lbCopyButton_Click(object sender, EventArgs e)
        {
            var rockContext      = new RockContext();
            var groupService     = new GroupService(rockContext);
            var authService      = new AuthService(rockContext);
            var attributeService = new AttributeService(rockContext);

            int groupId = hfGroupId.ValueAsInt();
            var group   = groupService.Queryable("GroupType")
                          .Where(g => g.Id == groupId)
                          .FirstOrDefault();

            if (group != null)
            {
                group.LoadAttributes(rockContext);

                // clone the group
                var newGroup = group.Clone(false);
                newGroup.CreatedByPersonAlias    = null;
                newGroup.CreatedByPersonAliasId  = null;
                newGroup.CreatedDateTime         = RockDateTime.Now;
                newGroup.ModifiedByPersonAlias   = null;
                newGroup.ModifiedByPersonAliasId = null;
                newGroup.ModifiedDateTime        = RockDateTime.Now;
                newGroup.Id       = 0;
                newGroup.Guid     = Guid.NewGuid();
                newGroup.IsSystem = false;
                newGroup.Name     = group.Name + " - Copy";

                if (GetAttributeValue("CopyLocation").AsBoolean(true))
                {
                    foreach (GroupLocation location in group.GroupLocations)
                    {
                        newGroup.GroupLocations.Add(location);
                    }
                }

                var auths = authService.GetByGroup(group.Id);
                rockContext.WrapTransaction(() =>
                {
                    groupService.Add(newGroup);
                    rockContext.SaveChanges();

                    newGroup.LoadAttributes(rockContext);
                    if (group.Attributes != null && group.Attributes.Any())
                    {
                        foreach (var attributeKey in group.Attributes.Select(a => a.Key))
                        {
                            string value = group.GetAttributeValue(attributeKey);
                            newGroup.SetAttributeValue(attributeKey, value);
                        }
                    }

                    newGroup.SaveAttributeValues(rockContext);

                    /* Take care of Group Member Attributes */
                    var entityTypeId       = EntityTypeCache.Get(typeof(GroupMember)).Id;
                    string qualifierColumn = "GroupId";
                    string qualifierValue  = group.Id.ToString();

                    // Get the existing attributes for this entity type and qualifier value
                    var attributes = attributeService.Get(entityTypeId, qualifierColumn, qualifierValue);

                    foreach (var attribute in attributes)
                    {
                        var newAttribute      = attribute.Clone(false);
                        newAttribute.Id       = 0;
                        newAttribute.Guid     = Guid.NewGuid();
                        newAttribute.IsSystem = false;
                        newAttribute.EntityTypeQualifierValue = newGroup.Id.ToString();

                        foreach (var qualifier in attribute.AttributeQualifiers)
                        {
                            var newQualifier      = qualifier.Clone(false);
                            newQualifier.Id       = 0;
                            newQualifier.Guid     = Guid.NewGuid();
                            newQualifier.IsSystem = false;

                            newAttribute.AttributeQualifiers.Add(qualifier);
                        }

                        attributeService.Add(newAttribute);
                    }

                    rockContext.SaveChanges();

                    var person = CurrentPerson;
                    GroupMember currentGroupMember = null;
                    if (person != null)
                    {
                        currentGroupMember = group.Members.Where(gm => gm.PersonId == person.Id).FirstOrDefault();
                    }
                    if (currentGroupMember != null)
                    {
                        var newGroupMember      = currentGroupMember.Clone(false);
                        newGroupMember.Id       = 0;
                        newGroupMember.Guid     = Guid.NewGuid();
                        newGroupMember.IsSystem = false;
                        newGroup.Members.Add(newGroupMember);
                    }

                    rockContext.SaveChanges();

                    foreach (var auth in auths)
                    {
                        var newAuth     = auth.Clone(false);
                        newAuth.Id      = 0;
                        newAuth.Guid    = Guid.NewGuid();
                        newAuth.GroupId = newGroup.Id;
                        newAuth.CreatedByPersonAlias    = null;
                        newAuth.CreatedByPersonAliasId  = null;
                        newAuth.CreatedDateTime         = RockDateTime.Now;
                        newAuth.ModifiedByPersonAlias   = null;
                        newAuth.ModifiedByPersonAliasId = null;
                        newAuth.ModifiedDateTime        = RockDateTime.Now;
                        authService.Add(newAuth);
                    }

                    rockContext.SaveChanges();
                    Rock.Security.Authorization.Clear();
                });

                NavigateToCurrentPage(new Dictionary <string, string> {
                    { "GroupId", newGroup.Id.ToString() }
                });
            }
        }