Revision: 1375
          http://mrbs.svn.sourceforge.net/mrbs/?rev=1375&view=rev
Author:   jberanek
Date:     2010-07-23 15:17:49 +0000 (Fri, 23 Jul 2010)

Log Message:
-----------
* Modified mrbsGetBookingInfo() to support custom entry fields.

* Modified view_entry.php to show custom entry fields. Supports text
 fields (any textual column) and boolean fields (any integer column).
 Uses "entry.COLUMN_NAME" from vocab file for field description.

Modified Paths:
--------------
    mrbs/branches/custom_entry_fields/web/mrbs_sql.inc
    mrbs/branches/custom_entry_fields/web/view_entry.php

Modified: mrbs/branches/custom_entry_fields/web/mrbs_sql.inc
===================================================================
--- mrbs/branches/custom_entry_fields/web/mrbs_sql.inc  2010-07-23 15:10:51 UTC 
(rev 1374)
+++ mrbs/branches/custom_entry_fields/web/mrbs_sql.inc  2010-07-23 15:17:49 UTC 
(rev 1375)
@@ -705,7 +705,7 @@
 // mrbsGetBookingInfo($id, $series)
 //
 // Gets all the details for a booking with $id, which is in the
-// repeat table id $series is set, otherwise in the entry table.
+// repeat table if $series is set, otherwise in the entry table.
 
 // Returns the results in an array with keys the same as the table
 // field names.  In the event of an error stops with a fatal error,
@@ -714,7 +714,7 @@
 {
   global $tbl_entry, $tbl_repeat, $tbl_room, $tbl_area;
   global $provisional_enabled;
-  
+
   // Check that we've got an id
   if (!isset($id))
   {
@@ -728,42 +728,88 @@
     }
   }
 
-  if ($series)
+  $entry_fields = sql_field_info($tbl_entry);
+  $repeat_fields = sql_field_info($tbl_repeat);
+
+  $rep_fields = array();
+  foreach ($repeat_fields as $field)
   {
-    $sql = "
-     SELECT T.name, T.description, T.create_by,
-            M.room_name, T.room_id, A.area_name, M.area_id,
-            T.type, T.reminded, T.private, T.room_id,
-            " . sql_syntax_timestamp_to_unix("T.timestamp") . " AS 
last_updated,
-            (T.end_time - T.start_time) AS duration,
-            T.start_time, T.end_time,
-            T.rep_type, T.end_date, T.rep_opt, T.rep_num_weeks,
-            T.info_time AS repeat_info_time, T.info_user AS repeat_info_user, 
T.info_text AS repeat_info_text
+    $rep_fields[$field['name']] = 1;
+  }
 
