From ca5603d5144bae64c29f2ba9e7da946b04e67235 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 24 Dec 2020 09:22:09 +1100 Subject: [PATCH] REF use centralised addRadio function more --- CRM/Contact/Form/Task/AddToParentClass.php | 11 +++++--- CRM/Contribute/BAO/Premium.php | 4 +-- CRM/Contribute/Form/ContributionBase.php | 21 +++++++++------- .../Form/ContributionPage/Amount.php | 6 ++--- CRM/Contribute/Form/ManagePremiums.php | 18 +++++++------ CRM/Contribute/Form/SoftCredit.php | 4 +-- CRM/Custom/Form/Field.php | 4 +-- CRM/Event/Form/ManageEvent/Fee.php | 10 ++++---- CRM/Event/Form/Registration/Register.php | 12 +++------ CRM/Member/Form/Membership.php | 1 - CRM/Member/Form/MembershipBlock.php | 5 ++-- CRM/Price/BAO/PriceField.php | 25 +++++++++---------- CRM/Price/Form/Field.php | 4 +-- 13 files changed, 65 insertions(+), 60 deletions(-) diff --git a/CRM/Contact/Form/Task/AddToParentClass.php b/CRM/Contact/Form/Task/AddToParentClass.php index aee54878d4..42caaba86a 100644 --- a/CRM/Contact/Form/Task/AddToParentClass.php +++ b/CRM/Contact/Form/Task/AddToParentClass.php @@ -52,12 +52,17 @@ class CRM_Contact_Form_Task_AddToParentClass extends CRM_Contact_Form_Task { $chekFlag = $id; } - $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id); + $checkBoxes[$id] = NULL; } - $this->addGroup($checkBoxes, 'contact_check'); + $group = $this->addRadio('contact_check', NULL, $checkBoxes); + $groupElements = $group->getElements(); if ($chekFlag) { - $checkBoxes[$chekFlag]->setChecked(TRUE); + foreach ($groupElements as $groupElement) { + if ($groupElement->getValue() == $chekFlag) { + $groupElement->setChecked(TRUE); + } + } } $this->assign('searchRows', $searchRows); } diff --git a/CRM/Contribute/BAO/Premium.php b/CRM/Contribute/BAO/Premium.php index 332ff2b4f6..b89c6ca158 100644 --- a/CRM/Contribute/BAO/Premium.php +++ b/CRM/Contribute/BAO/Premium.php @@ -171,7 +171,7 @@ class CRM_Contribute_BAO_Premium extends CRM_Contribute_DAO_Premium { CRM_Core_DAO::storeValues($productDAO, $products[$productDAO->id]); } - $radio[$productDAO->id] = $form->createElement('radio', NULL, NULL, NULL, $productDAO->id, NULL); + $radio[$productDAO->id] = NULL; $options = $temp = []; $temp = explode(',', $productDAO->options); foreach ($temp as $value) { @@ -181,7 +181,7 @@ class CRM_Contribute_BAO_Premium extends CRM_Contribute_DAO_Premium { $form->add('select', 'options_' . $productDAO->id, NULL, $options); } - $form->addGroup($radio, 'selectProduct', NULL); + $form->addRadio('selectProduct', NULL, $radio); $form->assign('showRadio', TRUE); $form->assign('showSelectOptions', TRUE); diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index 7bb940637f..27a6a5c86a 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -881,9 +881,9 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { // radio button for Honor Type foreach ($form->_values['soft_credit_types'] as $value) { - $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value); + $honorTypes[$value] = $softCreditTypes[$value]; } - $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE); + $form->addRadio('soft_credit_type_id', NULL, $honorTypes, ['allowClear' => TRUE]); $honoreeProfileFields = CRM_Core_BAO_UFGroup::getFields( $this->_values['honoree_profile_id'], FALSE, @@ -1175,7 +1175,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { if ($this->_membershipBlock) { $this->_currentMemberships = []; - $membershipTypeIds = $membershipTypes = $radio = []; + $membershipTypeIds = $membershipTypes = $radio = $radioOptAttrs = []; $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember); $allowAutoRenewMembership = $autoRenewOption = FALSE; @@ -1262,9 +1262,8 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { } //add membership type. - $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL, - $memType['id'], $javascriptMethod - ); + $radio[$memType['id']] = NULL; + $radioOptAttrs[$memType['id']] = $javascriptMethod; if ($cid) { $membership = new CRM_Member_DAO_Membership(); $membership->contact_id = $cid; @@ -1284,6 +1283,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { if ($membership->find(TRUE)) { if (!$membership->end_date) { unset($radio[$memType['id']]); + unset($radioOptAttrs[$memType['id']]); $this->assign('islifetime', TRUE); continue; } @@ -1329,8 +1329,8 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { if (!$membershipPriceset) { if (!$this->_membershipBlock['is_required']) { $this->assign('showRadioNoThanks', TRUE); - $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL); - $this->addGroup($radio, 'selectMembership', NULL); + $radio['no_thanks'] = NULL; + $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs); } elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) { $temp = array_keys($radio); @@ -1339,7 +1339,10 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { $this->assign('showRadio', FALSE); } else { - $this->addGroup($radio, 'selectMembership', NULL); + foreach ($radioOptAttrs as $opt => $attrs) { + $attrs['class'] = ' required'; + } + $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs); } $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required'); diff --git a/CRM/Contribute/Form/ContributionPage/Amount.php b/CRM/Contribute/Form/ContributionPage/Amount.php index 7aca7aac4f..3d8907b826 100644 --- a/CRM/Contribute/Form/ContributionPage/Amount.php +++ b/CRM/Contribute/Form/ContributionPage/Amount.php @@ -56,7 +56,7 @@ class CRM_Contribute_Form_ContributionPage_Amount extends CRM_Contribute_Form_Co //CRM-12055 $this->add('text', 'amount_label', ts('Contribution Amounts Label')); - $default = [$this->createElement('radio', NULL, NULL, NULL, 0)]; + $default = [0 => NULL]; $this->add('hidden', "price_field_id", '', ['id' => "price_field_id"]); $this->add('hidden', "price_field_other", '', ['id' => "price_field_option"]); for ($i = 1; $i <= self::NUM_OPTION; $i++) { @@ -70,10 +70,10 @@ class CRM_Contribute_Form_ContributionPage_Amount extends CRM_Contribute_Form_Co $this->addRule("value[$i]", ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency('99.99')]), 'money'); // default - $default[] = $this->createElement('radio', NULL, NULL, NULL, $i); + $default[$i] = NULL; } - $this->addGroup($default, 'default'); + $this->addRadio('default', '', $default); $this->addElement('checkbox', 'amount_block_is_active', ts('Contribution Amounts section enabled'), NULL, ['onclick' => "showHideAmountBlock( this, 'amount_block_is_active' );"]); diff --git a/CRM/Contribute/Form/ManagePremiums.php b/CRM/Contribute/Form/ManagePremiums.php index a5ad9e40e3..45904ea49f 100644 --- a/CRM/Contribute/Form/ManagePremiums.php +++ b/CRM/Contribute/Form/ManagePremiums.php @@ -86,13 +86,17 @@ class CRM_Contribute_Form_ManagePremiums extends CRM_Contribute_Form { $this->add('text', 'sku', ts('SKU'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'sku')); $this->add('textarea', 'description', ts('Description'), ['cols' => 60, 'rows' => 3]); - - $image['image'] = $this->createElement('radio', NULL, NULL, ts('Upload from my computer'), 'image', 'onclick="add_upload_file_block(\'image\');'); - $image['thumbnail'] = $this->createElement('radio', NULL, NULL, ts('Display image and thumbnail from these locations on the web:'), 'thumbnail', 'onclick="add_upload_file_block(\'thumbnail\');'); - $image['default_image'] = $this->createElement('radio', NULL, NULL, ts('Use default image'), 'default_image', 'onclick="add_upload_file_block(\'default\');'); - $image['noImage'] = $this->createElement('radio', NULL, NULL, ts('Do not display an image'), 'noImage', 'onclick="add_upload_file_block(\'noImage\');'); - - $this->addGroup($image, 'imageOption', ts('Premium Image')); + $imageJS = []; + $image['image'] = ts('Upload from my computer'); + $imageJS['image'] = ['onclick' => 'add_upload_file_block(\'image\');', 'class' => 'required']; + $image['thumbnail'] = ts('Display image and thumbnail from these locations on the web:'); + $imageJS['thumbnail'] = ['onclick' => 'add_upload_file_block(\'thumbnail\');', 'class' => 'required']; + $image['default_image'] = ts('Use default image'); + $imageJS['default_image'] = ['onclick' => 'add_upload_file_block(\'default\');', 'class' => 'required']; + $image['noImage'] = ts('Do not display an image'); + $imageJS['noImage'] = ['onclick' => 'add_upload_file_block(\'noImage\');', 'class' => 'required']; + + $this->addRadio('imageOption', ts('Premium Image'), $image, [], NULL, FALSE, $imageJS); $this->addRule('imageOption', ts('Please select an option for the premium image.'), 'required'); $this->addElement('text', 'imageUrl', ts('Image URL')); diff --git a/CRM/Contribute/Form/SoftCredit.php b/CRM/Contribute/Form/SoftCredit.php index d9fddead5d..78a848e9fc 100644 --- a/CRM/Contribute/Form/SoftCredit.php +++ b/CRM/Contribute/Form/SoftCredit.php @@ -43,9 +43,9 @@ class CRM_Contribute_Form_SoftCredit { // radio button for Honor Type foreach ($jsonData['soft_credit_types'] as $value) { - $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value); + $honorTypes[$value] = $softCreditTypes[$value]; } - $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE); + $form->addRadio('soft_credit_type_id', NULL, $honorTypes, ['allowClear' => TRUE]); } } return $form; diff --git a/CRM/Custom/Form/Field.php b/CRM/Custom/Form/Field.php index f2ed93cbbf..39a38af5a0 100644 --- a/CRM/Custom/Form/Field.php +++ b/CRM/Custom/Form/Field.php @@ -334,14 +334,14 @@ class CRM_Custom_Form_Field extends CRM_Core_Form { // is active ? $this->add('checkbox', "option_status[$i]", ts('Active?')); - $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i); + $defaultOption[$i] = NULL; //for checkbox handling of default option $this->add('checkbox', "default_checkbox_option[$i]", NULL); } //default option selection - $this->addGroup($defaultOption, 'default_option'); + $this->addRadio('default_option', NULL, $defaultOption); $_showHide->addToTemplate(); diff --git a/CRM/Event/Form/ManageEvent/Fee.php b/CRM/Event/Form/ManageEvent/Fee.php index fa96e1a4f6..97de92aed8 100644 --- a/CRM/Event/Form/ManageEvent/Fee.php +++ b/CRM/Event/Form/ManageEvent/Fee.php @@ -280,7 +280,7 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent { 'onchange' => "return showHideByValue('price_set_id', '', 'map-field', 'block', 'select', false);", ]); - $default = [$this->createElement('radio', NULL, NULL, NULL, 0)]; + $default = [0 => NULL]; $this->add('hidden', 'price_field_id', '', ['id' => 'price_field_id']); for ($i = 1; $i <= self::NUM_OPTION; $i++) { // label @@ -292,10 +292,10 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent { $this->addRule("value[$i]", ts('Please enter a valid money value for this field (e.g. %1).', [1 => CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency('99.99')]), 'money'); // default - $default[] = $this->createElement('radio', NULL, NULL, NULL, $i); + $default[$i] = NULL; } - $this->addGroup($default, 'default'); + $this->addRadio('default', '', $default); $this->addElement('checkbox', 'is_discount', ts('Discounts by Signup Date?'), NULL, ['onclick' => "warnDiscountDel(); return showHideByValue('is_discount','','discount','block','radio',false);"] @@ -523,10 +523,10 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent { } // default - $default[] = $this->createElement('radio', NULL, NULL, NULL, $i); + $default[$i] = NULL; } - $this->addGroup($default, 'discounted_default'); + $this->addRadio('discounted_default', NULL, $default); } /** diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index cc88542e02..d47d6afd4c 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -640,7 +640,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { $form->assign('priceSet', $form->_priceSet); } else { - $eventFeeBlockValues = []; + $eventFeeBlockValues = $elements = $elementJS = []; foreach ($form->_feeBlock as $fee) { if (is_array($fee)) { @@ -651,18 +651,14 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { } $eventFeeBlockValues['amount_id_' . $fee['amount_id']] = $fee['value']; - $elements[] = &$form->createElement('radio', NULL, '', - CRM_Utils_Money::format($fee['value']) . ' ' . - $fee['label'], - $fee['amount_id'], - $totalAmountJs - ); + $elements[$fee['amount_id']] = CRM_Utils_Money::format($fee['value']) . ' ' . $fee['label']; + $elementJS[$fee['amount_id']] = $totalAmountJs; } } $form->assign('eventFeeBlockValues', json_encode($eventFeeBlockValues)); $form->_defaults['amount'] = $form->_values['event']['default_fee_id'] ?? NULL; - $element = &$form->addGroup($elements, 'amount', ts('Event Fee(s)'), '
'); + $element = &$form->addRadio('amount', ts('Event Fee(s)'), $elements, [], '
', FALSE, $elementJS); if (isset($form->_online) && $form->_online) { $element->freeze(); } diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index a8030975f0..8a81ebdc7d 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -1936,7 +1936,6 @@ DESC limit 1"); // @todo this is the wrong place for this - it should be done as close to form submission // as possible $contributionParams['total_amount'] = $params['amount']; - $contribution = CRM_Contribute_BAO_Contribution::add($contributionParams); // lets store it in the form variable so postProcess hook can get to this and use it diff --git a/CRM/Member/Form/MembershipBlock.php b/CRM/Member/Form/MembershipBlock.php index 19d82ee244..49433bd838 100644 --- a/CRM/Member/Form/MembershipBlock.php +++ b/CRM/Member/Form/MembershipBlock.php @@ -130,7 +130,7 @@ class CRM_Member_Form_MembershipBlock extends CRM_Contribute_Form_ContributionPa $membership = $membershipDefault = $params = []; foreach ($membershipTypes as $k => $v) { $membership[] = $this->createElement('advcheckbox', $k, NULL, $v); - $membershipDefault[] = $this->createElement('radio', NULL, NULL, NULL, $k); + $membershipDefault[$k] = NULL; $membershipRequired[$k] = NULL; if ($isRecur) { $autoRenew = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $k, 'auto_renew'); @@ -160,8 +160,7 @@ class CRM_Member_Form_MembershipBlock extends CRM_Contribute_Form_ContributionPa $this->assign('auto_renew', $this->_renewOption); } $this->addGroup($membership, 'membership_type', ts('Membership Types')); - $this->addGroup($membershipDefault, 'membership_type_default', ts('Membership Types Default')) - ->setAttribute('allowClear', TRUE); + $this->addRadio('membership_type_default', ts('Membership Types Default'), $membershipDefault, ['allowClear' => TRUE]); $this->addFormRule(['CRM_Member_Form_MembershipBlock', 'formRule'], $this->_id); } diff --git a/CRM/Price/BAO/PriceField.php b/CRM/Price/BAO/PriceField.php index 61e570335a..4827d2f132 100644 --- a/CRM/Price/BAO/PriceField.php +++ b/CRM/Price/BAO/PriceField.php @@ -428,7 +428,8 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { $qf->assign('membershipFieldID', $field->id); } - $choice[$opId] = $qf->createElement('radio', NULL, '', $opt['label'], $opt['id'], $extra); + $choice[$opt['id']] = $opt['label']; + $choiceAttrs[$opt['id']] = $extra; if ($is_pay_later) { $qf->add('text', 'txt-' . $elementName, $label, ['size' => '4']); } @@ -437,17 +438,16 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { if (in_array($opId, $freezeOptions)) { self::freezeIfEnabled($choice[$opId], $customOption[$opId]); // CRM-14696 - Improve display for sold out price set options - $choice[$opId]->setText('' . $choice[$opId]->getText() . ' (' . ts('Sold out') . ')'); + $choice[$opt['id']] = '' . $opt['label'] . ' (' . ts('Sold out') . ')'; } } if (!empty($qf->_membershipBlock) && $field->name == 'contribution_amount') { - $choice[] = $qf->createElement('radio', NULL, '', ts('No thank you'), '-1', - [ - 'price' => json_encode([$elementName, '0|0']), - 'data-currency' => $currencyName, - 'onclick' => 'clearAmountOther();', - ] - ); + $choice['-1'] = ts('No thank you'); + $choiceAttrs['-1'] = [ + 'price' => json_encode([$elementName, '0|0']), + 'data-currency' => $currencyName, + 'onclick' => 'clearAmountOther();', + ]; } if (!$field->is_required) { @@ -462,12 +462,11 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField { $none = ts('- none -'); } - $choice[] = $qf->createElement('radio', NULL, '', $none, '0', - ['price' => json_encode([$elementName, '0'])] - ); + $choice['0'] = $none; + $choiceAttrs['0'] = ['price' => json_encode([$elementName, '0'])]; } - $element = &$qf->addGroup($choice, $elementName, $label); + $element = &$qf->addRadio($elementName, $label, $choice, [], NULL, FALSE, $choiceAttrs); // make contribution field required for quick config when membership block is enabled if (($field->name == 'membership_amount' || $field->name == 'contribution_amount') diff --git a/CRM/Price/Form/Field.php b/CRM/Price/Form/Field.php index 96aaf3b558..661d751894 100644 --- a/CRM/Price/Form/Field.php +++ b/CRM/Price/Form/Field.php @@ -316,13 +316,13 @@ class CRM_Price_Form_Field extends CRM_Core_Form { $this->add('checkbox', 'option_status[' . $i . ']', ts('Active?')); $this->add('select', 'option_visibility_id[' . $i . ']', ts('Visibility'), $visibilityType); - $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i); + $defaultOption[$i] = NULL; //for checkbox handling of default option $this->add('checkbox', "default_checkbox_option[$i]", NULL); } //default option selection - $this->addGroup($defaultOption, 'default_option'); + $this->addRadio('default_option', NULL, $defaultOption); $_showHide->addToTemplate(); // is_display_amounts -- 2.25.1