aboutsummaryrefslogtreecommitdiff
path: root/apps/csv.php
diff options
context:
space:
mode:
authorSilvio <silvio@devlet.com.br>2010-08-23 16:21:12 -0300
committerSilvio <silvio@devlet.com.br>2010-08-23 16:21:12 -0300
commit9fb011b14ef82caf9adf266d81a4984b17280822 (patch)
tree4cedf7656f254a731a681c73e1eb41c7f653331a /apps/csv.php
parent3fa521d2a74df3e243f36bbf6995eea8082573cb (diff)
downloadcinisis-9fb011b14ef82caf9adf266d81a4984b17280822.tar.gz
cinisis-9fb011b14ef82caf9adf266d81a4984b17280822.tar.bz2
Refactoring CSV app using IsisConnector
Diffstat (limited to 'apps/csv.php')
-rw-r--r--apps/csv.php122
1 files changed, 18 insertions, 104 deletions
diff --git a/apps/csv.php b/apps/csv.php
index ae99ad0..e1553e6 100644
--- a/apps/csv.php
+++ b/apps/csv.php
@@ -1,124 +1,38 @@
<?php
/**
* Cinisis - Isis db reading tool.
- *
- * @TODO: check what happens if theres a field and subfields with repetition.
*/
-/**
- * Format a value for CSV output.
- *
- * @param $field
- * Field entry.
- *
- * @return
- * Formatted CSV field.
- */
-function csv($field = NULL) {
- return '"'. preg_replace('/"/', '""', $field) .'",';
-}
-
-/**
- * Apply filters into the result.
- *
- * @param $field
- * Field entry.
- */
-function filter(&$field = NULL) {
- // Remove brackets from field content.
- $field = str_replace('<', '', $field);
- $field = str_replace('>', '', $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 Cinisis();
+$isis = new IsisConnector();
+$display = new CinisisDisplayHelper(NULL);
// 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";
+if ($isis) {
+ // Setup format, response header and CSV title.
+ $format = $isis->format;
+ $display->httpHeader('application/text/x-csv', 'export.csv');
+ $display->csvTitles($format);
+ $display->csvRow();
// Format output.
- for ($n = 1; $n <= $entries; $n++) {
- $result = $isis->db->read($n);
+ foreach(new IsisEntryIterator($isis) as $entry => $result) {
+ // Filter results.
+ array_walk_recursive($result, array($isis, 'removeBracketsCallback'));
- if ($result) {
- // Filter results.
- array_walk_recursive($result, 'filter');
+ foreach ($format['fields'] as $field) {
+ $display->mergeCsvItems($isis->getMainItems($field));
- 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]);
- }
- }
+ if (isset($field['subfields']) && is_array($field['subfields'])) {
+ foreach ($field['subfields'] as $subfield) {
+ $display->mergeCsvItems($isis->getSubfields($field, $subfield));
}
}
-
- // New roll.
- echo "\n";
}
+
+ $display->csvRow();
}
}