139 lines
3.0 KiB
PHP
139 lines
3.0 KiB
PHP
|
|
<?php
|
|
class Sp_csv extends MY_Controller
|
|
{
|
|
var $intervalHour;
|
|
var $tmp_body;
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->intervalHour = 12;
|
|
}
|
|
function param()
|
|
{
|
|
$body = file_get_contents("php://input");
|
|
$this->tmp_body = $body;
|
|
return json_decode($body, true);
|
|
}
|
|
function go()
|
|
{
|
|
$gets = $_GET;
|
|
$arr_param = [];
|
|
$idx = 1;
|
|
$sp = $_GET["sp"];
|
|
if ($sp == "") {
|
|
echo "Invalid Sp: $sp";
|
|
exit;
|
|
}
|
|
$format = $_GET["__format"];
|
|
if ($format == "") {
|
|
$format = "html";
|
|
}
|
|
$arr_place_holder = "";
|
|
foreach ($_GET as $k => $v) {
|
|
$p = "p" . $idx;
|
|
if ($k == $p) {
|
|
$arr_param[] = $_GET[$p];
|
|
$idx++;
|
|
if ($arr_place_holder != "") {
|
|
$arr_place_holder .= ",";
|
|
}
|
|
$arr_place_holder .= "?";
|
|
}
|
|
}
|
|
if ($arr_place_holder == "") {
|
|
$sql = "call $sp()";
|
|
} else {
|
|
$sql = "call $sp($arr_place_holder)";
|
|
}
|
|
$qry = $this->db->query($sql, $arr_param);
|
|
if (!$qry) {
|
|
echo "Err: " . print_r($this->db->error(), true);
|
|
exit;
|
|
}
|
|
$rows = $qry->result_array();
|
|
if ($format == "html") {
|
|
$this->print_table_style();
|
|
$this->print_table($rows, array_keys($rows[0]));
|
|
} else {
|
|
$fname = "rpt-$sp.csv";
|
|
header('Content-Type: text/csv');
|
|
header('Content-Disposition: attachment; filename="' . $fname . '"');
|
|
$this->print_csv($rows, array_keys($rows[0]));
|
|
}
|
|
}
|
|
|
|
public function print_table_style()
|
|
{
|
|
echo "
|
|
<style>
|
|
th, td {
|
|
padding: 15px;
|
|
text-align: left;
|
|
white-space:nowrap;
|
|
}
|
|
tr:nth-child(even) {background-color: #f2f2f2;}
|
|
table {
|
|
border: solid 1px ;
|
|
min-width:600px;
|
|
}
|
|
</style>
|
|
";
|
|
}
|
|
|
|
public function print_table($rows, $keys, $title = false)
|
|
{
|
|
echo "<table>";
|
|
if ($title) {
|
|
$col_span = count($keys);
|
|
echo "<tr>";
|
|
echo "<th colspan=$col_span>$title</th>";
|
|
echo "</tr>";
|
|
}
|
|
echo "<tr>";
|
|
foreach ($keys as $k) {
|
|
$k = str_replace("|R", "", $k);
|
|
echo "<td>$k</td>";
|
|
}
|
|
echo "</tr>\n";
|
|
foreach ($rows as $r) {
|
|
echo "<tr>";
|
|
foreach ($keys as $k) {
|
|
if (in_array($k, ["Debit", "Debit", "Kredit"])) {
|
|
echo "<td style='text-align:right' >" .
|
|
$r[$k] .
|
|
"</td>";
|
|
} else {
|
|
echo "<td>" . $r[$k] . "</td>";
|
|
}
|
|
}
|
|
echo "</tr>";
|
|
}
|
|
echo "</table>";
|
|
}
|
|
function print_csv($rows, $keys, $title = "")
|
|
{
|
|
$header = "";
|
|
if ($title != "") {
|
|
echo $title . "\n";
|
|
}
|
|
foreach ($keys as $idx => $k) {
|
|
if ($idx > 0) {
|
|
echo ",";
|
|
}
|
|
echo "\"$k\"";
|
|
}
|
|
echo "\n";
|
|
foreach ($rows as $r) {
|
|
foreach ($keys as $idx => $k) {
|
|
if ($idx > 0) {
|
|
echo ",";
|
|
}
|
|
$val = $r[$k];
|
|
echo "\"$val\"";
|
|
}
|
|
echo "\n";
|
|
}
|
|
}
|
|
}
|