330 lines
14 KiB
PHP
330 lines
14 KiB
PHP
<?php
|
|
|
|
/*******************************************************************
|
|
|
|
Module : /Web/Table.php
|
|
Created By : Siau Pheng ([email protected]).
|
|
Created Date : January 16th, 2008.
|
|
Last Modified : September 16th, 2026.
|
|
|
|
(c) 3FONIA Software; WWW.3FONIA.COM.
|
|
|
|
*******************************************************************/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Webapp\Web;
|
|
|
|
use \flight\Engine;
|
|
use \flight\database\SimplePdo;
|
|
|
|
/**
|
|
* Class Table
|
|
*
|
|
* Pembuatan DataTable (jQuery & Bootstrap).
|
|
*
|
|
* @license MIT, https://www.3fonia.com
|
|
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
|
|
*
|
|
* @package Webapp\Web
|
|
*/
|
|
|
|
final class Table {
|
|
private $__ttable = "";
|
|
private $__tfield = "";
|
|
private $__twhere = "";
|
|
private $__aparam = [];
|
|
private $__tfooter = "";
|
|
private $__afield = [];
|
|
private $__arowcol = [];
|
|
private $__arow_func = [];
|
|
private $__tfunc_select = "";
|
|
private $__bno_navi = false;
|
|
private $__btxtbutton = false;
|
|
private $__bnobutton = false;
|
|
private $__bscrollbar = false;
|
|
private $__acontrol = [ 'field' => false, 'value' => "" ];
|
|
private $__acbox = [];
|
|
private $__aoption = [];
|
|
private $__nscrollheight = 0;
|
|
private $__ord_field = "";
|
|
private $__ord_dir = "A";
|
|
private $__tcss = "";
|
|
private $__livedata = false;
|
|
protected Engine $app;
|
|
protected SimplePdo $db;
|
|
protected array $data;
|
|
|
|
/**
|
|
* @param Engine $app
|
|
*/
|
|
public function __construct(Engine $app) {
|
|
$this->app = $app;
|
|
$this->db = $app->db();
|
|
$this->data = $this->app->request()->data->getData();
|
|
}
|
|
|
|
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_footer(string $field = ""): void {
|
|
$this->__tfooter = $field;
|
|
}
|
|
|
|
public function set_css(string $css = ""): void {
|
|
$this->__tcss = $css;
|
|
}
|
|
|
|
public function set_field_order(string $field = "", string $order = "A"): void {
|
|
$this->__ord_field = $field;
|
|
$this->__ord_dir = $order;
|
|
}
|
|
|
|
public function set_textbtn(bool $istxt = true): void {
|
|
$this->__btxtbutton = $istxt;
|
|
}
|
|
|
|
public function set_scrollbar(bool $isscroll = true, int $height = 200): void {
|
|
$this->__bscrollbar = $isscroll;
|
|
$this->__nscrollheight = $height;
|
|
}
|
|
|
|
public function set_row_color(string $field = "", string $val = "", string $css = ""): void {
|
|
if (empty($field)) return;
|
|
$this->__arowcol[] = [ 'F' => $field, 'V' => $val, 'C' => $css ];
|
|
}
|
|
|
|
public function set_func_navi(string $func = ""): void {//compatible
|
|
$this->__bno_navi = ($func === '');
|
|
}
|
|
|
|
public function set_navi_off(): void {
|
|
$this->__bno_navi = true;
|
|
}
|
|
|
|
public function set_func_select(string $func = ""): void {
|
|
$this->__tfunc_select = $func;
|
|
}
|
|
|
|
public function set_func_btn(string $func = "", string $label = ""): void {
|
|
$this->__arow_func[] = [ 'label' => $label, 'func' => $func ];
|
|
}
|
|
|
|
public function set_row_control(string $field = "", string $value = "Y"): void {
|
|
$this->__acontrol['field'] = $field;
|
|
$this->__acontrol['value'] = $value;
|
|
}
|
|
|
|
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 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(string $field = "", string $header = "", string $style = "l", mixed $format = "", string $footer = "", string $otype = ""): void {
|
|
$this->__afield[] = [ 'a' => $field, 'h' => $header, 's' => $style, 'f' => $format, 't' => $footer, 'o' => $otype ];
|
|
}
|
|
|
|
public function json_table(string $nama_tabel = "tabel_", int $baris_tabel = 10): array {
|
|
$this->prepare_query();
|
|
$adata = [];
|
|
|
|
// adding header
|
|
foreach ($this->__afield as $col) {
|
|
$adata['header'][] = [ 'f' => $col['s'], 'o' => $col['a'], 'c' => $col['h'], 's' => $col['o'] ];
|
|
}
|
|
|
|
// adding footer
|
|
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 (isset($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 = $tval;
|
|
}
|
|
$adata['footer'][] = [ 'c' => $tval2 ];
|
|
}
|
|
}
|
|
|
|
$acontent = [];
|
|
if (!$this->__livedata) {
|
|
$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) || ($row[$this->__acontrol['field']]===$this->__acontrol['value'])) && $this->__bnobutton===false) {
|
|
// adding all option
|
|
foreach ($this->__aoption as $opsi) {
|
|
if (in_array($opsi['type'], ["button","button-dt"])) {
|
|
if (!empty($opsi['field']) && ((string) $row[$opsi['field']] !== (string) $opsi['value'])) {} 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'] = $row[$this->__acbox['field']];
|
|
$aoption['checkbox']['value'] = $row[$this->__acbox['control']];
|
|
}
|
|
}
|
|
}
|
|
if (count($this->__arowcol) > 0) {
|
|
foreach ($this->__arowcol as $rcol) {
|
|
if ($row[$rcol['F']] == $rcol['V']) $aoption['rowcss'] = $rcol['C'];
|
|
}
|
|
}
|
|
$abaris[] = $aoption;
|
|
foreach ($this->__afield as $col) {
|
|
$tval = $row[$col['a']];
|
|
if (is_array($col['f'])) {
|
|
$tval = $col['f'][$tval];
|
|
} else if (!is_null($col['f']) && str_contains($col['f'], '::')) {
|
|
$tclass = explode("::", $col['f']);
|
|
if (class_exists($tclass[0]) && method_exists($tclass[0], $tclass[1])) {
|
|
$tval = call_user_func($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;
|
|
}
|
|
$acontent[] = $abaris;
|
|
}
|
|
}
|
|
|
|
// navigator
|
|
$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'], $aval['value'], $aval['field_css'], $aval['avalue_css']);
|
|
$adata['option'][] = $aval;
|
|
}
|
|
$adata['navi'] = $anavi;
|
|
$adata['content'] = $acontent;
|
|
return $adata;
|
|
}
|
|
|
|
public function db_table(string $nama_tabel = "tabel_", int $baris_tabel = 10): array {
|
|
$this->prepare_query();
|
|
$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 = (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 ];
|
|
|
|
$t_order = "ASC";
|
|
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[$c_order]) ? " ORDER BY ".$this->__afield[$c_order]['a']." ".$t_order : "";
|
|
|
|
$adata['data'] = [];
|
|
$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) || ($row[$this->__acontrol['field']]==$this->__acontrol['value'])) && $this->__bnobutton===false) {
|
|
// adding all option
|
|
foreach ($this->__aoption as $opsi) {
|
|
if (in_array($opsi['type'], ["button","button-dt"])) {
|
|
if (!empty($opsi['field']) && ((string) $row[$opsi['field']] !== (string) $opsi['value'])) {} 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'] = $row[$this->__acbox['field']];
|
|
$aoption['checkbox']['value'] = $row[$this->__acbox['control']];
|
|
}
|
|
}
|
|
}
|
|
if (count($this->__arowcol)>0) {
|
|
foreach ($this->__arowcol as $rcol) {
|
|
if ($row[$rcol['F']] == $rcol['V']) $aoption['rowcss'] = $rcol['C'];
|
|
}
|
|
}
|
|
$abaris[] = $aoption;
|
|
foreach ($this->__afield as $col) {
|
|
$tval = $row[$col['a']];
|
|
if (is_array($col['f'])) {
|
|
$tval = $col['f'][$tval];
|
|
} else if (!is_null($col['f']) && str_contains($col['f'], '::')) {
|
|
$tclass = explode("::", $col['f']);
|
|
if (class_exists($tclass[0]) && method_exists($tclass[0], $tclass[1])) {
|
|
$tval = call_user_func($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;
|
|
}
|
|
$adata['data'][] = $abaris;
|
|
}
|
|
} else {
|
|
$adata = $this->json_table($nama_tabel, $baris_tabel);
|
|
}
|
|
return $adata;
|
|
}
|
|
|
|
private function prepare_query() {
|
|
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) {}
|
|
|
|
}
|
|
|
|
?>
|