<?php |
<?php |
|
|
include_once("../include/common.inc.php"); |
include_once("../include/common.inc.php"); |
|
|
$format = "csv"; |
$format = "csv"; |
//$format = "json"; |
//$format = "json"; |
if (isset($_REQUEST['format'])) $format = $_REQUEST['format']; |
if (isset($_REQUEST['format'])) |
|
$format = $_REQUEST['format']; |
setlocale(LC_CTYPE, 'C'); |
setlocale(LC_CTYPE, 'C'); |
if ($format == "csv") { |
if ($format == "csv") { |
$headers = Array("name"); |
$headers = Array("name"); |
} else { |
} else { |
$headers = Array(); |
$headers = Array(); |
} |
} |
|
|
$db = $server->get_db('disclosr-agencies'); |
$db = $server->get_db('disclosr-agencies'); |
try { |
try { |
$rows = $db->get_view("app", "all", null, true)->rows; |
$rows = $db->get_view("app", "all", null, true)->rows; |
|
|
$dataValues = Array(); |
$dataValues = Array(); |
foreach ($rows as $row) { |
foreach ($rows as $row) { |
if (isset($row->value->statistics->employees)) { |
if (isset($row->value->statistics->employees)) { |
|
|
$headers = array_unique(array_merge($headers, array_keys(object_to_array($row->value->statistics->employees)))); |
$headers = array_unique(array_merge($headers, array_keys(object_to_array($row->value->statistics->employees)))); |
|
|
} |
} |
} |
} |
} catch (SetteeRestClientException $e) { |
} catch (SetteeRestClientException $e) { |
setteErrorHandler($e); |
setteErrorHandler($e); |
} |
} |
|
|
$fp = fopen('php://output', 'w'); |
$fp = fopen('php://output', 'w'); |
if ($fp && $db) { |
if ($fp && $db) { |
if ($format == "csv") { |
if ($format == "csv") { |
header('Content-Type: text/csv; charset=utf-8'); |
header('Content-Type: text/csv; charset=utf-8'); |
header('Content-Disposition: attachment; filename="export.employeestats.' . date("c") . '.csv"'); |
header('Content-Disposition: attachment; filename="export.employeestats.' . date("c") . '.csv"'); |
} |
} |
header('Pragma: no-cache'); |
header('Pragma: no-cache'); |
header('Expires: 0'); |
header('Expires: 0'); |
if ($format == "csv") { |
if ($format == "csv") { |
fputcsv($fp, $headers); |
fputcsv($fp, $headers); |
} else if ($format == "json") { |
} else if ($format == "json") { |
echo '{ |
echo '{ |
"labels" : ["' . implode('","', $headers) . '"],'.PHP_EOL; |
"labels" : ["' . implode('","', $headers) . '"],' . PHP_EOL; |
} |
} |
try { |
try { |
$agencies = $db->get_view("app", "all", null, true)->rows; |
$agencies = $db->get_view("app", "all", null, true)->rows; |
//print_r($agencies); |
//print_r($agencies); |
$first = true; |
$first = true; |
if ($format == "json") { |
if ($format == "json") { |
echo '"data" : ['.PHP_EOL; |
echo '"data" : [' . PHP_EOL; |
|
|
} |
} |
foreach ($agencies as $agency) { |
foreach ($agencies as $agency) { |
|
|
if (isset($agency->value->statistics->employees)) { |
if (isset($agency->value->statistics->employees)) { |
$row = Array(); |
$row = Array(); |
$agencyEmployeesArray = object_to_array($agency->value->statistics->employees); |
$agencyEmployeesArray = object_to_array($agency->value->statistics->employees); |
foreach ($headers as $i => $fieldName) { |
foreach ($headers as $i => $fieldName) { |
|
if ($format == "csv") { |
|
if (isset($agencyEmployeesArray[$fieldName])) { |
|
$row[] = $agencyEmployeesArray[$fieldName]["value"] ; |
|
} else if ($i == 0) { |
|
$row[] = $agency->value->name; |
|
} else { |
|
$row[] = 0; |
|
} |
|
} else if ($format == "json") { |
if (isset($agencyEmployeesArray[$fieldName])) { |
if (isset($agencyEmployeesArray[$fieldName])) { |
$row[] = '['.$i.','.$agencyEmployeesArray[$fieldName]["value"].']'; |
$row[] = '[' . $i . ',' . $agencyEmployeesArray[$fieldName]["value"] . ']'; |
} else { |
} else { |
$row[] = '['.$i.',0]'; |
$row[] = '[' . $i . ',0]'; |
} |
} |
|
} |
} |
} |
if ($format == "csv") { |
if ($f |