Exemple #1
0
        /// <summary>
        /// Reads new values entered by the user for the field
        /// </summary>
        /// <param name="control">Parent control that controls were added to in the CreateEditControl() method</param>
        /// <param name="configurationValues"></param>
        /// <returns></returns>
        public override string GetEditValue(Control control, Dictionary <string, ConfigurationValue> configurationValues)
        {
            var groupMemberIdList = new List <int>();

            if (control != null && control is ListControl)
            {
                groupMemberIdList.AddRange(((ListControl)control).Items.Cast <ListItem>()
                                           .Where(i => i.Selected)
                                           .Select(i => i.Value).AsIntegerList());
            }

            var guids = new List <Guid>();

            using (var rockContext = new RockContext())
            {
                var groupMemberService = new GroupMemberService(rockContext);
                foreach (int groupMemberId in groupMemberIdList)
                {
                    var groupMember = groupMemberService.Get(groupMemberId);
                    if (groupMember != null)
                    {
                        guids.Add(groupMember.Guid);
                    }
                }
            }

            return(guids.AsDelimited(","));
        }
        /// <summary>
        /// Returns the field's current value(s)
        /// </summary>
        /// <param name="parentControl">The parent control.</param>
        /// <param name="value">Information about the value</param>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="condensed">Flag indicating if the value should be condensed (i.e. for use in a grid column)</param>
        /// <returns></returns>
        public override string FormatValue(Control parentControl, string value, Dictionary <string, ConfigurationValue> configurationValues, bool condensed)
        {
            string formattedValue = string.Empty;

            if (!string.IsNullOrWhiteSpace(value))
            {
                var names = new List <string>();
                using (var rockContext = new RockContext())
                {
                    var groupMemberService = new GroupMemberService(rockContext);
                    foreach (Guid guid in value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsGuidList())
                    {
                        var groupMember = groupMemberService.Get(guid);
                        if (groupMember != null)
                        {
                            names.Add(groupMember.Person.FullName);
                        }
                    }
                }

                formattedValue = names.AsDelimited(", ");
            }

            return(base.FormatValue(parentControl, formattedValue, null, condensed));
        }
Exemple #3
0
 /// <summary>
 /// Adds the person to group.
 /// </summary>
 /// <param name="rockContext">The rock context.</param>
 /// <param name="person">The person.</param>
 /// <param name="workflowType">Type of the workflow.</param>
 /// <param name="groupMembers">The group members.</param>
 private void AddPersonToGroup(RockContext rockContext, Person person)
 {
     if (person != null)
     {
         if (!CurrentGroup.Members
             .Any(m =>
                  m.PersonId == person.Id &&
                  m.GroupRoleId == _defaultGroupRole.Id))
         {
             var groupMemberService = new GroupMemberService(rockContext);
             var groupMember        = new GroupMember();
             groupMember.PersonId          = person.Id;
             groupMember.GroupRoleId       = _defaultGroupRole.Id;
             groupMember.GroupMemberStatus = ( GroupMemberStatus )GetAttributeValue("GroupMemberStatus").AsInteger();
             groupMember.GroupId           = CurrentGroup.Id;
             groupMemberService.Add(groupMember);
             rockContext.SaveChanges();
         }
         else
         {
             foreach (var groupMember in CurrentGroup.Members
                      .Where(m => m.PersonId == person.Id &&
                             m.GroupRoleId == _defaultGroupRole.Id))
             {
                 var groupMemberService = new GroupMemberService(rockContext);
                 var efGroupMember      = groupMemberService.Get(groupMember.Guid);
                 efGroupMember.GroupMemberStatus = ( GroupMemberStatus )GetAttributeValue("GroupMemberStatus").AsInteger();
                 debug.Text += groupMember.Person.FullName;
             }
             rockContext.SaveChanges();
         }
     }
 }
Exemple #4
0
        /// <summary>
        /// Handles the Click event of the DeleteGroupMember control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs" /> instance containing the event data.</param>
        protected void DeleteGroupMember_Click(object sender, Rock.Web.UI.Controls.RowEventArgs e)
        {
            RockContext        rockContext        = new RockContext();
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);
            GroupMember        groupMember        = groupMemberService.Get(e.RowKeyId);

            if (groupMember != null)
            {
                string errorMessage;
                if (!groupMemberService.CanDelete(groupMember, out errorMessage))
                {
                    mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                int groupId = groupMember.GroupId;

                groupMemberService.Delete(groupMember);
                rockContext.SaveChanges();

                Group group = new GroupService(rockContext).Get(groupId);
                if (group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()))
                {
                    // person removed from SecurityRole, Flush
                    Rock.Security.Role.Flush(group.Id);
                    Rock.Security.Authorization.Flush();
                }
            }

            BindGroupMembersGrid();
        }
        /// <summary>
        /// Handles the Click event of the DeleteGroupMember control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Rock.Web.UI.Controls.RowEventArgs" /> instance containing the event data.</param>
        protected void DeleteGroupMember_Click(object sender, Rock.Web.UI.Controls.RowEventArgs e)
        {
            RockTransactionScope.WrapTransaction(() =>
            {
                GroupMemberService groupMemberService = new GroupMemberService();
                GroupMember groupMember = groupMemberService.Get(e.RowKeyId);
                if (groupMember != null)
                {
                    string errorMessage;
                    if (!groupMemberService.CanDelete(groupMember, out errorMessage))
                    {
                        mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }

                    int groupId = groupMember.GroupId;

                    groupMemberService.Delete(groupMember, CurrentPersonId);
                    groupMemberService.Save(groupMember, CurrentPersonId);

                    Group group = new GroupService().Get(groupId);
                    if (group.IsSecurityRole)
                    {
                        // person removed from SecurityRole, Flush
                        Rock.Security.Authorization.Flush();
                    }
                }
            });

            BindGroupMembersGrid();
        }
