From: Coleman Watts Date: Sat, 1 Apr 2023 23:31:53 +0000 (-0400) Subject: BAO - deprecate create for writeRecord: Batch X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=92a555da61a144328e47c69ee581f55c9d76fe5b;p=civicrm-core.git BAO - deprecate create for writeRecord: Batch --- diff --git a/CRM/Batch/BAO/Batch.php b/CRM/Batch/BAO/Batch.php index 34d20ec6f9..100a4e9376 100644 --- a/CRM/Batch/BAO/Batch.php +++ b/CRM/Batch/BAO/Batch.php @@ -18,7 +18,7 @@ /** * Batch BAO class. */ -class CRM_Batch_BAO_Batch extends CRM_Batch_DAO_Batch { +class CRM_Batch_BAO_Batch extends CRM_Batch_DAO_Batch implements \Civi\Core\HookInterface { /** * Cache for the current batch object. @@ -34,21 +34,31 @@ class CRM_Batch_BAO_Batch extends CRM_Batch_DAO_Batch { public static $_exportFormat = NULL; /** - * Create a new batch. - * + * @deprecated * @param array $params - * - * @return object - * $batch batch object - * @throws \Exception + * @return CRM_Batch_DAO_Batch */ public static function create(&$params) { - if (empty($params['id']) && empty($params['name'])) { - $params['name'] = CRM_Utils_String::titleToVar($params['title'] ?? 'batch_ref_' . random_int(0, 100000)); - } + CRM_Core_Error::deprecatedFunctionWarning('writeRecord'); return self::writeRecord($params); } + /** + * 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): void { + if ($event->action === 'create') { + // Supply defaults for `title` + if (empty($event->params['title'])) { + $event->params['title'] = $event->params['name'] ?? self::generateBatchName(); + } + } + } + /** * Retrieve DB object and copy to defaults array. * @@ -611,9 +621,9 @@ class CRM_Batch_BAO_Batch extends CRM_Batch_DAO_Batch { $session = CRM_Core_Session::singleton(); $params['modified_date'] = date('YmdHis'); $params['modified_id'] = $session->get('userID'); - foreach ($batchIds as $key => $value) { - $params['id'] = $ids['batchID'] = $value; - self::create($params, $ids); + foreach ($batchIds as $id) { + $params['id'] = $id; + self::writeRecord($params); } $url = CRM_Utils_System::url('civicrm/financial/financialbatches', "reset=1&batchStatus={$params['status_id']}"); CRM_Utils_System::redirect($url); diff --git a/CRM/Batch/Form/Entry.php b/CRM/Batch/Form/Entry.php index 6058c00464..b8186c8ef5 100644 --- a/CRM/Batch/Form/Entry.php +++ b/CRM/Batch/Form/Entry.php @@ -543,7 +543,7 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form { 'total' => $params['actualBatchTotal'], ]; - CRM_Batch_BAO_Batch::create($paramValues); + CRM_Batch_BAO_Batch::writeRecord($paramValues); // set success status CRM_Core_Session::setStatus("", ts("Batch Processed."), "success"); diff --git a/CRM/Financial/Form/Export.php b/CRM/Financial/Form/Export.php index 07ba0153bb..161e562ae5 100644 --- a/CRM/Financial/Form/Export.php +++ b/CRM/Financial/Form/Export.php @@ -166,7 +166,7 @@ class CRM_Financial_Form_Export extends CRM_Core_Form { $batchParams['id'] = $batchId; // Update totals $batchParams = array_merge($batchParams, $totals[$batchId]); - CRM_Batch_BAO_Batch::create($batchParams); + CRM_Batch_BAO_Batch::writeRecord($batchParams); } CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat, $this->_downloadFile); diff --git a/CRM/Financial/Form/FinancialBatch.php b/CRM/Financial/Form/FinancialBatch.php index a417ab8a21..30f573cf1b 100644 --- a/CRM/Financial/Form/FinancialBatch.php +++ b/CRM/Financial/Form/FinancialBatch.php @@ -207,7 +207,7 @@ class CRM_Financial_Form_FinancialBatch extends CRM_Contribute_Form { // FIXME: What happens if we get to here and no activityType is defined? - $batch = CRM_Batch_BAO_Batch::create($params); + $batch = CRM_Batch_BAO_Batch::writeRecord($params); //set batch id $this->_id = $batch->id; diff --git a/tests/phpunit/CRM/Batch/BAO/BatchTest.php b/tests/phpunit/CRM/Batch/BAO/BatchTest.php index edb435a7e5..87a0b4b5d4 100644 --- a/tests/phpunit/CRM/Batch/BAO/BatchTest.php +++ b/tests/phpunit/CRM/Batch/BAO/BatchTest.php @@ -96,8 +96,10 @@ class CRM_Batch_BAO_BatchTest extends CiviUnitTestCase { $batchParams = ['title' => 'Test Batch']; $batchParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Open'); - $batch = CRM_Batch_BAO_Batch::create($batchParams); + $batch = CRM_Batch_BAO_Batch::writeRecord($batchParams); $entityId = $batch->id; + // Name should be autogenerated from title + $this->assertEquals('Test_Batch', $batch->name); $returnvalues = [ 'civicrm_financial_trxn.payment_instrument_id as payment_method', ];