From: Aidan Saunders Date: Tue, 22 Nov 2022 16:55:21 +0000 (+0000) Subject: Deprecate `Financial_BAO_EntityFinancialAccount::del()` X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=8cd283f383657b178f82ba6d9b100dde2829b0f9;p=civicrm-core.git Deprecate `Financial_BAO_EntityFinancialAccount::del()` Change 'setStatus' to exceptions and create status messages at form layer. --- diff --git a/CRM/Financial/BAO/EntityFinancialAccount.php b/CRM/Financial/BAO/EntityFinancialAccount.php index df7c195749..f100f717bb 100644 --- a/CRM/Financial/BAO/EntityFinancialAccount.php +++ b/CRM/Financial/BAO/EntityFinancialAccount.php @@ -75,59 +75,65 @@ class CRM_Financial_BAO_EntityFinancialAccount extends CRM_Financial_DAO_EntityF * Delete financial Types. * * @param int $financialTypeAccountId - * @param int $accountId - * @deprecated + * @param int $accountId (not used) + * * @throws \CRM_Core_Exception + * @deprecated */ public static function del($financialTypeAccountId, $accountId = NULL) { - // check if financial type is present - $check = FALSE; - $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship'); + static::deleteRecord(['id' => $financialTypeAccountId]); + } - $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id'); - // check dependencies - // FIXME hardcoded list = bad - $dependency = [ - ['Contribute', 'Contribution'], - ['Contribute', 'ContributionPage'], - ['Member', 'MembershipType'], - ['Price', 'PriceFieldValue'], - ['Grant', 'Grant'], - ['Contribute', 'PremiumsProduct'], - ['Contribute', 'Product'], - ['Price', 'LineItem'], - ]; + /** + * Callback for hook_civicrm_pre(). + * @param \Civi\Core\Event\PreEvent $event + * @throws CRM_Core_Exception + */ + public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) { + if ($event->action === 'delete' && $event->id) { + $financialTypeAccountId = $event->id; - foreach ($dependency as $name) { - $daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1]; - if (class_exists($daoString)) { - /** @var \CRM_Core_DAO $dao */ - $dao = new $daoString(); - $dao->financial_type_id = $financialTypeId; - if ($dao->find(TRUE)) { - $check = TRUE; - break; + // check if financial type is present + $check = FALSE; + $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship'); + + $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id'); + // check dependencies + // FIXME hardcoded list = bad + $dependency = [ + ['Contribute', 'Contribution'], + ['Contribute', 'ContributionPage'], + ['Member', 'MembershipType'], + ['Price', 'PriceFieldValue'], + ['Grant', 'Grant'], + ['Contribute', 'PremiumsProduct'], + ['Contribute', 'Product'], + ['Price', 'LineItem'], + ]; + + foreach ($dependency as $name) { + $daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1]; + if (class_exists($daoString)) { + /** @var \CRM_Core_DAO $dao */ + $dao = new $daoString(); + $dao->financial_type_id = $financialTypeId; + if ($dao->find(TRUE)) { + $check = TRUE; + break; + } } } - } - if ($check) { - if ($name[1] === 'PremiumsProduct' || $name[1] === 'Product') { - CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship while the Financial Type is used for a Premium.', [1 => $relationValues[$financialTypeAccountId]])); - } - else { - $accountRelationShipId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship'); - CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship because it is being referenced by one or more of the following types of records: Contributions, Contribution Pages, or Membership Types. Consider disabling this type instead if you no longer want it used.', [1 => $relationValues[$accountRelationShipId]]), NULL, 'error'); + if ($check) { + if ($name[1] === 'PremiumsProduct' || $name[1] === 'Product') { + throw new \CRM_Core_Exception(ts('You cannot remove an account with a %1 relationship while the Financial Type is used for a Premium.', [1 => $relationValues[$financialTypeAccountId]])); + } + else { + $accountRelationShipId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship'); + throw new \CRM_Core_Exception(ts('You cannot remove an account with a %1 relationship because it is being referenced by one or more of the following types of records: Contributions, Contribution Pages, or Membership Types. Consider disabling this type instead if you no longer want it used.', [1 => $relationValues[$accountRelationShipId]]), NULL, 'error'); + } } - CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$accountId}")); } - - // delete from financial Type table - $financialType = new CRM_Financial_DAO_EntityFinancialAccount(); - $financialType->id = $financialTypeAccountId; - $financialType->find(TRUE); - $financialType->delete(); - CRM_Core_Session::setStatus(ts('Unbalanced transactions may be created if you delete the account of type: %1.', [1 => $relationValues[$financialType->account_relationship]])); } /** diff --git a/CRM/Financial/BAO/FinancialTypeAccount.php b/CRM/Financial/BAO/FinancialTypeAccount.php index 4e0443aa64..fdc1de245f 100644 --- a/CRM/Financial/BAO/FinancialTypeAccount.php +++ b/CRM/Financial/BAO/FinancialTypeAccount.php @@ -4,4 +4,5 @@ * Class was consolidated in 5.57 to follow standard naming convention * @deprecated */ + class_alias('CRM_Financial_BAO_EntityFinancialAccount', 'CRM_Financial_BAO_FinancialTypeAccount'); diff --git a/CRM/Financial/Form/FinancialTypeAccount.php b/CRM/Financial/Form/FinancialTypeAccount.php index 561853ad38..15abe94df5 100644 --- a/CRM/Financial/Form/FinancialTypeAccount.php +++ b/CRM/Financial/Form/FinancialTypeAccount.php @@ -272,7 +272,14 @@ class CRM_Financial_Form_FinancialTypeAccount extends CRM_Core_Form { */ public function postProcess() { if ($this->_action & CRM_Core_Action::DELETE) { - CRM_Financial_BAO_EntityFinancialAccount::del($this->_id, $this->_aid); + try { + CRM_Financial_BAO_FinancialTypeAccount::del($this->_id); + CRM_Financial_BAO_EntityFinancialAccount::del($this->_id); + } + catch (CRM_Core_Exception $e) { + CRM_Core_Session::setStatus($e->message); + return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$this->_aid}")); + } CRM_Core_Session::setStatus(ts('Selected financial type account has been deleted.')); } else {