From 092cb2a184563ab161195d67fae7f17cc7bc84a8 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Mon, 19 Aug 2019 17:48:04 -0400 Subject: [PATCH] convert to using a civix-generated entity --- .../BAO/GrantBudget.php | 8 +- .../DAO/GrantBudget.php | 130 ++++++++++-------- CRM/Grant/Page/AnnualBudgets.php | 4 +- annualgrantbudgets.civix.php | 13 +- api/v3/GrantBudget.php | 2 +- sql/auto_install.sql | 106 ++++++++++++-- sql/auto_uninstall.sql | 38 +++++ .../GrantBudget.entityType.php | 11 ++ .../CRM/AnnualGrantBudgets/GrantBudget.xml | 77 +++++++++++ 9 files changed, 309 insertions(+), 80 deletions(-) rename CRM/{Grant => AnnualGrantBudgets}/BAO/GrantBudget.php (96%) rename CRM/{Grant => AnnualGrantBudgets}/DAO/GrantBudget.php (60%) create mode 100644 sql/auto_uninstall.sql create mode 100644 xml/schema/CRM/AnnualGrantBudgets/GrantBudget.entityType.php create mode 100644 xml/schema/CRM/AnnualGrantBudgets/GrantBudget.xml diff --git a/CRM/Grant/BAO/GrantBudget.php b/CRM/AnnualGrantBudgets/BAO/GrantBudget.php similarity index 96% rename from CRM/Grant/BAO/GrantBudget.php rename to CRM/AnnualGrantBudgets/BAO/GrantBudget.php index ff601f6..4b4ff1e 100644 --- a/CRM/Grant/BAO/GrantBudget.php +++ b/CRM/AnnualGrantBudgets/BAO/GrantBudget.php @@ -1,6 +1,6 @@ "value", @@ -56,7 +54,7 @@ class CRM_Grant_BAO_GrantBudget extends CRM_Grant_DAO_GrantBudget { $qParams[6] = [$params['financial_type_id'], 'Integer']; } - $grantBudgetTableName = CRM_Grant_DAO_GrantBudget::getTableName(); + $grantBudgetTableName = CRM_AnnualGrantBudgets_DAO_GrantBudget::getTableName(); $sql = "SELECT cgb.id, cft.name, IFNULL(cgb.budget, 0) AS budget, SUM(CASE WHEN cg.id IS NULL THEN 0 ELSE IFNULL(trxn.total_amount, 0) END) as total_amount_granted, cft.id AS financial_type_id diff --git a/CRM/Grant/DAO/GrantBudget.php b/CRM/AnnualGrantBudgets/DAO/GrantBudget.php similarity index 60% rename from CRM/Grant/DAO/GrantBudget.php rename to CRM/AnnualGrantBudgets/DAO/GrantBudget.php index 314a88c..e1807a7 100644 --- a/CRM/Grant/DAO/GrantBudget.php +++ b/CRM/AnnualGrantBudgets/DAO/GrantBudget.php @@ -1,67 +1,69 @@ __table = 'civicrm_grant_budget'; parent::__construct(); } + /** * Returns foreign keys and entity references. * @@ -70,12 +72,13 @@ class CRM_Grant_DAO_GrantBudget extends CRM_Core_DAO { */ public static function getReferenceColumns() { if (!isset(Civi::$statics[__CLASS__]['links'])) { - Civi::$statics[__CLASS__]['links'] = static ::createReferenceColumns(__CLASS__); - Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'financialtypeid', 'civicrm_financial_type', 'id'); + Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__); + Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'financial_type_id', 'civicrm_financial_type', 'id'); CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']); } return Civi::$statics[__CLASS__]['links']; } + /** * Returns all the column names of this table * @@ -87,25 +90,27 @@ class CRM_Grant_DAO_GrantBudget extends CRM_Core_DAO { 'id' => [ 'name' => 'id', 'type' => CRM_Utils_Type::T_INT, + 'description' => CRM_AnnualGrantBudgets_ExtensionUtil::ts('Unique GrantBudget ID'), 'required' => TRUE, + 'where' => 'civicrm_grant_budget.id', 'table_name' => 'civicrm_grant_budget', 'entity' => 'GrantBudget', - 'bao' => 'CRM_Grant_BAO_GrantBudget', - 'localizable' => 0, + 'bao' => 'CRM_AnnualGrantBudgets_DAO_GrantBudget', + 'localizable' => 1, ], 'financial_type_id' => [ 'name' => 'financial_type_id', 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('Financial Type Id'), - 'description' => 'FK to Financial Type', + 'title' => CRM_AnnualGrantBudgets_ExtensionUtil::ts('Financial Type Id'), + 'description' => CRM_AnnualGrantBudgets_ExtensionUtil::ts('FK to Financial Type'), 'required' => TRUE, 'import' => TRUE, + 'where' => 'civicrm_grant_budget.financial_type_id', 'export' => FALSE, 'table_name' => 'civicrm_grant_budget', 'entity' => 'GrantBudget', - 'bao' => 'CRM_Grant_BAO_GrantBudget', - 'localizable' => 0, - 'FKClassName' => 'CRM_Financial_DAO_FinancialType', + 'bao' => 'CRM_AnnualGrantBudgets_DAO_GrantBudget', + 'localizable' => 1, 'html' => [ 'type' => 'Select', ], @@ -113,43 +118,46 @@ class CRM_Grant_DAO_GrantBudget extends CRM_Core_DAO { 'table' => 'civicrm_financial_type', 'keyColumn' => 'id', 'labelColumn' => 'name', - ] + ], ], 'fiscal_year' => [ 'name' => 'fiscal_year', 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('Fiscal year'), - 'description' => 'Fiscal year', + 'title' => CRM_AnnualGrantBudgets_ExtensionUtil::ts('Fiscal Year'), + 'description' => CRM_AnnualGrantBudgets_ExtensionUtil::ts('Fiscal year'), 'required' => TRUE, 'import' => TRUE, + 'where' => 'civicrm_grant_budget.fiscal_year', 'export' => FALSE, 'table_name' => 'civicrm_grant_budget', 'entity' => 'GrantBudget', - 'bao' => 'CRM_Grant_BAO_GrantBudget', - 'localizable' => 0, + 'bao' => 'CRM_AnnualGrantBudgets_DAO_GrantBudget', + 'localizable' => 1, 'html' => [ 'type' => 'Select', ], 'pseudoconstant' => [ 'callback' => 'CRM_Grant_BAO_GrantBudget::getFiscalyear', - ] + ], ], 'budget' => [ 'name' => 'budget', 'type' => CRM_Utils_Type::T_MONEY, - 'title' => ts('Grant Annual Budget'), - 'description' => 'Grant Annual Budget.', + 'title' => CRM_AnnualGrantBudgets_ExtensionUtil::ts('Grant Annual Budget'), + 'description' => CRM_AnnualGrantBudgets_ExtensionUtil::ts('Grant Annual Budget'), 'required' => TRUE, 'precision' => [ 20, - 2 + 2, ], 'import' => TRUE, + 'where' => 'civicrm_grant_budget.budget', 'export' => TRUE, + 'default' => '0.00', 'table_name' => 'civicrm_grant_budget', 'entity' => 'GrantBudget', - 'bao' => 'CRM_Grant_BAO_GrantBudget', - 'localizable' => 0, + 'bao' => 'CRM_AnnualGrantBudgets_DAO_GrantBudget', + 'localizable' => 1, 'html' => [ 'type' => 'Text', ], @@ -159,6 +167,7 @@ class CRM_Grant_DAO_GrantBudget extends CRM_Core_DAO { } return Civi::$statics[__CLASS__]['fields']; } + /** * Return a mapping from field-name to the corresponding key (as used in fields()). * @@ -171,14 +180,16 @@ class CRM_Grant_DAO_GrantBudget extends CRM_Core_DAO { } return Civi::$statics[__CLASS__]['fieldKeys']; } + /** * Returns the names of this table * * @return string */ public static function getTableName() { - return self::$_tableName; + return CRM_Core_DAO::getLocaleTableName(self::$_tableName); } + /** * Returns if this table needs to be logged * @@ -187,6 +198,7 @@ class CRM_Grant_DAO_GrantBudget extends CRM_Core_DAO { public function getLog() { return self::$_log; } + /** * Returns the list of fields that can be imported * @@ -198,6 +210,7 @@ class CRM_Grant_DAO_GrantBudget extends CRM_Core_DAO { $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, 'grant_budget', $prefix, []); return $r; } + /** * Returns the list of fields that can be exported * @@ -209,8 +222,13 @@ class CRM_Grant_DAO_GrantBudget extends CRM_Core_DAO { $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, 'grant_budget', $prefix, []); return $r; } + /** * Returns the list of indices + * + * @param bool $localize + * + * @return array */ public static function indices($localize = TRUE) { $indices = []; diff --git a/CRM/Grant/Page/AnnualBudgets.php b/CRM/Grant/Page/AnnualBudgets.php index 06693bd..b3e3444 100644 --- a/CRM/Grant/Page/AnnualBudgets.php +++ b/CRM/Grant/Page/AnnualBudgets.php @@ -9,7 +9,7 @@ class CRM_Grant_Page_AnnualBudgets extends CRM_Core_Page_Basic { * Classname of BAO. */ public function getBAOName() { - return 'CRM_Grant_BAO_GrantBudget'; + return 'CRM_AnnualGrantBudgets_BAO_GrantBudget'; } /** @@ -40,7 +40,7 @@ class CRM_Grant_Page_AnnualBudgets extends CRM_Core_Page_Basic { $fiscalYear = date('Y'); } $fiscalYearOptions = ''; - foreach (CRM_Grant_BAO_GrantBudget::getFiscalyear() as $key => $value) { + foreach (CRM_AnnualGrantBudgets_BAO_GrantBudget::getFiscalyear() as $key => $value) { $extra = ''; if ($key == $fiscalYear) { $extra = 'selected="selected"'; diff --git a/annualgrantbudgets.civix.php b/annualgrantbudgets.civix.php index b7f8919..fe9262a 100644 --- a/annualgrantbudgets.civix.php +++ b/annualgrantbudgets.civix.php @@ -263,16 +263,17 @@ function _annualgrantbudgets_civix_find_files($dir, $pattern) { */ function _annualgrantbudgets_civix_civicrm_managed(&$entities) { $mgdFiles = _annualgrantbudgets_civix_find_files(__DIR__, '*.mgd.php'); + sort($mgdFiles); foreach ($mgdFiles as $file) { $es = include $file; foreach ($es as $e) { if (empty($e['module'])) { $e['module'] = E::LONG_NAME; } - $entities[] = $e; if (empty($e['params']['version'])) { $e['params']['version'] = '3'; } + $entities[] = $e; } } } @@ -453,6 +454,14 @@ function _annualgrantbudgets_civix_civicrm_alterSettingsFolders(&$metaDataFolder * * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes */ + function _annualgrantbudgets_civix_civicrm_entityTypes(&$entityTypes) { - $entityTypes = array_merge($entityTypes, []); + $entityTypes = array_merge($entityTypes, array ( + 'CRM_AnnualGrantBudgets_DAO_GrantBudget' => + array ( + 'name' => 'GrantBudget', + 'class' => 'CRM_AnnualGrantBudgets_DAO_GrantBudget', + 'table' => 'civicrm_grant_budget', + ), + )); } diff --git a/api/v3/GrantBudget.php b/api/v3/GrantBudget.php index 7ea41f0..688e471 100644 --- a/api/v3/GrantBudget.php +++ b/api/v3/GrantBudget.php @@ -55,7 +55,7 @@ function civicrm_api3_grant_budget_delete($params) { * Array of deleted values. */ function civicrm_api3_grant_budget_getbudget($params) { - $result = CRM_Grant_BAO_GrantBudget::getGrantBudget($params); + $result = CRM_AnnualGrantBudgets_BAO_GrantBudget::getGrantBudget($params); return civicrm_api3_create_success($result, $params, 'GrantBudget', 'get'); } diff --git a/sql/auto_install.sql b/sql/auto_install.sql index 16b4f48..1c01d29 100644 --- a/sql/auto_install.sql +++ b/sql/auto_install.sql @@ -1,17 +1,95 @@ -CREATE TABLE IF NOT EXISTS `civicrm_grant_budget` ( - `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Primary ID', - `financial_type_id` int(10) UNSIGNED NOT NULL COMMENT 'FK to Financial Type.', - `fiscal_year` int(10) UNSIGNED NOT NULL COMMENT 'Fiscal Year', - `budget` decimal(20,2) DEFAULT '0.00' COMMENT 'Grant annual budget', - `note` varchar(255) DEFAULT NULL COMMENT 'Note', - `is_reserved`, tinyint(4) DEFAULT 0 COMMENT 'Is this grant budget locked?', - PRIMARY KEY (`id`), - KEY `FK_civicrm_grant_budget_financial_type_id` (`financial_type_id`), - UNIQUE KEY `UI_financial_type_id_fiscal_year` (`financial_type_id`,`fiscal_year`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +-- +--------------------------------------------------------------------+ +-- | CiviCRM version 5 | +-- +--------------------------------------------------------------------+ +-- | Copyright CiviCRM LLC (c) 2004-2019 | +-- +--------------------------------------------------------------------+ +-- | This file is a part of CiviCRM. | +-- | | +-- | CiviCRM is free software; you can copy, modify, and distribute it | +-- | under the terms of the GNU Affero General Public License | +-- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | +-- | | +-- | CiviCRM is distributed in the hope that it will be useful, but | +-- | WITHOUT ANY WARRANTY; without even the implied warranty of | +-- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | +-- | See the GNU Affero General Public License for more details. | +-- | | +-- | You should have received a copy of the GNU Affero General Public | +-- | License and the CiviCRM Licensing Exception along | +-- | with this program; if not, contact CiviCRM LLC | +-- | at info[AT]civicrm[DOT]org. If you have questions about the | +-- | GNU Affero General Public License or the licensing of CiviCRM, | +-- | see the CiviCRM license FAQ at http://civicrm.org/licensing | +-- +--------------------------------------------------------------------+ +-- +-- Generated from schema.tpl +-- DO NOT EDIT. Generated by CRM_Core_CodeGen +-- + +-- +--------------------------------------------------------------------+ +-- | CiviCRM version 5 | +-- +--------------------------------------------------------------------+ +-- | Copyright CiviCRM LLC (c) 2004-2019 | +-- +--------------------------------------------------------------------+ +-- | This file is a part of CiviCRM. | +-- | | +-- | CiviCRM is free software; you can copy, modify, and distribute it | +-- | under the terms of the GNU Affero General Public License | +-- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | +-- | | +-- | CiviCRM is distributed in the hope that it will be useful, but | +-- | WITHOUT ANY WARRANTY; without even the implied warranty of | +-- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | +-- | See the GNU Affero General Public License for more details. | +-- | | +-- | You should have received a copy of the GNU Affero General Public | +-- | License and the CiviCRM Licensing Exception along | +-- | with this program; if not, contact CiviCRM LLC | +-- | at info[AT]civicrm[DOT]org. If you have questions about the | +-- | GNU Affero General Public License or the licensing of CiviCRM, | +-- | see the CiviCRM license FAQ at http://civicrm.org/licensing | +-- +--------------------------------------------------------------------+ -- --- Constraints for table `civicrm_grant_budget` +-- Generated from drop.tpl +-- DO NOT EDIT. Generated by CRM_Core_CodeGen -- -ALTER TABLE `civicrm_grant_budget` - ADD CONSTRAINT `FK_civicrm_grant_budget_financial_type_id` FOREIGN KEY (`financial_type_id`) REFERENCES `civicrm_financial_type` (`id`) ON DELETE CASCADE; +-- /******************************************************* +-- * +-- * Clean up the exisiting tables +-- * +-- *******************************************************/ + +SET FOREIGN_KEY_CHECKS=0; + +DROP TABLE IF EXISTS `civicrm_grant_budget`; + +SET FOREIGN_KEY_CHECKS=1; +-- /******************************************************* +-- * +-- * Create new tables +-- * +-- *******************************************************/ + +-- /******************************************************* +-- * +-- * civicrm_grant_budget +-- * +-- * FIXME +-- * +-- *******************************************************/ +CREATE TABLE `civicrm_grant_budget` ( + + + `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique ScholarshipBudget ID', + `financial_type_id` int unsigned NOT NULL COMMENT 'FK to Financial Type', + `fiscal_year` int unsigned NOT NULL COMMENT 'Fiscal year', + `budget` decimal(20,2) NOT NULL DEFAULT 0.00 COMMENT 'Scholarship Annual Budget' +, + PRIMARY KEY (`id`) + + +, CONSTRAINT FK_civicrm_grant_budget_financial_type_id FOREIGN KEY (`financial_type_id`) REFERENCES `civicrm_financial_type`(`id`) +) ; + + diff --git a/sql/auto_uninstall.sql b/sql/auto_uninstall.sql new file mode 100644 index 0000000..6ba06bb --- /dev/null +++ b/sql/auto_uninstall.sql @@ -0,0 +1,38 @@ +-- +--------------------------------------------------------------------+ +-- | CiviCRM version 5 | +-- +--------------------------------------------------------------------+ +-- | Copyright CiviCRM LLC (c) 2004-2019 | +-- +--------------------------------------------------------------------+ +-- | This file is a part of CiviCRM. | +-- | | +-- | CiviCRM is free software; you can copy, modify, and distribute it | +-- | under the terms of the GNU Affero General Public License | +-- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | +-- | | +-- | CiviCRM is distributed in the hope that it will be useful, but | +-- | WITHOUT ANY WARRANTY; without even the implied warranty of | +-- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | +-- | See the GNU Affero General Public License for more details. | +-- | | +-- | You should have received a copy of the GNU Affero General Public | +-- | License and the CiviCRM Licensing Exception along | +-- | with this program; if not, contact CiviCRM LLC | +-- | at info[AT]civicrm[DOT]org. If you have questions about the | +-- | GNU Affero General Public License or the licensing of CiviCRM, | +-- | see the CiviCRM license FAQ at http://civicrm.org/licensing | +-- +--------------------------------------------------------------------+ +-- +-- Generated from drop.tpl +-- DO NOT EDIT. Generated by CRM_Core_CodeGen +-- +-- /******************************************************* +-- * +-- * Clean up the exisiting tables +-- * +-- *******************************************************/ + +SET FOREIGN_KEY_CHECKS=0; + +DROP TABLE IF EXISTS `civicrm_grant_budget`; + +SET FOREIGN_KEY_CHECKS=1; diff --git a/xml/schema/CRM/AnnualGrantBudgets/GrantBudget.entityType.php b/xml/schema/CRM/AnnualGrantBudgets/GrantBudget.entityType.php new file mode 100644 index 0000000..9c3582a --- /dev/null +++ b/xml/schema/CRM/AnnualGrantBudgets/GrantBudget.entityType.php @@ -0,0 +1,11 @@ + + array ( + 'name' => 'GrantBudget', + 'class' => 'CRM_AnnualGrantBudgets_DAO_GrantBudget', + 'table' => 'civicrm_grant_budget', + ), +); diff --git a/xml/schema/CRM/AnnualGrantBudgets/GrantBudget.xml b/xml/schema/CRM/AnnualGrantBudgets/GrantBudget.xml new file mode 100644 index 0000000..9b12ca6 --- /dev/null +++ b/xml/schema/CRM/AnnualGrantBudgets/GrantBudget.xml @@ -0,0 +1,77 @@ + + + + CRM/AnnualGrantBudgets + GrantBudget + civicrm_grant_budget + FIXME + true + + + id + int unsigned + true + Unique GrantBudget ID + true + + + id + true + + + + financial_type_id + Financial Type Id + int unsigned + FK to Financial Type + true + true + false + false + +
civicrm_financial_type
+ id + name + + + Select + + + + financial_type_id + civicrm_financial_type
+ id +
+ + + fiscal_year + Fiscal Year + int unsigned + Fiscal year + true + true + false + false + + CRM_Grant_BAO_GrantBudget::getFiscalyear + + + Select + + + + + budget + Grant Annual Budget + decimal + Grant Annual Budget + true + true + true + false + 0.00 + + Text + + +