Webgui: filters and pagination
This commit is contained in:
+154
-92
@@ -11,8 +11,8 @@ $pagetitle = _('Equipment');
|
||||
|
||||
$id = gpc_get_int($_REQUEST, 'id', 0);
|
||||
|
||||
$opt_supplier = db_get_opt_supp(array(-1 => _('unknown')));
|
||||
$opt_manufacturer = db_get_opt_manuf(array(-1 => _('unknown')));
|
||||
$opt_supplier = db_get_opt_supp(array(-1 => _('--- unknown ---')));
|
||||
$opt_manufacturer = db_get_opt_manuf(array(-1 => _('--- unknown ---')));
|
||||
$opt_ecat = db_get_options(4); // Equipment categories
|
||||
|
||||
// ========== ACTIONS START ===================================================
|
||||
@@ -26,72 +26,47 @@ switch ($submit = form_get_action()) {
|
||||
case 'edit': $action = ACT_EDIT; break;
|
||||
case 'del': $action = ACT_DELETE; break;
|
||||
|
||||
case 'insert':
|
||||
$ename = gpc_get_string($_POST, 'ename');
|
||||
$ecat = gpc_get_int($_POST, 'category');
|
||||
$manufacturer = gpc_get_int($_POST, 'manufacturer');
|
||||
$model = gpc_get_string($_POST, 'model');
|
||||
$remarks = gpc_get_string($_POST, 'remarks', 150);
|
||||
$sql = "INSERT INTO equipment "
|
||||
. " (vid, ename, manufacturer, model, ecat, remarks) "
|
||||
. "VALUES "
|
||||
. " (:vid, :ename, :manufacturer, :model, :ecat, :remarks)";
|
||||
case 'filter':
|
||||
// TODO save new filter settings
|
||||
$flt = array();
|
||||
$flt['cat'] = gpc_get_int($_POST, 'flt_category');
|
||||
$flt['manuf'] = gpc_get_int($_POST, 'flt_manuf');
|
||||
$flt['supp'] = gpc_get_int($_POST, 'flt_supp');
|
||||
$flt['txt'] = gpc_get_string($_POST, 'flt_txt');
|
||||
$sql = "UPDATE settings SET valstr=? WHERE userid=? AND sno=200";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->bindValue(':vid', $user->vid, PDO::PARAM_INT);
|
||||
$sth->bindValue(':ename', $ename, PDO::PARAM_STR);
|
||||
$sth->bindValue(':model', $model, PDO::PARAM_STR);
|
||||
$sth->bindValue(':ecat', $ecat, PDO::PARAM_INT);
|
||||
$sth->bindValue(':manufacturer', $manufacturer, PDO::PARAM_INT);
|
||||
$sth->bindValue(':remarks', $remarks, PDO::PARAM_STR);
|
||||
try {
|
||||
$sth->execute();
|
||||
} catch (PDOException $e) {
|
||||
$g_error->Add('SQL-Error: '. $e->getMessage());
|
||||
}
|
||||
$id = $pdo->lastInsertId();
|
||||
$sth->execute([json_encode($flt), $user->id]);
|
||||
// Only warning in case of failure
|
||||
$action = ACT_DEFAULT;
|
||||
break;
|
||||
|
||||
case 'insert':
|
||||
$p[':vid'] = $user->vid;
|
||||
$p[':ename'] = gpc_get_string($_POST, 'ename');
|
||||
$p[':ecat'] = gpc_get_int($_POST, 'category');
|
||||
$p[':manufacturer'] = gpc_get_int($_POST, 'manufacturer');
|
||||
$p[':model'] = gpc_get_string($_POST, 'model');
|
||||
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
|
||||
$id = db_exec_insert('equipment', $p);
|
||||
$action = ACT_VIEW;
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
$ename = gpc_get_string($_POST, 'ename');
|
||||
$model = gpc_get_string($_POST, 'model');
|
||||
$serial = gpc_get_string($_POST, 'serial');
|
||||
$weight = min(gpc_get_float($_POST, 'weight'), 999.99); // limit max value
|
||||
$price = gpc_get_currency($_POST, 'price');
|
||||
$purchdate = gpc_get_date($_POST, 'purchdate');
|
||||
$supplier = gpc_get_int($_POST, 'supplier');
|
||||
$manufacturer = gpc_get_int($_POST, 'manufacturer');
|
||||
$ecat = gpc_get_int($_POST, 'category');
|
||||
$remarks = gpc_get_string($_POST, 'remarks', 150);
|
||||
$sql = "UPDATE equipment "
|
||||
. "SET ename=:ename,"
|
||||
. " model=:model,"
|
||||
. " serial=:serial,"
|
||||
. " weight=:weight,"
|
||||
. " price=:price,"
|
||||
. " purchdate=:purchdate,"
|
||||
. " supplier=:supplier,"
|
||||
. " manufacturer=:manufacturer,"
|
||||
. " ecat=:ecat,"
|
||||
. " remarks=:remarks "
|
||||
. "WHERE eid=:eid";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->bindValue(':eid', $id, PDO::PARAM_INT);
|
||||
$sth->bindValue(':ename', $ename, PDO::PARAM_STR);
|
||||
$sth->bindValue(':model', $model, PDO::PARAM_STR);
|
||||
$sth->bindValue(':serial', $serial, PDO::PARAM_STR);
|
||||
$sth->bindValue(':weight', $weight);
|
||||
$sth->bindValue(':price', $price);
|
||||
$sth->bindValue(':purchdate', $purchdate, PDO::PARAM_STR);
|
||||
$sth->bindValue(':supplier', $supplier, PDO::PARAM_INT);
|
||||
$sth->bindValue(':manufacturer', $manufacturer, PDO::PARAM_INT);
|
||||
$sth->bindValue(':ecat', $ecat, PDO::PARAM_INT);
|
||||
$sth->bindValue(':remarks', $remarks, PDO::PARAM_STR);
|
||||
try {
|
||||
$sth->execute();
|
||||
} catch (PDOException $e) {
|
||||
$g_error->Add('SQL-Error: '. $e->getMessage());
|
||||
}
|
||||
$p[':eid'] = $id;
|
||||
$p[':ename'] = gpc_get_string($_POST, 'ename');
|
||||
$p[':model'] = gpc_get_string($_POST, 'model');
|
||||
$p[':serial'] = gpc_get_string($_POST, 'serial');
|
||||
$p[':weight'] = min(gpc_get_float($_POST, 'weight'), 999.99); // limit max value
|
||||
$p[':price'] = gpc_get_currency($_POST, 'price');
|
||||
$p[':purchdate'] = gpc_get_date($_POST, 'purchdate');
|
||||
$p[':supplier'] = gpc_get_int($_POST, 'supplier');
|
||||
if ($p[':supplier'] <= 0) $p[':supplier'] = NULL;
|
||||
$p[':manufacturer'] = gpc_get_int($_POST, 'manufacturer');
|
||||
if ($p[':manufacturer'] <= 0) $p[':manufacturer'] = NULL;
|
||||
$p[':ecat'] = gpc_get_int($_POST, 'category');
|
||||
if ($p[':ecat'] <= 0) $p[':ecat'] = NULL;
|
||||
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
|
||||
db_exec_update('equipment', $p, 'eid');
|
||||
$action = ACT_VIEW;
|
||||
break;
|
||||
|
||||
@@ -208,24 +183,80 @@ require 'header.php';
|
||||
if ($action == ACT_DEFAULT):
|
||||
// ========== VARIANT: default behavior =======================================
|
||||
|
||||
// load filter from db
|
||||
$flt = db_get_filter($user->id, 200);
|
||||
/*$flt = new stdClass();
|
||||
$sth = $pdo->prepare("SELECT valstr FROM settings WHERE userid=? AND sno=200");
|
||||
try {
|
||||
$sth->execute([$user->id]);
|
||||
$json = $sth->fetchColumn();
|
||||
if (!$json) {
|
||||
// no settings record. create empty one for later updates
|
||||
$sth = $pdo->prepare("INSERT INTO settings (userid, sno, valstr) VALUES (?, 200, '[]')");
|
||||
$sth->execute([$user->id]);
|
||||
} else {
|
||||
$flt = json_decode($json);
|
||||
}
|
||||
} catch (PDOexception $e) {
|
||||
$g_warning->Add('SQL-Error: '. $e->getMessage());
|
||||
} */
|
||||
|
||||
$sort = array(
|
||||
1 => 'ename',
|
||||
2 => 'ename DESC'
|
||||
);
|
||||
|
||||
$w = array('vid=:vid');
|
||||
$p = array(':vid' => $user->vid);
|
||||
if ($flt->cat == -1) {
|
||||
$w[] = 'ecat IS NULL';
|
||||
} elseif ($flt->cat > 0) {
|
||||
$w[] = 'ecat=:ecat';
|
||||
$p[':ecat'] = $flt->cat;
|
||||
}
|
||||
if ($flt->manuf > 0) {
|
||||
$w[] = 'manufacturer=:manuf';
|
||||
$p[':manuf'] = $flt->manuf;
|
||||
}
|
||||
if ($flt->supp > 0) {
|
||||
$w[] = 'supplier=:supp';
|
||||
$p[':supp'] = $flt->supp;
|
||||
}
|
||||
if (strlen($flt->txt) > 1) {
|
||||
$w[] = '(ename LIKE :txt OR model LIKE :txt OR remarks LIK :txt';
|
||||
$p[':txt'] = '%'.$flt->txt.'%';
|
||||
}
|
||||
$where = join(' AND ', $w);
|
||||
$order = ' ORDER BY ename';
|
||||
|
||||
// get total recond count for pagination and limit
|
||||
$sth = $pdo->prepare("SELECT COUNT(*) FROM equipment WHERE " . $where);
|
||||
$sth->execute($p);
|
||||
$numrows = $sth->fetchColumn();
|
||||
$lastpage = ceil($numrows/$g_rows_pp);
|
||||
$page = gpc_get_int($_REQUEST, 'p', 1);
|
||||
|
||||
$sql = "SELECT eid, ename, manufacturer, model, serial, remarks,"
|
||||
. " TIMESTAMPDIFF(MONTH, purchdate, NOW()) AS age_mon "
|
||||
. "FROM equipment WHERE vid=? "
|
||||
. "ORDER BY eid";
|
||||
. "FROM equipment";
|
||||
$sql .= ' WHERE ' . $where;
|
||||
$sql .= $order;
|
||||
|
||||
// if pagination:
|
||||
$sql .= ' LIMIT ' . ($page - 1) * $g_rows_pp . ',' . $g_rows_pp;
|
||||
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute([$user->vid]);
|
||||
$sth->execute($p);
|
||||
$res = $sth->fetchAll();
|
||||
|
||||
echo "<h1>$pagetitle</h1>\n";
|
||||
|
||||
// Filter
|
||||
$opt_special = array(
|
||||
-2 => _('― all ―'),
|
||||
-1 => _('― none ―')
|
||||
);
|
||||
?>
|
||||
|
||||
<form method="post" action="<?=$g_scriptname?>">
|
||||
<div id="filter" class="card">
|
||||
<div class="card-header">Filter</div>
|
||||
@@ -233,35 +264,61 @@ echo "<h1>$pagetitle</h1>\n";
|
||||
<label for="flt_category">Category</label>
|
||||
<select id="flt_category" name="flt_category">
|
||||
<?php
|
||||
foreach ($opt_ecat as $k => $v) {
|
||||
echo '<option value="', $k,'">', $v, '</option>';
|
||||
foreach ($opt_special + $opt_ecat as $k => $v) {
|
||||
echo '<option value="', $k, '"';
|
||||
if ($k == $flt->cat) {
|
||||
echo ' selected';
|
||||
}
|
||||
echo '>', $v, '</option>';
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
<label for="flt_manuf">Manufacturer</label>
|
||||
<select id="flt_category" name="flt_category">
|
||||
<select id="flt_manuf" name="flt_manuf">
|
||||
<?php
|
||||
foreach ($opt_manufacturer as $k => $v) {
|
||||
echo '<option value="', $k,'">', $v, '</option>';
|
||||
foreach ($opt_special + $opt_manufacturer as $k => $v) {
|
||||
echo '<option value="', $k,'"';
|
||||
if ($k == $flt->manuf) {
|
||||
echo ' selected';
|
||||
}
|
||||
echo '>', $v, '</option>';
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
<button class="btn btn-sm btn-primary" title="<?=_('Apply')?>">Go</button>
|
||||
<label for="flt_supp">Supplier</label>
|
||||
<select id="flt_supp" name="flt_supp">
|
||||
<?php
|
||||
foreach ($opt_special + $opt_supplier as $k => $v) {
|
||||
echo '<option value="', $k,'"';
|
||||
if ($k == $flt->supp) {
|
||||
echo ' selected';
|
||||
}
|
||||
echo '>', $v, '</option>';
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
<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>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php
|
||||
// Pagination on top
|
||||
$pagination = get_pagination($g_scriptname, $page, $lastpage);
|
||||
echo $pagination;
|
||||
|
||||
// Data
|
||||
echo '<table class="table table-striped">', "\n";
|
||||
echo "<thead>\n";
|
||||
echo "<tr>";
|
||||
echo "<th>" . _('Description') . "</th>";
|
||||
echo "<th>" . _('Manufacturer') . "</th>";
|
||||
echo "<th>" . _('Model') . "</th>";
|
||||
echo "<th>" . _('Serial') . "</th>";
|
||||
echo "<th>" . _('Age') . "</th>";
|
||||
echo "<th>" . _('Remarks') . "</th>";
|
||||
echo '<tr>';
|
||||
echo '<th>', _('Description'), "</th>";
|
||||
echo '<th>', _('Manufacturer'), "</th>";
|
||||
echo '<th>', _('Model'), "</th>";
|
||||
echo '<th>', _('Serial'), "</th>";
|
||||
echo '<th>', _('Age'), "</th>";
|
||||
echo '<th>', _('Remarks'), "</th>";
|
||||
echo "<th> </th>";
|
||||
echo "</tr>\n";
|
||||
echo "</thead>\n";
|
||||
@@ -275,20 +332,22 @@ foreach ($res as $row) {
|
||||
$age = sprintf(_('%dy'), intdiv($row['age_mon'], 12));
|
||||
}
|
||||
echo "<tr>\n";
|
||||
echo "<td>". $row['ename'] ."</td>\n";
|
||||
echo "<td>". $opt_manufacturer[$row['manufacturer']] ."</td>\n";
|
||||
echo "<td>". $row['model'] ."</td>\n";
|
||||
echo "<td>". $row['serial'] ."</td>\n";
|
||||
echo "<td>". $age ."</td>\n";
|
||||
echo "<td>". $row['remarks'] ."</td>\n";
|
||||
echo "<td>", $row['ename'], "</td>\n";
|
||||
echo "<td>", ($opt_manufacturer[$row['manufacturer']] ?? ''), "</td>\n";
|
||||
echo "<td>", $row['model'], "</td>\n";
|
||||
echo "<td>", $row['serial'], "</td>\n";
|
||||
echo "<td>", $age, "</td>\n";
|
||||
echo "<td>", $row['remarks'], "</td>\n";
|
||||
// Action buttons
|
||||
form_action_buttons($g_scriptname, $row['eid']);
|
||||
}
|
||||
?>
|
||||
<tfoot>
|
||||
<tr><td colspan="6"><?php printf(_('%d records'), count($res)); ?></td></tr>
|
||||
<tr><td colspan="6"><?php printf(_('%d records of %d'), count($res), $numrows); ?></td></tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
<?php
|
||||
/*
|
||||
<nav>
|
||||
<ul class="pagination justify-content-center">
|
||||
<li class="page-item"><a class="page-link" href="#">Previous</a></li>
|
||||
@@ -296,13 +355,16 @@ foreach ($res as $row) {
|
||||
<li class="page-item"><a class="page-link" href="#">2</a></li>
|
||||
<li class="page-item"><a class="page-link" href="#">Next</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
<?php
|
||||
</nav> */
|
||||
echo $pagination;
|
||||
|
||||
form_add_button($g_scriptname);
|
||||
|
||||
elseif ($action == ACT_ADD):
|
||||
// ========== VARIANT: add record =============================================
|
||||
|
||||
// filter used to preselect fields
|
||||
$flt = db_get_filter($user->id, 200);
|
||||
?>
|
||||
<h2><?=_('Add Equipment')?></h2>
|
||||
<form method="post" action="<?=$g_scriptname?>">
|
||||
@@ -311,8 +373,8 @@ elseif ($action == ACT_ADD):
|
||||
<input type="text" class="form-control" id="ename" name="ename">
|
||||
</div>
|
||||
<?php
|
||||
form_create_select('category', _('Category'), $opt_ecat);
|
||||
form_create_select('manufacturer', _('Manufacturer'), $opt_manufacturer);
|
||||
form_create_select('category', _('Category'), [-1 => '--- none ---'] + $opt_ecat, $flt->cat);
|
||||
form_create_select('manufacturer', _('Manufacturer'), $opt_manufacturer, $flt->manuf);
|
||||
?>
|
||||
<div class="mb-3">
|
||||
<label for="model" class="form-label"><?=_('Model')?></label>
|
||||
@@ -446,7 +508,7 @@ $equipment = $sth->fetch(PDO::FETCH_OBJ);
|
||||
</div>
|
||||
<?php
|
||||
form_create_select('supplier', _('Supplier'), $opt_supplier, $equipment->supplier);
|
||||
form_create_select('category', _('Category'), $opt_ecat, $equipment->ecat);
|
||||
form_create_select('category', _('Category'), [-1 => _('--- unknown ---')] + $opt_ecat, $equipment->ecat);
|
||||
?>
|
||||
<div class="mb-3">
|
||||
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
|
||||
|
||||
Reference in New Issue
Block a user