From f54abaaec0bf6109b5b39a46979145e6bc685645 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 21 Jul 2022 10:36:06 -0400 Subject: [PATCH] APIv3 - Don't handle custom data already handled by BAO Fixes dev/core#3747 It's confusing to handle saving custom data in the APIv3 layer, because APIv4 expects the BAO to handle it. Until every BAO consistently uses WriteRecord this will continue to be a pain point. --- CRM/ACL/BAO/ACLEntityRole.php | 1 + CRM/Core/BAO/ActionSchedule.php | 1 + CRM/Core/BAO/Domain.php | 2 ++ CRM/Core/BAO/RecurringEntity.php | 1 + CRM/Core/BAO/Website.php | 5 ++-- CRM/Mailing/BAO/Mailing.php | 40 ++++++++---------------------- CRM/Member/BAO/MembershipBlock.php | 1 + CRM/Pledge/BAO/PledgeBlock.php | 8 +++--- api/v3/utils.php | 25 ++++++++++++++++--- 9 files changed, 44 insertions(+), 40 deletions(-) diff --git a/CRM/ACL/BAO/ACLEntityRole.php b/CRM/ACL/BAO/ACLEntityRole.php index 9d577f56f6..1e6899d721 100644 --- a/CRM/ACL/BAO/ACLEntityRole.php +++ b/CRM/ACL/BAO/ACLEntityRole.php @@ -35,6 +35,7 @@ class CRM_ACL_BAO_ACLEntityRole extends CRM_ACL_DAO_ACLEntityRole { /** * @param array $params * + * @deprecated * @return CRM_ACL_BAO_ACLEntityRole */ public static function create(&$params) { diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index d56afbe689..6f9d8ea9c4 100644 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -174,6 +174,7 @@ FROM civicrm_action_schedule cas * @param array $params * An assoc array of name/value pairs. * + * @deprecated * @return CRM_Core_DAO_ActionSchedule * @throws \CRM_Core_Exception */ diff --git a/CRM/Core/BAO/Domain.php b/CRM/Core/BAO/Domain.php index 69595743d5..73d850c1f3 100644 --- a/CRM/Core/BAO/Domain.php +++ b/CRM/Core/BAO/Domain.php @@ -149,6 +149,7 @@ class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { * @param array $params * @param int $id * + * @deprecated * @return CRM_Core_DAO_Domain * @throws \CRM_Core_Exception */ @@ -160,6 +161,7 @@ class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { /** * Create or update domain. * + * @deprecated * @param array $params * @return CRM_Core_DAO_Domain */ diff --git a/CRM/Core/BAO/RecurringEntity.php b/CRM/Core/BAO/RecurringEntity.php index 8b7da449ee..5e3e4c2d4b 100644 --- a/CRM/Core/BAO/RecurringEntity.php +++ b/CRM/Core/BAO/RecurringEntity.php @@ -144,6 +144,7 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity implemen /** * Create or update a RecurringEntity. * + * @deprecated * @param array $params * @return CRM_Core_DAO_RecurringEntity */ diff --git a/CRM/Core/BAO/Website.php b/CRM/Core/BAO/Website.php index 7436a040fd..8ba619e679 100644 --- a/CRM/Core/BAO/Website.php +++ b/CRM/Core/BAO/Website.php @@ -26,6 +26,7 @@ class CRM_Core_BAO_Website extends CRM_Core_DAO_Website { * * @param array $params * + * @deprecated * @return CRM_Core_DAO_Website * @throws \CRM_Core_Exception */ @@ -36,8 +37,6 @@ class CRM_Core_BAO_Website extends CRM_Core_DAO_Website { /** * Create website. * - * If called in a legacy manner this, temporarily, fails back to calling the legacy function. - * * @param array $params * * @return CRM_Core_DAO_Website @@ -46,7 +45,7 @@ class CRM_Core_BAO_Website extends CRM_Core_DAO_Website { */ public static function add($params) { CRM_Core_Error::deprecatedFunctionWarning('use apiv4'); - return self::create($params); + return self::writeRecord($params); } /** diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php index f75c0401cb..24c2cfba7e 100644 --- a/CRM/Mailing/BAO/Mailing.php +++ b/CRM/Mailing/BAO/Mailing.php @@ -1442,52 +1442,32 @@ ORDER BY civicrm_email.is_bulkmail DESC * Add the mailings. * * @param array $params - * Reference array contains the values submitted by the form. - * @param array $ids - * Reference array contains the id. - * * * @return CRM_Mailing_DAO_Mailing * @throws \Civi\API\Exception\UnauthorizedException */ - public static function add(&$params, $ids = []) { - $id = $params['id'] ?? $ids['mailing_id'] ?? NULL; + public static function add($params) { + $id = $params['id'] ?? NULL; - if (empty($params['id']) && !empty($ids)) { - CRM_Core_Error::deprecatedWarning('Parameter $ids is no longer used by Mailing::add. Use the api or just pass $params'); - } if (!empty($params['check_permissions']) && CRM_Mailing_Info::workflowEnabled()) { $params = self::processWorkflowPermissions($params); } - $action = $id ? 'create' : 'edit'; - CRM_Utils_Hook::pre($action, 'Mailing', $id, $params); - - $mailing = new static(); - if ($id) { - $mailing->id = $id; - $mailing->find(TRUE); + if (!$id) { + $params['domain_id'] = $params['domain_id'] ?? CRM_Core_Config::domainID(); } - $mailing->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); - - if (((!$id && empty($params['replyto_email'])) || !isset($params['replyto_email'])) && + if ( + ((!$id && empty($params['replyto_email'])) || !isset($params['replyto_email'])) && isset($params['from_email']) ) { $params['replyto_email'] = $params['from_email']; } - $mailing->copyValues($params); - // CRM-20892 Unset Modifed Date here so that MySQL can correctly set an updated modfied date. - unset($mailing->modified_date); - $result = $mailing->save(); + unset($params['modified_date']); - // CRM-20892 Re find record after saing so we can set the updated modified date in the result. - $mailing->find(TRUE); + $result = static::writeRecord($params); - if (isset($mailing->modified_date)) { - $result->modified_date = $mailing->modified_date; - } - - CRM_Utils_Hook::post($action, 'Mailing', $mailing->id, $mailing); + // CRM-20892 Re find record after saing so we can set the updated modified date in the result. + $result->find(TRUE); return $result; } diff --git a/CRM/Member/BAO/MembershipBlock.php b/CRM/Member/BAO/MembershipBlock.php index 08458deee8..6c208c9b4b 100644 --- a/CRM/Member/BAO/MembershipBlock.php +++ b/CRM/Member/BAO/MembershipBlock.php @@ -19,6 +19,7 @@ class CRM_Member_BAO_MembershipBlock extends CRM_Member_DAO_MembershipBlock { /** * Create or update a MembershipBlock. * + * @deprecated * @param array $params * @return CRM_Member_DAO_MembershipBlock */ diff --git a/CRM/Pledge/BAO/PledgeBlock.php b/CRM/Pledge/BAO/PledgeBlock.php index 4cea3ce74c..3a38a8e2c4 100644 --- a/CRM/Pledge/BAO/PledgeBlock.php +++ b/CRM/Pledge/BAO/PledgeBlock.php @@ -38,8 +38,8 @@ class CRM_Pledge_BAO_PledgeBlock extends CRM_Pledge_DAO_PledgeBlock { * * @param array $params * (reference ) an assoc array of name/value pairs. - * - * @return CRM_Pledge_BAO_PledgeBlock + * @deprecated + * @return CRM_Pledge_DAO_PledgeBlock */ public static function &create(&$params) { $transaction = new CRM_Core_Transaction(); @@ -61,8 +61,8 @@ class CRM_Pledge_BAO_PledgeBlock extends CRM_Pledge_DAO_PledgeBlock { * Add or update pledgeBlock. * * @param array $params - * - * @return object + * @deprecated + * @return CRM_Pledge_DAO_PledgeBlock */ public static function add($params) { // FIXME: This is assuming checkbox input like ['foo' => 1, 'bar' => 0, 'baz' => 1]. Not API friendly. diff --git a/api/v3/utils.php b/api/v3/utils.php index d454535bfe..53fa5e0399 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1334,9 +1334,28 @@ function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) { else { // If we have custom fields the BAO may have taken care of it or we may have to. // DAO::writeRecord always handles custom data. - // Otherwise guess based on the $extendsMap hard-coded list of BAOs that take care of custom data. - if (isset($params['custom']) && $fct !== 'writeRecord' && empty(CRM_Core_BAO_CustomQuery::$extendsMap[$entity])) { - CRM_Core_BAO_CustomValueTable::store($params['custom'], CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($entity)), $bao->id); + if (isset($params['custom']) && $fct !== 'writeRecord') { + // List of BAOs that write custom data in their create or add function. + $alreadyHandled = array_keys(CRM_Core_BAO_CustomQuery::$extendsMap); + $alreadyHandled[] = 'ActivityContact'; + $alreadyHandled[] = 'Batch'; + $alreadyHandled[] = 'CustomField'; + $alreadyHandled[] = 'EntityBatch'; + $alreadyHandled[] = 'IM'; + $alreadyHandled[] = 'Mailing'; + $alreadyHandled[] = 'MailingAB'; + $alreadyHandled[] = 'OpenID'; + $alreadyHandled[] = 'Phone'; + $alreadyHandled[] = 'PledgePayment'; + $alreadyHandled[] = 'PriceField'; + $alreadyHandled[] = 'PriceFieldValue'; + $alreadyHandled[] = 'RelationshipType'; + $alreadyHandled[] = 'SavedSearch'; + $alreadyHandled[] = 'Tag'; + $alreadyHandled[] = 'Website'; + if (!in_array($entity, $alreadyHandled)) { + CRM_Core_BAO_CustomValueTable::store($params['custom'], CRM_Core_DAO_AllCoreTables::getTableForClass(CRM_Core_DAO_AllCoreTables::getFullName($entity)), $bao->id); + } } $values = []; _civicrm_api3_object_to_array($bao, $values[$bao->id]); -- 2.25.1