1: <?php
2: /**
3: * Horde_Data implementation for Outlook comma-separated data (CSV).
4: *
5: * Copyright 1999-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Jan Schneider <jan@horde.org>
11: * @author Chuck Hagenbuch <chuck@horde.org>
12: * @category Horde
13: * @package Data
14: */
15: class Horde_Data_Outlookcsv extends Horde_Data_Csv
16: {
17: /**
18: * Builds a CSV file from a given data structure and returns it as a
19: * string.
20: *
21: * @param array $data A two-dimensional array containing the data
22: * set.
23: * @param boolean $header If true, the rows of $data are associative
24: * arrays with field names as their keys.
25: *
26: * @return string The CSV data.
27: */
28: public function exportData($data, $header = false,
29: $export_mapping = array())
30: {
31: if (!is_array($data) || (count($data) == 0)) {
32: return '';
33: }
34:
35: $export = '';
36: $eol = "\r\n";
37: $head = array_keys(current($data));
38:
39: if ($header) {
40: foreach ($head as $key) {
41: if (!empty($key)) {
42: if (isset($export_mapping[$key])) {
43: $key = $export_mapping[$key];
44: }
45: $export .= '"' . $key . '"';
46: }
47: $export .= ',';
48: }
49: $export = substr($export, 0, -1) . $eol;
50: }
51:
52: foreach ($data as $row) {
53: foreach ($head as $key) {
54: $cell = $row[$key];
55: if (!empty($cell) || $cell === 0) {
56: $cell = preg_replace("/\"/", "\"\"", $cell);
57: $export .= '"' . $cell . '"';
58: }
59: $export .= ',';
60: }
61: $export = substr($export, 0, -1) . $eol;
62: }
63:
64: return $export;
65: }
66:
67: }
68: