Revision
2822
Author
cimorrison
Date
2014-03-03 17:38:31 +0000 (Mon, 03 Mar 2014)

Log Message

Changed the operation for generate_radio_group() so that it operates in the same way as generate_select() and can take either a simple or an associative array.   This enables custom fields to be presented as radio buttons instead of as <select> elements if desired.   See SF Support Requests #489.

Modified Paths

Diff

Modified: mrbs/trunk/web/edit_area_room.php (2821 => 2822)


--- mrbs/trunk/web/edit_area_room.php	2014-02-25 16:13:17 UTC (rev 2821)
+++ mrbs/trunk/web/edit_area_room.php	2014-03-03 17:38:31 UTC (rev 2822)
@@ -669,6 +669,7 @@
                         'name'          => 'room_disabled',
                         'value'         => ($row['disabled']) ? '1' : '0',
                         'options'       => $options,
+                        'force_assoc'   => TRUE,
                         'disabled'      => $disabled,
                         'create_hidden' => FALSE);
         generate_radio_group($params);
@@ -862,7 +863,8 @@
                   'label_title' => get_vocab("disabled_area_note"),
                   'name'        => 'area_disabled',
                   'value'       => ($row['disabled']) ? '1' : '0',
-                  'options'     => $options);
+                  'options'     => $options,
+                  'force_assoc' => TRUE);
   generate_radio_group($params);
   echo "</div>\n";
         
@@ -893,10 +895,11 @@
   echo "<div id=\"mode\">\n";
   $options = array('1' => get_vocab("mode_periods"),
                    '0' => get_vocab("mode_times"));
-  $params = array('label'   => get_vocab("mode") . ":",
-                  'name'    => 'area_enable_periods',
-                  'value'   => ($enable_periods) ? '1' : '0',
-                  'options' => $options);
+  $params = array('label'       => get_vocab("mode") . ":",
+                  'name'        => 'area_enable_periods',
+                  'value'       => ($enable_periods) ? '1' : '0',
+                  'options'     => $options,
+                  'force_assoc' => TRUE);
   generate_radio_group($params);
   echo "</div>\n";
       
@@ -1142,10 +1145,11 @@
   
   $options = array('1' => get_vocab("default_confirmed"),
                    '0' => get_vocab("default_tentative"));
-  $params = array('label'   => get_vocab("default_settings_conf") . ":",
-                  'name'    => 'area_confirmed_default',
-                  'options' => $options,
-                  'value'   => ($confirmed_default) ? '1' : '0');
+  $params = array('label'       => get_vocab("default_settings_conf") . ":",
+                  'name'        => 'area_confirmed_default',
+                  'options'     => $options,
+                  'force_assoc' => TRUE,
+                  'value'       => ($confirmed_default) ? '1' : '0');
   generate_radio_group($params);
 
   echo "</fieldset>\n";
@@ -1190,10 +1194,11 @@
   // Default privacy settings
   $options = array('1' => get_vocab("default_private"),
                    '0' => get_vocab("default_public"));
-  $params = array('label' => get_vocab("default_settings"),
-                  'name'  => 'area_private_default',
-                  'options' => $options,
-                  'value'   => ($private_default) ? '1' : '0');
+  $params = array('label'       => get_vocab("default_settings"),
+                  'name'        => 'area_private_default',
+                  'options'     => $options,
+                  'force_assoc' => TRUE,
+                  'value'       => ($private_default) ? '1' : '0');
   generate_radio_group($params);
 
   echo "</fieldset>\n";

Modified: mrbs/trunk/web/edit_entry.php (2821 => 2822)


--- mrbs/trunk/web/edit_entry.php	2014-02-25 16:13:17 UTC (rev 2821)
+++ mrbs/trunk/web/edit_entry.php	2014-03-03 17:38:31 UTC (rev 2822)
@@ -493,11 +493,12 @@
     $buttons[0] = get_vocab("tentative");
     $buttons[1] = get_vocab("confirmed");
     
