fix unit plural in contribution page
authorSamuele Masetto <samuele.masetto@gmail.com>
Sat, 30 Oct 2021 17:14:53 +0000 (19:14 +0200)
committerSamuele Masetto <samuele.masetto@gmail.com>
Tue, 23 Nov 2021 07:38:42 +0000 (08:38 +0100)
style fixes for compiling check

fix a typo

fix `recur_frequency_unit` hidden field value when there is only one unit option

CRM/Contribute/BAO/Contribution.php
CRM/Contribute/Form/Contribution/Main.php

index 89917d0a16ab62c464ee9e7dfe1a864259ff44ad..5ae6f8b73e31ffb7a3e82bfd3d28d3205dd7496c 100644 (file)
@@ -5030,4 +5030,29 @@ LIMIT 1;";
     return [$sourceContactId, $targetContactId];
   }
 
+  /**
+   * Get the unit label with the plural option
+   *
+   * @param string $unit
+   * @return string
+   */
+  public static function getUnitLabelWithPlural($unit) {
+    switch ($unit) {
+      case 'day':
+        return ts('day(s)');
+
+      case 'week':
+        return ts('week(s)');
+
+      case 'month':
+        return ts('month(s)');
+
+      case 'year':
+        return ts('year(s)');
+
+      default:
+        throw new CRM_Core_Exception('Unknown unit: ' . $unit);
+    }
+  }
+
 }
index 818fde0b2a2c892190a610e996902d90e68847d7..b3c5c1cd35c2543ac612afc56e5814ca859afe5e 100644 (file)
@@ -757,11 +757,12 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     }
 
     $frUnits = $form->_values['recur_frequency_unit'] ?? NULL;
+    $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, TRUE);
     if (empty($frUnits) &&
       $className == 'CRM_Contribute_Form_Contribution'
     ) {
       $frUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR,
-        CRM_Core_OptionGroup::values('recur_frequency_units')
+        CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, FALSE, NULL, 'value')
       );
     }
 
@@ -783,15 +784,14 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     // CRM 10860, display text instead of a dropdown if there's only 1 frequency unit
     if (count($unitVals) == 1) {
       $form->assign('one_frequency_unit', TRUE);
-      $unit = $unitVals[0];
-      $form->add('hidden', 'frequency_unit', $unit);
+      $form->add('hidden', 'frequency_unit', $unitVals[0]);
       if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') {
-        $unit .= "(s)";
+        $unit = CRM_Contribute_BAO_Contribution::getUnitLabelWithPlural($unitVals[0]);
         $form->assign('frequency_unit', $unit);
       }
       else {
         $is_recur_label = ts('I want to contribute this amount every %1',
-          [1 => $unit]
+          [1 => $frequencyUnits[$unitVals[0]]]
         );
         $form->assign('all_text_recur', TRUE);
       }
@@ -799,12 +799,11 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     else {
       $form->assign('one_frequency_unit', FALSE);
       $units = [];
-      $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units', FALSE, FALSE, TRUE);
       foreach ($unitVals as $key => $val) {
         if (array_key_exists($val, $frequencyUnits)) {
           $units[$val] = $frequencyUnits[$val];
           if (!empty($form->_values['is_recur_interval']) || $className == 'CRM_Contribute_Form_Contribution') {
-            $units[$val] = "{$frequencyUnits[$val]}(s)";
+            $units[$val] = CRM_Contribute_BAO_Contribution::getUnitLabelWithPlural($val);
             $unit = ts('Every');
           }
         }