/// <summary>
        /// Binds the group members grid.
        /// </summary>
        protected void BindGroupMembersGrid()
        {
            var showGoal      = GetAttributeValue("ShowMemberFundingGoal").AsBoolean();
            var showTotal     = GetAttributeValue("ShowMemberTotalFunding").AsBoolean();
            var showRemaining = GetAttributeValue("ShowMemberFundingRemaining").AsBoolean();

            gGroupMembers.Columns[4].Visible = showGoal;
            gGroupMembers.Columns[5].Visible = showTotal;
            gGroupMembers.Columns[6].Visible = showRemaining;

            var rockContext = new RockContext();

            int groupId = hfGroupId.Value.AsInteger();
            var totalFundraisingGoal  = 0.00M;
            var totalContribution     = 0.00M;
            var totalFundingRemaining = 0.00M;
            var groupMembersQuery     = new GroupMemberService(rockContext).Queryable().Where(a => a.GroupId == groupId);
            var group = new GroupService(rockContext).Get(groupId);

            group.LoadAttributes(rockContext);
            var defaultIndividualFundRaisingGoal = group.GetAttributeValue("IndividualFundraisingGoal").AsDecimalOrNull();

            groupMembersQuery = groupMembersQuery.Sort(gGroupMembers.SortProperty ?? new SortProperty {
                Property = "Person.LastName, Person.NickName"
            });

            var entityTypeIdGroupMember = EntityTypeCache.GetId <Rock.Model.GroupMember>();
            var entityTypeIdPerson      = EntityTypeCache.GetId <Rock.Model.Person>();

            var groupMemberList = groupMembersQuery.ToList().Select(a =>
            {
                var groupMember = a;
                groupMember.LoadAttributes(rockContext);

                var contributionTotal = new FinancialTransactionDetailService(rockContext).Queryable()
                                        .Where(d => d.EntityTypeId == entityTypeIdGroupMember &&
                                               d.EntityId == groupMember.Id)
                                        .Sum(d => ( decimal? )d.Amount) ?? 0.00M;

                var individualFundraisingGoal          = groupMember.GetAttributeValue("IndividualFundraisingGoal").AsDecimalOrNull();
                bool disablePublicContributionRequests = groupMember.GetAttributeValue("DisablePublicContributionRequests").AsBoolean();
                if (!individualFundraisingGoal.HasValue)
                {
                    individualFundraisingGoal = group.GetAttributeValue("IndividualFundraisingGoal").AsDecimalOrNull();
                }

                var fundingRemaining = individualFundraisingGoal - contributionTotal;
                if (disablePublicContributionRequests || !showRemaining)
                {
                    fundingRemaining = null;
                }
                else if (fundingRemaining < 0)
                {
                    fundingRemaining = 0.00M;
                }

                totalFundraisingGoal  += (individualFundraisingGoal != null ? ( decimal )individualFundraisingGoal : 0);
                totalContribution     += contributionTotal;
                totalFundingRemaining += (fundingRemaining != null ? ( decimal )fundingRemaining : 0);

                if (!showGoal)
                {
                    individualFundraisingGoal = null;
                    totalFundraisingGoal      = 0.00M;
                }

                if (!showTotal)
                {
                    contributionTotal = 0.00M;
                    totalContribution = 0.00M;
                }

                return(new
                {
                    groupMember.Id,
                    PersonId = groupMember.PersonId,
                    DateTimeAdded = groupMember.DateTimeAdded,
                    groupMember.Person.FullName,
                    groupMember.Person.Gender,
                    FundingRemaining = fundingRemaining,
                    GroupRoleName = a.GroupRole.Name,
                    FundingGoal = individualFundraisingGoal,
                    TotalFunding = contributionTotal,
                    Email = groupMember.Person.Email
                });
            }).ToList();

            _groupTotals.Add("TotalFundraisingGoal", totalFundraisingGoal);
            _groupTotals.Add("TotalContribution", totalContribution);
            _groupTotals.Add("TotalFundingRemaining", totalFundingRemaining);

            //
            // Attributes
            //

            BindAttributes(group);
            AddDynamicControls(group);

            // Get all the person ids in current page of query results
            var personIds = groupMemberList
                            .Select(m => m.PersonId)
                            .Distinct()
                            .ToList();

            // Get all the group member ids and the group id in current page of query results
            var groupMemberIds = new List <int>();

            foreach (var groupMember in groupMemberList
                     .Select(m => m))
            {
                groupMemberIds.Add(groupMember.Id);
            }

            var groupMemberAttributesIds = new List <int>();

            foreach (var gma in AvailableAttributes.Where(a => a.EntityTypeId == entityTypeIdGroupMember))
            {
                groupMemberAttributesIds.Add(gma.Id);
            }

            var personAttributesIds = new List <int>();

            foreach (var pa in AvailableAttributes.Where(a => a.EntityTypeId == entityTypeIdPerson))
            {
                personAttributesIds.Add(pa.Id);
            }

            // If there are any attributes that were selected to be displayed, we're going
            // to try and read all attribute values in one query and then put them into a
            // custom grid ObjectList property so that the AttributeField columns don't need
            // to do the LoadAttributes and querying of values for each row/column

            // Query the attribute values for all rows and attributes
            var attributeValues = new AttributeValueService(rockContext)
                                  .Queryable("Attribute").AsNoTracking()
                                  .Where(v =>
                                         v.EntityId.HasValue &&
                                         (
                                             (
                                                 personAttributesIds.Contains(v.AttributeId) &&
                                                 personIds.Contains(v.EntityId.Value)
                                             ) ||
                                             (
                                                 groupMemberAttributesIds.Contains(v.AttributeId) &&
                                                 groupMemberIds.Contains(v.EntityId.Value)
                                             )
                                         )
                                         )
                                  .ToList();

            // Get the attributes to add to each row's object
            BindAttributes(group);
            var attributes = new Dictionary <string, AttributeCache>();

            AvailableAttributes.ForEach(a => attributes
                                        .Add(a.Id.ToString() + a.Key, a));

            // Initialize the grid's object list
            gGroupMembers.ObjectList = new Dictionary <string, object>();

            // Loop through each of the current page's registrants and build an attribute
            // field object for storing attributes and the values for each of the registrants
            foreach (var gm in groupMemberList)
            {
                // Create a row attribute object
                var attributeFieldObject = new AttributeFieldObject();

                // Add the attributes to the attribute object
                attributeFieldObject.Attributes = attributes;

                // Add any person attribute values to object
                attributeValues
                .Where(v =>
                       personAttributesIds.Contains(v.AttributeId) &&
                       v.EntityId.Value == gm.PersonId)
                .ToList()
                .ForEach(v => attributeFieldObject.AttributeValues
                         .Add(v.AttributeId.ToString() + v.Attribute.Key, new AttributeValueCache(v)));

                // Add any group member attribute values to object
                attributeValues
                .Where(v =>
                       groupMemberAttributesIds.Contains(v.AttributeId) &&
                       v.EntityId.Value == gm.Id)
                .ToList()
                .ForEach(v => attributeFieldObject.AttributeValues
                         .Add(v.AttributeId.ToString() + v.Attribute.Key, new AttributeValueCache(v)));

                // Add row attribute object to grid's object list
                gGroupMembers.ObjectList.Add(gm.Id.ToString(), attributeFieldObject);
            }

            gGroupMembers.DataSource = groupMemberList;
            gGroupMembers.DataBind();
        }
