More webgui work. Filters e.g.

This commit is contained in:
2024-04-19 13:25:56 +02:00
parent 851f7e13a2
commit c3b5ef95a9
20 changed files with 853 additions and 585 deletions
+123 -14
View File
@@ -288,24 +288,50 @@ function db_exec_insert($table, $params) {
$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)");
$sql = "INSERT INTO $table ($fields) VALUES ($pnames)";
// $sth = $pdo->prepare("INSERT INTO $table ($fields) VALUES ($pnames)");
$sth = $pdo->prepare($sql);
try {
$sth->execute($params);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
$g_error->Add($sql);
$g_error->Add(print_r($params, true));
}
return $pdo->LastInsertId();
}
function db_exec_update($table, $params, $pk) {
// TODO primary key with multiple fields: $pk is an array
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");
$sql = "UPDATE $table SET $fields WHERE $pk=:$pk";
$sth = $pdo->prepare($sql);
try {
$sth->execute($params);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
$g_error->Add($sql);
$g_error->Add(print_r($params, true));
return false;
}
return true;
}
function db_save_filter($flt, $sno) {
global $user;
global $pdo;
global $g_warning;
$sql = "UPDATE settings SET valstr=? WHERE userid=? AND sno=?";
$sth = $pdo->prepare($sql);
try {
$sth->execute([json_encode($flt), $user->id, $sno]);
} catch (PDOexception $e) {
$g_warning->Add('SQL-Error: '. $e->getMessage());
$g_warning->Add($sql);
$g_warning->Add(print_r($flt, true));
$g_warning->Add('Setting: ', $sno);
return false;
}
return true;
@@ -320,7 +346,7 @@ function get_enum($enum) {
'closed' => _('Closed'),
'defect' => _('Defect'),
'done' => _('Done'),
'drawing' => _('Drawng'),
'drawing' => _('Drawing'),
'equipment' => _('Equipment'),
'estimated' => _('Estimated'),
'finished' => _('Finished'),
@@ -330,7 +356,7 @@ function get_enum($enum) {
'invoice' => _('Invoice'),
'locked' => _('Locked'),
'manual' => _('Manual'),
'mono' => _('Monoholl'),
'mono' => _('Monohull'),
'new' => _('New'),
'normal' => _('Normal'),
'ongoing' => _('Ongoing'),
@@ -338,6 +364,7 @@ function get_enum($enum) {
'picture' => _('Picture'),
'planned' => _('Planned'),
'precise' => _('Precise'),
'repairable' => _('Repairable'),
'rough' => _('Rough'),
'storage' => _('Storage'),
'task' => _('Task'),
@@ -536,6 +563,27 @@ function db_get_opt_proj($vid, $default=NULL) {
return $list;
}
function db_get_filter($user, $sno) {
global $pdo;
global $g_warning;
$flt = new stdClass();
$sth = $pdo->prepare("SELECT valstr FROM settings WHERE userid=? AND sno=?");
try {
$sth->execute([$user, $sno]);
$json = $sth->fetchColumn();
if (!$json) {
// no settings record. create empty one for later updates
$sth = $pdo->prepare("INSERT INTO settings (userid, sno, valstr) VALUES (?, ?, '{}')");
$sth->execute([$user, $sno]);
} else {
$flt = json_decode($json);
}
} catch (PDOexception $e) {
$g_warning->Add('SQL-Error: '. $e->getMessage());
}
return $flt;
}
function date_to_sql($var) {
if (!isset($var) || $var == '') {
return NULL;
@@ -563,7 +611,6 @@ function float_to_sql($var) {
// ========== FORM FUNCTIONS ==================================================
function form_create_select($fieldname, $label, $optlist, $selval=NULL) {
//if (
echo '<div class="mb-3">', "\n";
echo '<label for="', $fieldname, '">', $label, "</label>\n";
echo '<select class="form-select" name="', $fieldname,'" id="', $fieldname,'">';
@@ -782,29 +829,29 @@ function format_filesize($size, $format=2) {
switch ($format) {
case 1: // short
if ($size < 1024) {
$size_string = sprintf(_('%d&tinysp;B'), $size);
$size_string = sprintf(_('%d&thinsp;B'), $size);
} elseif ($size < 1048576) {
$size_string = sprintf(_('%.1f&tinysp;kB'), $size/1024);
$size_string = sprintf(_('%.1f&thinsp;kB'), $size/1024);
} else {
$size_string = sprintf(_('%.1f&tinysp;MB'), $size/1048576);
$size_string = sprintf(_('%.1f&thinsp;MB'), $size/1048576);
}
break;
case 2: // medium
if ($size < 1024) {
$size_string = sprintf(_('%d&tinysp;Byte'), $size);
$size_string = sprintf(_('%d&thinsp;Byte'), $size);
} elseif ($size < 1048576) {
$size_string = sprintf(_('%.1f&tinysp;kByte'), $size/1024);
$size_string = sprintf(_('%.1f&thinsp;kByte'), $size/1024);
} else {
$size_string = sprintf(_('%.1f&tinysp;MByte'), $size/1048576);
$size_string = sprintf(_('%.1f&thinsp;MByte'), $size/1048576);
}
break;
case 3: // long
if ($size < 1024) {
$size_string = sprintf(_('%d Byte'), $size);
$size_string = sprintf(_('%d&thinsp;Byte'), $size);
} elseif ($size < 1048576) {
$size_string = sprintf(_('approx. %.1f kByte'), $size/1024);
$size_string = sprintf(_('approx. %.1f&thinsp;kByte'), $size/1024);
} else {
$size_string = sprintf(_('approx. %.1f MByte'), $size/1048576);
$size_string = sprintf(_('approx. %.1f&thinsp;MByte'), $size/1048576);
}
break;
default:
@@ -813,6 +860,68 @@ function format_filesize($size, $format=2) {
return $size_string;
}
function get_pagination($script, $page, $lastpage) {
// no direct output, returns string for multiple output
if ($lastpage < 2) {
return '';
}
$out = '<nav class="my-3">' . "\n";
$out .= '<ul class="pagination justify-content-center">' . "\n";
// First Prev ... Next Last
// TODO build entries for first, prev, next last
if ($lastpage < 10) {
if ($page > 1) {
// Go to first page or previous page
$out .= '<li class="page-item"><a class="page-link" href="'. $script. '?p=1">First</a></li>'. "\n";
$out .= '<li class="page-item"><a class="page-link" href="'. $script. '?p='. ($page - 1). '">Previous</a></li>'. "\n";
} else {
$out .= '<li class="page-item disabled"><a class="page-link href="#">First</a></li>'. "\n";
$out .= '<li class="page-item disabled"><a class="page-link href="#">Previous</a></li>'. "\n";
}
for ($n = 1; $n <= $lastpage; $n++) {
$out .='<li class="page-item"><a class="page-link';
if ($n == $page) $out .= ' active';
$out .= '" href="'. $script. '?p='. $n. '">'. $n. '</a></li>'. "\n";
}
if ($page < $lastpage) {
// go to last page or next page
$out .= '<li class="page-item"><a class="page-link" href="'. $script. '?p='. ($page + 1). '">Next</a></li>'. "\n";
$out .= '<li class="page-item"><a class="page-link" href="'. $script. '?p='. $lastpage. '">Last</a></li>'. "\n";
} else {
$out .= '<li class="page-item disabled"><a class="page-link href="#">Next</a></li>'. "\n";
$out .= '<li class="page-item disabled"><a class="page-link href="#">Last</a></li>'. "\n";
}
} else {
// start working with ... dots
}
// idea: small entry to input page number
$out .= "</ul>\n</nav>\n";
return $out;
}
function pdf_get_pagecount($filename) {
$pages = exec("/usr/bin/mutool show $filename Root/Pages/Count", $output, $retval);
// TODO error checking
return $pages;
}
function pdf_get_preview($docid) {
global $g_doc_basepath;
// create file in thumbs if needed
$thumbfile = $g_doc_basepath . '/thumbs/doc-' . $docid . '.jpg';
if (file_exists($thumbfile)) {
//return "<img src=""
// return existing thumbfile
}
}
// ========== MENU FUNCTIONS ==================================================
$g_menu = Array (