More work an webgui

This commit is contained in:
2024-04-10 20:13:37 +02:00
parent 4b5ba85a77
commit aaa452cbb5
20 changed files with 842 additions and 359 deletions
+121 -3
View File
@@ -280,8 +280,78 @@ function get_userlist() {
// ========== DATABASE FUNCTIONS ==============================================
function db_load_enum($table, $column) {
function db_exec_insert($table, $params) {
// $params must have keys corresponding to table fields prefixed with ':'
// e.g. $param[':field']
global $pdo;
global $g_error;
$keys = array_keys($params);
$fields = join(',', array_map(function($s) { return ltrim($s, ':'); }, $keys));
$pnames = join(',', $keys);
$sth = $pdo->prepare("INSERT INTO $table ($fields) VALUES ($pnames)");
try {
$sth->execute($params);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
return $pdo->LastInsertId();
}
function db_exec_update($table, $params, $pk) {
global $pdo;
global $g_error;
$fields = join(',', array_map(function($s) { return ltrim($s, ':') . '=' . $s; }, array_keys($params)));
$sth = $pdo->prepare("UPDATE $table SET $fields WHERE $pk=:$pk");
try {
$sth->execute($params);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
return false;
}
return true;
}
function get_enum($enum) {
// Translate and beautify enums
$lookup = array(
'bad' => _('Bad'),
'box' => _('Box'),
'cat' => _('Catamaran'),
'closed' => _('Closed'),
'defect' => _('Defect'),
'done' => _('Done'),
'drawing' => _('Drawng'),
'equipment' => _('Equipment'),
'estimated' => _('Estimated'),
'finished' => _('Finished'),
'fixed' => _('Fixed'),
'generic' => _('Generic'),
'good' => _('Good'),
'invoice' => _('Invoice'),
'locked' => _('Locked'),
'manual' => _('Manual'),
'mono' => _('Monoholl'),
'new' => _('New'),
'normal' => _('Normal'),
'ongoing' => _('Ongoing'),
'open' => _('Open'),
'picture' => _('Picture'),
'planned' => _('Planned'),
'precise' => _('Precise'),
'rough' => _('Rough'),
'storage' => _('Storage'),
'task' => _('Task'),
'tri' => _('Trimaran'),
'unknown' => _('Unknown'),
'vessel' => _('Vessel'),
);
return $lookup[$enum] ?? $enum;
}
function db_load_enum($table, $column, $lookup=false, $as_dict=false) {
// returns array of enum-values as defined in database
// if lookup is true the name lookup and translaton is applied
// if as_dict is true the enum value is returned as the array key
global $pdo;
$sql = "SELECT TRIM(TRAILING ')' FROM SUBSTRING(column_type,6)) "
. "FROM information_schema.columns "
@@ -291,7 +361,20 @@ function db_load_enum($table, $column) {
// for PHP < 7.4
// return array_map(function($x) { return trim($x, "'"); }, explode(',', $sth->fetchColumn()));
// for PHP => 7.4
return array_map(fn($x) => trim($x, "'"), explode(',', $sth->fetchColumn()));
$arr = array_map(fn($x) => trim($x, "'"), explode(',', $sth->fetchColumn()));
if ($lookup) {
if ($as_dict) {
$dict = array();
foreach ($arr as $x) {
$dict[$x] = get_enum($x);
}
return $dict;
} else {
return array_map(fn($x) => get_enum($x), $arr);
}
} else {
return $arr;
}
}
function db_get_ddtext($ddid, $ddval) {
@@ -381,7 +464,25 @@ function db_get_opt_box($vid) {
return $list;
}
function db_get_opt_equip($vid) {
function db_get_opt_equip($vid, $default=NULL) {
global $pdo;
global $g_error;
if (isset($default)) {
$list = $default;
} else {
$list = array();
}
$sql = "SELECT eid, ename FROM equipment WHERE vid=? ORDER BY ename";
$sth = $pdo->prepare($sql);
try {
$sth->execute([$vid]);
} catch(PDOException $e) {
$g_error->Add($e->getMessage());
}
foreach ($sth->fetchAll(PDO::FETCH_NUM) as $rec) {
$list[$rec[0]] = $rec[1];
}
return $list;
}
function db_get_opt_manuf($default=NULL) {
@@ -504,6 +605,13 @@ function form_view_buttons($script, $id) {
echo "</div>\n";
}
function form_new_buttons($script) {
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<button type="submit" name="submit[insert]" class="btn btn-primary">', _('Save'), "</button>\n";
echo '<a href="', $script, '" class="btn btn-secondary">', _('Back'), "</a>\n";
echo "</div>\n";
}
function form_edit_buttons($script, $id) {
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<button type="submit" name="submit[update]" class="btn btn-primary">', _('Save'), "</button>\n";
@@ -660,6 +768,16 @@ function format_currency($val) {
}
}
function format_color($color) {
$colstr = '<span style="';
$style = "padding:2px 4px;border-radius:4px;background-color:#".$color;
if (get_color_brightness($color) < 0.4) {
$style .= ';color:white';
}
$colstr .= $style . '">#' . $color . '</span>' ;
return $colstr;
}
function format_filesize($size, $format=2) {
switch ($format) {
case 1: // short