* * @package Webapp\Web */ final class Form { private $__aform = []; private $__ahide = []; protected Engine $app; protected SimplePdo $db; protected Session $session; /** * @param Engine $app */ public function __construct(Engine $app){ $this->app = $app; $this->db = $app->db(); $this->session = $app->session(); } public function option(string $label, string $field, mixed $value=null, bool $enable=true, bool $req=false, array $arr=[], ?string $help=null, ?string $change=null, bool $multi=false, bool $group=false): string { $_opsi = ''; if (!empty($change)) $_opsi .= ' ochan="'.rtrim($change, '();').'"'; if ($enable===false) $_opsi .= ' disabled=true'; if ($req===true) $_opsi .= ' required=true'; if ($multi===true) $_opsi .= ' multiple'; $tout = ''; $this->__aform[$field] = [ 'label' => $label, 'help' => $help, 'form' => $tout ]; return $tout; } public function option_db(string $label, string $field, mixed $value=null, bool $enable=true, bool $req=false, ?string $table=null, ?array $afield=null, ?string $where=null, ?array $aarray=null, ?string $help=null, ?string $change=null, bool $multi=false): string { $_twhere = ''; if (!is_array($afield)) $afield = ["ID", "Nama", "Label"]; if (!empty($where)) { $_twhere = ' WHERE '.$where; $_twhere .= (stripos($where,"ORDER BY")===false) ? ' ORDER BY '.$afield[1] : ''; } $ahasil = []; if (is_array($aarray)) $ahasil[$aarray[0]] = (!isset($afield[2])) ? $aarray[1] : [$aarray[1], $aarray[2]]; if (!empty($table)) { $aopsi = $this->db->fetchAll("SELECT ".implode(",", $afield)." FROM ".$table.$_twhere); foreach ($aopsi as $opsi) { $ahasil[$opsi[$afield[0]]] = (!isset($afield[2])) ? $opsi[$afield[1]] : [$opsi[$afield[1]], $opsi[$afield[2]]]; } } return $this->option($label, $field, $value, $enable, $req, $ahasil, $help, $change, $multi); } public function option_que(string $label, string $field, mixed $value=null, bool $enable=true, bool $req=false, ?string $query=null, ?array $afield=null, ?array $aarray=null, ?string $help=null, ?string $change=null): string { if (!is_array($afield)) $afield = ["ID", "Nama", "Label"]; $ahasil = []; if (is_array($aarray)) $ahasil[$aarray[0]] = (!isset($afield[2])) ? $aarray[1] : [$aarray[1], $aarray[2]]; if (!empty($query)) { $stat = $this->db->runQuery($query); while ($row = $stat->fetch()) { $ahasil[$row[$afield[0]]] = (!isset($afield[2])) ? $row[$afield[1]] : [$row[$afield[1]], $row[$afield[2]]]; } } return $this->option($label, $field, $value, $enable, $req, $ahasil, $help, $change); } public function option_jkl(string $label, string $field, mixed $value=null, bool $enable=true, bool $req=false, ?string $help=null, ?string $change=null): string { return $this->option($label, $field, $value, $enable, $req, ["L" => "Laki-Laki", "P" => "Perempuan"], $help, $change); } public function option_ulevel(string $label, string $field, int $value=9, bool $enable=true, bool $req=false, ?string $help=null, ?string $change=null): string { return $this->option($label, $field, $value, $enable, $req, $this->session->get('WEB_APP')['USER_LEVEL'], $help, $change); } public function radio(string $label, string $field, mixed $value=null, bool $enable=true, bool $req=false, array $arr=[], ?string $help=null, ?string $change=null): string { $_val = array_keys($arr); if (is_null($value)) $value = $_val[0]; $_opsi = ''; if (!empty($change)) $_opsi .= ' ochan="'.rtrim($change, '();').'"'; if ($enable===false) $_opsi .= ' disabled=true'; if ($req===true) $_opsi .= ' required=true'; $tout = ''; if (count($arr)>0) { foreach ($arr as $key => $val) { $tout .= '
'.$val.'
'; } } $this->__aform[$field] = [ 'label' => $label, 'help' => $help, 'form' => $tout ]; return $tout; } public function radio_jkl(string $label, string $field, ?string $value=null, bool $enable=true, bool $req=false, ?string $help=null, ?string $change=null): string { return $this->radio($label, $field, $value, $enable, $req, ["L" => "Laki-Laki", "P" => "Perempuan"], $help, $change); } public function radio_gd(string $label, string $field, ?string $value=null, bool $enable=true, bool $req=false, ?string $help=null, ?string $change=null): string { if (!empty($value)) $value = 'X'; return $this->radio($label, $field, $value, $enable, $req, ["A"=>"A", "B"=>"B", "AB"=>"AB", "O"=>"O", "X"=>"NONE"], $help, $change); } public function checkbox(string $label, string $field, mixed $value=null, bool $enable=true, bool $req=false, array $arr=[], ?string $help=null, ?string $change=null): string { $_val = array_values($arr); if (is_null($value)) $value = $_val[0]; $_opsi = ' odata="'.htmlspecialchars(json_encode($arr)).'"'; if (!empty($change)) $_opsi .= ' ochan="'.rtrim($change, '();').'"'; if ($enable===false) $_opsi .= ' disabled=true'; if ($req===true) $_opsi .= ' required=true'; $tout = '
__aform[$field] = [ 'label' => $label, 'help' => $help, 'form' => $tout]; return $tout; } public function switch(string $label, string $field, mixed $value=null, bool $enable=true, bool $req=false, array $arr=[], ?string $help=null, ?string $change=null): string { $_val = array_values($arr); if (is_null($value)) $value = $_val[0]; $_opsi = ' odata="'.htmlspecialchars(json_encode($arr)).'"'; if (!empty($change)) $_opsi .= ' ochan="'.rtrim($change, '();').'"'; if ($enable===false) $_opsi .= ' disabled=true'; if ($req===true) $_opsi .= ' required=true'; $tout = '
__aform[$field] = [ 'label' => $label, 'help' => $help, 'form' => $tout ]; return $tout; } public function textbox(string $label, string $field, mixed $value=null, bool $enable=true, bool $req=false, string $type="text", ?string $holdtxt=null, ?int $maxsize=null, ?string $help=null, ?string $keyup=null, ?string $blur=null): string { $_opsi = ''; if (!empty($keyup)) $_opsi .= ' okyup="'.rtrim($keyup, '();').'"'; if (!empty($blur)) $_opsi .= ' oblur="'.rtrim($blur, '();').'"'; if (!empty($holdtxt)) $_opsi .= ' placeholder="'.$holdtxt.'"'; if (!empty($maxsize)) $_opsi .= ' maxlength="'.$maxsize.'"'; if ($enable===false) $_opsi .= ' readonly'; if ($req===true) $_opsi .= ' required=true'; $_tclass = ''; if (in_array($type, ['pickdate','picktime','integer','angka','double'])) { $_tclass = ' '.$type; if ($type === 'double') $value = uang2($value); $type = "text"; } $tout = ''; $this->__aform[$field] = [ 'label' => $label, 'help' => $help, 'form' => $tout ]; return $tout; } public function textauto(string $label, string $field, array $avalue, bool $enable=true, bool $req=false, ?string $holdtxt=null, ?int $maxsize=null, ?string $help=null): string { $_opsi = ''; if (count($avalue) === 0) $avalue = [ 'value' => 0, 'text' => "" ]; if (!empty($holdtxt)) $_opsi .= ' placeholder="'.$holdtxt.'"'; if (!empty($maxsize)) $_opsi .= ' maxlength="'.$maxsize.'"'; if ($enable===false) $_opsi .= ' disabled=true'; if ($req===true) $_opsi .= ' required=true'; $tout = ''; $this->__aform[$field] = [ 'label' => $label, 'help' => $help, 'form' => $tout]; return $tout; } public function datebox(string $label, string $field, ?string $value=null, bool $enable=true, bool $req=false, ?string $help=null, ?string $keyup=null, ?string $blur=null): string { if (!empty($value)) $value = tgl_sql2str($value); $tout = '
'; $tout .= $this->textbox($label, $field, $value, $enable, $req, "pickdate", "dd-mm-yyyy", null, $help, $keyup, $blur); $tout .= '
'; $this->__aform[$field] = [ 'label' => $label, 'help' => $help, 'form' => $tout]; return $tout; } public function timebox(string $label, string $field, ?string $value=null, bool $enable=true, bool $req=false, ?string $help=null, ?string $keyup=null, ?string $blur=null): string { return $this->textbox($label, $field, $value, $enable, $req, "picktime", "hh:mm:ss", 10, $help, $keyup, $blur); } public function textarea(string $label, string $field, ?string $value=null, bool $enable=true, bool $req=false, int $rows=1, ?string $holdtxt=null, ?string $help=null, ?string $keyup=null, ?string $blur=null): string { $_opsi = ''; if (!empty($keyup)) $_opsi .= ' okyup="'.rtrim($keyup, '();').'"'; if (!empty($blur)) $_opsi .= ' oblur="'.rtrim($blur, '();').'"'; if (!empty($holdtxt)) $_opsi .= ' placeholder="'.$holdtxt.'"'; if ($enable===false) $_opsi .= ' disabled=true'; if ($req===true) $_opsi .= ' required=true'; $tout = ''; $this->__aform[$field] = [ 'label' => $label, 'help' => $help, 'form' => $tout]; return $tout; } public function plaintext(string $label, string $field, mixed $value=null): string { $tout = ''; $this->__aform[$field] = ['label' => $label, 'help' => "", 'form' => $tout]; return $tout; } public function password(string $label, string $field, ?string $value=null, bool $enable=true, bool $req=false, ?string $holdtxt=null, ?int $maxsize=null, ?string $help=null, ?string $keyup=null): string { return $this->textbox($label, $field, $value, $enable, $req, "password", $holdtxt, $maxsize, $help, $keyup, null); } public function color(string $label, string $field, ?string $value=null, bool $enable=true, bool $req=false, ?string $help=null, ?string $change=null): string { $_opsi = ''; if (!empty($change)) $_opsi .= ' ochan="'.rtrim($change, '();').'"'; if ($enable===false) $_opsi .= ' disabled=true'; if ($req===true) $_opsi .= ' required=true'; $tout = ''; $this->__aform[$field] = ['label' => $label, 'help' => $help, 'form' => $tout]; return $tout; } public function hidden(string $field, mixed $value=null, string $class="text"): string { $tout = ''; $this->__ahide[$field] = $tout; return $tout; } public function clear(): void { $this->__aform = []; } public function show(int $lebar_kolom = 4): string { $col_left = 'col-sm-' . ($lebar_kolom+2) . ' col-lg-' . $lebar_kolom; $col_right = 'col-sm-' . (10-$lebar_kolom) . ' col-lg-' . (12-$lebar_kolom); $tres = ''; foreach ($this->__aform as $_key => $_val) { $tres .= sprintf('
%s %s
', $col_left, $_key, $_val['label'], $col_right.(str_contains($_val['form'],'type="radio"')?' d-flex gap-2':''), $_val['form'], (empty($_val['help'])?'':''.$_val['help'].'')); } foreach ($this->__ahide as $_key => $_val) { $tres .= $_val; } return $tres; } } ?>