first commit

This commit is contained in:
2026-07-25 16:54:11 +07:00
commit 36395c2b55
84 changed files with 40698 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
<?php
/*******************************************************************
Module : /Data/CSV.php
Desc. : Class pembuatan file CSV
Created By : Siau Pheng ([email protected]).
Created Date : April 2rd, 2011.
Last Modified : July 25th, 2026.
(c) 2011 - 2026, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace siaupheng\webapp\Data;
define( DELI_CSV, ";" );
define( CRLF_CSV, "\r\n" );
final class CSV {
private $__avalue = [];
private $__atabel = [];
public function add_field($tnama="", $ffunc="") {
$this->__atabel[] = [$tnama, $ffunc];
}
public function add_value() {
$tmp_arr = func_get_args();
if (is_array($tmp_arr[0])) $tmp_arr = $tmp_arr[0];
$tmp_out = "";
for ($i=0; $i<count($this->__atabel); $i++) {
$tmp_val = $tmp_arr[$i];
if (is_array($this->__atabel[$i][1]) && trim($tmp_val)<>"") {
$tmp_val = $this->__atabel[$i][1][$tmp_val];
} else if (function_exists($this->__atabel[$i][1]) && trim($tmp_val)<>"") {
$tmp_val = call_user_func($this->__atabel[$i][1], $tmp_val);
}
$tmp_out .= DELI_CSV . $tmp_val;
}
$tmp_out = substr($tmp_out, 1);
$this->__avalue[] = $tmp_out;
}
public function download($nama_file="export.csv") {
$out = ""; $tmp_out = "";
// add header
for ($i=0; $i<count($this->__atabel); $i++) {
$tmp_out .= DELI_CSV . $this->__atabel[$i][0];
}
$out .= substr($tmp_out, 1) . CRLF_CSV;
// add content
for ($i=0; $i<count($this->__avalue); $i++) {
$out .= $this->__avalue[$i] . CRLF_CSV;
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$nama_file."\"");
echo $out;
}
}
?>
+193
View File
@@ -0,0 +1,193 @@
<?php
/*******************************************************************
Module : /Data/Cache.php
Desc. : Class cache aplikasi
Created By : Siau Pheng ([email protected]).
Created Date : March 16th, 2008.
Last Modified : July 25th, 2026.
(c) 2008 - 2026, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace siaupheng\webapp\Data;
use MatthiasMullie\Minify;
final class Cache {
private $__cachedays = 0;
private $__cachedir = "";
private $__cssdir = "";
private $__localedir = "";
private $__jsdir = "";
private $__jsapp = "";
private $__realpath = "";
private $__ini = [];
protected $__db = null;
public function __construct($db = null){
if (is_resource($db)) { $this->__db = $db; }
else $this->__db = new \siaupheng\webapp\DB\MySQL();
$rootdir = $_SESSION['__WEB_APP']['REAL_PATH'];
$this->__cachedir = $rootdir . 'chunks/';
$this->__cssdir = __DIR__ . '/../../assets/css/';
$this->__localedir = $rootdir . 'assets/locale/';
$this->__jsdir = __DIR__ . '/../../assets/js/';
$this->__jsapp = $rootdir . 'apps/js/';
$this->__realpath = $rootdir;
if (!is_dir($this->__cachedir)) mkdir($this->__cachedir, 0755, true);
if (!file_exists($this->__cachedir . "cache.ini")) {
$this->__ini = [
'css' => ['login_stamp'=>"0",'login_file'=>"",'index_stamp'=>"0",'index_file'=>"",'display_stamp'=>"0",'display_file'=>""],
'js' => ['login_stamp'=>"0",'login_file'=>"",'index_stamp'=>"0",'index_file'=>"",'display_stamp'=>"0",'display_file'=>""]
];
$this->save_ini();
}
$this->__ini = parse_ini_file($this->__cachedir . "cache.ini", true);
}
private function gen_file($type="css", $mode="index") {
$afile = [];
if ($type == "css") {
$_afile = array_unique($GLOBALS['A_CSS']);
foreach ($_afile as $__key => $val) {
$afile[] = $this->__cssdir .$val;
}
if (file_exists($this->__realpath . 'assets/css/')) {
if ($handle = opendir($this->__realpath . 'assets/css/')) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..' || is_dir($this->__realpath . 'assets/css/'.$file)) continue;
$afile[] = $this->__realpath . 'assets/css/' . $file;
}
closedir($handle);
}
}
} else if ($type == "js") {
$_afile = array_unique($GLOBALS['A_JS']);
foreach ($_afile as $__key => $val) {
$afile[] = $this->__jsdir .$val;
}
if (file_exists($this->__realpath . 'assets/js/')) {
if ($handle = opendir($this->__realpath . 'assets/js/')) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..' || is_dir($this->__realpath . 'assets/js/'.$file)) continue;
$afile[] = $this->__realpath . 'assets/js/' . $file;
}
closedir($handle);
}
}
// add locale file
$ljs = (isset($_SESSION['__WEB_USER']['LOCALE'])?$_SESSION['__WEB_USER']['LOCALE']:$_SESSION['__WEB_APP']['DEF_LOCALE']).".js";
if (is_dir($this->__localedir)) {
if ($handle = opendir($this->__localedir)) {
while (false !== ($file = readdir($handle))) {
$file = $this->__localedir . $file;
if (substr($file, -5) == $ljs) $afile[] = $this->__localedir .basename(str_replace("\\", "/", $file));
}
closedir($handle);
}
}
$afile[] = $this->__jsapp . $mode . ".js";
} else if ($type == "app") {
$afile[] = $this->__jsapp ."sys/profile.js";
$afile[] = $this->__jsapp ."sys/chpasswd.js";
$amenu = explode(",", $_SESSION['__WEB_USER']['Menu_Grup'] .",". $_SESSION['__WEB_USER']['Menu']);
$tmenu = implode("','", array_unique($amenu));
$this->__db->Query("SELECT * FROM app_menu WHERE Link<>'' AND Kode IN ('".$tmenu."') ORDER BY Level, Urut");
while ($this->__db->Next()) {
$afile[] = $this->__jsapp . str_replace("php", "js", $this->__db->Row("Link"));
}
}
return $afile;
}
private function check_file($afile) {
$lastmod = 0;
foreach ($afile as $nfile) {
if (file_exists($nfile)) {
$lastmod += filemtime($nfile);
}
}
if ($this->__cachedays > 0) {
if ($handle = opendir($this->__cachedir)) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..' || is_dir($this->__cachedir.$file)) continue;
if ((time()-filemtime($this->__cachedir.$file)) > (86400*$this->__cachedays)) {
unlink($this->__cachedir . $file);
}
}
closedir($handle);
}
}
return $lastmod;
}
private function create_cache($type="css", $mode="index", $afile=[], $lastmod=0) {
$contents = ''; $smini = null;
$bpublic = ($_SERVER['SERVER_ADDR'] == "127.0.0.1") ? false : true;
if ($bpublic == true) {
if ($type=="css") {
$smini = new Minify\CSS;
} else {
$smini = new Minify\JS;
}
}
foreach ($afile as $nfile) {
if (file_exists($nfile)) {
if ($bpublic == true) {
$smini->add($nfile);
} else {
$contents .= "\n". file_get_contents($nfile);
}
}
}
if ($bpublic) $contents = $smini->minify();
$cachefile = md5($mode . $lastmod) . ($type=="app"?".app":"") . ($type=="css" ? ".css" : ".js");
file_put_contents($this->__cachedir . $cachefile, $contents);
if ($type == "app") return;
$this->__ini[$type][$mode.'_stamp'] = $lastmod;
$this->__ini[$type][$mode.'_file'] = $cachefile;
$this->save_ini();
}
private function save_ini(){
$content = "";
foreach ($this->__ini as $key => $elem) {
$content .= ($content==""?"":"\n"). "[".$key."]\n";
foreach ($elem as $key2 => $elem2) {
if ($elem2=="") $content .= $key2." = \n";
else $content .= $key2." = \"".$elem2."\"\n";
}
}
file_put_contents($this->__cachedir . "cache.ini", $content);
}
public function Main($type="css", $mode="") {
if ($mode == "") $mode = "login";
if (($_SESSION['__WEB_USER']['UserID'] ?? 0) > 0) $mode = "index";
$afile = $this->gen_file($type, $mode);
$lastmod = $this->check_file($afile);
if ($lastmod <> intval($this->__ini[$type][$mode.'_stamp']) || !file_exists($this->__cachedir . $this->__ini[$type][$mode.'_file'])) {
$this->create_cache($type, $mode, $afile, $lastmod);
}
return $_SESSION['__WEB_APP']['WEB_PATH'] ."chunks/". $this->__ini[$type][$mode.'_file'];
}
public function Apps() {
$id_user = (isset($_SESSION['__WEB_USER']) && is_array($_SESSION['__WEB_USER'])) ? $_SESSION['__WEB_USER']['UserID'] : 0;
$afile = $this->gen_file("app", $id_user);
$lastmod = $this->check_file($afile);
$appfile = md5($id_user . $lastmod) . ".app.js";
if (!file_exists($this->__cachedir . $appfile)) {
$this->create_cache("app", $id_user, $afile, $lastmod);
}
return $_SESSION['__WEB_APP']['WEB_PATH'] ."chunks/". $appfile;
}
}
?>
+181
View File
@@ -0,0 +1,181 @@
<?php
/*******************************************************************
Module : /Data/PDF.php
Desc. : Class pembuatan file PDF
Created By : Siau Pheng ([email protected]).
Created Date : April 18th, 2015.
Last Modified : July 25th, 2026.
(c) 2015 - 2026, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace siaupheng\webapp\Data;
use Mpdf;
final class PDF {
private $__avalue = [];
private $__atabel = [];
private $__theader = "";
private $__tcolname = "";
public function __construct() {
}
public function add_field($tnama="", $nlebar=0, $talign="L", $ffunc="", $nkol=1) {
$this->__atabel[] = array($tnama, $nlebar.'%', $ffunc, $talign, $nkol);
}
public function show_fields() {
$tmp_out = "<thead><tr>";
for ($i=0; $i<count($this->__atabel); $i++) {
if ($this->__atabel[$i][4]==0) continue;
$tmp_out .= $this->set_align($this->__atabel[$i][0], $this->__atabel[$i][1], "C", false, true, $this->__atabel[$i][4]);
}
$tmp_out .= "</tr></thead>";
$this->__tcolname = $tmp_out;
}
public function clear() {
$this->__avalue = [];
$this->__theader = "";
$this->__tcolname = "";
}
public function set_header($tlapor="", $tperiod="") {
$thasil = '<div style="text-align:center;font-size:12pt;"><b>'.strtoupper($_SESSION['__WEB_APP']['REG_NAME']);
$thasil .= (($tlapor=="") ? "" : '<br>'.$tlapor) . '</b>';
$thasil .= ($tperiod=="") ? "" : '<br><span style="font-size:11pt;">'.$tperiod.'</span>';
$thasil .= '</div><br>';
$this->__theader = $thasil;
}
public function add_value() {
$tmp_arr = func_get_args();
if (is_array($tmp_arr[0])) $tmp_arr = $tmp_arr[0];
$tmp_out = "<tr>";
for ($i=0; $i<count($this->__atabel); $i++) {
$tmp_val = $tmp_arr[$i];
if (is_array($this->__atabel[$i][2]) && trim($tmp_val)<>"") {
$tmp_val = $this->__atabel[$i][2][$tmp_val];
} else if (function_exists($this->__atabel[$i][2]) && trim($tmp_val)<>"") {
$tmp_val = call_user_func($this->__atabel[$i][2], $tmp_val);
}
$tmp_val = $this->set_align($tmp_val, $this->__atabel[$i][1], $this->__atabel[$i][3]);
$tmp_out .= $tmp_val;
}
$tmp_out .= "</tr>";
$this->__avalue[] = $tmp_out;
}
public function add_span() {
$tmp_arr = func_get_args();
$tmp_out = "<tr>";
for ($i=0; $i<count($tmp_arr); $i++) {
$tmp_val = $tmp_arr[$i];// CONTENT, COLSPAN=1, L, FUNC
if (function_exists($tmp_val[3]) && trim($tmp_val[0])<>"") {
$tmp_val[0] = call_user_func($tmp_val[3], $tmp_val[0]);
}
$tmp_val = $this->set_align($tmp_val[0], 0, $tmp_val[2], false, false, $tmp_val[1]);
$tmp_out .= $tmp_val;
}
$tmp_out .= "</tr>";
$this->__avalue[] = $tmp_out;
}
public function add_empty() {
$this->__avalue[] = $this->set_align("&nbsp;", 0, "L,NB", true);
}
public function add_stamp($ctk_ke=1, $bnama=false) {
$this->__avalue[] = $this->set_align($_SESSION['__WEB_APP']['APP_NAME']."v".$_SESSION['__WEB_APP']['APP_VER'].date("\@ymd.His")."-".user_id(3).(($bnama==true)?"/".strtoupper(user_user()):"").(($ctk_ke>0)?"#".isi_nol($ctk_ke,2):""), 0, "L,NB", true);
}
public function get_stamp($ctk_ke=1, $bnama=false) {
return $_SESSION['__WEB_APP']['APP_NAME']."v".$_SESSION['__WEB_APP']['APP_VER'].date("\@ymd.His")."-".user_id(3).(($bnama==true)?"/".strtoupper(user_user()):"").(($ctk_ke>0)?"#".isi_nol($ctk_ke,2):"");
}
public function createPDF($nama_file = "Nama_File.pdf", $nfontz = 8, $orien = "P") {
$html = '
<style>
#pdf_area {
width: 100%;
color: #000;
}
#pdf_area p { margin: 0; padding: 0; }
#pdf_cont table {
border: 1px solid #ccc;
border-spacing: 0;
border-collapse: collapse;
overflow: auto;
}
#pdf_cont table th, #pdf_cont table td {
font-size: 12pt;
text-align: left;
}
#pdf_cont table th {
text-align: center;
font-weight: bold;
padding: 2mm;
border: 1px solid #ccc;
}
#pdf_cont table td {
padding: 1mm;
}
#pdf_cont table td.left { text-align: left; }
#pdf_cont table td.center { text-align: center; }
#pdf_cont table td.right { text-align: right; }
#pdf_cont table td.bold { font-weight: bold; }
#pdf_cont table td.italic { font-style: italic; }
</style>
<body class="pdf_area">
<htmlpagefooter name="RepFoot">
<table width="100%" style="border:none;"><tr><td width="50%" style="text-align:left;border:none;">'.$this->get_stamp(1, true).'</td><td width="50%" style="text-align:right;font-style:italic;border:none;">Halaman : {PAGENO} dari {nbpg}</td></tr></table>
</htmlpagefooter>
<sethtmlpagefooter name="RepFoot" show-this-page="1" value="on" />';
if ($this->__theader<>"") $html .= $this->__theader;
$html .= '<div id="pdf_cont"><table>';
if ($this->__tcolname<>"") $html .= $this->__tcolname;
$html .= '<tbody>';
// content out
for ($i=0; $i<count($this->__avalue); $i++) {
$html .= $this->__avalue[$i];
}
$html .= '</tbody></table></div></div></body>';
$mpdf = new mPDF('c', 'A4'.($orien=="L"?"-L":""), $nfontz, '', 5, 5, 5, 10, 5, 5);
$mpdf->useSubstitutions = false;
$mpdf->WriteHTML($html);
$mpdf->Output($nama_file.".pdf", "I");
}
public function set_align($ttext, $nlen, $tdir="L", $bline=false, $bhead=false, $ncol=1) {
$tstyle = $this->create_style($tdir);
if (strpos(strtolower($ttext), "<tr>")===false) { //jika tidak ada <tr> up 2011-04-14
if ($bline===true) {
return '<tr><td colspan="'.count($this->__atabel).'" class="'.$tstyle.'">'.$ttext."</td></tr>";
} else {
return (($bhead===true)?"<th":"<td").(($ncol>1)?' colspan="'.$ncol.'"':"").' class="'.$tstyle.'" '.(($bhead===true)?'width="'.$nlen.'"':"").">".$ttext.(($bhead===true)?"</th>":"</td>");
}
} else {
return (($bline===true) ? $ttext : (($bhead===true)?"<th":"<td").(($ncol>1)?" colspan=\"".$ncol."\"":"")." class=\"".$tstyle." \" ".(($bhead===true)?"width=\"".$nlen."\"":"").">".$ttext.(($bhead===true)?"</th>":"</td>"));
}
}
private function create_style($tstyle="L") {
$apad = ["L"=>"left", "R"=>"right", "C"=>"center", "NB"=>"nb", "NL"=>"noleft", "NR"=>"noright", "B"=>"bold", "I"=>"italic"];
$_astyle = explode(",", $tstyle);
$thasil = "";
foreach ($_astyle as $_key => $_val) {
$thasil .= " ".$apad[$_val];
}
return substr($thasil,1);
}
}
?>
+163
View File
@@ -0,0 +1,163 @@
<?php
/*******************************************************************
Module : /Data/SimpleXLS.php
Desc. : Class pembuatan file Simple XLS
Created By : Siau Pheng ([email protected]).
Created Date : November 18th, 2012.
Last Modified : September 30th, 2025.
(c) 2012 - 2025, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace siaupheng\webapp\Data;
final class SimpleXLS {
private $__wbook = null;
private $__atabel = [];
private $__astyle = [];
private $__aheader = [];
private $__amerge = [];
private $__avalue = [];
private $__tsheet = null;
public function __construct($oprop=[]) {
$this->__wbook = new \Shuchkin\SimpleXLSXGen();
$this->__wbook
->setAuthor($_SESSION['__WEB_APP']['APP_NAME']." v".$_SESSION['__WEB_APP']['APP_VER'])
->setLastModifiedBy($_SESSION['__WEB_APP']['APP_NAME']." v".$_SESSION['__WEB_APP']['APP_VER'])
->setTitle($oprop['title'] ?? "")
->setSubject($oprop['subject'] ?? $_SESSION['__WEB_APP']['REG_NAME'])
->setDescription($oprop['desc'] ?? "")
->setCompany($_SESSION['__WEB_APP']['REG_NAME'])
->setDefaultFont("Calibri")
->setDefaultFontSize(11);
}
private function coord2cell($x, $y) {
$c = '';
for ($i = $x; $i >= 0; $i = ((int)($i / 26)) - 1) {
$c = chr(ord('A') + $i % 26) . $c;
}
return $c . ($y + 1);
}
private function create_style($tstyle="L") {
$_astyle = explode(",", $tstyle);
$_s_style = ""; $_e_style = "";
foreach ($_astyle as $_key => $_val) {
if ($_val == "L") {$_s_style .= '<left>'; $_e_style .= '</left>';}
if ($_val == "R") {$_s_style .= '<right>'; $_e_style .= '</right>';}
if ($_val == "C") {$_s_style .= '<center>'; $_e_style .= '</center>';}
if ($_val == "W") {$_s_style .= '<wraptext>'; $_e_style .= '</wraptext>';}
if ($_val == "B") {$_s_style .= '<b>'; $_e_style .= '</b>';}
if ($_val == "I") {$_s_style .= '<i>'; $_e_style .= '</i>';}
if ($_val == "S") {$_s_style .= "\0"; $_e_style .= '';}
if ($_val == "N") {$_s_style .= '<style nf="#,##0.00">'; $_e_style .= '</style>';}
if ($_val == "N0") {$_s_style .= '<style nf="#,##0">'; $_e_style .= '</style>';}
}
return ['s'=>$_s_style, 'e'=>$_e_style];
}
public function clear_field() {
$this->__atabel = [];
}
public function add_header($tjudul="") {
if ($tjudul=="") return;
$this->__aheader[] = $tjudul;
}
public function add_empty_row($nrow=1) {
$_jkolom = count($this->__atabel);
for ($i=0; $i<$nrow; $i++) {
$this->__avalue[] = array_fill(0,$_jkolom,"");
}
}
public function add_field($tnama="", $nlebar=0, $talign="L", $ffunc="") {
$this->__atabel[] = [$tnama, $nlebar, $ffunc];
$this->__astyle[] = $this->create_style($talign);
}
public function add_sheet($tnama_sheet="BARU", $bw_header=true) {
$this->__tsheet = $tnama_sheet;
$_jkolom = count($this->__atabel);
foreach ($this->__aheader as $_key => $_val) {
$a_out = ['<style font-size="12"><center>'.$_val."</center></style>"];
$this->__amerge[] = $this->coord2cell(0, count($this->__avalue)) . ":" . $this->coord2cell($_jkolom-1, count($this->__avalue));
$this->__avalue[] = $a_out;
}
if ($bw_header==true) $this->show_headers();
}
public function add_value() {
$tmp_arr = func_get_args();
if (is_array($tmp_arr[0])) $tmp_arr = $tmp_arr[0];
$a_out = [];
for ($i=0; $i<count($this->__atabel); $i++) {
$tmp_val = $tmp_arr[$i];
if (is_array($this->__atabel[$i][2]) && trim($tmp_val)<>"") {
$tmp_val = $this->__atabel[$i][2][$tmp_val];
} else if (function_exists($this->__atabel[$i][2]) && trim($tmp_val)<>"") {
$tmp_val = call_user_func($this->__atabel[$i][2], $tmp_val);
}
$a_out[] = $this->__astyle[$i]['s'] . $tmp_val . $this->__astyle[$i]['e'];
}
$this->__avalue[] = $a_out;
}
public function add_span() {
$tmp_arr = func_get_args();
$a_out = [];
for ($i=0; $i<count($tmp_arr); $i++) {
$tmp_val = $tmp_arr[$i]; // CONTENT, COLSPAN=1, L, FUNC
if (!isset($tmp_val[1])) $tmp_val[1] = 1;
if (!isset($tmp_val[2])) $tmp_val[2] = "L";
if (isset($tmp_val[3])) {
if (function_exists($tmp_val[3]) && trim("".$tmp_val[0])<>"") {
$tmp_val[0] = call_user_func($tmp_val[3], $tmp_val[0]);
}
}
$tmp_style = $this->create_style($tmp_val[2]);
if ($tmp_val[1]>1) $this->__amerge[] = $this->coord2cell(count($a_out), count($this->__avalue)) . ":" . $this->coord2cell((count($a_out)+$tmp_val[1])-1, count($this->__avalue));
$a_out[] = $tmp_style['s'] . $tmp_val[0] . $tmp_style['e'];
for ($k=1; $k<$tmp_val[1]; $k++) $a_out[] = null;
}
$this->__avalue[] = $a_out;
}
public function add_stamp($ctk_ke=1, $bnama=false) {
$this->__avalue[] = ["<i>".$_SESSION['__WEB_APP']['APP_NAME']."v".$_SESSION['__WEB_APP']['APP_VER'].date("\@ymd.His")."-".user_id(3).(($bnama==true)?"/".strtoupper(user_user()):"").(($ctk_ke>0)?"#".isi_nol($ctk_ke,2):"")."</i>"];
}
public function get_stamp($ctk_ke=1, $bnama=false) {
return $_SESSION['__WEB_APP']['APP_NAME']."v".$_SESSION['__WEB_APP']['APP_VER'].date("\@ymd.His")."-".user_id(3).(($bnama==true)?"/".strtoupper(user_user()):"").(($ctk_ke>0)?"#".isi_nol($ctk_ke,2):"");
}
public function show_headers() {
$a_out = [];
for ($i=0; $i<count($this->__atabel); $i++) {
$a_out[] = '<style bgcolor="#D0D0D0"><center>'.$this->__atabel[$i][0].'</center></style>';
}
$this->__avalue[] = $a_out;
}
public function send($tnama_file="File_Excel") {
$this->__wbook->addSheet($this->__avalue, $this->__tsheet);
for ($i=0; $i<count($this->__atabel); $i++) {
$this->__wbook->setColWidth(($i+1), $this->__atabel[$i][1]);
}
for ($i=0; $i<count($this->__amerge); $i++) {
$this->__wbook->mergeCells($this->__amerge[$i]);
}
$this->__wbook->downloadAs($tnama_file);
}
}
?>
+155
View File
@@ -0,0 +1,155 @@
<?php
/*******************************************************************
Module : /Data/XLS.php
Desc. : Class pembuatan file XLS
Created By : Siau Pheng ([email protected]).
Created Date : November 18th, 2012.
Last Modified : October 26th, 2023.
(c) 2012 - 2023, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
declare(strict_types=1);
namespace siaupheng\webapp\Data;
use PhpOffice\PhpSpreadsheet\IOFactory;
final class XLS {
private $__tnamafile = null;
private $__bline = true;
private $__wbook = null;
private $__wsheet = null;
private $__baris = 0;
private $__kolom = 0;
private $__atabel = [];
private $__astyle =[];
private $__aheader = [];
private $__fhead = null;
private $__isheet = 0;
public function __construct($tnama_file="", $oprop=[]) {//oke
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );
$this->__tnamafile = $tnama_file;
$this->__wbook = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$this->__wbook->getProperties()
->setCreator($_SESSION['__WEB_APP']['APP_NAME']." v".$_SESSION['__WEB_APP']['APP_VER'])
->setLastModifiedBy($_SESSION['__WEB_APP']['APP_NAME']." v".$_SESSION['__WEB_APP']['APP_VER'])
->setTitle($oprop['title'] ?? "")
->setSubject($oprop['subject'] ?? "")
->setDescription($oprop['desc'] ?? "")
->setCompany($_SESSION['__WEB_APP']['REG_NAME']);
$this->__wsheet = $this->__wbook->getActiveSheet();
$this->__fhead = [
'font' => ['bold' => true],
'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, 'wrapText' => true],
'fill' => ['fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID, 'startColor' => ['rgb'=>"D0D0D0"]],
'borders' => ['allBorders' => ['borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, 'color' => ['rgb'=>"C0C0C0"]]]
];
}
public function create_style($tstyle="L") {//oke
$_astyle = explode(",", $tstyle);
$_tmp_style = [];
$_bline = $this->__bline;
foreach ($_astyle as $_key => $_val) {
if ($_val == "L") $_tmp_style['alignment'] = ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT];
if ($_val == "R") $_tmp_style['alignment'] = ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT];
if ($_val == "C") $_tmp_style['alignment'] = ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER];
if ($_val == "W") $_tmp_style['alignment'] = ['wrapText' => true];
if ($_val == "B") $_tmp_style['font'] = ['bold' => true];
if ($_val == "S") $_tmp_style['numberFormat'] = ['formatCode' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT];
if ($_val == "N") $_tmp_style['numberFormat'] = ['formatCode' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED2];
if ($_val == "N0") $_tmp_style['numberFormat'] = ['formatCode' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED2];
if ($_val == "NL") $_bline = false;
if ($_val == "H") {
$_bline = false;
$_tmp_style = $this->__fhead;
}
}
return $_tmp_style;
}
public function clear_field() {//up : 2014-06-19
$this->__atabel = [];
$this->__astyle = [];
}
public function set_pos($nbaris=0, $nkolom=0) {
$this->__baris = $nbaris;
$this->__kolom = $nkolom;
}
public function set_garis_bawah($bgaris=true) {}//todo: deleted
public function add_header($tjudul="") {//oke
if ($tjudul=="") return;
$this->__aheader[] = $tjudul;
}
public function add_empty_row($nrow=1) {//oke
$this->__baris += $nrow;
}
public function add_field($tnama="", $nlebar=0, $talign="L", $ffunc="") {//oke
$this->__atabel[] = [$tnama, $nlebar, $ffunc];
$this->__astyle[] = $this->create_style($talign);
}
public function add_sheet($tnama_sheet="BARU", $bw_header=true) {//not yet !!!
if ($this->__isheet > 0) {
$this->__wbook->createSheet();
$this->__wsheet = $this->__wbook->setActiveSheetIndex(++$this->__isheet);
}
$this->__wsheet->setTitle($tnama_sheet);
for ($i=0; $i<count($this->__atabel); $i++) {
//sampai disini dulu !!
$this->__wsheet->getColumnDimension($this->__kolom+$i)->setWidth($this->__atabel[$i][1]);
}
$_fjudul = $this->__wbook->addFormat();
$_fjudul->setBold();
$_fjudul->setAlign("center");
$_jkolom = count($this->__atabel);
if ($_jkolom>0) $_jkolom--;
foreach ($this->__aheader as $_key => $_val) {
$this->__wsheet->write($this->__baris,$this->__kolom,$_val,$_fjudul);
$this->__wsheet->setMerge($this->__baris,$this->__kolom,$this->__baris,$this->__kolom+$_jkolom);
$this->__baris++;
}
$this->__baris++;
if ($bw_header==true) $this->show_headers();
}
public function add_value() {
$tmp_arr = func_get_args();
if (is_array($tmp_arr[0])) $tmp_arr = $tmp_arr[0];
$tmp_out = "";
for ($i=0; $i<count($this->__atabel); $i++) {
$tmp_val = $tmp_arr[$i];
if (is_array($this->__atabel[$i][1]) && trim($tmp_val)<>"") {
$tmp_val = $this->__atabel[$i][1][$tmp_val];
} else if (function_exists($this->__atabel[$i][1]) && trim($tmp_val)<>"") {
$tmp_val = call_user_func($this->__atabel[$i][1], $tmp_val);
}
$tmp_out .= DELI_CSV . $tmp_val;
}
$tmp_out = substr($tmp_out, 1);
$this->__avalue[] = $tmp_out;
}
public function send($tnama_file="File_Excel") {//oke
$this->__wsheet->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$tnama_file.'"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($this->__wsheet, 'Xlsx');
$writer->save('php://output');
}
}
?>