Exemple #6
0
        /// <summary>
        /// Adds the person to group.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="person">The person.</param>
        /// <param name="workflowType">Type of the workflow.</param>
        /// <param name="groupMembers">The group members.</param>
        private void AddPersonToGroup(RockContext rockContext, Person person, WorkflowTypeCache workflowType, List <GroupMember> groupMembers)
        {
            if (person != null)
            {
                GroupMember groupMember = null;
                if (!_group.Members
                    .Any(m =>
                         m.PersonId == person.Id &&
                         m.GroupRoleId == _defaultGroupRole.Id))
                {
                    var groupMemberService = new GroupMemberService(rockContext);
                    groupMember                   = new GroupMember();
                    groupMember.PersonId          = person.Id;
                    groupMember.GroupRoleId       = _defaultGroupRole.Id;
                    groupMember.GroupMemberStatus = ( GroupMemberStatus )GetAttributeValue("GroupMemberStatus").AsInteger();
                    groupMember.GroupId           = _group.Id;
                    groupMemberService.Add(groupMember);
                    rockContext.SaveChanges();
                }
                else
                {
                    GroupMemberStatus status = ( GroupMemberStatus )GetAttributeValue("GroupMemberStatus").AsInteger();
                    groupMember = _group.Members.Where(m =>
                                                       m.PersonId == person.Id &&
                                                       m.GroupRoleId == _defaultGroupRole.Id).FirstOrDefault();
                    if (groupMember.GroupMemberStatus != status)
                    {
                        var groupMemberService = new GroupMemberService(rockContext);

                        // reload this group member in the current context
                        groupMember = groupMemberService.Get(groupMember.Id);
                        groupMember.GroupMemberStatus = status;
                        rockContext.SaveChanges();
                    }
                }

                if (groupMember != null)
                {
                    groupMember.LoadAttributes();
                    groupMember.SetAttributeValue("RSVPCount", numHowMany.Value);
                    groupMember.SaveAttributeValues();

                    SendGroupEmail(groupMember);
                }

                if (groupMember != null && workflowType != null && (workflowType.IsActive ?? true))
                {
                    try
                    {
                        List <string> workflowErrors;
                        var           workflow = Workflow.Activate(workflowType, person.FullName);
                        new WorkflowService(rockContext).Process(workflow, groupMember, out workflowErrors);
                    }
                    catch (Exception ex)
                    {
                        ExceptionLogService.LogException(ex, this.Context);
                    }
                }
            }
        }
        /// <summary>
        /// Handles the Click event of the btnEdit 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 btnEdit_Click(object sender, EventArgs e)
        {
            GroupMemberService groupMemberService = new GroupMemberService();
            GroupMember        groupMember        = groupMemberService.Get(int.Parse(hfGroupMemberId.Value));

            ShowEditDetails(groupMember);
        }
        void rGroupMembers_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        {
            int groupMemberId = int.MinValue;

            if (int.TryParse(e.CommandArgument.ToString(), out groupMemberId))
            {
                var service     = new GroupMemberService();
                var groupMember = service.Get(groupMemberId);
                if (groupMember != null)
                {
                    if (e.CommandName == "EditRole")
                    {
                        ShowModal(groupMember.Person, groupMember.GroupRoleId);
                    }

                    else if (e.CommandName == "RemoveRole")
                    {
                        if (IsKnownRelationships)
                        {
                            var inverseGroupMember = service.GetInverseRelationship(groupMember, false, CurrentPersonId);
                            if (inverseGroupMember != null)
                            {
                                service.Delete(inverseGroupMember, CurrentPersonId);
                            }
                        }

                        service.Delete(groupMember, CurrentPersonId);
                        service.Save(groupMember, CurrentPersonId);

                        BindData();
                    }
                }
            }
        }
Exemple #9
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="configurationValues"></param>
        /// <param name="value">The value.</param>
        public override void SetEditValue(Control control, Dictionary <string, ConfigurationValue> configurationValues, string value)
        {
            if (value != null)
            {
                if (control != null && control is ListControl)
                {
                    var ids = new List <string>();
                    using (var rockContext = new RockContext())
                    {
                        var groupMemberService = new GroupMemberService(rockContext);
                        foreach (Guid guid in value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsGuidList())
                        {
                            var groupMember = groupMemberService.Get(guid);
                            if (groupMember != null)
                            {
                                ids.Add(groupMember.Id.ToString());
                            }
                        }
                    }

                    var listControl = control as ListControl;
                    foreach (ListItem li in listControl.Items)
                    {
                        li.Selected = ids.Contains(li.Value);
                    }
                }
            }
        }
        /// <summary>
        /// Displays the edit group member.
        /// </summary>
        /// <param name="groupMemberId">The group member identifier.</param>
        private void DisplayEditGroupMember(int groupMemberId)
        {
            // persist the group member id for use in partial postbacks
            CurrentGroupMemberId = groupMemberId;

            pnlGroupEdit.Visible       = false;
            pnlGroupView.Visible       = false;
            pnlEditGroupMember.Visible = true;

            RockContext        rockContext        = new RockContext();
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);

            var groupMember = groupMemberService.Get(groupMemberId);

            if (groupMember == null)
            {
                groupMember = new GroupMember {
                    Id = 0
                };
                groupMember.GroupId           = _groupId;
                groupMember.Group             = new GroupService(rockContext).Get(groupMember.GroupId);
                groupMember.GroupRoleId       = groupMember.Group.GroupType.DefaultGroupRoleId ?? 0;
                groupMember.GroupMemberStatus = GroupMemberStatus.Active;
            }


