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 | <?php include_once("../include/common.inc.php"); $db = $server->get_db('disclosr-agencies'); $format = "csv"; //$format = "json"; if (isset($_REQUEST['format'])) $format = $_REQUEST['format']; setlocale(LC_CTYPE, 'C'); $headers = Array(); $fp = fopen('php://output', 'w'); if ($fp && $db) { if ($format == "csv") { header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename="export.score.' . date("c") . '.csv"'); } header('Pragma: no-cache'); header('Expires: 0'); try { $agencies = $db->get_view("score", "score", null, true)->rows; //print_r($agencies); $first = true; if ($format == "json") { echo '"data" : ['.PHP_EOL; } foreach ($agencies as $agency) { $agencyArray = object_to_array($agency->value); if ($first) { $headers = array_keys($agencyArray); if ($format == "csv") { fputcsv($fp, $headers); } else if ($format == "json") { echo '{ "labels" : ["' . implode('","', $headers) . '"],'.PHP_EOL; } } $row = Array(); foreach ($headers as $i => $fieldName) { if (isset($agencyArray[$fieldName])) { $row[] = $agencyArray[$fieldName]; } else { $row[] = ''; } } if ($format == "csv") { fputcsv($fp, array_values($row)); } else if ($format == "json") { if (!$first) echo ","; echo '{"data" : [' . implode(",", array_values($row)) . '], "label": "'.$agency->value->name.'", "lines" : { "show" : true }, "points" : { "show" : true }}'.PHP_EOL; } $first = false; } if ($format == "json") { echo '] }'.PHP_EOL; } } catch (SetteeRestClientException $e) { setteErrorHandler($e); } die; } ?> |