Revision: 2648
          https://sourceforge.net/p/mrbs/code/2648/
Author:   cimorrison
Date:     2013-01-19 10:09:50 +0000 (Sat, 19 Jan 2013)
Log Message:
-----------
Restructured code

Modified Paths:
--------------
    mrbs/branches/linked_bookings/web/edit_entry.php
    mrbs/branches/linked_bookings/web/functions.inc

Modified: mrbs/branches/linked_bookings/web/edit_entry.php
===================================================================
--- mrbs/branches/linked_bookings/web/edit_entry.php    2013-01-17 13:45:00 UTC 
(rev 2647)
+++ mrbs/branches/linked_bookings/web/edit_entry.php    2013-01-19 10:09:50 UTC 
(rev 2648)
@@ -363,10 +363,47 @@
 }
 
 
+// Return an array of room options for the area with $area_id
+function get_room_options($area_id)
+{
+  global $tbl_room, $tbl_area, $areas;
+  
+  static $all_rooms;
+  
+  // Get the details of all the enabled rooms
+  if (!isset($all_rooms))
+  {
+    $all_rooms = array();
+    $sql = "SELECT R.id, R.room_name, R.area_id
+              FROM $tbl_room R, $tbl_area A
+             WHERE R.area_id = A.id
+               AND R.disabled=0
+               AND A.disabled=0
+          ORDER BY R.area_id, R.sort_key";
+    $res = sql_query($sql);
+    if ($res === FALSE)
+    {
+      trigger_error(sql_error(), E_USER_WARNING);
+      fatal_error(FALSE, get_vocab("fatal_db_error"));
+    }
+    for ($i = 0; ($row = sql_row_keyed($res, $i)); $i++)
+    {
+      $all_rooms[$row['area_id']][$row['id']] = $row['room_name'];
+    }
+  }
+  
+  $options = array();
+  if (isset($all_rooms[$area_id]))
+  {
+    $options = array($areas[$area_id]['area_name'] => $all_rooms[$area_id]);
+  }
+  return $options;
+}
+
+
 function create_field_entry_rooms($disabled=FALSE)
 {
   global $multiroom_allowed, $rooms, $area_id, $selected_rooms, $areas;
-  global $tbl_room, $tbl_area;
   
   // $selected_rooms will be populated if we've come from a drag selection
   if (empty($selected_rooms))
@@ -374,25 +411,6 @@
     $selected_rooms = $rooms;
   }
   
-  // Get the details of all the enabled rooms
-  $all_rooms = array();
-  $sql = "SELECT R.id, R.room_name, R.area_id
-            FROM $tbl_room R, $tbl_area A
-           WHERE R.area_id = A.id
-             AND R.disabled=0
-             AND A.disabled=0
-        ORDER BY R.area_id, R.sort_key";
-  $res = sql_query($sql);
-  if ($res === FALSE)
-  {
-    trigger_error(sql_error(), E_USER_WARNING);
-    fatal_error(FALSE, get_vocab("fatal_db_error"));
-  }
-  for ($i = 0; ($row = sql_row_keyed($res, $i)); $i++)
-  {
-    $all_rooms[$row['area_id']][$row['id']] = $row['room_name'];
-  }
-
   echo "<div id=\"div_rooms\">\n";
   echo "<label for=\"rooms\">" . get_vocab("rooms") . ":</label>\n";
   echo "<div class=\"group\">\n";
