Skip to content

Commit

Permalink
Php8.x fixes on Main online contribution page
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Oct 15, 2023
1 parent 18fb1fe commit 9faf023
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 70 deletions.
123 changes: 95 additions & 28 deletions CRM/Contribute/Form/Contribution/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ public function setDefaultValues() {
// remove component related fields
foreach ($this->_fields as $name => $fieldInfo) {
//don't set custom data Used for Contribution (CRM-1344)
if (substr($name, 0, 7) == 'custom_') {
if (substr($name, 0, 7) === 'custom_') {
$id = substr($name, 7);
if (!CRM_Core_BAO_CustomGroup::checkCustomField($id, ['Contribution', 'Membership'])) {
continue;
}
// ignore component fields
}
elseif (array_key_exists($name, $contribFields) || (substr($name, 0, 11) == 'membership_') || (substr($name, 0, 13) == 'contribution_')) {
elseif (array_key_exists($name, $contribFields) || (substr($name, 0, 11) === 'membership_') || (substr($name, 0, 13) == 'contribution_')) {
continue;
}
$fields[$name] = $fieldInfo;
Expand All @@ -146,8 +146,9 @@ public function setDefaultValues() {
$billingDefaults = $this->getProfileDefaults('Billing', $contactID);
$this->_defaults = array_merge($this->_defaults, $billingDefaults);
}
if (!empty($this->_ccid) && !empty($this->_pendingAmount)) {
$this->_defaults['total_amount'] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($this->_pendingAmount);
$balance = $this->getContributionBalance();
if ($balance) {
$this->_defaults['total_amount'] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($balance);
}

/*
Expand Down Expand Up @@ -401,10 +402,9 @@ public function buildQuickForm() {
CRM_Core_BAO_CMSUser::buildForm($this, $profileID, TRUE);
}
}
if ($this->_pcpId && empty($this->_ccid)) {
if ($this->getPcpID() && empty($this->_ccid)) {
if (CRM_PCP_BAO_PCP::displayName($this->_pcpId)) {
$pcp_supporter_text = CRM_PCP_BAO_PCP::getPcpSupporterText($this->_pcpId, $this->_id, 'contribute');
$this->assign('pcpSupporterText', $pcp_supporter_text);
}
$prms = ['id' => $this->_pcpId];
CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $prms, $pcpInfo);
Expand All @@ -420,6 +420,7 @@ public function buildQuickForm() {
$this->addField('pcp_personal_note', ['entity' => 'ContributionSoft', 'context' => 'create', 'style' => 'height: 3em; width: 40em;']);
}
}
$this->assign('pcpSupporterText', $pcp_supporter_text ?? NULL);
if (empty($this->_values['fee']) && empty($this->_ccid)) {
throw new CRM_Core_Exception(ts('This page does not have any price fields configured or you may not have permission for them. Please contact the site administrator for more details.'));
}
Expand Down Expand Up @@ -542,20 +543,76 @@ private function buildPriceSet(&$form, $component = NULL) {
}
if (!empty($options)) {
$label = (!empty($this->_membershipBlock) && $field['name'] === 'contribution_amount') ? ts('Additional Contribution') : $field['label'];
$extra = [];
$fieldID = (int) $field['id'];
if ($fieldID === $this->getPriceFieldOtherID()) {
$extra = [
'onclick' => 'useAmountOther("price_' . $this->getPriceFieldMainID() . '");',
'autocomplete' => 'off',
];
}
if ($fieldID === $this->getPriceFieldMainID()) {
$extra = [
'onclick' => 'clearAmountOther("price_' . $this->getPriceFieldOtherID() . '");',
];
}

CRM_Price_BAO_PriceField::addQuickFormElement($form,
'price_' . $field['id'],
'price_' . $fieldID,
$field['id'],
FALSE,
$field['is_required'] ?? FALSE,
$label,
$options
$options,
[],
$extra
);
}
}
}
$form->assign('ispricelifetime', $checklifetime);
}

/**
* Get the idea of the other amount field if the form is configured to offer it.
*
* The other amount field is an alternative to the configured radio options,
* specific to this form.
*
* @return int|null
*/
private function getPriceFieldOtherID(): ?int {
if (!$this->isQuickConfig()) {
return NULL;
}
foreach ($this->order->getPriceFieldsMetadata() as $field) {
if ($field['name'] === 'other_amount') {
return (int) $field['id'];
}
}
return NULL;
}

/**
* Get the idea of the other amount field if the form is configured to offer an other amount.
*
* The other amount field is an alternative to the configured radio options,
* specific to this form.
*
* @return int|null
*/
private function getPriceFieldMainID(): ?int {
if (!$this->isQuickConfig() || !$this->getPriceFieldOtherID()) {
return NULL;
}
foreach ($this->order->getPriceFieldsMetadata() as $field) {
if ($field['name'] !== 'other_amount') {
return (int) $field['id'];
}
}
return NULL;
}

/**
* Build Membership Block in Contribution Pages.
* @todo this was shared on CRM_Contribute_Form_ContributionBase but we are refactoring and simplifying for each
Expand Down Expand Up @@ -1000,7 +1057,7 @@ public static function formRule($fields, $files, $self) {
}

if (isset($fields['selectProduct']) &&
$fields['selectProduct'] != 'no_thanks'
$fields['selectProduct'] !== 'no_thanks'
) {
$productDAO = new CRM_Contribute_DAO_Product();
$productDAO->id = $fields['selectProduct'];
Expand Down Expand Up @@ -1223,9 +1280,9 @@ public function submit($params) {
}
}
}

if (!empty($this->_ccid) && !empty($this->_pendingAmount)) {
$params['amount'] = $this->_pendingAmount;
$balance = $this->getContributionBalance();
if ($balance) {
$params['amount'] = $balance;
}
else {
// from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID
Expand Down Expand Up @@ -1425,24 +1482,11 @@ public function assignFormVariablesByContributionID(): void {
}
}
$this->assign('dummyTitle', $dummyTitle);

$this->assign('pendingAmount', $this->getContributionBalance());
if (empty($this->getExistingContributionID())) {
return;
}
if (!$this->getContactID()) {
CRM_Core_Error::statusBounce(ts("Returning since there is no contact attached to this contribution id."));
}

$paymentBalance = CRM_Contribute_BAO_Contribution::getContributionBalance($this->_ccid);
//bounce if the contribution is not pending.
if ((float) $paymentBalance <= 0) {
CRM_Core_Error::statusBounce(ts("Returning since contribution has already been handled."));
}
if (!empty($paymentBalance)) {
$this->_pendingAmount = $paymentBalance;
$this->assign('pendingAmount', $this->_pendingAmount);
}

// @todo - all this stuff is likely obsolete.
if ($taxAmount = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_ccid, 'tax_amount')) {
$this->assign('taxAmount', $taxAmount);
}
Expand All @@ -1453,6 +1497,29 @@ public function assignFormVariablesByContributionID(): void {
$this->assign('priceSetID', $this->getPriceSetID());
}

/**
* Get the balance amount if an existing contribution is being paid.
*
* @return float|null
*
* @throws \CRM_Core_Exception
*/
private function getContributionBalance(): ?float {
if (empty($this->getExistingContributionID())) {
return NULL;
}
if (!$this->getContactID()) {
CRM_Core_Error::statusBounce(ts('Returning since there is no contact attached to this contribution id.'));
}

$paymentBalance = CRM_Contribute_BAO_Contribution::getContributionBalance($this->_ccid);
//bounce if the contribution is not pending.
if ((float) $paymentBalance <= 0) {
CRM_Core_Error::statusBounce(ts('Returning since contribution has already been handled.'));
}
return $paymentBalance;
}

/**
* Function for unit tests on the postProcess function.
*
Expand All @@ -1474,7 +1541,7 @@ public function testSubmit($params) {
*
* @param array $params
*
* @return mixed
* @return bool
* @throws \CRM_Core_Exception
*/
protected function hasSeparateMembershipPaymentAmount($params) {
Expand Down
26 changes: 19 additions & 7 deletions CRM/Contribute/Form/ContributionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
/**
* Pcp id
*
* @internal use getPcpID().
*
* @var int
*/
public $_pcpId;
Expand Down Expand Up @@ -468,10 +470,9 @@ public function preProcess() {
$this->set('membershipBlock', $this->getMembershipBlock());

// Handle PCP
$pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this);
if ($pcpId) {
$pcpId = $this->getPcpID();
if ($this->getPcpID()) {
$pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'contribute', $this->_values);
$this->_pcpId = $pcp['pcpId'];
$this->_pcpBlock = $pcp['pcpBlock'];
$this->_pcpInfo = $pcp['pcpInfo'];
}
Expand Down Expand Up @@ -800,8 +801,6 @@ public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType =
}
}

$this->assign($name, $fields);

if ($profileContactType && count($viewOnlyFileValues[$profileContactType])) {
$this->assign('viewOnlyPrefixFileValues', $viewOnlyFileValues);
}
Expand All @@ -810,6 +809,7 @@ public function buildCustom($id, $name, $viewOnly = FALSE, $profileContactType =
}
}
}
$this->assign($name, $fields ?? NULL);
}

/**
Expand Down Expand Up @@ -987,7 +987,6 @@ public function buildComponentForm($id, $form): void {
];
}
$locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE);
$form->assign('locDataURL', $locDataURL);
}
if (count($organizations) > 0) {
$form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $organizations));
Expand Down Expand Up @@ -1022,7 +1021,6 @@ public function buildComponentForm($id, $form): void {
CRM_Core_Permission::CREATE, NULL
);

$form->assign('onBehalfOfFields', $profileFields);
if (!empty($form->_submitValues['onbehalf'])) {
if (!empty($form->_submitValues['onbehalfof_id'])) {
$form->assign('submittedOnBehalf', $form->_submitValues['onbehalfof_id']);
Expand Down Expand Up @@ -1055,6 +1053,8 @@ public function buildComponentForm($id, $form): void {
}
}
}
$form->assign('locDataURL', $locDataURL ?? NULL);
$form->assign('onBehalfOfFields', $profileFields ?? NULL);

}

Expand Down Expand Up @@ -1406,4 +1406,16 @@ protected function getExistingContributionLineItems(): array {
return $lineItems;
}

/**
* Get the PCP ID being contributed to.
*
* @return int|null
*/
protected function getPcpID(): ?int {
if ($this->_pcpId === NULL) {
$this->_pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this);
}
return $this->_pcpId ? (int) $this->_pcpId : NULL;
}

}
Loading

0 comments on commit 9faf023

Please sign in to comment.