* * @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 .= '