//            lGroupName.Text = groupMember.Group.Name;

            // load dropdowns
            LoadGroupMemberDropDowns(_groupId);

            // set values
            ppGroupMemberPerson.SetValue(groupMember.Person);
            ddlGroupRole.SetValue(groupMember.GroupRoleId);
            rblStatus.SetValue(( int )groupMember.GroupMemberStatus);
            bool hideGroupMemberInactiveStatus = GetAttributeValue("HideInactiveGroupMemberStatus").AsBooleanOrNull() ?? false;

            if (hideGroupMemberInactiveStatus)
            {
                var inactiveItem = rblStatus.Items.FindByValue((( int )GroupMemberStatus.Inactive).ToString());
                if (inactiveItem != null)
                {
                    rblStatus.Items.Remove(inactiveItem);
                }
            }

            // set attributes
            groupMember.LoadAttributes();
            phGroupMemberAttributes.Controls.Clear();
            Helper.AddEditControls(groupMember, phGroupMemberAttributes, true, string.Empty, true);

            IsEditingGroupMember = true;
        }
Exemple #11
0
        /// <summary>
        /// Deletes the group member.
        /// </summary>
        /// <param name="groupMemberId">The group member identifier.</param>
        private void DeleteGroupMember(int groupMemberId)
        {
            RockContext        rockContext        = new RockContext();
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);

            var groupMember = groupMemberService.Get(groupMemberId);

            if (groupMember != null)
            {
                groupMemberService.Delete(groupMember);
            }

            rockContext.SaveChanges();
        }
        /// <summary>
        /// Gets the edit value as the IEntity.Id
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="configurationValues">The configuration values.</param>
        /// <returns></returns>
        public int?GetEditValueAsEntityId(Control control, Dictionary <string, ConfigurationValue> configurationValues)
        {
            GroupMember item = null;

            using (var rockContext = new RockContext())
            {
                var groupMemberService = new GroupMemberService(rockContext);

                Guid guid = GetEditValue(control, configurationValues).AsGuid();
                item = groupMemberService.Get(guid);
            }

            return(item != null ? item.Id : (int?)null);
        }
        /// <summary>
        /// Gets a filter expression for an entity property value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="filterValues">The filter values.</param>
        /// <param name="parameterExpression">The parameter expression.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <returns></returns>
        public override Expression PropertyFilterExpression(Dictionary <string, ConfigurationValue> configurationValues, List <string> filterValues, Expression parameterExpression, string propertyName, Type propertyType)
        {
            List <string> selectedValues = filterValues[0].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();

            if (selectedValues.Any())
            {
                MemberExpression propertyExpression = Expression.Property(parameterExpression, propertyName);

                var  type           = propertyType;
                bool isNullableType = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>);
                if (isNullableType)
                {
                    type = Nullable.GetUnderlyingType(type);
                    propertyExpression = Expression.Property(propertyExpression, "Value");
                }

                Type   genericListType  = typeof(List <>);
                Type   specificListType = genericListType.MakeGenericType(type);
                object specificList     = Activator.CreateInstance(specificListType);

                using (var rockContext = new RockContext())
                {
                    var groupMemberService = new GroupMemberService(rockContext);

                    foreach (string value in selectedValues)
                    {
                        string tempValue = value;

                        // if this is not for an attribute value, look up the id for the group member
                        if (propertyName != "Value" || propertyType != typeof(string))
                        {
                            var gm = groupMemberService.Get(value.AsGuid());
                            tempValue = gm != null?gm.Id.ToString() : string.Empty;
                        }

                        if (!string.IsNullOrWhiteSpace(tempValue))
                        {
                            object obj = Convert.ChangeType(tempValue, type);
                            specificListType.GetMethod("Add").Invoke(specificList, new object[] { obj });
                        }
                    }
                }

                ConstantExpression constantExpression = Expression.Constant(specificList, specificListType);
                return(Expression.Call(constantExpression, specificListType.GetMethod("Contains", new Type[] { type }), propertyExpression));
            }

            return(null);
        }
        private void DeleteGroupMember(int groupMemberId)
        {
            RockContext        rockContext        = new RockContext();
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);
            var groupMember = groupMemberService.Get(groupMemberId);

            if (groupMember != null &&
                groupMember.PersonId == Person.Id &&
                groupMember.Group.IsAuthorized(Authorization.EDIT, CurrentPerson))
            {
                groupMemberService.Delete(groupMember);
                rockContext.SaveChanges();
            }
            ShowEdit();
        }
 /// <summary>
 /// Handles the Click event of the btnCancel 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 btnCancel_Click(object sender, EventArgs e)
 {
     if (hfGroupMemberId.Value.Equals("0"))
     {
         // Cancelling on Add.  Return to Grid
         Dictionary <string, string> qryString = new Dictionary <string, string>();
         qryString["groupId"] = hfGroupId.Value;
         NavigateToParentPage(qryString);
     }
     else
     {
         // Cancelling on Edit.  Return to Details
         GroupMemberService groupMemberService = new GroupMemberService();
         GroupMember        groupMember        = groupMemberService.Get(int.Parse(hfGroupMemberId.Value));
         ShowReadonlyDetails(groupMember);
     }
 }
        /// <summary>
        /// Displays the delete group member.
        /// </summary>
        /// <param name="groupMemberId">The group member identifier.</param>
        private void DisplayDeleteGroupMember(int groupMemberId)
        {
            RockContext        rockContext        = new RockContext();
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);

            var groupMember = groupMemberService.Get(groupMemberId);

            if (groupMember != null)
            {
                // persist the group member id for use in partial postbacks
                this.CurrentGroupMemberId = groupMember.Id;

                lConfirmDeleteMsg.Text = string.Format("Are you sure you want to delete (remove) {0} from {1}?", groupMember.Person.FullName, groupMember.Group.Name);

                mdConfirmDelete.Show();
                //mdConfirmDelete.Header.Visible = false;
            }
        }
        /// <summary>
        /// Sets the edit value from IEntity.Id value
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="id">The identifier.</param>
        public void SetEditValueFromEntityId(Control control, Dictionary <string, ConfigurationValue> configurationValues, int?id)
        {
            GroupMember item = null;

            if (id.HasValue)
            {
                using (var rockContext = new RockContext())
                {
                    var groupMemberService = new GroupMemberService(rockContext);

                    item = groupMemberService.Get(id.Value);
                }
            }

            string guidValue = item != null?item.Guid.ToString() : string.Empty;

            SetEditValue(control, configurationValues, guidValue);
        }
