Changeset:
        017e435c99b5
        
https://sourceforge.net/p/mrbs/hg-code/ci/017e435c99b52b7301ebacb82dc28f1058be1af4
Author:
        Campbell Morrison <[email protected]>
Date:
        Tue Jan 31 15:52:10 2017 +0000
Log message:

Added the ability to specify a default room if no LOCATION property is given 
when importing a VEVENT

diffstat:

 web/functions.inc |  71 ++++++++++++++++++++++++++++++++++++++++++++----------
 web/import.php    |  43 +++++++++++++++++++++++++++++++-
 web/lang/lang.en  |   2 +
 web/mrbs_sql.inc  |  30 +++++++++++++++++++++++
 4 files changed, 130 insertions(+), 16 deletions(-)

diffs (238 lines):

diff -r 80052eb62baa -r 017e435c99b5 web/functions.inc
--- a/web/functions.inc Mon Jan 30 17:24:45 2017 +0000
+++ b/web/functions.inc Tue Jan 31 15:52:10 2017 +0000
@@ -1187,6 +1187,42 @@
   echo $html;
 }
 
+// Generates a set of <option> elements
+//
+//        $options        An array of options for the element.   Can be a 
simple
+//                        array or an associative array with value => text 
members for
+//                        each <option>.   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 ''.   Can be a 
single value
+//                        or an array of values.
+function generate_options($options, $force_assoc=false, $value='')
+{
+  if (!is_array($value))
+  {
+    $value = array($value);
+  }
+  
+  $html = '';
+  
+  foreach ($options as $key => $text)
+  {
+    // We can cope with both associative and ordinary arrays
+    if (!$force_assoc && !is_assoc($options))
+    {
+      $key = $text;
+    }
+    $html .= "<option value=\"" . htmlspecialchars($key) . "\"";
+    $html .= (in_array($key, $value)) ? " selected=\"selected\"" : '';
+    $html .= ">".htmlspecialchars($text)."</option>\n";
+  }
+  
+  return $html;
+}
+
 
 // Generates a select box with an associated label
 //
@@ -1197,8 +1233,10 @@
 //        'id'            The id of the element.  Defaults to the same as the 
name.
 //        'label'         The text to be used for the field label.
 //        'label_title'   The text to be used for the title attribute for the 
field label
-//        'options'       An array of options for the select element.   Can be 
a simple
-//                        array or an associative array with value => text 
members for
+//        'options'       An array of options for the select element.   Can be 
a one-
+//                        or two-dimensional array.  If it's two-dimensional 
then the keys of
+//                        the outer level represent <optgroup> labels.  The 
inner level 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
@@ -1286,11 +1324,6 @@
     }
   }
   
-  if (!is_array($params['value']))
-  {
-    $params['value'] = array($params['value']);
-  }
-  
   if (isset($params['attributes']) && is_array($params['attributes']))
   {
     $params['attributes'] = implode(' ', $params['attributes']);
@@ -1312,16 +1345,26 @@
   $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']))
+    // Test whether $params['options'] options is a one-dimeensial or 
two-dimensional array.
+    // If two-dimensional then we need to use <optgroup>s.
+    if (is_array(reset($params['options'])))   // cannot use 
$params['options'][0] because it may be associative
     {
-      $value = $text;
+      foreach($params['options'] as $label => $options)
+      {
+        if (count($options) > 0)
+        {
+          $html .= "<optgroup label=\"" . htmlspecialchars($label) . "\">\n";
+          $html .= generate_options($options, $params['force_assoc'], 
$params['value']);
+          $html .= "</optgroup>\n";
+        }
+      }
     }
-    $html .= "<option value=\"" . htmlspecialchars($value) . "\"";
-    $html .= (in_array($value, $params['value'])) ? " selected=\"selected\"" : 
'';
-    $html .= ">".htmlspecialchars($text)."</option>\n";
+    else
+    {
+      $html .= generate_options($params['options'], $params['force_assoc'], 
$params['value']);
+    }
   }
 
   $html .= "</select>\n";
diff -r 80052eb62baa -r 017e435c99b5 web/import.php
--- a/web/import.php    Mon Jan 30 17:24:45 2017 +0000
+++ b/web/import.php    Tue Jan 31 15:52:10 2017 +0000
@@ -237,7 +237,7 @@
 // Add a VEVENT to MRBS.   Returns TRUE on success, FALSE on failure
 function process_event($vevent)
 {
-  global $import_default_type, $skip;
+  global $import_default_room, $import_default_type, $skip;
   global $morningstarts, $morningstarts_minutes, $resolution;
   global $booking_types;
   
@@ -250,6 +250,7 @@
   $booking['status'] = 0;
   $booking['rep_type'] = REP_NONE;
   $booking['type'] = $import_default_type;
+  $booking['room_id'] = $import_default_room;
   
   // Parse all the lines first because we'll need to get the start date
   // for calculating some of the other settings
@@ -394,7 +395,9 @@
   }
   
   // LOCATION is optional in RFC 5545 but is obviously mandatory in MRBS.
