From 3c609ef046571d4838530aa1864f09a91e0f347a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 18 Nov 2021 16:54:17 -0500 Subject: [PATCH] CiviGrant - Remove or mitigate hardcoded references to civicrm_grant table in core Removes CiviGrant from hardcoded lists when possible, or at least puts up guards to check that it's available before attempting to use it. --- CRM/Contact/BAO/Contact.php | 3 --- CRM/Core/BAO/Mapping.php | 2 +- CRM/Financial/BAO/FinancialTypeAccount.php | 16 +++++++++------- CRM/Utils/VersionCheck.php | 20 ++++++++++++-------- ext/civigrant/CRM/Grant/Page/Tab.php | 2 +- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 9e8aceb496..6b0f077009 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -2742,9 +2742,6 @@ LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id ) case 'case': return CRM_Case_BAO_Case::caseCount($contactId); - case 'grant': - return CRM_Grant_BAO_Grant::getContactGrantCount($contactId); - case 'activity': $input = [ 'contact_id' => $contactId, diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index f4c2378f45..b9d8c7d19a 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -760,7 +760,7 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping implements \Civi\Test\Ho } } if (($mappingType == 'Search Builder') || ($exportMode == CRM_Export_Form_Select::GRANT_EXPORT)) { - if (CRM_Core_Permission::access('CiviGrant')) { + if (CRM_Core_Permission::check('access CiviGrant')) { $fields['Grant'] = CRM_Grant_BAO_Grant::exportableFields(); unset($fields['Grant']['grant_contact_id']); if ($mappingType == 'Search Builder') { diff --git a/CRM/Financial/BAO/FinancialTypeAccount.php b/CRM/Financial/BAO/FinancialTypeAccount.php index 2682d11f2d..f54d4c0a20 100644 --- a/CRM/Financial/BAO/FinancialTypeAccount.php +++ b/CRM/Financial/BAO/FinancialTypeAccount.php @@ -86,7 +86,7 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id'); // check dependencies - // FIXME more table containing financial_type_id to come + // FIXME hardcoded list = bad $dependency = [ ['Contribute', 'Contribution'], ['Contribute', 'ContributionPage'], @@ -100,12 +100,14 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin foreach ($dependency as $name) { $daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1]; - /* @var \CRM_Core_DAO $dao */ - $dao = new $daoString(); - $dao->financial_type_id = $financialTypeId; - if ($dao->find(TRUE)) { - $check = TRUE; - break; + if (class_exists($daoString)) { + /* @var \CRM_Core_DAO $dao */ + $dao = new $daoString(); + $dao->financial_type_id = $financialTypeId; + if ($dao->find(TRUE)) { + $check = TRUE; + break; + } } } diff --git a/CRM/Utils/VersionCheck.php b/CRM/Utils/VersionCheck.php index fbb05a1929..4a9407f5db 100644 --- a/CRM/Utils/VersionCheck.php +++ b/CRM/Utils/VersionCheck.php @@ -178,6 +178,7 @@ class CRM_Utils_VersionCheck { * Add info to the 'entities' array */ private function getEntityStats() { + // FIXME hardcoded list = bad $tables = [ 'CRM_Activity_DAO_Activity' => 'is_test = 0', 'CRM_Case_DAO_Case' => 'is_deleted = 0', @@ -203,15 +204,18 @@ class CRM_Utils_VersionCheck { 'CRM_Mailing_Event_DAO_Delivered' => NULL, ]; foreach ($tables as $daoName => $where) { - $dao = new $daoName(); - if ($where) { - $dao->whereAdd($where); + if (class_exists($daoName)) { + /* @var \CRM_Core_DAO $dao */ + $dao = new $daoName(); + if ($where) { + $dao->whereAdd($where); + } + $short_name = substr($daoName, strrpos($daoName, '_') + 1); + $this->stats['entities'][] = [ + 'name' => $short_name, + 'size' => $dao->count(), + ]; } - $short_name = substr($daoName, strrpos($daoName, '_') + 1); - $this->stats['entities'][] = [ - 'name' => $short_name, - 'size' => $dao->count(), - ]; } } diff --git a/ext/civigrant/CRM/Grant/Page/Tab.php b/ext/civigrant/CRM/Grant/Page/Tab.php index 4f2219364a..40fe12bcd6 100644 --- a/ext/civigrant/CRM/Grant/Page/Tab.php +++ b/ext/civigrant/CRM/Grant/Page/Tab.php @@ -46,7 +46,7 @@ class CRM_Grant_Page_Tab extends CRM_Contact_Page_View { if ($this->_contactId) { $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId); $this->assign('displayName', $displayName); - $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent('grant', $this->_contactId); + $this->ajaxResponse['tabCount'] = CRM_Grant_BAO_Grant::getContactGrantCount($this->_contactId); } } -- 2.25.1