From 6c41af3b6c17bf68e4edebfdc72e0eb15a2b3b34 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 7 Jan 2022 00:49:43 -0500 Subject: [PATCH] DAO - Update writeRecords function to also save custom data Refactors a couple BAOs which use writeRecords, as they no longer need to write the custom data themselves. --- CRM/Campaign/BAO/Campaign.php | 5 ----- CRM/Campaign/BAO/Survey.php | 3 --- CRM/Core/DAO.php | 11 ++++++++--- CRM/Pledge/BAO/Pledge.php | 19 +------------------ 4 files changed, 9 insertions(+), 29 deletions(-) diff --git a/CRM/Campaign/BAO/Campaign.php b/CRM/Campaign/BAO/Campaign.php index cff5bcaa07ff..c9808c5b070d 100644 --- a/CRM/Campaign/BAO/Campaign.php +++ b/CRM/Campaign/BAO/Campaign.php @@ -65,11 +65,6 @@ public static function create(&$params) { } } - //store custom data - if (!empty($params['custom']) && is_array($params['custom'])) { - CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_campaign', $campaign->id); - } - return $campaign; } diff --git a/CRM/Campaign/BAO/Survey.php b/CRM/Campaign/BAO/Survey.php index b37e6b9bce24..2fde9f3ec466 100644 --- a/CRM/Campaign/BAO/Survey.php +++ b/CRM/Campaign/BAO/Survey.php @@ -74,9 +74,6 @@ public static function create(&$params) { $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; } diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 84bf0251e9ff..745b42ca1bd1 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -909,20 +909,25 @@ public static function getAttribute($class, $fieldName = NULL) { * @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; } diff --git a/CRM/Pledge/BAO/Pledge.php b/CRM/Pledge/BAO/Pledge.php index 06412eda4c01..64d44d1729cb 100644 --- a/CRM/Pledge/BAO/Pledge.php +++ b/CRM/Pledge/BAO/Pledge.php @@ -153,24 +153,7 @@ public static function create(array $params): 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) {