export all
[disclosr.git] / documents / exportAll.csv.php
blob:a/documents/exportAll.csv.php -> blob:b/documents/exportAll.csv.php
  <?php
   
  include_once("../include/common.inc.php");
   
   
  setlocale(LC_CTYPE, 'C');
   
  $db = $server->get_db('disclosr-foidocuments');
  $headers = Array();
  try {
  $rows = $db->get_view("app", "fieldNames?group=true", null, true)->rows;
   
  $dataValues = Array();
  foreach ($rows as $row) {
  $headers[] = $row->key;
  }
  } catch (SetteeRestClientException $e) {
  setteErrorHandler($e);
  }
   
  $fp = fopen('php://output', 'w');
  if ($fp && $db) {
  header('Content-Type: text/csv; charset=utf-8');
  header('Content-Disposition: attachment; filename="export.' . date("c") . '.csv"');
  header('Pragma: no-cache');
  header('Expires: 0');
  fputcsv($fp, $headers);
  try {
  $agencies = $db->get_view("app", "all", null, true)->rows;
  //print_r($rows);
  foreach ($agencies as $agency) {
  // print_r($agency);
   
  if (!isset($agency->value->status)) {
  $row = Array();
  $agencyArray = object_to_array($agency->value);
  foreach ($headers as $fieldName) {
  if (isset($agencyArray[$fieldName])) {
  if (is_array($agencyArray[$fieldName])) {
  $row[] = implode(";", $agencyArray[$fieldName]);
  } else {
  $row[] = str_replace(Array("\n", '"', "\t"),"",$agencyArray[$fieldName]);
  }
  } else {
  $row[] = "";
  }
  }
   
  fputcsv($fp, array_values($row));
   
   
  }
  }
  } catch (SetteeRestClientException $e) {
  setteErrorHandler($e);
  }
   
  die;
  }
  ?>