Exemple #18
0
        /// <summary>
        /// Handles the Delete event of the gList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        protected void gList_Delete(object sender, RowEventArgs e)
        {
            RockTransactionScope.WrapTransaction(() =>
            {
                var groupMemberService = new GroupMemberService();
                int groupMemberId      = (int)e.RowKeyValue;

                GroupMember groupMember = groupMemberService.Get(groupMemberId);
                if (groupMember != null)
                {
                    // check if person can be removed from the Group and also check if person can be removed from all the person assigned competencies
                    string errorMessage;
                    if (!groupMemberService.CanDelete(groupMember, out errorMessage))
                    {
                        mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                        return;
                    }

                    var competencyPersonService = new ResidencyService <CompetencyPerson>();
                    var personCompetencyList    = competencyPersonService.Queryable().Where(a => a.PersonId.Equals(groupMember.PersonId));
                    foreach (var item in personCompetencyList)
                    {
                        if (!competencyPersonService.CanDelete(item, out errorMessage))
                        {
                            mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                            return;
                        }
                    }

                    // if you made it this far, delete all person's assigned competencies, and finally delete from Group
                    foreach (var item in personCompetencyList)
                    {
                        competencyPersonService.Delete(item, CurrentPersonId);
                        competencyPersonService.Save(item, CurrentPersonId);
                    }

                    groupMemberService.Delete(groupMember, CurrentPersonId);
                    groupMemberService.Save(groupMember, CurrentPersonId);
                }
            });

            BindGrid();
        }
        /// <summary>
        /// Formats the filter value value.
        /// </summary>
        /// <param name="configurationValues">The configuration values.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public override string FormatFilterValueValue(Dictionary <string, ConfigurationValue> configurationValues, string value)
        {
            var values = new List <string>();

            using (var rockContext = new RockContext())
            {
                var groupMemberService = new GroupMemberService(rockContext);
                foreach (Guid guid in value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsGuidList())
                {
                    var groupMember = groupMemberService.Get(guid);
                    if (groupMember != null)
                    {
                        values.Add(groupMember.Person.FullName);
                    }
                }
            }

            return(values.Select(v => "'" + v + "'").ToList().AsDelimited(" or "));
        }
        public override MobileBlock GetMobile(string parameter)
        {
            RockContext rockContext = new RockContext();

            Person person = null;

            if (GetAttributeValue("EntityType") == "GroupMember")
            {
                GroupMemberService groupMemberService = new GroupMemberService(rockContext);
                var groupMember = groupMemberService.Get(parameter.AsGuid());
                if (groupMember != null)
                {
                    person = groupMember.Person;
                }
            }
            else
            {
                PersonService personService = new PersonService(rockContext);
                person = personService.Get(parameter.AsGuid());
            }


            if (person != null)
            {
                CustomAttributes["PersonGuid"] = person.Guid.ToString();
                CustomAttributes["Name"]       = person.FullName;
                CustomAttributes["Image"]      = GlobalAttributesCache.Value("InternalApplicationRoot") + person.PhotoUrl;

                CustomAttributes["AccentColor"] = GetAttributeValue("AccentColor");

                return(new MobileBlock()
                {
                    BlockType = "Avalanche.Blocks.PersonCard",
                    Attributes = CustomAttributes
                });
            }

            return(new MobileBlock()
            {
                BlockType = "Avalanche.Blocks.Null",
                Attributes = CustomAttributes
            });
        }
        /// <summary>
        /// Handles the Click event of the btnCancel 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 btnCancel_Click(object sender, EventArgs e)
        {
            if (hfGroupMemberId.Value.Equals("0"))
            {
                // Cancelling on Add.
                Dictionary <string, string> qryString = new Dictionary <string, string>();
                qryString["GroupId"] = hfGroupId.Value;
                NavigateToParentPage(qryString);
            }
            else
            {
                // Cancelling on Edit.  Return to Details
                GroupMemberService groupMemberService = new GroupMemberService(new RockContext());
                GroupMember        groupMember        = groupMemberService.Get(int.Parse(hfGroupMemberId.Value));

                Dictionary <string, string> qryString = new Dictionary <string, string>();
                qryString["GroupId"] = groupMember.GroupId.ToString();
                NavigateToParentPage(qryString);
            }
        }
        /// <summary>
        /// Handles the Click event of the btnConfirmDelete 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 mdConfirmDelete_Click(object sender, EventArgs e)
        {
            mdConfirmDelete.Hide();

            if (GetAttributeValue("AllowGroupMemberDelete").AsBoolean())
            {
                RockContext        rockContext        = new RockContext();
                GroupMemberService groupMemberService = new GroupMemberService(rockContext);

                var groupMember = groupMemberService.Get(this.CurrentGroupMemberId);
                if (groupMember != null)
                {
                    groupMemberService.Delete(groupMember);
                }

                rockContext.SaveChanges();
            }

            DisplayViewGroup();
        }
