aboutsummaryrefslogtreecommitdiff
path: root/tests/csv.php
blob: c08b18fc3437809e64bb6ffca7531654f980877e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
 * Cinisis - Isis db reading tool.
 *
 * @fixme: script is broken after BiblioIsis repetition support
 *         was enhanced.
 */

/**
 * 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 CinisisDb();

// Test connection.
if ($isis->db) {
  // Get format and number of rows.
  $rows   = $isis->db->rows();
  $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 <= $rows; $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']])) {
          echo csv();
        }
        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";
    }
  }
}