@@ -400,7 +418,7 @@
   // First of all generate the rooms for this area
   $params = array('name'        => 'rooms[]',
                   'id'          => 'rooms',
-                  'options'     => $all_rooms[$area_id],
+                  'options'     => get_room_options($area_id),
                   'force_assoc' => TRUE,
                   'value'       => $selected_rooms,
                   'multiple'    => $multiroom_allowed,
@@ -411,7 +429,7 @@
   // Then generate templates for all the rooms
   $params['disabled']      = TRUE;
   $params['create_hidden'] = FALSE;
-  foreach ($all_rooms as $a => $enabled_rooms)
+  foreach ($areas as $a => $properties)
   {
     $attributes = array();
     $attributes[] = 'style="display: none"';
@@ -426,7 +444,7 @@
     $attributes[] = 'data-timezone="'            . 
htmlspecialchars($areas[$a]['timezone']) . '"';
     
     $params['id']         = 'rooms' . $a;
-    $params['options']    = $enabled_rooms;
+    $params['options']    = get_room_options($a);
     $params['attributes'] = $attributes;
     generate_select($params);
   }

Modified: mrbs/branches/linked_bookings/web/functions.inc
===================================================================
--- mrbs/branches/linked_bookings/web/functions.inc     2013-01-17 13:45:00 UTC 
(rev 2647)
+++ mrbs/branches/linked_bookings/web/functions.inc     2013-01-19 10:09:50 UTC 
(rev 2648)
@@ -983,9 +983,68 @@
 }
 
 
+// Generate the HTML for the options to go inside a <select> element
 // Generates a select box with an associated label
 //
 //   $params    an associative array holding the function parameters:
+//
+//      OPTIONAL
+//        '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.
+//                        Can also be a two dimensional array where the first 
index is
+//                        the option group.
+//        '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 ''.   Can be a 
single value
+//                        or an array of values.
+//
+function generate_options($params)
+{
+  // some sanity checking on params
+  foreach (array('options', 'force_assoc', 'value') as $key)
+  {
+    if (!isset($params[$key]))
+    {
+      switch ($key)
+      {
+        case 'options':
+        case 'value':
+          $params[$key] = array();
+          break;
+        case 'force_assoc':
+          $params[$key] = FALSE;
+          break;
+        default:
+          break;
+      }
+    }
+  }
+  
+  $html = '';
+  
+  foreach ($params['options'] as $value => $text)
+  {
+    // We can cope with both associative and ordinary arrays
+    if (!$params['force_assoc'] && !is_assoc($params['options']))
+    {
+      $value = $text;
+    }
+    $html .= "<option value=\"" . htmlspecialchars($value) . "\"";
+    $html .= (in_array($value, $params['value'])) ? " selected=\"selected\"" : 
'';
+    $html .= ">".htmlspecialchars($text)."</option>\n";
+  }
+  
+  return $html;
+}
+
+
+// Generates a select box with an associated label
+//
+//   $params    an associative array holding the function parameters:
 //      MANDATORY
 //        'name'          The name of the element.
 //      OPTIONAL
@@ -995,6 +1054,8 @@
 //        '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.
+//                        Can also be a two dimensional array where the first 
index is
+//                        the option group.
 //        '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
@@ -1107,18 +1168,39 @@
   $html .= (isset($params['attributes'])) ? " " . $params['attributes'] : "";
   $html .= ">\n";
 
-  foreach ($params['options'] as $value => $text)
+  if (count($params['options']) > 0)
   {
-    // We can cope with both associative and ordinary arrays
-    if (!$params['force_assoc'] && !is_assoc($params['options']))
+    $options_params = array('force_assoc' => $params['force_assoc'],
+                            'value'       => $params['value']);
+    // If it's a two dimensional array then we've got option groups,
+    // but don't bother displaying the option group if there's only
+    // one of them
+    if (is_array(current($params['options'])))
     {
-      $value = $text;
+      if (count($params['options'] == 1))
+      {
+        $options_params['options'] = current($params['options']);
+        $html .= generate_options($options_params);
+      }
+      else
+      {
+        foreach ($params['options'] as $optgroup => $options)
+        {
+          $html .= "<optgroup label=\"" . htmlspecialchars($optgroup) . 
"\">\n";
+          $options_params['options'] = $options;
+          $html .= generate_options($options_params);
+          $html .= "</optgroup>\n";
+        }
+      }
     }
-    $html .= "<option value=\"" . htmlspecialchars($value) . "\"";
-    $html .= (in_array($value, $params['value'])) ? " selected=\"selected\"" : 
'';
-    $html .= ">".htmlspecialchars($text)."</option>\n";
+    else
+    {
+      // It's just a one dimensional array
+      $options_params['options'] = $params['options'];
+      $html .= generate_options($options_params);
+    }
   }
-
+  
   $html .= "</select>\n";
   
   // and hidden inputs if the select box is disabled
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to