[dev] [framework-patch] csv export: Outlook friendly
Francois Marier
francois at nit.ca
Mon Aug 2 15:44:57 PDT 2004
On Mon, Aug 02, 2004 at 12:26:31AM -0400, Chuck Hagenbuch wrote:
> This sounds good. However, what I'd like to see is:
>
> - don't store maps inside Horde_Data; pass them in from the application
> instead.
> - no need for two maps, you can just array_flip() it.
>
> Can you submit an updated patch?
Here's an updated patch.
The import changes should probably go in since they don't break
support for other types of CSV files.
The export code could be moved to a new "Outlook CSV" data type if
there are too many clients that can't deal with DOS EOL for instance.
If you'd prefer this then how to deal with the fact that the code is
almost exactly the same? Should Horde_Data_csv_outlook just inherit
from Horde_Data_csv and replace the exportData() method?
Francois
-------------- next part --------------
diff -rpuN -X ../ignorelist ../build/framework/Data/Data/csv.php framework/Data/Data/csv.php
--- ../build/framework/Data/Data/csv.php Fri Apr 16 13:20:03 2004
+++ framework/Data/Data/csv.php Mon Aug 2 18:19:08 2004
@@ -56,7 +56,7 @@ class Horde_Data_csv extends Horde_Data
* If $header was true the rows are associative arrays
* with the field/column names as the keys.
*/
- function importFile($filename, $header = false, $sep = '', $quote = '', $fields = null)
+ function importFile($filename, $header = false, $sep = '', $quote = '', $fields = null, $import_mapping = array())
{
@include_once('File/CSV.php');
@@ -93,7 +93,12 @@ class Horde_Data_csv extends Horde_Data
} else {
$newline = array();
for ($i = 0; $i < count($head); $i++) {
- $newline[$head[$i]] = empty($line[$i]) ? '' : $line[$i];
+ if (array_key_exists($head[$i], $import_mapping)) {
+ $head[$i] = $import_mapping[$head[$i]];
+ }
+ $cell = $line[$i];
+ $cell = preg_replace("/\"\"/", "\"", $cell);
+ $newline[$head[$i]] = empty($cell) ? '' : $cell;
}
$data[] = $newline;
}
@@ -122,7 +127,12 @@ class Horde_Data_csv extends Horde_Data
} else {
$newline = array();
for ($i = 0; $i < count($head); $i++) {
- $newline[$head[$i]] = empty($line[$i]) ? '' : $line[$i];
+ if (array_key_exists($head[$i], $import_mapping)) {
+ $head[$i] = $import_mapping[$head[$i]];
+ }
+ $cell = $line[$i];
+ $cell = preg_replace("/\"/", "\"\"", $cell);
+ $newline[$head[$i]] = empty($cell) ? '' : $cell;
}
$data[] = $newline;
}
@@ -146,32 +156,37 @@ class Horde_Data_csv extends Horde_Data
*
* @return string The CSV data.
*/
- function exportData($data, $header = false)
+ function exportData($data, $header = false, $export_mapping = array())
{
if (!is_array($data) || count($data) == 0) {
return '';
}
$export = '';
+ $eol = "\r\n";
if ($header) {
$head = current($data);
foreach (array_keys($head) as $key) {
if (!empty($key)) {
+ if (array_key_exists($key, $export_mapping)) {
+ $key = $export_mapping[$key];
+ }
$export .= '"' . $key . '"';
}
$export .= ',';
}
- $export = substr($export, 0, -1) . "\n";
+ $export = substr($export, 0, -1) . $eol;
}
foreach ($data as $row) {
foreach ($row as $cell) {
if (!empty($cell) || $cell === 0) {
+ $cell = preg_replace("/\"/", "\"\"", $cell);
$export .= '"' . $cell . '"';
}
$export .= ',';
}
- $export = substr($export, 0, -1) . "\n";
+ $export = substr($export, 0, -1) . $eol;
}
return $export;
@@ -189,9 +204,9 @@ class Horde_Data_csv extends Horde_Data
* @param boolean $header If true, the rows of $data are associative arrays
* with field names as their keys.
*/
- function exportFile($filename, $data, $header = false)
+ function exportFile($filename, $data, $header = false, $export_mapping = array())
{
- $export = $this->exportData($data, $header);
+ $export = $this->exportData($data, $header, $export_mapping);
$GLOBALS['browser']->downloadHeaders($filename, 'application/csv', false, strlen($export));
echo $export;
}
@@ -250,11 +265,16 @@ class Horde_Data_csv extends Horde_Data
case IMPORT_CSV:
$_SESSION['import_data']['header'] = Util::getFormData('header');
+ $import_mapping = array();
+ if (array_key_exists('import_mapping', $param)) {
+ $import_mapping = $param['import_mapping'];
+ }
$import_data = $this->importFile($_SESSION['import_data']['file_name'],
$_SESSION['import_data']['header'],
Util::getFormData('sep'),
Util::getFormData('quote'),
- Util::getFormData('fields'));
+ Util::getFormData('fields'),
+ $import_mapping);
$_SESSION['import_data']['data'] = $import_data;
unset($_SESSION['import_data']['map']);
return IMPORT_MAPPED;
-------------- next part --------------
diff -rpuN -X ../ignorelist ../build/turba/data.php turba/data.php
--- ../build/turba/data.php Mon Aug 2 18:25:18 2004
+++ turba/data.php Mon Aug 2 17:59:24 2004
@@ -48,8 +48,29 @@ $next_step = IMPORT_FILE;
$app_fields = array();
$time_fields = array();
$error = false;
+$outlook_mapping = array(
+ 'firstname' => 'first_name',
+ 'middlename' => 'middle_name',
+ 'lastname' => 'last_name',
+ 'e-mail' => 'email',
+ 'homeaddress' => 'homeAddress',
+ 'businessaddress' => 'workAddress',
+ 'homephone' => 'homePhone',
+ 'businessphone' => 'workPhone',
+ 'mobilephone' => 'cellPhone',
+ 'businessfax' => 'fax',
+ 'jobtitle' => 'title',
+ 'company' => 'company',
+ 'notes' => 'notes',
+ 'name' => 'name',
+ 'internetfreebusy' => 'freebusyUrl',
+ 'nickname' => 'alias',
+ 'pgpPublicKey' => 'pgpPublicKey',
+ 'smimePublicKey' => 'smimePublicKey',
+ );
$param = array('time_fields' => $time_fields,
- 'file_types' => $file_types);
+ 'file_types' => $file_types,
+ 'import_mapping' => $outlook_mapping);
$import_format = Util::getFormData('import_format', '');
$driver = $import_format;
if ($driver == 'mulberry' || $driver == 'pine') {
@@ -104,7 +125,7 @@ case 'export':
switch ($exportID) {
case EXPORT_CSV:
$csv = &Horde_Data::singleton('csv');
- $csv->exportFile(_("contacts.csv"), $data, true);
+ $csv->exportFile(_("contacts.csv"), $data, true, array_flip($outlook_mapping));
exit;
case EXPORT_TSV:
$tsv = &Horde_Data::singleton('tsv');
More information about the dev
mailing list