Ejemplo n.º 1
0
        /// <summary>
        /// Handles the Click event of the btnDelete 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 btnDelete_Click(object sender, EventArgs e)
        {
            int?        parentAccountId = null;
            RockContext rockContext     = new RockContext();

            FinancialAccountService accountService = new FinancialAccountService(rockContext);
            AuthService             authService    = new AuthService(rockContext);
            FinancialAccount        account        = accountService.Get(hfAccountId.Value.AsInteger());

            if (account != null)
            {
                if (!account.IsAuthorized(Authorization.EDIT, this.CurrentPerson))
                {
                    mdDeleteWarning.Show("You are not authorized to delete this account.", ModalAlertType.Information);
                    return;
                }

                parentAccountId = account.ParentAccountId;
                string errorMessage;
                if (!accountService.CanDelete(account, out errorMessage))
                {
                    mdDeleteWarning.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                accountService.Delete(account);

                rockContext.SaveChanges();
            }

            // reload page, selecting the deleted account's parent
            var qryParams = new Dictionary <string, string>();

            if (parentAccountId != null)
            {
                qryParams["AccountId"] = parentAccountId.ToString();
            }

            qryParams["ExpandedIds"] = PageParameter("ExpandedIds");

            NavigateToPage(RockPage.Guid, qryParams);
        }