Exemple #2
0
        /// <summary>
        /// Binds the group members grid.
        /// </summary>
        /// <remarks>Multiple methods depend on the GroupMember object type</remarks>
        /// <param name="isExporting">if set to <c>true</c> [is exporting].</param>
        protected void BindGroupMembersGrid(bool isExporting = false)
        {
            if (_group != null && _group.GroupType.Roles.Any())
            {
                pnlGroupMembers.Visible = true;

                using (var rockContext = new RockContext())
                {
                    // Start query for group members
                    var qry = new GroupMemberService(rockContext)
                              .Queryable("Person,GroupRole", true)
                              .Where(m => m.GroupId == _group.Id && m.Person != null);

                    // Sort the query
                    IOrderedQueryable <GroupMember> orderedQry = null;
                    var sortProperty = pnlGroupMembers.SortProperty;
                    if (sortProperty != null)
                    {
                        orderedQry = qry.Sort(sortProperty);
                    }
                    else
                    {
                        orderedQry = qry
                                     .OrderBy(r => r.Person.LastName)
                                     .ThenBy(r => r.Person.NickName);
                    }

                    // increase the timeout just in case. A complex filter on the grid might slow things down
                    rockContext.Database.CommandTimeout = 180;

                    // Set the grids LinqDataSource which will run query and set results for current page
                    pnlGroupMembers.SetLinqDataSource(orderedQry);

                    var homePhoneType = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_PHONE_TYPE_HOME);
                    var cellPhoneType = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.PERSON_PHONE_TYPE_MOBILE);

                    var groupAttributes = GetGroupAttributes();
                    if (groupAttributes.Any())
                    {
                        // Get the query results for the current page
                        var currentGroupMembers = pnlGroupMembers.DataSource as List <GroupMember>;
                        if (currentGroupMembers != null)
                        {
                            // Get all the person ids in current page of query results
                            var personIds = currentGroupMembers
                                            .Select(r => r.PersonId)
                                            .Distinct()
                                            .ToList();

                            var groupMemberIds = currentGroupMembers
                                                 .Select(r => r.Id)
                                                 .Distinct()
                                                 .ToList();

                            var groupMemberAttributesIds = groupAttributes.Select(a => a.Id).Distinct().ToList();

                            // If there are any attributes that were selected to be displayed, we're going
                            // to try and read all attribute values in one query and then put them into a
                            // custom grid ObjectList property so that the AttributeField columns don't need
                            // to do the LoadAttributes and querying of values for each row/column
                            if (groupMemberAttributesIds.Any())
                            {
                                // Query the attribute values for all rows and attributes
                                var attributeValues = new AttributeValueService(rockContext)
                                                      .Queryable("Attribute").AsNoTracking()
                                                      .Where(v =>
                                                             v.EntityId.HasValue &&
                                                             groupMemberAttributesIds.Contains(v.AttributeId) &&
                                                             groupMemberIds.Contains(v.EntityId.Value)
                                                             )
                                                      .ToList();

                                // Get the attributes to add to each row's object
                                var attributes = new Dictionary <string, AttributeCache>();
                                groupAttributes.ForEach(a =>
                                                        attributes.Add(a.Id + a.Key, a));

                                // Initialize the grid's object list
                                pnlGroupMembers.ObjectList   = new Dictionary <string, object>();
                                pnlGroupMembers.EntityTypeId = EntityTypeCache.Get(Rock.SystemGuid.EntityType.GROUP_MEMBER.AsGuid()).Id;

                                // Loop through each of the current group's members and build an attribute
                                // field object for storing attributes and the values for each of the members
                                foreach (var groupMember in currentGroupMembers)
                                {
                                    // Create a row attribute object
                                    var attributeFieldObject = new AttributeFieldObject
                                    {
                                        // Add the attributes to the attribute object
                                        Attributes = attributes
                                    };

                                    // Add any group member attribute values to object
                                    if (groupMember.Id > 0)
                                    {
                                        attributeValues
                                        .Where(v =>
                                               groupMemberAttributesIds.Contains(v.AttributeId) &&
                                               v.EntityId.Value == groupMember.Id)
                                        .ToList()
                                        .ForEach(v => attributeFieldObject.AttributeValues
                                                 .Add(v.AttributeId + v.Attribute.Key, new AttributeValueCache(v)));
                                    }

                                    // Add row attribute object to grid's object list
                                    pnlGroupMembers.ObjectList.Add(groupMember.Id.ToString(), attributeFieldObject);
                                }
                            }
                        }
                    }

                    pnlGroupMembers.DataBind();
                }
            }
        }