-  // We could maybe have a default room on the form and use that
+  // If there is no LOCATION property we use the default_room specified on
+  // the form, but if there is no default room (most likely because no rooms
+  // have been created) then this error message is created).
   if (!isset($booking['room_id']))
   {
     $problems[] = get_vocab("no_LOCATION");
@@ -618,6 +621,7 @@
 print_header($day, $month, $year, $area, $room);
 
 $import = get_form_var('import', 'string');
+$import_default_room = get_form_var('import_default_room', 'int');
 $area_room_order = get_form_var('area_room_order', 'string', 'area_room');
 $area_room_delimiter = get_form_var('area_room_delimiter', 'string', 
$default_area_room_delimiter);
 $area_room_create = get_form_var('area_room_create', 'string', '0');
@@ -742,6 +746,41 @@
 echo "<fieldset>\n";
 echo "<legend>" . get_vocab("area_room_settings") . "</legend>\n";
 
+// Default room
+$areas = get_area_names($all=true);
+if (count($areas) > 0)
+{
+  $options = array();
+  
+  foreach($areas as $area_id => $area_name)
+  {
+    $rooms = get_room_names($area_id, $all=true);
+    if (count($rooms) > 0)
+    {
+      $options[$area_name] = array();
+      foreach($rooms as $room_id => $room_name)
+      {
+        $options[$area_name][$room_id] = $room_name;
+      }
+    }
+  }
+  
+  if (count($options) > 0)
+  {
+    $params = array('name'        => 'import_default_room',
+                    'label'       => get_vocab("default_room") . ':',
+                    'label_title' => get_vocab("default_room_note"),
+                    'options'     => $options,
+                    'force_assoc' => true,
+                    'value'       => $default_room);
+    echo "<div>\n";
+    generate_select($params);
+    echo "</div>\n";
+  }
+}
+
+
+// Area-room order
 echo "<div>\n";
 echo "<label title=\"" . get_vocab("area_room_order_note") . "\">" . 
      get_vocab("area_room_order") . ":</label>\n";
diff -r 80052eb62baa -r 017e435c99b5 web/lang/lang.en
--- a/web/lang/lang.en  Mon Jan 30 17:24:45 2017 +0000
+++ b/web/lang/lang.en  Tue Jan 31 15:52:10 2017 +0000
@@ -474,6 +474,8 @@
 $vocab["no_file"]                     = "No file was uploaded";
 $vocab["could_not_process"]           = "Import failed: could not process 
file";
 $vocab["badly_formed_ics"]            = "Badly formed VCALENDAR file";
+$vocab["default_room"]                = "Default room";
+$vocab["default_room_note"]           = "The room to be used if no LOCATION 
property is specified";
 $vocab["area_room_order"]             = "Order";
 $vocab["area_room_order_note"]        = "The order of the area and room names 
in the LOCATION property";
 $vocab["area_room"]                   = "Area-Room";
diff -r 80052eb62baa -r 017e435c99b5 web/mrbs_sql.inc
--- a/web/mrbs_sql.inc  Mon Jan 30 17:24:45 2017 +0000
+++ b/web/mrbs_sql.inc  Tue Jan 31 15:52:10 2017 +0000
@@ -1548,6 +1548,36 @@
   return $row[0];
 }
 
+
+// Gets an array of room names in an area indexed by room id.
+// If $all=TRUE then all areas are returned, otherwise just the ones that
+// are not disabled
+function get_room_names($area_id, $all=FALSE)
+{
+  global $tbl_room;
+  
+  $rooms = array();
+  
+  $sql = "SELECT id, room_name
+            FROM $tbl_room
+           WHERE area_id = :area_id";
+  if (empty($all))
+  {
+    $sql .= " AND disabled=0";
+  }
+  $sql .= " ORDER BY sort_key";
+  
+  $res = db()->query($sql, array(':area_id' => $area_id));
+  
+  for ($i=0; $row = $res->row_keyed($i); $i++)
+  {
+    $rooms[$row['id']] = $row['room_name'];
+  }
+  
+  return $rooms;
+}
+
+
 // Get all the room details for $room_id.
 // Returns FALSE on error, NULL if $room_id does not exist.
 function get_room_details($room_id)

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to