upgrade first session to flightphp

This commit is contained in:
2026-08-19 17:22:07 +07:00
parent 36395c2b55
commit 27ebdca49b
31 changed files with 1383 additions and 7659 deletions
+153 -160
View File
@@ -6,214 +6,201 @@
Desc. : HTML Table (jQuery & Bootstrap)
Created By : Siau Pheng ([email protected]).
Created Date : January 16th, 2008.
Last Modified : January 26th, 2026.
Last Modified : August 16th, 2026.
(c) 2008 - 2026, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
//OK
declare(strict_types=1);
namespace siaupheng\webapp\Web;
use flight\Engine;
use flight\database\SimplePdo;
final class Table {
private $__ttable = "";
private $__tfield = "";
private $__twhere = "";
private $__tquery = "";
private $__tquery_footer = "";
private $__aheader = [];
private $__afield = [];
private $__astyle = [];
private $__aformat = [];
private $__afooter = [];
private $__aotype = [];
private $__aparam = [];
private $__tfooter = "";
private $__afield = [];
private $__arowcol = [];
private $__arow_func = [];
private $__tfunc_select = "";
private $__tfunc_navi = "";
private $__bno_navi = false;
private $__btxtbutton = false;
private $__bnobutton = false;
private $__bscrollbar = false;
private $__acontrol = ['field'=>false, 'value'=>""];
private $__acontrol = [ 'field' => false, 'value' => "" ];
private $__acbox = [];
private $__aoption = [];
private $__npage_rec = 10;
private $__nreclimit = 0;
private $__nscrollheight = 0;
private $__ord_field = "";
private $__ord_dir = "A";
private $__tcss = "";
private $__livedata = false;
protected $__cdb = null;
protected $__cpa = null;
protected Engine $app;
protected SimplePdo $db;
protected array $data;
public function __construct($db = null){
if (is_resource($db)) { $this->__cdb = $db; }
else { $this->__cdb = new \siaupheng\webapp\DB\MySQL(); }
$this->__cpa = new \siaupheng\webapp\Web\Param();
public function __construct(?Engine $app = null) {
$this->app = (empty($app) === true) ? \Flight::app() : $app;
$this->db = $this->app->db();
$this->data = $this->app->request()->data->getData();
}
public function set_table($ttable = "", $tfield = "", $twhere = "") {
$this->__ttable = $ttable;
if ($tfield != "") $this->__tfield = $tfield;
if ($twhere != "") $this->__twhere = $twhere;
public function set_table(string $table = "", string $field = "", string $where = "", array $params = []): void {
$this->__ttable = $table;
$this->__aparam = $params;
if (!empty($field)) $this->__tfield = $field;
if (!empty($where)) $this->__twhere = $where;
}
public function set_query($tquery = "") {
$this->__tquery = $tquery;
public function set_footer(string $field = ""): void {
$this->__tfooter = $field;
}
public function set_query_footer($tquery = "") {
$this->__tquery_footer = $tquery;
public function set_css(string $css = ""): void {
$this->__tcss = $css;
}
public function set_css($tcss = "") {
$this->__tcss = $tcss;
public function set_field_order(string $field = "", string $order = "A"): void {
$this->__ord_field = $field;
$this->__ord_dir = $order;
}
public function set_limit($nlimit = 0) {
$this->__nreclimit = $nlimit;
public function set_textbtn(bool $istxt = true): void {
$this->__btxtbutton = $istxt;
}
public function set_field_order($tfield = "", $torder = "A") {
$this->__ord_field = $tfield;
$this->__ord_dir = $torder;
public function set_scrollbar(bool $isscroll = true, int $height = 200): void {
$this->__bscrollbar = $isscroll;
$this->__nscrollheight = $height;
}
public function set_textbtn($bistxt = true) {
$this->__btxtbutton = $bistxt;
public function set_row_color(string $field = "", string $val = "", string $css = ""): void {
if (epmty($field)) return;
$this->__arowcol[] = [ 'F' => $field, 'V' => $val, 'C' => $css ];
}
public function set_scrollbar($bisscroll = true, $nheight = 200) {
$this->__bscrollbar = $bisscroll;
$this->__nscrollheight = $nheight;
}
public function set_row_color($tfield = "", $tval = "", $tcss = "") {
if ($tfield=="") return;
$this->__arowcol[] = ['F'=>$tfield, 'V'=>$tval, 'C'=>$tcss];
}
public function set_func_navi($tfunc = "") {
$this->__tfunc_navi = $tfunc;
public function set_func_navi(string $func = ""): void {//compatible
$this->__bno_navi = ($func === '');
}
public function set_func_select($tfunc = "") {
$this->__tfunc_select = $tfunc;
public function set_navi_off(): void {
$this->__bno_navi = true;
}
public function set_func_btn($tfunc = "", $tlabel = "") {
$this->__arow_func[] = ['label' => $tlabel, 'func' => $tfunc];
}
public function set_row_control($tfield = "", $tvalue = "Y") {
$this->__acontrol['field'] = $tfield;
$this->__acontrol['value'] = $tvalue;
public function set_func_select(string $func = ""): void {
$this->__tfunc_select = $func;
}
public function add_row_form($ticon = "pencil", $tlabel = "", $tfunc = "", $tfield = "", $tvalue = "") {
if ($tfunc == "") {
$this->__aoption[] = ['type' => "button-dt", 'func' => $tlabel, 'icon' => "pencil", 'label' => $ticon];
} else {//todo: new format with icon, upgrade all later !
$this->__aoption[] = ['type' => "button-dt", 'func' => $tfunc, 'icon' => $ticon, 'label' => $tlabel, 'field' => $tfield, 'value' => $tvalue];
}
public function set_func_btn(string $func = "", string $label = ""): void {
$this->__arow_func[] = [ 'label' => $label, 'func' => $func ];
}
public function add_row_btn($ticon = "", $tlabel = "", $tfunc = "", $tfield = "", $tvalue = "", $tfield_css = "", $avalue_css = []) {
$this->__aoption[] = ['type' => "button", 'func' => $tfunc, 'icon' => $ticon, 'label' => $tlabel, 'field' => $tfield, 'value' => $tvalue, 'field_css' => $tfield_css, 'avalue_css' => $avalue_css];
}
public function add_row_del($tlabel = "", $tfunc = "", $tfield = "Unix", $tvalue = "") {
$this->__aoption[] = ['type' => "button", 'func' => $tfunc, 'icon' => "x", 'label' => $tlabel, 'field' => $tfield, 'value' => $tvalue];
}
public function add_row_edit($tlabel = "", $tfunc = "") {
$this->add_row_btn("note", $tlabel, $tfunc);
public function set_row_control(string $field = "", string $value = "Y"): void {
$this->__acontrol['field'] = $field;
$this->__acontrol['value'] = $value;
}
public function add_row_checkbox($tfield = "ID", $tcontrol = "Edit", $avalue = ["Y","N"]) {
$this->__acbox['field'] = $tfield;
$this->__acbox['control'] = $tcontrol;
$this->__acbox['avalue'] = $avalue;
$this->__aoption[] = ['type' => "checkbox", 'field' => $tfield, 'avalue' => $avalue];
public function add_row_form(string $icon = "pencil", string $label = "", string $func = "", string $field = "", string $value = ""): void {
$this->__aoption[] = [ 'type' => "button-dt", 'func' => $func, 'icon' => $icon, 'label' => $label, 'field' => $field, 'value' => $value ];
}
public function add_row_btn(string $icon = "", string $label = "", string $func = "", string $field = "", string|int $value = "", string $field_css = "", array $avalue_css = []): void {
$this->__aoption[] = [ 'type' => 'button', 'func' => $func, 'icon' => $icon, 'label' => $label, 'field' => $field, 'value' => (string)$value, 'field_css' => $field_css, 'avalue_css' => $avalue_css ];
}
public function add_row_del(string $label = "", string $func = "", string $field = "Unix", string $value = ""): void {
$this->__aoption[] = [ 'type' => 'button', 'func' => $func, 'icon' => 'x', 'label' => $label, 'field' => $field, 'value' => $value ];
}
public function add_row_edit(string $label = "", string $func = ""): void {
$this->add_row_btn('clipboard', $label, $func);
}
public function no_button() {
public function add_row_checkbox(string $field = "ID", string $control = "Edit", array $value = ['Y','N']): void {
$this->__acbox = [ 'field' => $field, 'control' => $control, 'avalue' => $value ];
$this->__aoption[] = ['type' => "checkbox", 'field' => $field, 'avalue' => $value];
}
public function no_button(): void {
$this->__bnobutton = true;
}
public function add_field($tfield = "", $theader = "", $tstyle = "l", $tformat = "", $tfooter = "", $totype = "") {
$this->__aheader[] = $theader;
$this->__afield[] = $tfield;
$this->__astyle[] = $tstyle;
$this->__aformat[] = $tformat;
$this->__afooter[] = $tfooter;
$this->__aotype[] = $totype;
public function add_field(string $field = "", string $header = "", string $style = "l", string $format = "", string $footer = "", string $otype = ""): void {
$this->__afield[] = [ 'a' => $field, 'h' => $header, 's' => $style, 'f' => $format, 't' => $footer, 'o' => $otype ];
}
public function json_table($nama_tabel = "tabel_", $baris_tabel = 10) {
$adata = []; $njml_kol = 0;
public function json_table(string $nama_tabel = "tabel_", int $baris_tabel = 10): array {
$this->prepare_query();
$adata = [];
// adding header
for ($i=0; $i<count($this->__afield); $i++) {
$adata['header'][] = ['f'=>$this->__astyle[$i], 'o'=>$this->__afield[$i], 'c'=>$this->__aheader[$i], 's'=>$this->__aotype[$i]];
$njml_kol += ($this->__astyle[$i]=="hidden") ? 0 : 1;
foreach ($this->__afield as $col) {
$adata['header'][] = [ 'f' => $col['s'], 'o' => $col['a'], 'c' => $col['h'], 's' => $col['o'] ];
}
// adding footer
if ($this->__tquery_footer) {
$this->__cdb->Query($this->__tquery_footer);
$this->__cdb->Next();
for ($i=0; $i<count($this->__afield); $i++) {
$tval = $this->__cdb->Row($this->__afield[$i], 0);
if (is_array($this->__afooter[$i])) {
$tval2 = $this->__afooter[$i][$tval];
} else if (!is_null($this->__afooter[$i]) && function_exists($this->__afooter[$i])) {
$tval2 = call_user_func($this->__afooter[$i], $tval);
if (!empty($this->__tfooter)) {
$row = $this->db->fetchRow("SELECT ".$this->__tfooter." FROM ".$this->__ttable.$this->__twhere, $this->__aparam);
foreach ($this->__afield as $col) {
$tval = $row[$col['a']] ?? '';
if (is_set($col['t']) && !empty($col['t'])) {
if (is_array($col['t'])) {
$tval2 = $col['t'][$tval] ?? '';
} else if (function_exists($col['t'])) {
$tval2 = call_user_func($col['t'], $tval);
}
} else {
$tval2 = ($this->__afooter[$i]<>"") ? $this->__afooter[$i] : $tval;
$tval2 = $tval;
}
$adata['footer'][] = ['c'=>$tval2];
$adata['footer'][] = [ 'c' => $tval2 ];
}
}
$acontent = [];
if (!$this->__livedata) {
$this->__cdb->Query($this->__tquery);
while ($this->__cdb->Next()) {
$stat = $this->db->runQuery("SELECT ".$this->__tfield." FROM ".$this->__ttable.$this->__twhere, $this->__aparam);
while ($row = $stat->fetch(SimplePdo::FETCH_ASSOC)) {
$abaris = [];
$aoption = ['button'=>[]];
if (count($this->__aoption)>0) {
if ((($this->__acontrol['field']===false) || ($this->__cdb->Row($this->__acontrol['field'])==$this->__acontrol['value'])) && $this->__bnobutton===false) {
if ((($this->__acontrol['field']===false) || ($row[$this->__acontrol['field']]===$this->__acontrol['value'])) && $this->__bnobutton===false) {
// adding all option
for ($i=0; $i<count($this->__aoption); $i++) {
if (in_array($this->__aoption[$i]['type'], ["button","button-dt"])) {
if (!isset($this->__aoption[$i]['field'])) continue;
if (($this->__aoption[$i]['field']!="") && ($this->__cdb->Row($this->__aoption[$i]['field'])<>(trim("".$this->__aoption[$i]['value'])!="" ? $this->__aoption[$i]['value'] : $_SESSION['__WEB_USER']['UNIX']))) {} else {
$aoption['button'][] = [$this->__aoption[$i]['icon'], (empty($this->__aoption[$i]['field_css'])?'':$this->__aoption[$i]['avalue_css'][$this->__cdb->Row($this->__aoption[$i]['field_css'])]??'')];
foreach ($this->__aoption as $opsi) {
if (in_array($opsi['type'], ["button","button-dt"])) {
if (!isset($opsi['field'])) continue;
if (!empty($opsi['field']) && ($row[$opsi['field']] !== (!empty($opsi['value']) ? $opsi['value'] : (string)$this->app->session()->get('__WEB_USER')['UNIX']))) {} else {
$aoption['button'][] = [ $opsi['icon'], (empty($opsi['field_css'])?'':$opsi['avalue_css'][$row[$opsi['field_css']]]??'') ];
}
}
}
// adding checkbox option
if (array_key_exists("field", $this->__acbox)) {
$aoption['checkbox']['field'] = $this->__cdb->Row($this->__acbox['field']);
$aoption['checkbox']['value'] = $this->__cdb->Row($this->__acbox['control']);
if (array_key_exists('field', $this->__acbox)) {
$aoption['checkbox']['field'] = $row[$this->__acbox['field']];
$aoption['checkbox']['value'] = $row[$this->__acbox['control']];
}
}
}
if (count($this->__arowcol)>0) {//up: 2014-06-13
for ($i=0; $i<count($this->__arowcol); $i++) {
if ($this->__cdb->Row($this->__arowcol[$i]['F']) == $this->__arowcol[$i]['V']) $aoption['rowcss'] = $this->__arowcol[$i]['C'];
if (count($this->__arowcol) > 0) {
foreach ($this->__arowcol as $rcol) {
if ($row[$rcol['F']] == $rcol['V']) $aoption['rowcss'] = $rcol['C'];
}
}
$abaris[] = $aoption;
for ($i=0; $i<count($this->__afield); $i++) {
$tval = stripslashes($this->__cdb->Row($this->__afield[$i]));//up: 2015-01-05
if (is_array($this->__aformat[$i])) {
$tval = $this->__aformat[$i][$tval];
} else if (!is_null($this->__aformat[$i]) && function_exists($this->__aformat[$i])) {
$tval = call_user_func($this->__aformat[$i], $tval);
foreach ($this->__afield as $col) {
$tval = $row[$col['a']];
if (is_array($col['f'])) {
$tval = $col['f'][$tval];
} else if (!is_null($col['f']) && function_exists($col['f'])) {
$tval = call_user_func($col['f'], $tval);
} else if (is_string($tval)) {
$tval = stripslashes($tval);
}
$abaris[] = $tval;
}
@@ -222,80 +209,82 @@ final class Table {
}
// navigator
$anavi = [ 'func_navi' => $this->__tfunc_navi, 'button' => $this->__arow_func ];
$anavi = [ 'no_navi' => $this->__bno_navi, 'button' => $this->__arow_func ];
$adata['type'] = "table";
$adata['id'] = $nama_tabel;
$adata['config'] = ['css' => $this->__tcss, 'row_count' => $baris_tabel, 'func_select' => $this->__tfunc_select, 'nobutton' => $this->__bnobutton, 'txtbutton' => $this->__btxtbutton, 'scrollbar' => $this->__bscrollbar, 'scroll_height' => $this->__nscrollheight, 'sort_col' => $this->__ord_field, 'sort_dir' => $this->__ord_dir, 'live' => $this->__livedata];
$adata['option'] = [];
foreach ($this->__aoption as $aval) {
if ($aval['type']!=="checkbox") unset($aval['field']); unset($aval['value']); unset($aval['field_css']); unset($aval['value_css']); $adata['option'][] = $aval;
if ($aval['type'] !== 'checkbox') unset($aval['field'], $aval['value'], $aval['field_css'], $aval['avalue_css']);
$adata['option'][] = $aval;
}
$adata['navi'] = $anavi;
$adata['content'] = $acontent;
return $adata;
}
public function db_table($nama_tabel = "tabel_", $baris_tabel = 10) {
$adata = [];
$this->__livedata = true;
public function db_table(string $nama_tabel = "tabel_", int $baris_tabel = 10): array {
$this->prepare_query();
$id_draw = intval($this->__cpa->Get("draw", 0));
$i_start = intval($this->__cpa->Get("start", 0));
$i_length = intval($this->__cpa->Get("length", 0));
$i_order = $this->__cpa->Get("order", 0);
$this->__livedata = true;
$adata = [];
$id_draw = (int) ($this->data['draw'] ?? 0);
$i_start = (int) ($this->data['start'] ?? 0);
$i_length = (int) ($this->data['length'] ?? 0);
$a_order = (array) ($this->data['order'] ?? []);
if ($id_draw > 0) {
$record = 0;
$this->__cdb->Query("SELECT COUNT(*) Item FROM ".$this->__ttable.$this->__twhere);
if ($this->__cdb->Next()) $record = intval($this->__cdb->Row("Item", 0));
$record = (int)$this->db->fetchField("SELECT COUNT(*) FROM ".$this->__ttable.$this->__twhere, $this->__aparam) ?? 0;
$adata['type'] = "table-live";
$adata['id'] = $nama_tabel;
$adata['config'] = ['draw' => $id_draw, 'recordsTotal' => $record, 'recordsFiltered' => $record];
$adata['config'] = [ 'draw' => $id_draw, 'recordsTotal' => $record, 'recordsFiltered' => $record ];
$t_order = "ASC";
if ($i_order <> 0) {
$t_order = ($i_order[0]['dir']=="asc") ? "ASC" : "DESC";
$i_order = $i_order[0]['column'];
if (count($a_order) > 0) {
$t_order = ($a_order[0]['dir']=="asc") ? "ASC" : "DESC";
$c_order = $a_order[0]['column'];
}
if (($i_length > $record) || ($i_start >= $record) || ($record <= 10)) $i_length = -1;
$sql_limit = ($i_start>=0 && $i_length != -1) ? " LIMIT ".$i_start.", ".$i_length : "";
$sql_order = ($this->__afield[$i_order]) ? " ORDER BY ".$this->__afield[$i_order]." ".$t_order : "";
$sql_order = ($this->__afield[$c_order]) ? " ORDER BY ".$this->__afield[$c_order]['a']." ".$t_order : "";
$adata['data'] = [];
$this->__cdb->Query("SELECT ".$this->__tfield." FROM ".$this->__ttable.$this->__twhere.$sql_order.$sql_limit);
while ($this->__cdb->Next()) {
$stat = $this->db->runQuery("SELECT ".$this->__tfield." FROM ".$this->__ttable.$this->__twhere.$sql_order.$sql_limit, $this->__aparam);
while ($row = $stat->fetch(SimplePdo::FETCH_ASSOC)) {
$abaris = [];
$aoption = ['button'=>[]];
if (count($this->__aoption)>0) {
if ((($this->__acontrol['field']===false) || ($this->__cdb->Row($this->__acontrol['field'])==$this->__acontrol['value'])) && $this->__bnobutton===false) {
if ((($this->__acontrol['field']===false) || ($row[$this->__acontrol['field']]==$this->__acontrol['value'])) && $this->__bnobutton===false) {
// adding all option
for ($i=0; $i<count($this->__aoption); $i++) {
if (in_array($this->__aoption[$i]['type'], ["button","button-dt"])) {
if (($this->__aoption[$i]['field']!="") && ($this->__cdb->Row($this->__aoption[$i]['field'])<>(trim("".$this->__aoption[$i]['value'])!="" ? $this->__aoption[$i]['value'] : $_SESSION['__WEB_USER']['UNIX']))) {} else {
$aoption['button'][] = [$this->__aoption[$i]['icon'], (empty($this->__aoption[$i]['field_css'])?'':$this->__aoption[$i]['avalue_css'][$this->__cdb->Row($this->__aoption[$i]['field_css'])]??'')];
foreach ($this->__aoption as $opsi) {
if (in_array($opsi['type'], ["button","button-dt"])) {
if (!isset($opsi['field'])) continue;
if (!empty($opsi['field']) && ($row[$opsi['field']] !== (!empty($opsi['value']) ? $opsi['value'] : $this->app->session()->get('__WEB_USER')['UNIX']))) {} else {
$aoption['button'][] = [ $opsi['icon'], (empty($opsi['field_css'])?'':$opsi['avalue_css'][$row[$opsi['field_css']]]??'') ];
}
}
}
// adding checkbox option
if (array_key_exists("field", $this->__acbox)) {
$aoption['checkbox']['field'] = $this->__cdb->Row($this->__acbox['field']);
$aoption['checkbox']['value'] = $this->__cdb->Row($this->__acbox['control']);
if (array_key_exists('field', $this->__acbox)) {
$aoption['checkbox']['field'] = $row[$this->__acbox['field']];
$aoption['checkbox']['value'] = $row[$this->__acbox['control']];
}
}
}
if (count($this->__arowcol)>0) {//up: 2014-06-13
for ($i=0; $i<count($this->__arowcol); $i++) {
if ($this->__cdb->Row($this->__arowcol[$i]['F']) == $this->__arowcol[$i]['V']) $aoption['rowcss'] = $this->__arowcol[$i]['C'];
if (count($this->__arowcol)>0) {
foreach ($this->__arowcol as $rcol) {
if ($row[$rcol['F']] == $rcol['V']) $aoption['rowcss'] = $rcol['C'];
}
}
$abaris[] = $aoption;
for ($i=0; $i<count($this->__afield); $i++) {
$tval = stripslashes($this->__cdb->Row($this->__afield[$i]));//up: 2015-01-05
if (is_array($this->__aformat[$i])) {
$tval = $this->__aformat[$i][$tval];
} else if (!is_null($this->__aformat[$i]) && function_exists($this->__aformat[$i])) {
$tval = call_user_func($this->__aformat[$i], $tval);
foreach ($this->__afield as $col) {
$tval = $row[$col['a']];
if (is_array($col['f'])) {
$tval = $col['f'][$tval];
} else if (!is_null($col['f']) && function_exists($col['f'])) {
$tval = call_user_func($col['f'], $tval);
} else if (is_string($tval)) {
$tval = stripslashes($tval);
}
$abaris[] = $tval;
}
@@ -308,9 +297,13 @@ final class Table {
}
private function prepare_query() {
if ($this->__tfield == "") $this->__tfield = "`".implode('`,`', $this->__afield)."`";
if ($this->__twhere != "") $this->__twhere = " WHERE " . $this->__twhere;
if (empty($this->__tfield)) $this->__tfield = '`'. implode('`,`', array_column($this->__afield, 'a')) .'`';
if (!empty($this->__twhere)) $this->__twhere = ' WHERE ' . $this->__twhere;
}
//public function set_query($tquery = "") {}
//public function set_limit($nlimit = 0) {}
}
?>