Webgui: filter and pagination working

This commit is contained in:
2026-07-15 19:42:55 +02:00
parent 2177834b39
commit a183a4725f
33 changed files with 2088 additions and 750 deletions
+71 -36
View File
@@ -9,20 +9,23 @@
require 'globals.inc';
require 'lib/gpc.inc';
// TODO move to somewhere else
function format_vals($n, $v1, $v2, $v3) {
if ($n == 1) {
return $v1;
} elseif ($n == 2) {
return $v1 . 'x'. $v2;
} else {
return $v1 . 'x'. $v2 . 'x' . $v3;
}
}
$pagetitle = _('Measurements');
$id = gpc_get_int($_REQUEST, 'id', 0);
$opt_mtype = db_load_enum('measurement', 'mtype', true, false);
$opt_unit = db_get_options(8);
// TODO use enum function
$opt_accuracy = array(
'unknown' => _('Unknown'),
'precise' => _('Precise'),
'normal' => _('Normal'),
'rough' => _('Rough'),
'estimated' => _('Estimated')
);
$opt_accuracy = db_load_enum('measurement', 'accuracy', true, true);
$opt_dimensions = array(
1 => _('1D'),
2 => _('2D'),
@@ -54,9 +57,13 @@ switch ($submit = form_get_action()) {
$action = ACT_DEFAULT;
break;
case 'freset':
db_clear_filter(207);
$action = ACT_DEFAULT;
break;
case 'insert':
$p[':mname'] = gpc_get_string($_POST, 'mname');
$p[':mtype'] = gpc_get_string($_POST, 'mtype');
$p[':note'] = gpc_get_string($_POST, 'note');
$p[':nval'] = gpc_get_int($_POST, 'nval'); // 1 to 3 dimensions
$p[':val1'] = gpc_get_int($_POST, 'val1');
@@ -81,7 +88,6 @@ switch ($submit = form_get_action()) {
case 'update':
$p[':mid'] = $id;
$p[':mname'] = gpc_get_string($_POST, 'mname');
$p[':mtype'] = gpc_get_string($_POST, 'mtype');
$p[':nval'] = gpc_get_int($_POST, 'nval'); // 1 to 3 dimensions
$p[':val1'] = gpc_get_int($_POST, 'val1');
$p[':val2'] = NULL;
@@ -130,20 +136,37 @@ require 'header.php';
if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================
page_caption_search($pagetitle);
// load filter from db
$flt = db_get_filter($user->id, 207);
$flt = db_get_filter($user->id, 207, ['txt']);
function format_vals($n, $v1, $v2, $v3) {
if ($n == 1) {
return $v1;
} elseif ($n == 2) {
return $v1 . 'x'. $v2;
} else {
return $v1 . 'x'. $v2 . 'x' . $v3;
}
$w = array('(vid=:vid OR vid IS NULL)');
$p = array(':vid' => $user->vid);
// TODO precision additionally as filter
if (strlen($flt->txt) > 1) {
$w[] = 'mname LIKE :txt';
$p[':txt'] = '%'.$flt->txt.'%';
}
$where = join(' AND ', $w);
$order = ' ORDER BY mname';
echo "<h1>$pagetitle</h1>\n";
$sql = "SELECT COUNT(*) FROM measurement WHERE " . $where;
$sth = $pdo->prepare($sql);
try {
$sth->execute($p);
} catch(PDOException $e) {
$g_error->Add($e->getMessage());
$g_error->Add($sql);
$g_error->Add(print_r($p, true));
$g_error->PrintOut();
}
$numrows = $sth->fetchColumn();
$page = gpc_get_int($_REQUEST, 'p', 1);
$rows_pp = gpc_get_int($_REQUEST, 'n', $user->rows_pp);
$lastpage = ceil($numrows/$rows_pp);
// Filter
$opt_special = array(
@@ -154,23 +177,36 @@ $opt_special = array(
<form method="post" action="<?=$g_scriptname?>">
<div id="filter" class="card">
<div class="card-header"><i class="bi bi-funnel me-2"></i>Filter</div>
<div class="card-body">
<?php
filter_create_select('flt_mtype', _('Type'), $g_opt_all + $g_opt_none + $opt_mtype);
?>
<label for="flt_txt">Text</label>
<input type="text" name="flt_txt" size="15" maxlength="30" value="<?=$flt->txt?>">
<button name="submit[filter]" class="btn btn-sm btn-primary" title="<?=_('Apply')?>"><?=_('Go')?></button>
<div class="card-body d-flex flex-wrap gap-3">
<div class="d-flex flex-nowrap gap-2">
<label for="flt_txt">Text</label>
<input type="text" class="form-control" name="flt_txt" size="15" maxlength="30" value="<?=$flt->txt ?>">
</div>
</div>
<div class="card-footer">
<button name="submit[filter]" class="btn btn-sm btn-primary" title="<?=_('Apply')?>"><?=_('Go')?></button>
<button name="submit[freset]" class="btn btn-sm btn-secondary float-end" title="<?=_('Reset filter to defaults')?>"><?=_('Clear')?></button>
</div>
</div>
</form>
<?php
$sql = "SELECT mid, mname, nval, val1, val2, val3, unit, accuracy, mdate, note, eid "
. "FROM measurement "
. "ORDER BY mname";
$sth = $pdo->query($sql);
. "FROM measurement ";
$sql .= ' WHERE ' . $where;
$sql .= $order;
// if pagination:
$sql .= ' LIMIT ' . ($page - 1) * $rows_pp . ',' . $rows_pp;
$sth = $pdo->prepare($sql);
$sth->execute($p);
$res = $sth->fetchAll();
// Pagination on top
$pagination = get_pagination($g_scriptname, '', $page, $lastpage);
echo $pagination;
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>\n";
@@ -206,6 +242,8 @@ echo '<tr><td colspan="6">', sprintf(_('%d records'), count($res)), '</td></tr>'
echo "</tfoot>\n";
echo "</table>\n";
echo $pagination;
form_add_button($g_scriptname);
elseif ($action == ACT_ADD):
@@ -224,7 +262,6 @@ echo '<h2>', _('Add measurement'), "</h2>\n";
<input type="text" class="form-control" id="note" name="note">
</div>
<?php
form_create_select('mtype', _('Type'), $opt_mtype);
form_create_select('nval', _('Dimensions'), $opt_dimensions);
?>
<div class="mb-3">
@@ -254,7 +291,7 @@ form_create_select('eid', _('Equipment'), $opt_equipment, $id);
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
$sql = "SELECT mname, mtype, unit, nval, val1, val2, val3, accuracy, mdate, note, eid "
$sql = "SELECT mname, unit, nval, val1, val2, val3, accuracy, mdate, note, eid "
. "FROM measurement "
. "WHERE mid=?";
$sth = $pdo->prepare($sql);
@@ -265,7 +302,6 @@ echo '<h2>', _('Measurement'), "</h2>\n";
echo '<table class="table">', "\n";
echo '<tr><th style="width:20%">', _('Name'),"</th><td>", $measurement->mname, "</td></tr>\n";
echo '<tr><th>', _('Type'),"</th><td>", $opt_mtype[$measurement->mtype], "</td></tr>\n";
echo '<tr><th>', _('Unit'),"</th><td>", $opt_unit[$measurement->unit], "</td></tr>\n";
echo '<tr><th>', _('Values'),"</th><td>$measurement->nval</td></tr>\n";
echo '<tr><th>', _('Value 1'),"</th><td>$measurement->val1</td></tr>\n";
@@ -283,7 +319,7 @@ form_view_buttons($g_scriptname, $id);
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT mname, mtype, nval, val1, val2, val3, unit, accuracy, note, eid "
$sql = "SELECT mname, nval, val1, val2, val3, unit, accuracy, note, eid "
. "FROM measurement "
. "WHERE mid=?";
$sth = $pdo->prepare($sql);
@@ -303,7 +339,6 @@ echo '<h2>', _('Edit measurement'), "</h2>\n";
<input type="text" class="form-control" id="note" name="note" value="<?=$measurement->note ?>">
</div>
<?php
form_create_select('mtype', _('Type'), $opt_mtype, $measurement->mtype);
form_create_select('nval', _('Dimensions'), $opt_dimensions, $measurement->nval);
?>
<div class="mb-3">