CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'group' => 'contribute', 'name' => 'check_payment_instrument_ids', 'type' => 'Array', 'html_type' => 'Select', 'quick_form_type' => 'Select', 'html_attributes' => array( 'multiple' => 1, 'class' => 'crm-select2', ), 'pseudoconstant' => array( 'callback' => 'CRM_Contribute_BAO_CheckNumberPaymentMethod::getPaymentInstrument', ), 'default' => NULL, 'add' => '5.5', 'title' => ts('Check-Payment Instruments'), 'is_domain' => '1', 'is_contact' => 0, 'description' => NULL, 'help_text' => NULL, ]; } /** * Implements hook_civicrm_preProcess(). * * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess */ function checknumberpaymentmethod_civicrm_preProcess($formName, &$form) { if ('CRM_Admin_Form_Preferences_Contribute' == $formName) { $vars = $form->getVar('_settings'); $vars['check_payment_instrument_ids'] = CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME; $form->setVar('_settings', $vars); $form->setVar('settingsMetadata', ''); } } /** * Implements hook_civicrm_preProcess(). * * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess */ function checknumberpaymentmethod_civicrm_buildForm($formName, &$form) { if (('CRM_Contribute_Form_AdditionalPayment' == $formName && $form->getVar('_view') == 'transaction' && ($form->getVar('_action') & CRM_Core_Action::BROWSE)) || 'CRM_Contribute_Form_Contribution' == $formName || 'CRM_Contribute_Form_ContributionView' == $formName ) { $payments = $form->get_template_vars('payments'); _checknumberpaymentmethod_alterpayments($payments); $form->assign('payments', $payments); } if ('CRM_Admin_Form_Preferences_Contribute' == $formName) { $form->addElement( 'select', 'check_payment_instrument_ids', ts('Check-Payment Instruments'), CRM_Contribute_BAO_CheckNumberPaymentMethod::getPaymentInstrument(), [ 'multiple' => 1, 'class' => 'crm-select2', ] ); $form->assign('fields', $form->getVar('settingsMetadata')); } if ($formName == 'CRM_Financial_Form_Payment' && !empty($form->paymentInstrumentID)) { $paymentInstrumentsFromSettings = civicrm_api3('Setting', 'getvalue', [ 'name' => 'check_payment_instrument_ids', ]); if (in_array($form->paymentInstrumentID, $paymentInstrumentsFromSettings)) { $form->assign('paymentFields', array('check_number')); $form->add('text', 'check_number', ts('Check Number')); $form->assign('paymentTypeLabel', ts('Check Information')); } } if ('CRM_Financial_Form_PaymentEdit' == $formName) { $paymentInstrumentsFromSettings = civicrm_api3('Setting', 'getvalue', [ 'name' => 'check_payment_instrument_ids', ]); $paymentInstrumentsFromSettings[] = (string) CRM_Core_PseudoConstant::getKey( 'CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check' ); $form->assign('paymentInstrumentsFromSettings', json_encode($paymentInstrumentsFromSettings)); } } /** * Implements hook_civicrm_pre(). * * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_pre */ function checknumberpaymentmethod_civicrm_pre($op, $objectName, $id, &$params) { if ($objectName == 'Contribution' && in_array($op, ['create', 'edit'])) { if (!CRM_Utils_System::isNull($params['check_number']) || !empty($_POST['check_number'])) { $paymentInstrumentId = !empty($params['payment_instrument_id']) ? $params['payment_instrument_id'] : $params['prevContribution']->payment_instrument_id; $paymentInstrumentsFromSettings = civicrm_api3('Setting', 'getvalue', [ 'name' => 'check_payment_instrument_ids', ]); if (!in_array($paymentInstrumentId, $paymentInstrumentsFromSettings)) { return; } if (!CRM_Utils_System::isNull($params['check_number'])) { $params['check_number'] = $params['check_number']; } else { $params['check_number'] = $_POST['check_number']; } } } } function _checknumberpaymentmethod_alterpayments(&$payments) { $paymentInstrumentsFromSettings = civicrm_api3('Setting', 'getvalue', [ 'name' => 'check_payment_instrument_ids', ]); foreach ($payments as &$payment) { try { $checkNumber = civicrm_api3('FinancialTrxn', 'getvalue', [ 'return' => "check_number", 'id' => $payment['id'], 'payment_instrument_id' => ['IN' => $paymentInstrumentsFromSettings], ]); if (!empty($checkNumber)) { $payment['payment_instrument'] .= " (#{$checkNumber})"; } } catch (Exception $e) { // Ignore } } }