-     FROM  $tbl_repeat T, $tbl_room M, $tbl_area A
-     WHERE T.room_id = M.id
-        AND M.area_id = A.id
-        AND T.id=$id
-     ";
+  $t_or_e = $series ? "T" : "E";
+
+  $terms = array("M.room_name",
+                 "M.area_id",
+                 "A.area_name",
+                 "($t_or_e.end_time - $t_or_e.start_time) AS duration");
+  foreach ($entry_fields as $field)
+  {
+    switch ($field['name'])
+    {
+      case 'id':
+      case 'entry_type':
+        // Nothing to do
+        break;
+
+      case 'status':
+      case 'repeat_id':
+        array_push($terms, "E.".$field['name']);
+        break;
+
+      case 'timestamp':
+        array_push($terms,
+                   sql_syntax_timestamp_to_unix(($series ? "T" : "E" ).
+                                                ".timestamp").
+                   "AS last_updated");
+        break;
+
+      case 'info_time':
+      case 'info_user':
+      case 'info_text':
+        if ($series)
+        {
+          array_push($terms, "T.".$field['name']." AS repeat_".$field['name']);
+        }
+        else
+        {
+          array_push($terms, "E.".$field['name']." AS entry_".$field['name']);
+        }
+        break;
+
+      default:
+        if (!array_key_exists($field['name'], $rep_fields))
+        {
+          fatal_error(0, "Custom fields problem, '".$field['name']."' exists 
in entry table but not in repeat table");
+        }
+        
+        array_push($terms, ($series ? "T" : "E" ).".".$field['name']);
+        break;
+    }
   }
+  if ($series)
+  {
+    array_push($terms,
+               "T.rep_type",
+               "T.end_date",
+               "T.rep_opt",
+               "T.rep_num_weeks");
+    
+    $sql = "SELECT ".implode(",\n       ",$terms)."
+FROM $tbl_repeat T, $tbl_room M, $tbl_area A
+WHERE T.room_id = M.id
+      AND M.area_id = A.id
+      AND T.id=$id
+";
+  }
   else
   {
-    $sql = "
-     SELECT E.name, E.description, E.create_by,
-            M.room_name, E.room_id, A.area_name, M.area_id,
-            E.status,
-            E.type, E.reminded, E.private, E.room_id,
-            " . sql_syntax_timestamp_to_unix("E.timestamp") . " AS 
last_updated,
-            (E.end_time - E.start_time) AS duration,
-            E.start_time, E.end_time,
-            E.repeat_id,
-            E.info_time AS entry_info_time, E.info_user AS entry_info_user, 
E.info_text AS entry_info_text
-
-     FROM  $tbl_entry E, $tbl_room M, $tbl_area A
-     WHERE E.room_id = M.id
+    $sql = "SELECT ".implode(",\n       ",$terms)."
+FROM  $tbl_entry E, $tbl_room M, $tbl_area A
+WHERE E.room_id = M.id
         AND M.area_id = A.id
         AND E.id=$id
-     ";
+";
   }
 
   $res = sql_query($sql);

Modified: mrbs/branches/custom_entry_fields/web/view_entry.php
===================================================================
--- mrbs/branches/custom_entry_fields/web/view_entry.php        2010-07-23 
15:10:51 UTC (rev 1374)
+++ mrbs/branches/custom_entry_fields/web/view_entry.php        2010-07-23 
15:17:49 UTC (rev 1375)
@@ -159,23 +159,58 @@
 
 $row = mrbsGetBookingInfo($id, $series);
 
-$name          = htmlspecialchars($row['name']);
-$description   = htmlspecialchars($row['description']);
-$create_by     = htmlspecialchars($row['create_by']);
-$room_name     = htmlspecialchars($row['room_name']);
-$area_name     = htmlspecialchars($row['area_name']);
-$type          = $row['type'];
-$status        = $row['status'];
-$private       = $row['private'];
-$room_id       = $row['room_id'];
-$updated       = time_date_string($row['last_updated']);
+$custom_fields = array();
+foreach ($row as $column => $value)
+{
+  switch ($column)
+  {
+    // Don't bother with these columns
+    case 'area_id':
+    case 'duration':
+    case 'repeat_id':
+    case 'reminded':
+    case 'rep_type':
+    case 'rep_enddate':
+    case 'rep_opt':
+    case 'rep_num_weeks':
+    case 'start_time':
+    case 'end_time':
+      break;
+
+    case 'name':
+    case 'description':
+    case 'create_by':
+    case 'room_name':
+    case 'area_name':
+      $$column = htmlspecialchars($value);
+      break;
+
+    case 'type':
+    case 'status':
+    case 'private':
+    case 'room_id':
+    case 'entry_info_time':
+    case 'entry_info_user': // HTML escaping done later
+    case 'entry_info_text': // HTML escaping done later
+    case 'repeat_info_time':
+    case 'repeat_info_user': // HTML escaping done later
+    case 'repeat_info_text': // HTML escaping done later
+      $$column = $row[$column];
+      break;
+
+    case 'last_updated':
+      $updated = time_date_string($row['last_updated']);
+      break;
+
+    default:
+      $custom_fields[$column] = $row[$column];
+      break;
+  }
+}
+
+// Very special cases
 $last_reminded = (empty($row['reminded'])) ? $row['last_updated'] : 
$row['reminded'];
-$entry_info_time  = $row['entry_info_time'];
-$entry_info_user  = $row['entry_info_user'];  // HTML escaping is done later
-$entry_info_text  = $row['entry_info_text'];  // HTML escaping is done later
-$repeat_info_time = $row['repeat_info_time'];
-$repeat_info_user = $row['repeat_info_user'];  // HTML escaping is done later
-$repeat_info_text = $row['repeat_info_text'];  // HTML escaping is done later
+
 // need to make DST correct in opposite direction to entry creation
 // so that user see what he expects to see
 $duration      = $row['duration'] - cross_dst($row['start_time'],
@@ -441,7 +476,37 @@
     <td><?php echo get_vocab("lastupdate") ?>:</td>
     <td><?php    echo $updated ?></td>
   </tr>
+<?php
+  if (count($custom_fields))
+  {
+    $fields = sql_field_info($tbl_entry);
+    $field_natures = array();
+    foreach ($fields as $field)
+    {
+      $field_natures[$field['name']] = $field['nature'];
+    }
+    foreach ($custom_fields as $key => $value)
+    {
+      $desc = get_vocab("entry.$key");
+      if (!isset($desc))
+      {
+        $desc = htmlspecialchars($key);
+      }
+      $shown_value = htmlspecialchars($value);
+      if ($field_natures[$key] == 'integer')
+      {
+        $shown_value = ($value == 0) ? get_vocab("YES") : get_vocab("NO");
+      }
+      print "
   <tr>
+    <td>$desc:</td>
+    <td>$shown_value</td>
+  </tr>
+";
+    }
+  }
+?>
+  <tr>
     <td><?php echo get_vocab("rep_type") ?>:</td>
     <td><?php    echo get_vocab($repeat_key) ?></td>
   </tr>


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to