From: eileen Date: Tue, 22 Sep 2020 03:23:53 +0000 (+1200) Subject: dev/core#2056 Only retrieve pcp & soft_credit info when needed X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=23b81ed69104246d300d22b6d10d5ca9f7cbc344;p=civicrm-core.git dev/core#2056 Only retrieve pcp & soft_credit info when needed Currently we do 2 queries per contribution create to retrieve pcp & soft credit info. However, the results of the queries are only used if pcp or soft credit keys exist in the params array --- diff --git a/CRM/Contribute/BAO/ContributionSoft.php b/CRM/Contribute/BAO/ContributionSoft.php index 40fe273e22..670e30adfa 100644 --- a/CRM/Contribute/BAO/ContributionSoft.php +++ b/CRM/Contribute/BAO/ContributionSoft.php @@ -53,39 +53,17 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio * Process the soft contribution and/or link to personal campaign page. * * @param array $params - * @param object $contribution CRM_Contribute_DAO_Contribution + * @param CRM_Contribute_BAO_Contribution $contribution * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public static function processSoftContribution($params, $contribution) { - //retrieve existing soft-credit and pcp id(s) if any against $contribution - $softIDs = self::getSoftCreditIds($contribution->id); - $pcpId = self::getSoftCreditIds($contribution->id, TRUE); - - if ($pcp = CRM_Utils_Array::value('pcp', $params)) { - $softParams = []; - $softParams['id'] = $pcpId ? $pcpId : NULL; - $softParams['contribution_id'] = $contribution->id; - $softParams['pcp_id'] = $pcp['pcp_made_through_id']; - $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', - $pcp['pcp_made_through_id'], 'contact_id' - ); - $softParams['currency'] = $contribution->currency; - $softParams['amount'] = $contribution->total_amount; - $softParams['pcp_display_in_roll'] = $pcp['pcp_display_in_roll'] ?? NULL; - $softParams['pcp_roll_nickname'] = $pcp['pcp_roll_nickname'] ?? NULL; - $softParams['pcp_personal_note'] = $pcp['pcp_personal_note'] ?? NULL; - $softParams['soft_credit_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp'); - $contributionSoft = self::add($softParams); - //Send notification to owner for PCP - if ($contributionSoft->pcp_id && empty($pcpId)) { - CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft); - } - } - //Delete PCP against this contribution and create new on submitted PCP information - elseif (array_key_exists('pcp', $params) && $pcpId) { - civicrm_api3('ContributionSoft', 'delete', ['id' => $pcpId]); + if (array_key_exists('pcp', $params)) { + self::processPCP($params['pcp'], $contribution); } if (isset($params['soft_credit'])) { + $softIDs = self::getSoftCreditIds($contribution->id); $softParams = $params['soft_credit']; foreach ($softParams as $softParam) { if (!empty($softIDs)) { @@ -602,4 +580,42 @@ class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_Contributio } } + /** + * Process the pcp associated with a contribution. + * + * @param array $pcp + * @param \CRM_Contribute_BAO_Contribution $contribution + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + protected static function processPCP($pcp, $contribution) { + $pcpId = self::getSoftCreditIds($contribution->id, TRUE); + + if ($pcp) { + $softParams = []; + $softParams['id'] = $pcpId ?: NULL; + $softParams['contribution_id'] = $contribution->id; + $softParams['pcp_id'] = $pcp['pcp_made_through_id']; + $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', + $pcp['pcp_made_through_id'], 'contact_id' + ); + $softParams['currency'] = $contribution->currency; + $softParams['amount'] = $contribution->total_amount; + $softParams['pcp_display_in_roll'] = $pcp['pcp_display_in_roll'] ?? NULL; + $softParams['pcp_roll_nickname'] = $pcp['pcp_roll_nickname'] ?? NULL; + $softParams['pcp_personal_note'] = $pcp['pcp_personal_note'] ?? NULL; + $softParams['soft_credit_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp'); + $contributionSoft = self::add($softParams); + //Send notification to owner for PCP + if ($contributionSoft->pcp_id && empty($pcpId)) { + CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft); + } + } + //Delete PCP against this contribution and create new on submitted PCP information + elseif ($pcpId) { + civicrm_api3('ContributionSoft', 'delete', ['id' => $pcpId]); + } + } + }