Exemple #23
0
        private void DisplayEditGroupMember(int groupMemberId)
        {
            // persist the group member id for use in partial postbacks
            this.CurrentGroupMemberId = groupMemberId;

            pnlGroupEdit.Visible       = false;
            pnlGroupView.Visible       = false;
            pnlEditGroupMember.Visible = true;

            RockContext        rockContext        = new RockContext();
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);

            var groupMember = groupMemberService.Get(groupMemberId);

            if (groupMember == null)
            {
                groupMember = new GroupMember {
                    Id = 0
                };
                groupMember.GroupId           = _groupId;
                groupMember.Group             = new GroupService(rockContext).Get(groupMember.GroupId);
                groupMember.GroupRoleId       = groupMember.Group.GroupType.DefaultGroupRoleId ?? 0;
                groupMember.GroupMemberStatus = GroupMemberStatus.Active;
            }

            // load dropdowns
            LoadGroupMemberDropDowns(_groupId);

            // set values
            ppGroupMemberPerson.SetValue(groupMember.Person);
            ddlGroupRole.SetValue(groupMember.GroupRoleId);
            rblStatus.SetValue((int)groupMember.GroupMemberStatus);

            // set attributes
            groupMember.LoadAttributes();
            phGroupMemberAttributes.Controls.Clear();
            Rock.Attribute.Helper.AddEditControls(groupMember, phGroupMemberAttributes, true, "", true);

            this.IsEditingGroupMember = true;
        }
        /// <summary>
        /// Handles the SaveClick event of the mdPlaceElsewhere 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 mdPlaceElsewhere_SaveClick(object sender, EventArgs e)
        {
            using (var rockContext = new RockContext())
            {
                var groupService       = new GroupService(rockContext);
                var groupMemberService = new GroupMemberService(rockContext);
                var groupMember        = groupMemberService.Get(hfPlaceElsewhereGroupMemberId.Value.AsInteger());
                if (groupMember != null)
                {
                    string errorMessage;
                    if (!groupMemberService.CanDelete(groupMember, out errorMessage))
                    {
                        nbPlaceElsewhereWarning.Text = errorMessage;
                        return;
                    }


                    var trigger = _group.GetGroupMemberWorkflowTriggers().FirstOrDefault(a => a.Id == hfPlaceElsewhereTriggerId.Value.AsInteger());
                    if (trigger != null)
                    {
                        // create a transaction for the selected trigger
                        var transaction = new Rock.Transactions.GroupMemberPlacedElsewhereTransaction(groupMember, tbPlaceElsewhereNote.Text, trigger);

                        // delete the group member from the current group
                        groupMemberService.Delete(groupMember);

                        rockContext.SaveChanges();

                        // queue up the transaction
                        Rock.Transactions.RockQueue.TransactionQueue.Enqueue(transaction);
                    }
                }

                mdPlaceElsewhere.Hide();
                mdPlaceElsewhere.Visible = false;
                BindGroupMembersGrid();
            }
        }
Exemple #25
0
        /// <summary>
        /// Handles the ItemCommand event of the rGroupMembers control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param>
        protected void rGroupMembers_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        {
            if (CanEdit)
            {
                int groupMemberId = e.CommandArgument.ToString().AsIntegerOrNull() ?? 0;
                if (groupMemberId != 0)
                {
                    var rockContext = new RockContext();
                    var service     = new GroupMemberService(rockContext);
                    var groupMember = service.Get(groupMemberId);
                    if (groupMember != null)
                    {
                        if (e.CommandName == "EditRole")
                        {
                            ShowModal(groupMember.Person, groupMember.GroupRoleId, groupMemberId);
                        }
                        else if (e.CommandName == "RemoveRole")
                        {
                            if (IsKnownRelationships)
                            {
                                var inverseGroupMember = service.GetInverseRelationship(groupMember, false);
                                if (inverseGroupMember != null)
                                {
                                    service.Delete(inverseGroupMember);
                                }
                            }

                            service.Delete(groupMember);

                            rockContext.SaveChanges();

                            BindData();
                        }
                    }
                }
            }
        }
Exemple #26
0
        /// <summary>
        /// Restores the view-state information from a previous user control request that was saved by the <see cref="M:System.Web.UI.UserControl.SaveViewState" /> method.
        /// </summary>
        /// <param name="savedState">An <see cref="T:System.Object" /> that represents the user control state to be restored.</param>
        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);

            if (IsEditingGroup == true)
            {
                Group group = new GroupService(new RockContext()).Get(_groupId);
                group.LoadAttributes();

                phAttributes.Controls.Clear();
                Rock.Attribute.Helper.AddEditControls(group, phAttributes, false, BlockValidationGroup);
            }

            if (IsEditingGroupMember == true)
            {
                RockContext        rockContext        = new RockContext();
                GroupMemberService groupMemberService = new GroupMemberService(rockContext);

                var groupMember = groupMemberService.Get(this.CurrentGroupMemberId);

                if (groupMember == null)
                {
                    groupMember = new GroupMember {
                        Id = 0
                    };
                    groupMember.GroupId           = _groupId;
                    groupMember.Group             = new GroupService(rockContext).Get(groupMember.GroupId);
                    groupMember.GroupRoleId       = groupMember.Group.GroupType.DefaultGroupRoleId ?? 0;
                    groupMember.GroupMemberStatus = GroupMemberStatus.Active;
                }

                // set attributes
                groupMember.LoadAttributes();
                phGroupMemberAttributes.Controls.Clear();
                Rock.Attribute.Helper.AddEditControls(groupMember, phGroupMemberAttributes, true, string.Empty, true);
            }
        }
