BAO - deprecate create for writeRecord: Batch
authorColeman Watts <coleman@civicrm.org>
Sat, 1 Apr 2023 23:31:53 +0000 (19:31 -0400)
committerColeman Watts <coleman@civicrm.org>
Sat, 1 Apr 2023 23:33:59 +0000 (19:33 -0400)
CRM/Batch/BAO/Batch.php
CRM/Batch/Form/Entry.php
CRM/Financial/Form/Export.php
CRM/Financial/Form/FinancialBatch.php
tests/phpunit/CRM/Batch/BAO/BatchTest.php

index 34d20ec6f9a26c905b5f6eb3822069199db667e5..100a4e93761444006486446eee4740d72e3ced13 100644 (file)
@@ -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);
index 6058c004649b998b680e1e07301096ac4302641d..b8186c8ef5969e4f33ed72b817d4897c2509552a 100644 (file)
@@ -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");
index 07ba0153bb6a43611a04dd75258100b8890c68d2..161e562ae5c5a5e978232d9540e8a3390fee64b6 100644 (file)
@@ -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);
index a417ab8a2180d4f9582a8ff39455f084dfd69fd3..30f573cf1b643131bc9bbe5f64bcc3fe05406975 100644 (file)
@@ -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;
index edb435a7e5854dc7cea3aac410319d0f7adf6f4f..87a0b4b5d4d7d04df5d1ee419b7d40b8945f21e5 100644 (file)
@@ -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',
     ];