Revision: 2163
          http://mrbs.svn.sourceforge.net/mrbs/?rev=2163&view=rev
Author:   cimorrison
Date:     2011-11-02 15:22:38 +0000 (Wed, 02 Nov 2011)
Log Message:
-----------
Fixed a problem whereby "foreign" characters in CSV reports opened by Microsoft 
Excel did not display properly.   This is actually an Excel problem, rather 
than an MRBS problem, but there is now a circumvention in MRBS.   CSV reports 
are now output by default in UTF-16 rather than UTF-8 which means that Excel 
2010 on Windows and Excel 2011 on Mac, and probably some earlier versions as 
well, now open CSV reports properly.   This requires the iconv module to be 
installed.   If this is not possible, then CSV reports can be made to open 
correctly on Excel 2010 for Windows (but not Excel 2011 for Mac) by using UTF-8 
and outputting a BOM.   Two new config variables are introduced:  $csv_charset 
(default 'utf-16') and $csv_bom (default FALSE).  (CSV reports can always be 
made to open correctly in Excel by renaming the file from a .csv to a .txt 
file.  Then when the file is opened an import dialogue appears which allows the 
user to choose the character set).

Modified Paths:
--------------
    mrbs/trunk/web/language.inc
    mrbs/trunk/web/report.php
    mrbs/trunk/web/systemdefaults.inc.php

Modified: mrbs/trunk/web/language.inc
===================================================================
--- mrbs/trunk/web/language.inc 2011-11-02 10:45:58 UTC (rev 2162)
+++ mrbs/trunk/web/language.inc 2011-11-02 15:22:38 UTC (rev 2163)
@@ -469,7 +469,7 @@
 
 // 2003/11/09 JF Larvoire: Help new admins understand what to do in case the 
iconv error occurs...
 if (!function_exists('iconv') && 
-    (($server_os == 'windows') || ($server_os == 'aix')) )
+    (($server_os == 'windows') || ($server_os == 'aix') || (get_charset() != 
get_csv_charset())) )
 {
   exit('
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd";>
@@ -700,6 +700,38 @@
   return 'utf-8';
 }
 
+
+function get_csv_charset()
+{
+  global $csv_charset;
+  if (empty($csv_charset))
+  {
+    return get_charset();
+  }
+  else
+  {
+    return $csv_charset;
+  }
+}
+
+
+function get_bom($charset)
+{
+  switch(strtolower($charset))
+  {
+    case 'utf-8':
+      return "\xEF\xBB\xBF";
+      break;
+    case 'utf-16':
+      return "\xFE\xFF";
+      break;
+    default:
+      return '';
+      break;
+  }
+}
+
+
 function get_server_os()
 {
   if (stristr(PHP_OS,"Darwin"))

Modified: mrbs/trunk/web/report.php
===================================================================
--- mrbs/trunk/web/report.php   2011-11-02 10:45:58 UTC (rev 2162)
+++ mrbs/trunk/web/report.php   2011-11-02 15:22:38 UTC (rev 2163)
@@ -38,6 +38,23 @@
 define('FORMAT_PERIODS', "%d");
 
 
+// Converts a string from the standard MRBS character set to the character set
+// to be used for CSV files
+function csv_conv($string)
+{
+  $in_charset = strtoupper(get_charset());
+  $out_charset = strtoupper(get_csv_charset());
+  if ($in_charset == $out_charset)
+  {
+    return $string;
+  }
+  else
+  {
+    return iconv($in_charset, $out_charset, $string);
+  }
+}
+
+
 // Escape a string for either HTML or CSV output
 function escape($string)
 {
@@ -119,6 +136,7 @@
     $line .= '"' . $csv_row_sep;
   
     // Output the row
+    $line = csv_conv($line);
     echo $line;
   }
   elseif ($output_as_html)
@@ -368,6 +386,7 @@
       $line = '"';
       $line .= implode("\"$csv_col_sep\"", $values);
       $line .= '"' . $csv_row_sep;
+      $line = csv_conv($line);
     }
     else
     {
@@ -1060,8 +1079,12 @@
 elseif ($output_as_csv)
 {
   $filename = ($summarize & REPORT) ? $report_filename : $summary_filename;
-  header("Content-Type: text/csv; charset=" . get_charset());
+  header("Content-Type: text/csv; charset=" . get_csv_charset());
   header("Content-Disposition: attachment; filename=\"$filename\"");
+  if ($csv_bom)
+  {
+    echo get_bom(get_csv_charset());
+  }
 }
 else // Assumed to be output_as_ical
 {

Modified: mrbs/trunk/web/systemdefaults.inc.php
===================================================================
--- mrbs/trunk/web/systemdefaults.inc.php       2011-11-02 10:45:58 UTC (rev 
2162)
+++ mrbs/trunk/web/systemdefaults.inc.php       2011-11-02 15:22:38 UTC (rev 
2163)
@@ -914,7 +914,20 @@
 $csv_row_sep = "\n";  // Separator between rows/records
 $csv_col_sep = ",";   // Separator between columns/fields
 
+// CSV charset
+// Set the character set to be used for CSV files.   If $csv_charset is not set
+// then CSV files are written using the MRBS default charset (utf-8).  However
+// Microsoft Excel (at least up to Excel 2010 on Windows and 2011 on Mac) is 
not
+// guaranteed to recognise utf-8, but does recognise utf-16, so the default 
setting
+// for $csv_charset is 'utf-16'.   This does however require that you have the 
iconv
+// module installed and enabled on your PHP system.   If this is not possible, 
setting
+// $csv_charset to 'utf-8' and $csv_bom to TRUE (ie requiring MRBS to output a 
Byte
+// Order Mark) will make Excel 2010 on Windows, and maybe earlier versions, 
work. 
+// But utf-8 with, or without, a BOM will not work on Excel 2011 for Mac.
+$csv_charset = 'utf-16';
+$csv_bom = FALSE;
 
+
 /*************
  * Entry Types
  *************/

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


------------------------------------------------------------------------------
RSA&#174; Conference 2012
Save $700 by Nov 18
Register now&#33;
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to