Revision: 2520
          https://sourceforge.net/p/mrbs/code/2520/
Author:   cimorrison
Date:     2012-10-24 09:26:27 +0000 (Wed, 24 Oct 2012)
Log Message:
-----------
Simplified code

Modified Paths:
--------------
    mrbs/trunk/web/edit_entry.php
    mrbs/trunk/web/edit_entry_handler.php
    mrbs/trunk/web/edit_users.php
    mrbs/trunk/web/functions.inc
    mrbs/trunk/web/internalconfig.inc.php

Modified: mrbs/trunk/web/edit_entry.php
===================================================================
--- mrbs/trunk/web/edit_entry.php       2012-10-22 16:53:57 UTC (rev 2519)
+++ mrbs/trunk/web/edit_entry.php       2012-10-24 09:26:27 UTC (rev 2520)
@@ -1274,12 +1274,6 @@
   }
 }
 
-// Apply the string prefix to the rep_day values.  (The string prefix is 
-// necessary because we want the options array to be associative)
-for ($i=0; $i<count($rep_day); $i++)
-{
-  $rep_day[$i] = STRING_PREFIX . $rep_day[$i];
-}
 
 // Show the repeat fields if (a) it's a new booking and repeats are allowed,
 // or else if it's an existing booking.  (It's not particularly obvious but
@@ -1335,9 +1329,9 @@
         // Display day name checkboxes according to language and preferred 
weekday start.
         $wday = ($i + $weekstarts) % 7;
         // We need to ensure the index is a string to force the array to be 
associative
-        $v = STRING_PREFIX . $wday;
-        $params['options'][$v] = day_name($wday, 
$strftime_format['dayname_edit']);
+        $params['options'][$wday] = day_name($wday, 
$strftime_format['dayname_edit']);
       }
+      $params['force_assoc'] = TRUE;
       generate_checkbox_group($params);
       echo "</div>\n";
 
@@ -1400,12 +1394,13 @@
       $options = array();
       foreach (array('1', '2', '3', '4', '-1', '-2', '-3', '-4') as $i)
       {
-        $options[STRING_PREFIX . $i] = get_vocab("ord_" . $i);
+        $options[$i] = get_vocab("ord_" . $i);
       }
-      $params = array('name'     => 'month_relative_ord',
-                      'value'    => STRING_PREFIX . $month_relative_ord,
-                      'disabled' => $disabled,
-                      'options'  => $options);
+      $params = array('name'        => 'month_relative_ord',
+                      'value'       => $month_relative_ord,
+                      'disabled'    => $disabled,
+                      'options'     => $options,
+                      'force_assoc' => TRUE);
       generate_select($params);
       
       $options = array();

Modified: mrbs/trunk/web/edit_entry_handler.php
===================================================================
--- mrbs/trunk/web/edit_entry_handler.php       2012-10-22 16:53:57 UTC (rev 
2519)
+++ mrbs/trunk/web/edit_entry_handler.php       2012-10-24 09:26:27 UTC (rev 
2520)
@@ -81,23 +81,6 @@
 foreach($formvars as $var => $var_type)
 {
   $$var = get_form_var($var, $var_type);
-  // deal with some special cases
-  switch ($var)
-  {
-    // we need to strip off the string prefix, which was put there to
-    // force an associative array
-    case 'rep_day':
-      for ($i=0; $i<count($rep_day); $i++)
-      {
-        $rep_day[$i] = substr($rep_day[$i], strlen(STRING_PREFIX));
-      }
-      break;
-    case 'month_relative_ord':
-      $$var = substr($$var, strlen(STRING_PREFIX));
-      break;
-    default:
-      break;
-  }
 }
 
 // BACK:  we didn't really want to be here - send them to the returl

Modified: mrbs/trunk/web/edit_users.php
===================================================================
--- mrbs/trunk/web/edit_users.php       2012-10-22 16:53:57 UTC (rev 2519)
+++ mrbs/trunk/web/edit_users.php       2012-10-24 09:26:27 UTC (rev 2520)
@@ -366,10 +366,7 @@
                     $params['options'] = array();     
                     for ($i=0; $i<=$level; $i++)
                     {
-                      // We add a string prefix to the level to force the 
array to be
-                      // associative.   We strip it off when we get the form 
variable
-                      $v = STRING_PREFIX . $i;
-                      $params['options'][$v] = get_vocab("level_$i");
+                      $params['options'][$i] = get_vocab("level_$i");
                       // Work out which option should be selected by default:
                       //   if we're editing an existing entry, then it should 
be the current value;
                       //   if we're adding the very first entry, then it 
should be an admin;
@@ -381,6 +378,7 @@
                         $params['value'] = $v;
                       }
                     }
+                    $params['force_assoc'] = TRUE;
                     generate_select($params);
                     break;
                   case 'name':