Exemple #27
0
        /// <summary>
        /// Handles the Click event of the btnSaveGroupMember 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 btnSaveGroupMember_Click(object sender, EventArgs e)
        {
            var rockContext = new RockContext();
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);

            GroupTypeRole role = new GroupTypeRoleService(rockContext).Get(ddlGroupRole.SelectedValueAsInt() ?? 0);

            var groupMember = groupMemberService.Get(this.CurrentGroupMemberId);

            if (this.CurrentGroupMemberId == 0)
            {
                groupMember = new GroupMember {
                    Id = 0
                };
                groupMember.GroupId = _groupId;

                // check to see if the person is alread a member of the gorup/role
                var existingGroupMember = groupMemberService.GetByGroupIdAndPersonIdAndGroupRoleId(
                    _groupId, ppGroupMemberPerson.SelectedValue ?? 0, ddlGroupRole.SelectedValueAsId() ?? 0);

                if (existingGroupMember != null)
                {
                    // if so, don't add and show error message
                    var person = new PersonService(rockContext).Get((int)ppGroupMemberPerson.PersonId);

                    nbGroupMemberErrorMessage.Title = "Person Already In Group";
                    nbGroupMemberErrorMessage.Text  = string.Format(
                        "{0} already belongs to the {1} role for this {2}, and cannot be added again with the same role.",
                        person.FullName,
                        ddlGroupRole.SelectedItem.Text,
                        role.GroupType.GroupTerm,
                        RockPage.PageId,
                        existingGroupMember.Id);
                    return;
                }
            }

            groupMember.PersonId    = ppGroupMemberPerson.PersonId.Value;
            groupMember.GroupRoleId = role.Id;

            // set their status.  If HideInactiveGroupMemberStatus is True, and they are already Inactive, keep their status as Inactive;
            bool hideGroupMemberInactiveStatus = this.GetAttributeValue("HideInactiveGroupMemberStatus").AsBooleanOrNull() ?? false;
            var  selectedStatus = rblStatus.SelectedValueAsEnumOrNull <GroupMemberStatus>();

            if (!selectedStatus.HasValue)
            {
                if (hideGroupMemberInactiveStatus)
                {
                    selectedStatus = GroupMemberStatus.Inactive;
                }
                else
                {
                    selectedStatus = GroupMemberStatus.Active;
                }
            }

            groupMember.GroupMemberStatus = selectedStatus.Value;

            groupMember.LoadAttributes();

            Rock.Attribute.Helper.GetEditValues(phAttributes, groupMember);

            if (!Page.IsValid)
            {
                return;
            }

            // if the groupMember IsValid is false, and the UI controls didn't report any errors, it is probably because the custom rules of GroupMember didn't pass.
            // So, make sure a message is displayed in the validation summary
            cvEditGroupMember.IsValid = groupMember.IsValid;

            if (!cvEditGroupMember.IsValid)
            {
                cvEditGroupMember.ErrorMessage = groupMember.ValidationResults.Select(a => a.ErrorMessage).ToList().AsDelimited("<br />");
                return;
            }

            // using WrapTransaction because there are two Saves
            rockContext.WrapTransaction(() =>
            {
                if (groupMember.Id.Equals(0))
                {
                    groupMemberService.Add(groupMember);
                }

                rockContext.SaveChanges();
                groupMember.SaveAttributeValues(rockContext);
            });

            Group group = new GroupService(rockContext).Get(groupMember.GroupId);

            if (group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()))
            {
                Rock.Security.Role.Flush(group.Id);
            }

            pnlEditGroupMember.Visible = false;
            pnlGroupView.Visible       = true;
            DisplayViewGroup();
            this.IsEditingGroupMember = 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)
        {
            if (Page.IsValid)
            {
                var rockContext = new RockContext();

                GroupMemberService groupMemberService = new GroupMemberService(rockContext);
                GroupMember        groupMember;

                int groupMemberId = int.Parse(hfGroupMemberId.Value);

                GroupTypeRole role = new GroupTypeRoleService(rockContext).Get(ddlGroupRole.SelectedValueAsInt() ?? 0);

                // check to see if the user selected a role
                if (role == null)
                {
                    nbErrorMessage.Title = "Please select a Role";
                    return;
                }

                // if adding a new group member
                if (groupMemberId.Equals(0))
                {
                    groupMember = new GroupMember {
                        Id = 0
                    };
                    groupMember.GroupId = hfGroupId.ValueAsInt();
                }
                else
                {
                    // load existing group member
                    groupMember = groupMemberService.Get(groupMemberId);
                }

                groupMember.PersonId          = ppGroupMemberPerson.PersonId.Value;
                groupMember.GroupRoleId       = role.Id;
                groupMember.GroupMemberStatus = rblStatus.SelectedValueAsEnum <GroupMemberStatus>();

                groupMember.LoadAttributes();

                Rock.Attribute.Helper.GetEditValues(phAttributes, groupMember);

                if (!Page.IsValid)
                {
                    return;
                }

                cvGroupMember.IsValid = groupMember.IsValid;

                if (!cvGroupMember.IsValid)
                {
                    cvGroupMember.ErrorMessage = groupMember.ValidationResults.Select(a => a.ErrorMessage).ToList().AsDelimited("<br />");
                    return;
                }

                // using WrapTransaction because there are two Saves
                rockContext.WrapTransaction(() =>
                {
                    if (groupMember.Id.Equals(0))
                    {
                        groupMemberService.Add(groupMember);
                    }

                    rockContext.SaveChanges();
                    groupMember.SaveAttributeValues(rockContext);
                });

                Group group = new GroupService(rockContext).Get(groupMember.GroupId);
                if (group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()))
                {
                    Rock.Security.Role.Flush(group.Id);
                    Rock.Security.Authorization.Flush();
                }
            }

            Dictionary <string, string> qryString = new Dictionary <string, string>();

            qryString["GroupId"] = hfGroupId.Value;
            NavigateToParentPage(qryString);
        }
