Exemple #1
0
        private void BuildDynamicControls()
        {
            // Get all the accounts grouped by campus
            if (_campusAccounts == null)
            {
                using (var rockContext = new RockContext())
                {
                    _campusAccounts = new Dictionary <int, Dictionary <int, string> >();

                    Guid contributionGuid       = Rock.SystemGuid.DefinedValue.TRANSACTION_TYPE_CONTRIBUTION.AsGuid();
                    var  contributionAccountIds = new FinancialTransactionDetailService(rockContext)
                                                  .Queryable().AsNoTracking()
                                                  .Where(d =>
                                                         d.Transaction != null &&
                                                         d.Transaction.TransactionTypeValue != null &&
                                                         d.Transaction.TransactionTypeValue.Guid.Equals(contributionGuid))
                                                  .Select(d => d.AccountId)
                                                  .Distinct()
                                                  .ToList();

                    foreach (var campusAccounts in new FinancialAccountService(rockContext)
                             .Queryable().AsNoTracking()
                             .Where(a =>
                                    a.IsActive &&
                                    contributionAccountIds.Contains(a.Id))
                             .GroupBy(a => a.CampusId ?? 0)
                             .Select(c => new
                    {
                        CampusId = c.Key,
                        Accounts = c.OrderBy(a => a.Name).Select(a => new { a.Id, a.Name }).ToList()
                    }))
                    {
                        _campusAccounts.Add(campusAccounts.CampusId, new Dictionary <int, string>());
                        foreach (var account in campusAccounts.Accounts)
                        {
                            _campusAccounts[campusAccounts.CampusId].Add(account.Id, account.Name);
                        }
                    }
                }
            }

            phAccounts.Controls.Clear();

            foreach (var campusId in _campusAccounts)
            {
                var cbList = new RockCheckBoxList();
                cbList.ID = "cblAccounts" + campusId.Key.ToString();

                if (campusId.Key > 0)
                {
                    var campus = CampusCache.Read(campusId.Key);
                    cbList.Label = campus != null ? campus.Name + " Accounts" : "Campus " + campusId.Key.ToString();
                }
                else
                {
                    cbList.Label = "Accounts";
                }

                cbList.RepeatDirection = RepeatDirection.Vertical;
                cbList.DataValueField  = "Key";
                cbList.DataTextField   = "Value";
                cbList.DataSource      = campusId.Value;
                cbList.DataBind();

                phAccounts.Controls.Add(cbList);
            }
        }