@@ -587,7 +585,6 @@
         case 'level':
           // level:  set a safe default (lowest level of access)
           // if there is no value set
-          $values[$fieldname] = substr($values[$fieldname], 
strlen(STRING_PREFIX));
           $q_string .= "&$fieldname=" . $values[$fieldname];
           if (!isset($values[$fieldname]))
           {

Modified: mrbs/trunk/web/functions.inc
===================================================================
--- mrbs/trunk/web/functions.inc        2012-10-22 16:53:57 UTC (rev 2519)
+++ mrbs/trunk/web/functions.inc        2012-10-24 09:26:27 UTC (rev 2520)
@@ -767,12 +767,17 @@
 //        '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
+//        '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
 //
 function generate_checkbox_group($params)
 {
   // some sanity checking on params
-  foreach (array('label', 'name', 'options', 'value', 'disabled') as $key)
+  foreach (array('label', 'name', 'options', 'force_assoc', 'value', 
'disabled') as $key)
   {
     if (!isset($params[$key]))
     {
@@ -788,6 +793,7 @@
         case 'value':
           $params[$key] = array();
           break;
+        case 'force_assoc':
         case 'disabled':
           $params[$key] = FALSE;
         default:
@@ -808,6 +814,11 @@
   // Output each checkbox
   foreach ($params['options'] as $value => $token)
   {
+    // We can cope with both associative and ordinary arrays
+    if (!$params['force_assoc'] && !is_assoc($params['options']))
+    {
+      $value = $token;
+    }
     $html .= "<label>";
     $html .= "<input class=\"checkbox\" type=\"checkbox\" name=\"" . 
$params['name'] . "\" value=\"$value\"";          
     $html .= (in_array($value, $params['value'])) ? " checked=\"checked\"" : 
"";
@@ -836,6 +847,11 @@
 //        'options'     An array of options for the select element.   Can be a 
simple
 //                      array or an associative array with value => text 
members for
 //                      each <option> in the <select> element.   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)
 //        'value'       The value of the input.  Default ''
 //        'disabled'    Whether the field should be disabled.  Default FALSE
 //        'mandatory'   Whether the field is a required field.  Default FALSE
@@ -843,7 +859,7 @@
 function generate_select($params)
 {
   // some sanity checking on params
-  foreach (array('label', 'name', 'options', 'value', 'disabled', 'mandatory') 
as $key)
+  foreach (array('label', 'name', 'options', 'force_assoc', 'value', 
'disabled', 'mandatory') as $key)
   {
     if (!isset($params[$key]))
     {
@@ -858,6 +874,7 @@
         case 'value':
           $params[$key] = '';
           break;
+        case 'force_assoc':
         case 'disabled':
         case 'mandatory':
           $params[$key] = FALSE;
@@ -879,26 +896,20 @@
   $html .= ($params['mandatory']) ? " required aria-required=\"true\"" : "";
   $html .= ">\n";
 
-  // We can cope with both associative and ordinary arrays
-  if (is_assoc($params['options']))
+  foreach ($params['options'] as $value => $text)
   {
-    foreach ($params['options'] as $value => $text)
+    // We can cope with both associative and ordinary arrays
+    if (!$params['force_assoc'] && !is_assoc($params['options']))
     {
-      $html .= "<option value=\"$value\"";
-      $html .= ($params['value'] == $value) ? " selected=\"selected\"" : '';
-      $html .= ">".htmlspecialchars($text)."</option>\n";
+      $value = $text;
     }
+    $html .= "<option value=\"$value\"";
+    $html .= ($params['value'] == $value) ? " selected=\"selected\"" : '';
+    $html .= ">".htmlspecialchars($text)."</option>\n";
   }
-  else
-  {
-    foreach ($params['options'] as $text)
-    {
-      $html .= "<option";
-      $html .= ($params['value'] == $text) ? " selected=\"selected\"" : '';
-      $html .= ">".htmlspecialchars($text)."</option>\n";
-    }
-  }
+
   $html .= "</select>\n";
+  
   // and a hidden input if the select box is disabled
   if ($params['disabled'])
   {

Modified: mrbs/trunk/web/internalconfig.inc.php
===================================================================
--- mrbs/trunk/web/internalconfig.inc.php       2012-10-22 16:53:57 UTC (rev 
2519)
+++ mrbs/trunk/web/internalconfig.inc.php       2012-10-24 09:26:27 UTC (rev 
2520)
@@ -347,9 +347,7 @@
 // Interval types used in booking policies
 $interval_types = array('day', 'week', 'month', 'year', 'future');
 
-define('STRING_PREFIX', 'S');  // Just some string - doesn't matter what it is
 
-
 /********************************************************
  * JavaScript - internal use, do not change
  ********************************************************/
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to