-    $params = array('label'    => get_vocab("confirmation_status") . ":",
-                    'name'     => 'confirmed',
-                    'value'    => ($confirmed) ? 1 : 0,
-                    'options'  => $buttons,
-                    'disabled' => $disabled);
+    $params = array('label'       => get_vocab("confirmation_status") . ":",
+                    'name'        => 'confirmed',
+                    'value'       => ($confirmed) ? 1 : 0,
+                    'options'     => $buttons,
+                    'force_assoc' => TRUE,
+                    'disabled'    => $disabled);
                     
     generate_radio_group($params);
 
@@ -518,11 +519,12 @@
     $buttons[0] = get_vocab("public");
     $buttons[1] = get_vocab("private");
     
-    $params = array('label'    => get_vocab("privacy_status") . ":",
-                    'name'     => 'private',
-                    'value'    => ($private) ? 1 : 0,
-                    'options'  => $buttons,
-                    'disabled' => $private_mandatory || $disabled);
+    $params = array('label'       => get_vocab("privacy_status") . ":",
+                    'name'        => 'private',
+                    'value'       => ($private) ? 1 : 0,
+                    'options'     => $buttons,
+                    'force_assoc' => TRUE,
+                    'disabled'    => $private_mandatory || $disabled);
                     
     generate_radio_group($params);
 
@@ -1199,7 +1201,8 @@
                   'name'          => 'rep_type',
                   'value'         => $rep_type,
                   'disabled'      => $disabled,
-                  'options'       => array());
+                  'options'       => array(),
+                  'force_assoc'   => TRUE);
   foreach (array(REP_NONE, REP_DAILY, REP_WEEKLY, REP_MONTHLY, REP_YEARLY) as $i)
   {
     $params['options'][$i] = get_vocab("rep_type_$i");

Modified: mrbs/trunk/web/functions.inc (2821 => 2822)


--- mrbs/trunk/web/functions.inc	2014-02-25 16:13:17 UTC (rev 2821)
+++ mrbs/trunk/web/functions.inc	2014-03-03 17:38:31 UTC (rev 2822)
@@ -840,8 +840,14 @@
 //      OPTIONAL
 //        'label_title'   The text to be used for the title attribute for the field label
 //        'value'         The value of the input.  Default ''
-//        'options'       An associative array where the key is the value of the
-//                        button and the value is the button text
+//        'options'       An array of options for the radio buttons.   Can be a simple
+//                        array or an associative array with value => label members for
+//                        each button.   Default is an empty array.
+//        'force_assoc'   Boolean.  Forces the options array to be treated as an
+//                        associative array.  Default FALSE, ie it is treated as whatever
+//                        it looks like.  (This parameter is necessary because if you
+//                        index an array with strings that look like integers then PHP
+//                        casts the keys to integers and the array becomes a simple array)
 //        'disabled'      Whether the field should be disabled.  Default FALSE
 //        'create_hidden' Boolean.  If TRUE hidden inputs are created if 'disabled' is set
 //                        Default TRUE
@@ -849,7 +855,8 @@
 function generate_radio_group($params)
 {
   // some sanity checking on params
-  foreach (array('label', 'label_title', 'name', 'options', 'value', 'disabled', 'create_hidden') as $key)
+  foreach (array('label', 'label_title', 'name', 'options', 'value', 'force_assoc', 
+                 'disabled', 'create_hidden') as $key)
   {
     if (!isset($params[$key]))
     {
@@ -867,6 +874,7 @@
           $params[$key] = '';
           break;
         case 'disabled':
+        case 'force_assoc':
           $params[$key] = FALSE;
           break;
         case 'create_hidden':
@@ -888,8 +896,18 @@
   // Output each radio button
   while ($option = each($params['options']))
   {
+    // We can cope with both associative and ordinary arrays
+    $button_label = $option['value'];
+    if (!$params['force_assoc'] && !is_assoc($params['options']))
+    {
+      $button_value = $button_label;
+    }
+    else
+    {
+      $button_value = $option['key'];
+    }
     generate_radio(array('name'          => $params['name'],
-                         'options'       => array($option['key'] => $option['value']),
+                         'options'       => array($button_value => $button_label),
                          'value'         => $params['value'],
                          'disabled'      => $params['disabled'],
                          'create_hidden' => $params['create_hidden']));
@@ -913,8 +931,9 @@
 //        'name'        The name of the input.
 //      OPTIONAL
 //        'value'       The value of the input.  Can be an array. Default array()
-//        'options'     An associative array where the key is the value of the
-//                      button and the value is the button text
+//        'options'     An array of options for the checkboxes.   Can be a simple
+//                      array or an associative array with value => label members for
+//                      each checkbox.   Default is an empty array.
 //        'force_assoc' Boolean.  Forces the options array to be treated as an
 //                      associative array.  Default FALSE, ie it is treated as whatever
 //                      it looks like.  (This parameter is necessary because if you

Modified: mrbs/trunk/web/report.php (2821 => 2822)


--- mrbs/trunk/web/report.php	2014-02-25 16:13:17 UTC (rev 2821)
+++ mrbs/trunk/web/report.php	2014-03-03 17:38:31 UTC (rev 2822)
@@ -141,10 +141,11 @@
           {
             echo "<div id=\"div_privacystatus\">\n";
             $options = array(PRIVATE_BOTH => get_vocab("both"), PRIVATE_NO => get_vocab("default_public"), PRIVATE_YES => get_vocab("default_private"));
-            $params = array('label'   => get_vocab("privacy_status") . ':',
-                            'name'    => 'match_private',
-                            'options' => $options,
-                            'value'   => $vars['match_private']);
+            $params = array('label'       => get_vocab("privacy_status") . ':',
+                            'name'        => 'match_private',
+                            'options'     => $options,
+                            'force_assoc' => TRUE,
+                            'value'       => $vars['match_private']);
             generate_radio_group($params);
             echo "</div>\n";
           }
@@ -159,10 +160,11 @@
         {
           echo "<div id=\"div_confirmationstatus\">\n";
           $options = array(CONFIRMED_BOTH => get_vocab("both"), CONFIRMED_YES => get_vocab("confirmed"), CONFIRMED_NO => get_vocab("tentative"));
-          $params = array('label'   => get_vocab("confirmation_status") . ':',
-                          'name'    => 'match_confirmed',
-                          'options' => $options,
-                          'value'   => $vars['match_confirmed']);
+          $params = array('label'       => get_vocab("confirmation_status") . ':',
+                          'name'        => 'match_confirmed',
+                          'options'     => $options,
+                          'force_assoc' => TRUE,
+                          'value'       => $vars['match_confirmed']);
           generate_radio_group($params);
           echo "</div>\n";
         }
@@ -176,10 +178,11 @@
         {
           echo "<div id=\"div_approvalstatus\">\n";
           $options = array(APPROVED_BOTH => get_vocab("both"), APPROVED_YES => get_vocab("approved"), APPROVED_NO => get_vocab("awaiting_approval"));
-          $params = array('label'   => get_vocab("approval_status") . ':',
-                          'name'    => 'match_approved',
-                          'options' => $options,
-                          'value'   => $vars['match_approved']);
+          $params = array('label'       => get_vocab("approval_status") . ':',
+                          'name'        => 'match_approved',
+                          'options'     => $options,
+                          'force_assoc' => TRUE,
+                          'value'       => $vars['match_approved']);
           generate_radio_group($params);
           echo "</div>\n";
         }
@@ -248,10 +251,11 @@
         echo "<div id=\"div_output\">\n";
         $buttons = array(REPORT  => get_vocab('report'),
                          SUMMARY => get_vocab('summary'));
-        $params = array('label'   => get_vocab('output') . ":",
-                        'name'    => 'output',
-                        'value'   => $vars['output'],
-                        'options' => $buttons);
+        $params = array('label'       => get_vocab('output') . ":",
+                        'name'        => 'output',
+                        'value'       => $vars['output'],
+                        'options'     => $buttons,
+                        'force_assoc' => TRUE);
         generate_radio_group($params);                  
         echo "</div>\n";
         break;
@@ -266,10 +270,11 @@
         {
           $buttons[OUTPUT_ICAL] = "ical";
         }
-        $params = array('label'   => get_vocab('format') . ":",
-                        'name'    => 'output_format',
-                        'value'   => $vars['output_format'],
-                        'options' => $buttons);
+        $params = array('label'       => get_vocab('format') . ":",
+                        'name'        => 'output_format',
+                        'value'       => $vars['output_format'],
+                        'options'     => $buttons,
+                        'force_assoc' => TRUE);
         generate_radio_group($params);
         echo "</div>\n";
         break;