Exemple #29
0
        /// <summary>
        /// Handles the Click event of the btnMoveGroupMember 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 btnMoveGroupMember_Click(object sender, EventArgs e)
        {
            var rockContext        = new RockContext();
            var groupMemberService = new GroupMemberService(rockContext);
            var groupMember        = groupMemberService.Get(hfGroupMemberId.Value.AsInteger());

            groupMember.LoadAttributes();
            int destGroupId = gpMoveGroupMember.SelectedValue.AsInteger();
            var destGroup   = new GroupService(rockContext).Get(destGroupId);

            var destGroupMember = groupMemberService.Queryable().Where(a =>
                                                                       a.GroupId == destGroupId &&
                                                                       a.PersonId == groupMember.PersonId &&
                                                                       a.GroupRoleId == grpMoveGroupMember.GroupRoleId).FirstOrDefault();

            if (destGroupMember != null)
            {
                nbMoveGroupMemberWarning.Visible = true;
                nbMoveGroupMemberWarning.Text    = string.Format("{0} is already in {1}", groupMember.Person, destGroupMember.Group);
                return;
            }

            if (!grpMoveGroupMember.GroupRoleId.HasValue)
            {
                nbMoveGroupMemberWarning.Visible = true;
                nbMoveGroupMemberWarning.Text    = string.Format("Please select a Group Role");
                return;
            }

            string canDeleteWarning;

            if (!groupMemberService.CanDelete(groupMember, out canDeleteWarning))
            {
                nbMoveGroupMemberWarning.Visible = true;
                nbMoveGroupMemberWarning.Text    = string.Format("Unable to remove {0} from {1}: {2}", groupMember.Person, groupMember.Group, canDeleteWarning);
                return;
            }

            destGroupMember             = new GroupMember();
            destGroupMember.GroupId     = destGroupId;
            destGroupMember.GroupRoleId = grpMoveGroupMember.GroupRoleId.Value;
            destGroupMember.PersonId    = groupMember.PersonId;
            destGroupMember.LoadAttributes();

            foreach (var attribute in groupMember.Attributes)
            {
                if (destGroupMember.Attributes.Any(a => a.Key == attribute.Key && a.Value.FieldTypeId == attribute.Value.FieldTypeId))
                {
                    destGroupMember.SetAttributeValue(attribute.Key, groupMember.GetAttributeValue(attribute.Key));
                }
            }

            // Un-link any registrant records that point to this group member.
            foreach (var registrant in new RegistrationRegistrantService(rockContext).Queryable()
                     .Where(r => r.GroupMemberId == groupMember.Id))
            {
                registrant.GroupMemberId = null;
            }

            rockContext.WrapTransaction(() =>
            {
                groupMemberService.Add(destGroupMember);
                rockContext.SaveChanges();
                destGroupMember.SaveAttributeValues(rockContext);

                // move any Note records that were associated with the old groupMember to the new groupMember record
                if (cbMoveGroupMemberMoveNotes.Checked)
                {
                    destGroupMember.Note        = groupMember.Note;
                    int groupMemberEntityTypeId = EntityTypeCache.GetId <Rock.Model.GroupMember>().Value;
                    var noteService             = new NoteService(rockContext);
                    var groupMemberNotes        = noteService.Queryable().Where(a => a.NoteType.EntityTypeId == groupMemberEntityTypeId && a.EntityId == groupMember.Id);
                    foreach (var note in groupMemberNotes)
                    {
                        note.EntityId = destGroupMember.Id;
                    }

                    rockContext.SaveChanges();
                }

                groupMemberService.Delete(groupMember);
                rockContext.SaveChanges();

                destGroupMember.CalculateRequirements(rockContext, true);
            });

            var queryString = new Dictionary <string, string>();

            queryString.Add("GroupMemberId", destGroupMember.Id.ToString());
            this.NavigateToPage(this.RockPage.Guid, queryString);
        }
