From 70539fe94d67cb4bdde7edce8a26873343d3c240 Mon Sep 17 00:00:00 2001 From: Silvio Date: Wed, 18 Aug 2010 12:00:21 -0300 Subject: Moving tests to apps folder --- apps/csv.php | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 apps/csv.php (limited to 'apps/csv.php') diff --git a/apps/csv.php b/apps/csv.php new file mode 100644 index 0000000..e50249b --- /dev/null +++ b/apps/csv.php @@ -0,0 +1,124 @@ +', '', $field); +} + +/** + * Merge fields in a single cel. + * + * @param $data + * Array with field data. + * + * @param $field + * Field name. + * + * @return + * Cel with merged fields. + */ +function merge_fields($data, $field) { + $cel = ''; + $sep = (count($data) > 1) ? '; ': ''; + foreach ($data as $subkey => $subvalue) { + $cel = $cel . $data[$subkey][$field] . $sep; + } + + return $cel; +} + +// Import Cinisis Library. +require_once '../index.php'; + +// Get a db instance. +$isis = new CinisisDb(); + +// Test connection. +if ($isis->db) { + // Get format and number of entries. + $entries = $isis->db->entries(); + $format = $isis->db->format; + + // Prepare output. + header("Content-type: application/text/x-csv"); + header("Content-Disposition: attachment; filename=export.csv"); + header("Pragma: no-cache"); + header("Expires: 0"); + + // Format fields. + foreach ($format['fields'] as $field) { + echo csv($field['name']); + if (is_array($field['subfields'])) { + foreach ($field['subfields'] as $key => $value) { + echo csv($field['name'] .': '. $value); + } + } + } + + // New roll. + echo "\n"; + + // Format output. + for ($n = 1; $n <= $entries; $n++) { + $result = $isis->db->read($n); + + if ($result) { + // Filter results. + array_walk_recursive($result, 'filter'); + + foreach ($format['fields'] as $field) { + if (is_array($result[$field['name']])) { + // Print main field if needed. + if (is_array($result[$field['name']][0])) { + echo csv(); + } + else { + echo csv($result[$field['name']][0]); + } + } + else { + echo csv($result[$field['name']]); + } + if (is_array($field['subfields'])) { + foreach ($field['subfields'] as $key => $value) { + // Deals with subfield repetition. + if (isset($result[$field['name']][0][$value])) { + echo csv(merge_fields($result[$field['name']], $value)); + } + else { + echo csv($result[$field['name']][$value]); + } + } + } + } + + // New roll. + echo "\n"; + } + } +} -- cgit v1.2.3