DAO - Update writeRecords function to also save custom data
authorColeman Watts <coleman@civicrm.org>
Fri, 7 Jan 2022 05:49:43 +0000 (00:49 -0500)
committerColeman Watts <coleman@civicrm.org>
Sat, 8 Jan 2022 19:12:52 +0000 (14:12 -0500)
Refactors a couple BAOs which use writeRecords, as they no longer need to
write the custom data themselves.

CRM/Campaign/BAO/Campaign.php
CRM/Campaign/BAO/Survey.php
CRM/Core/DAO.php
CRM/Pledge/BAO/Pledge.php

index cff5bcaa07ffc47f8e854cbe4df3a2e2730c9cd2..c9808c5b070d70833540f2691a65eba1035fb2d0 100644 (file)
@@ -65,11 +65,6 @@ class CRM_Campaign_BAO_Campaign extends CRM_Campaign_DAO_Campaign {
       }
     }
 
-    //store custom data
-    if (!empty($params['custom']) && is_array($params['custom'])) {
-      CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_campaign', $campaign->id);
-    }
-
     return $campaign;
   }
 
index b37e6b9bce24775ffb43ab1b2d68f6045991d3c2..2fde9f3ec466be2e0b6bf07685ff5a03b24e43e4 100644 (file)
@@ -74,9 +74,6 @@ class CRM_Campaign_BAO_Survey extends CRM_Campaign_DAO_Survey implements Civi\Te
 
     $dao = self::writeRecord($params);
 
-    if (!empty($params['custom']) && is_array($params['custom'])) {
-      CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_survey', $dao->id);
-    }
     return $dao;
   }
 
index 84bf0251e9ffa827a7afcdb65680f30e0a5ba9f2..745b42ca1bd1e7c11f4a57871b1ef2e8d1e50a50 100644 (file)
@@ -909,20 +909,25 @@ class CRM_Core_DAO extends DB_DataObject {
    * @throws \CRM_Core_Exception
    */
   public static function writeRecord(array $record): CRM_Core_DAO {
-    $hook = empty($record['id']) ? 'create' : 'edit';
+    $op = empty($record['id']) ? 'create' : 'edit';
     $className = CRM_Core_DAO_AllCoreTables::getCanonicalClassName(static::class);
     if ($className === 'CRM_Core_DAO') {
       throw new CRM_Core_Exception('Function writeRecord must be called on a subclass of CRM_Core_DAO');
     }
     $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($className);
 
-    \CRM_Utils_Hook::pre($hook, $entityName, $record['id'] ?? NULL, $record);
+    \CRM_Utils_Hook::pre($op, $entityName, $record['id'] ?? NULL, $record);
     $instance = new $className();
     // Ensure fields exist before attempting to write to them
     $values = array_intersect_key($record, self::getSupportedFields());
     $instance->copyValues($values);
     $instance->save();
-    \CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
+
+    if (!empty($record['custom']) && is_array($record['custom'])) {
+      CRM_Core_BAO_CustomValueTable::store($record['custom'], static::$_tableName, $instance->id, $op);
+    }
+
+    \CRM_Utils_Hook::post($op, $entityName, $instance->id, $instance);
 
     return $instance;
   }
index 06412eda4c018ee1ba348a2f6a20efcf20cc6ddb..64d44d1729cba44faf195b57e5fb9d7428db50e5 100644 (file)
@@ -153,24 +153,7 @@ class CRM_Pledge_BAO_Pledge extends CRM_Pledge_DAO_Pledge {
     }
     $paymentParams['status_id'] = $params['status_id'] ?? NULL;
 
-    CRM_Utils_Hook::pre($action, 'Pledge', $params['id'] ?? NULL, $params);
-    $pledge = new CRM_Pledge_DAO_Pledge();
-
-    // if pledge is complete update end date as current date
-    if ($pledge->status_id == 1) {
-      $pledge->end_date = date('Ymd');
-    }
-
-    $pledge->copyValues($params);
-    $pledge->save();
-    CRM_Utils_Hook::post($action, 'Pledge', $pledge->id, $pledge);
-
-    // handle custom data.
-    if (!empty($params['custom']) &&
-      is_array($params['custom'])
-    ) {
-      CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_pledge', $pledge->id);
-    }
+    $pledge = self::writeRecord($params);
 
     // skip payment stuff in edit mode
     if (empty($params['id']) || $isRecalculatePledgePayment) {