REF use centralised addRadio function more
authorSeamus Lee <seamuslee001@gmail.com>
Wed, 23 Dec 2020 22:22:09 +0000 (09:22 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Wed, 30 Dec 2020 03:33:49 +0000 (14:33 +1100)
13 files changed:
CRM/Contact/Form/Task/AddToParentClass.php
CRM/Contribute/BAO/Premium.php
CRM/Contribute/Form/ContributionBase.php
CRM/Contribute/Form/ContributionPage/Amount.php
CRM/Contribute/Form/ManagePremiums.php
CRM/Contribute/Form/SoftCredit.php
CRM/Custom/Form/Field.php
CRM/Event/Form/ManageEvent/Fee.php
CRM/Event/Form/Registration/Register.php
CRM/Member/Form/Membership.php
CRM/Member/Form/MembershipBlock.php
CRM/Price/BAO/PriceField.php
CRM/Price/Form/Field.php

index aee54878d4754157a8addbf0f0562c59e53ce435..42caaba86a218d888d0ff13c8d06ecf23a22be0c 100644 (file)
@@ -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);
     }
index 332ff2b4f6363bb8d937d04cf777a8216588ee57..b89c6ca158d9234e1e131377e94782611395b01b 100644 (file)
@@ -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);
index 7bb940637f2b98ec50567385996753a3553bbc6c..27a6a5c86a879fdd67805af16e7d86c17876e445 100644 (file)
@@ -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');
index 7aca7aac4fa75fdd73012287d10c11f5273fabb7..3d8907b826c8a7161ad45fcdd89b1eb5d8e15699 100644 (file)
@@ -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' );"]);
 
index a5ad9e40e36357a330801293a5766c09ab2f089e..45904ea49f2d114aa79026729bac73fb019aa9d8 100644 (file)
@@ -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'));
index d9fddead5dd94983792223e2493aeb475c045877..78a848e9fc88d134a1192bf2e1ea363be41cfd7b 100644 (file)
@@ -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;
index f2ed93cbbf1734fd3d1084250e19d0b0c316ccb8..39a38af5a0d4c4ccfaedeb76a547d10ae306de6b 100644 (file)
@@ -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();
 
index fa96e1a4f6c4a7f304124294eafb68d50ca18cb5..97de92aed885c64a39059d363af6edd67cd1da2c 100644 (file)
@@ -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);
   }
 
   /**
index cc88542e02fbd9976d3bb65a6773d89d1f75749e..d47d6afd4c23549c07dd6f418da9baac79021537 100644 (file)
@@ -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)'), '<br />');
+      $element = &$form->addRadio('amount', ts('Event Fee(s)'), $elements, [], '<br />', FALSE, $elementJS);
       if (isset($form->_online) && $form->_online) {
         $element->freeze();
       }
index a8030975f02d04c408b668cba6ee1c5c0a2ad143..8a81ebdc7dd0e3ea2cab653e8d860978c06e43ae 100644 (file)
@@ -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
index 19d82ee2445f8ce308e393ab4aa334f302070ea1..49433bd8388311021d071a04c3dd4e243da9623b 100644 (file)
@@ -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);
     }
index 61e570335a60af037842382172d30c38a0773ffc..4827d2f132f1d7c2316072cc3ace0dc914f2fc1e 100644 (file)
@@ -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('<span class="sold-out-option">' . $choice[$opId]->getText() . '&nbsp;(' . ts('Sold out') . ')</span>');
+            $choice[$opt['id']] = '<span class="sold-out-option">' . $opt['label'] . '&nbsp;(' . ts('Sold out') . ')</span>';
           }
         }
         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')
index 96aaf3b55854961d0c8737820e073966b87d6b08..661d751894c3f04bb8b8e4b94df569be40e8d920 100644 (file)
@@ -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