From 9fb011b14ef82caf9adf266d81a4984b17280822 Mon Sep 17 00:00:00 2001 From: Silvio Date: Mon, 23 Aug 2010 16:21:12 -0300 Subject: Refactoring CSV app using IsisConnector --- apps/csv.php | 122 +++++-------------------------- classes/IsisConnector.php | 2 + classes/IsisReader.php | 14 ++++ classes/helpers/CinisisDisplayHelper.php | 72 +++++++++++++++++- 4 files changed, 104 insertions(+), 106 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 @@ ', '', $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(); } } diff --git a/classes/IsisConnector.php b/classes/IsisConnector.php index 901e15b..2ab2ee8 100644 --- a/classes/IsisConnector.php +++ b/classes/IsisConnector.php @@ -116,6 +116,8 @@ class IsisConnector extends IsisMap { * Field data. */ public function getMainItems($field) { + $values = array(); + foreach (new IsisRowIterator($this, $field) as $row) { $values[$row] = $this->getMainItem($field, $row); } diff --git a/classes/IsisReader.php b/classes/IsisReader.php index bb818a5..9926d3b 100644 --- a/classes/IsisReader.php +++ b/classes/IsisReader.php @@ -52,6 +52,9 @@ class IsisReader { * * @param $value * Array with bracketed strings. + * + * @return + * Array with strings without brackets. */ public function removeBrackets($value) { $value = str_replace('<', '', $value); @@ -59,6 +62,17 @@ class IsisReader { return $value; } + /** + * Remove brackets from strings whithin an array. + * Callback version + * + * @param $value + * Array with bracketed strings. + */ + public function removeBracketsCallback(&$value = NULL) { + $value = $this->removeBrackets($value); + } + /** * Remove brackets from strings whithin an array. * diff --git a/classes/helpers/CinisisDisplayHelper.php b/classes/helpers/CinisisDisplayHelper.php index 0a4b7f7..2f8fa65 100644 --- a/classes/helpers/CinisisDisplayHelper.php +++ b/classes/helpers/CinisisDisplayHelper.php @@ -11,8 +11,10 @@ class CinisisDisplayHelper { * Page title; */ function __construct($title) { - $this->header($title); - $this->title($title); + if ($title != NULL) { + $this->header($title); + $this->title($title); + } } /** @@ -347,4 +349,70 @@ class CinisisDisplayHelper { protected static function cliDump($var) { print_r($var); } + + /** + * Set the response helper. + * + * @param $mime + * MIME type. + * + * @param $filename + * File name. + */ + protected static function webHttpHeader($mime, $filename) { + header("Content-type: $mime"); + header("Content-Disposition: attachment; filename=$filename"); + header("Pragma: no-cache"); + header("Expires: 0"); + } + + /** + * Display a value with CSV format. + * + * @param $value + * Value entry. + */ + protected static function webCsv($value = NULL) { + echo '"'. preg_replace('/"/', '""', $value) .'",'; + } + + /** + * Display CSV titles. + * + * @param $format + * ISIS database format. + */ + protected static function webCsvTitles($format) { + // Format fields. + foreach ($format['fields'] as $field) { + self::csv($field['name']); + if (isset($field['subfields']) && is_array($field['subfields'])) { + foreach ($field['subfields'] as $key => $value) { + self::csv($field['name'] .': '. $value); + } + } + } + } + + /** + * Display a new CSV row. + */ + protected static function webCsvRow() { + echo "\n"; + } + + /** + * Merge items in a CSV roll. + * + * @param $items + * Array with items to be merged. + */ + protected function webMergeCsvItems($items) { + if (!empty($items)) { + self::csv(implode(';', $items)); + } + else { + self::csv(); + } + } } -- cgit v1.2.3