From 46e67587387e10139fd62b683b8e96398b42ba6d Mon Sep 17 00:00:00 2001 From: "Matthew Wire (MJW Consulting)" Date: Mon, 25 Feb 2019 14:41:26 +0000 Subject: [PATCH] Add pseudoconstant support for payment_processor_id on CRM_Contribute_BAO_ContributionRecur --- CRM/Contribute/BAO/ContributionRecur.php | 53 ++++++++++++++++++++---- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index 126cf8f67d..4e4107a052 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -801,14 +801,7 @@ INNER JOIN civicrm_contribution con ON ( con.id = mp.contribution_id ) $form->addElement('text', 'contribution_recur_processor_id', ts('Processor ID'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionRecur', 'processor_id')); $form->addElement('text', 'contribution_recur_trxn_id', ts('Transaction ID'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionRecur', 'trxn_id')); - $paymentProcessorParams = [ - 'return' => ["id", "name", 'is_test'], - ]; - $paymentProcessors = civicrm_api3('PaymentProcessor', 'get', $paymentProcessorParams); - $paymentProcessorOpts = []; - foreach ($paymentProcessors['values'] as $key => $value) { - $paymentProcessorOpts[$key] = $value['name'] . ($value['is_test'] ? ' (Test)' : ''); - } + $paymentProcessorOpts = CRM_Contribute_BAO_ContributionRecur::buildOptions('payment_processor_id', 'get'); $form->add('select', 'contribution_recur_payment_processor_id', ts('Payment Processor ID'), $paymentProcessorOpts, FALSE, ['class' => 'crm-select2', 'multiple' => 'multiple']); CRM_Core_BAO_Query::addCustomFormFields($form, array('ContributionRecur')); @@ -972,4 +965,48 @@ INNER JOIN civicrm_contribution con ON ( con.id = mp.contribution_id ) return self::$inactiveStatuses; } + /** + * Get options for the called BAO object's field. + * + * This function can be overridden by each BAO to add more logic related to context. + * The overriding function will generally call the lower-level CRM_Core_PseudoConstant::get + * + * @param string $fieldName + * @param string $context + * @see CRM_Core_DAO::buildOptionsContext + * @param array $props + * whatever is known about this bao object. + * + * @return array|bool + */ + public static function buildOptions($fieldName, $context = NULL, $props = array()) { + + switch ($fieldName) { + case 'payment_processor_id': + if (isset(\Civi::$statics[__CLASS__]['buildoptions_payment_processor_id'])) { + return \Civi::$statics[__CLASS__]['buildoptions_payment_processor_id']; + } + $baoName = 'CRM_Contribute_BAO_ContributionRecur'; + $props['condition']['test'] = "is_test = 0"; + $liveProcessors = CRM_Core_PseudoConstant::get($baoName, $fieldName, $props, $context); + $props['condition']['test'] = "is_test != 0"; + $testProcessors = CRM_Core_PseudoConstant::get($baoName, $fieldName, $props, $context); + foreach ($testProcessors as $key => $value) { + if ($context === 'validate') { + // @fixme: Ideally the names would be different in the civicrm_payment_processor table but they are not. + // So we append '_test' to the test one so that we can select the correct processor by name using the ContributionRecur.create API. + $testProcessors[$key] = $value . '_test'; + } + else { + $testProcessors[$key] = CRM_Core_TestEntity::appendTestText($value); + } + } + $allProcessors = $liveProcessors + $testProcessors; + ksort($allProcessors); + \Civi::$statics[__CLASS__]['buildoptions_payment_processor_id'] = $allProcessors; + return $allProcessors; + } + return parent::buildOptions($fieldName, $context, $props); + } + } -- 2.25.1