Exemple #30
0
        private void SaveGroupMember()
        {
            if (Page.IsValid)
            {
                var rockContext = new RockContext();

                // Verify valid group
                var groupService = new GroupService(rockContext);
                var group        = groupService.Get(hfGroupId.ValueAsInt());
                if (group == null)
                {
                    nbErrorMessage.Title = "Please select a Role";
                    return;
                }

                // Check to see if a person was selected
                int?personId      = ppGroupMemberPerson.PersonId;
                int?personAliasId = ppGroupMemberPerson.PersonAliasId;
                if (!personId.HasValue || !personAliasId.HasValue)
                {
                    nbErrorMessage.Title = "Please select a Person";
                    return;
                }

                // check to see if the user selected a role
                var role = new GroupTypeRoleService(rockContext).Get(ddlGroupRole.SelectedValueAsInt() ?? 0);
                if (role == null)
                {
                    nbErrorMessage.Title = "Please select a Role";
                    return;
                }

                var         groupMemberService            = new GroupMemberService(rockContext);
                var         groupMemberRequirementService = new GroupMemberRequirementService(rockContext);
                GroupMember groupMember;

                int groupMemberId = int.Parse(hfGroupMemberId.Value);

                // if adding a new group member
                if (groupMemberId.Equals(0))
                {
                    groupMember = new GroupMember {
                        Id = 0
                    };
                    groupMember.GroupId = group.Id;
                }
                else
                {
                    // load existing group member
                    groupMember = groupMemberService.Get(groupMemberId);
                }

                groupMember.PersonId          = personId.Value;
                groupMember.GroupRoleId       = role.Id;
                groupMember.Note              = tbNote.Text;
                groupMember.GroupMemberStatus = rblStatus.SelectedValueAsEnum <GroupMemberStatus>();

                if (cbIsNotified.Visible)
                {
                    groupMember.IsNotified = cbIsNotified.Checked;
                }

                if (pnlRequirements.Visible)
                {
                    foreach (var checkboxItem in cblManualRequirements.Items.OfType <ListItem>())
                    {
                        int  groupRequirementId     = checkboxItem.Value.AsInteger();
                        var  groupMemberRequirement = groupMember.GroupMemberRequirements.FirstOrDefault(a => a.GroupRequirementId == groupRequirementId);
                        bool metRequirement         = checkboxItem.Selected;
                        if (metRequirement)
                        {
                            if (groupMemberRequirement == null)
                            {
                                groupMemberRequirement = new GroupMemberRequirement();
                                groupMemberRequirement.GroupRequirementId = groupRequirementId;

                                groupMember.GroupMemberRequirements.Add(groupMemberRequirement);
                            }

                            // set the RequirementMetDateTime if it hasn't been set already
                            groupMemberRequirement.RequirementMetDateTime = groupMemberRequirement.RequirementMetDateTime ?? RockDateTime.Now;

                            groupMemberRequirement.LastRequirementCheckDateTime = RockDateTime.Now;
                        }
                        else
                        {
                            if (groupMemberRequirement != null)
                            {
                                // doesn't meets the requirement
                                groupMemberRequirement.RequirementMetDateTime       = null;
                                groupMemberRequirement.LastRequirementCheckDateTime = RockDateTime.Now;
                            }
                        }
                    }
                }

                if (group.RequiredSignatureDocumentTemplate != null)
                {
                    var person = new PersonService(rockContext).Get(personId.Value);

                    var documentService        = new SignatureDocumentService(rockContext);
                    var binaryFileService      = new BinaryFileService(rockContext);
                    SignatureDocument document = null;

                    int?signatureDocumentId = hfSignedDocumentId.Value.AsIntegerOrNull();
                    int?binaryFileId        = fuSignedDocument.BinaryFileId;
                    if (signatureDocumentId.HasValue)
                    {
                        document = documentService.Get(signatureDocumentId.Value);
                    }

                    if (document == null && binaryFileId.HasValue)
                    {
                        document = new SignatureDocument();
                        document.SignatureDocumentTemplateId = group.RequiredSignatureDocumentTemplate.Id;
                        document.AppliesToPersonAliasId      = personAliasId.Value;
                        document.AssignedToPersonAliasId     = personAliasId.Value;
                        document.Name = string.Format("{0}_{1}",
                                                      group.Name.RemoveSpecialCharacters(),
                                                      (person != null ? person.FullName.RemoveSpecialCharacters() : string.Empty));
                        document.Status         = SignatureDocumentStatus.Signed;
                        document.LastStatusDate = RockDateTime.Now;
                        documentService.Add(document);
                    }

                    if (document != null)
                    {
                        int?origBinaryFileId = document.BinaryFileId;
                        document.BinaryFileId = binaryFileId;

                        if (origBinaryFileId.HasValue && origBinaryFileId.Value != document.BinaryFileId)
                        {
                            // if a new the binaryFile was uploaded, mark the old one as Temporary so that it gets cleaned up
                            var oldBinaryFile = binaryFileService.Get(origBinaryFileId.Value);
                            if (oldBinaryFile != null && !oldBinaryFile.IsTemporary)
                            {
                                oldBinaryFile.IsTemporary = true;
                            }
                        }

                        // ensure the IsTemporary is set to false on binaryFile associated with this document
                        if (document.BinaryFileId.HasValue)
                        {
                            var binaryFile = binaryFileService.Get(document.BinaryFileId.Value);
                            if (binaryFile != null && binaryFile.IsTemporary)
                            {
                                binaryFile.IsTemporary = false;
                            }
                        }
                    }
                }

                groupMember.LoadAttributes();

                Rock.Attribute.Helper.GetEditValues(phAttributes, groupMember);

                if (!Page.IsValid)
                {
                    return;
                }

                // if the groupMember IsValid is false, and the UI controls didn't report any errors, it is probably because the custom rules of GroupMember didn't pass.
                // So, make sure a message is displayed in the validation summary
                cvGroupMember.IsValid = groupMember.IsValidGroupMember(rockContext);

                if (!cvGroupMember.IsValid)
                {
                    cvGroupMember.ErrorMessage = groupMember.ValidationResults.Select(a => a.ErrorMessage).ToList().AsDelimited("<br />");
                    return;
                }

                // using WrapTransaction because there are three Saves
                rockContext.WrapTransaction(() =>
                {
                    if (groupMember.Id.Equals(0))
                    {
                        groupMemberService.Add(groupMember);
                    }

                    rockContext.SaveChanges();
                    groupMember.SaveAttributeValues(rockContext);
                });

                groupMember.CalculateRequirements(rockContext, true);

                if (group.IsSecurityRole || group.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()))
                {
                    Rock.Security.Role.Flush(group.Id);
                }
            }
        }