Files
YMS/webgui/measurement.php
T
2024-04-09 10:26:32 +02:00

285 lines
9.3 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
require 'globals.inc';
$pagetitle = _('Measurements');
$id = gpc_get_int($_REQUEST, 'id', 0);
$opt_unit = db_get_options(8);
$opt_accuracy = array(
'unknown' => _('Unknown'),
'precise' => _('Precise'),
'normal' => _('Normal'),
'rough' => _('Rough'),
'estimated' => _('Estimated')
);
$opt_dimensions = array(
1 => _('1D'),
2 => _('2D'),
3 => _('3D')
);
$opt_equipment = array(-1 => _('unassigned'));
$sth = $pdo->prepare("SELECT eid, ename FROM equipment WHERE vid=? ORDER BY ename");
$sth->execute([$user->vid]);
foreach ($sth->fetchAll() as $row) {
$opt_equipment[$row['eid']] = $row['ename'];
}
// ========== ACTIONS START ===================================================
switch ($submit = form_get_action()) {
case NULL: break;
case 'add': $action = ACT_ADD; break;
case 'view': $action = ACT_VIEW; break;
case 'edit': $action = ACT_EDIT; break;
case 'del': $action = ACT_DELETE; break;
case 'insert':
$p[':mname'] = gpc_get_string($_POST, 'mname');
$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');
$p[':val2'] = NULL;
$p[':val3'] = NULL;
if ($p[':nval'] > 1) {
$p[':val2'] = gpc_get_int($_POST, 'val2');
}
if ($p[':nval'] > 2) {
$p[':val3'] = gpc_get_int($_POST, 'val3');
}
$p[':unit'] = gpc_get_int($_POST, 'unit');
$p[':accuracy'] = gpc_get_string($_POST, 'accuracy');
$p[':eid'] = gpc_get_int($_POST, 'eid');
if ($p[':eid'] == -1) {
$p[':eid'] = NULL;
}
$keys = array_keys($p);
$fields = join(',', array_map(function($s) { return ltrim($s, ':'); }, $keys));
$pnames = join(',', $keys);
$sth = $pdo->prepare("INSERT INTO measurement ($fields) VALUES ($pnames)");
try {
$sth->execute($p);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
$id = $pdo->LastInsertId();
$action = ACT_VIEW;
break;
case 'update':
$p[':mname'] = gpc_get_string($_POST, 'mname');
$p[':nval'] = gpc_get_int($_POST, 'nval'); // 1 to 3 dimensions
$p[':val1'] = gpc_get_int($_POST, 'val1');
$p[':val2'] = NULL;
$p[':val3'] = NULL;
if ($p[':nval'] > 1) {
$p[':val2'] = gpc_get_int($_POST, 'val2');
}
if ($p[':nval'] > 2) {
$p[':val3'] = gpc_get_int($_POST, 'val3');
}
$p[':unit'] = gpc_get_int($_POST, 'unit');
$p[':accuracy'] = gpc_get_string($_POST, 'accuracy');
$p[':mdate'] = gpc_get_date($_POST, 'mdate');
$p[':note'] = gpc_get_string($_POST, 'note');
$p[':eid'] = gpc_get_int($_POST, 'eid');
if ($p[':eid'] == -1) {
$p[':eid'] = NULL;
}
$fields = join(',', array_map(function($s) { return ltrim($s, ':') . '=' . $s; }, array_keys($p)));
$sth = $pdo->prepare("UPDATE measurement SET $fields WHERE mid=:mid");
$p[':mid'] = $id;
try {
$sth->execute($p);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
$action = ACT_VIEW;
break;
case 'delete':
// Security token needed!
if (gpc_get_string($_POST, 'token', 16) != $_SESSION['token']) {
$g_error->Add(_('Delete prohibited, invalid security token!'));
$action = ACT_VIEW;
break;
}
$action = ACT_DEFAULT;
break;
default:
$g_error->Add(sprintf(_('Unknown function!'), $submit));
$valid = FALSE;
}
// ========== ACTIONS END =====================================================
require 'header.php';
// ========== PAGE CONTENT ====================================================
if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================
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;
}
}
echo "<h1>$pagetitle</h1>\n";
$sql = "SELECT mid, mname, nval, val1, val2, val3, unit, accuracy, mdate, note, eid "
. "FROM measurement "
. "ORDER BY mname";
$sth = $pdo->query($sql);
$res = $sth->fetchAll();
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>\n";
echo '<th>', _('Measurement'), "</th>";
echo '<th>', _('Note'), "</th>";
echo '<th class="text-end">', _('Measured'), "</th>";
echo '<th>', _('Unit'), "</th>";
echo '<th>', _('Accuracy'), "</th>";
echo "<th></th>";
echo "</tr>\n";
echo "</thead>\n";
foreach ($res as $row) {
echo "<tr>\n";
echo "<td>", $row['mname'], "</td>\n";
echo "<td>", $row['note'], "</td>\n";
echo '<td class="text-end">', format_vals($row['nval'], $row['val1'], $row['val2'], $row['val3']), "</td>\n";
echo "<td>", $opt_unit[$row['unit']], "</td>\n";
echo "<td>", $opt_accuracy[$row['accuracy']], "</td>\n";
echo "<td>";
// Action buttons
form_action_buttons($g_scriptname, $row['mid']);
echo "</td>\n";
echo "</tr>\n";
}
// Table summary
echo "<tfoot>\n";
echo '<tr><td colspan="6">', sprintf(_('%d records'), count($res)), '</td></tr>', "\n";
echo "</tfoot>\n";
echo "</table>\n";
form_add_button($g_scriptname);
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
echo '<h2>', _('Add measurement'), "</h2>\n";
?>
<form method="post">
<div class="mb-3">
<label for="mname" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="mname" name="mname" required autofocus>
</div>
<div class="mb-3">
<label for="note" class="form-label"><?=_('Note')?></label>
<input type="text" class="form-control" id="note" name="note">
</div>
<?php
form_create_select('nval', _('Dimensions'), $opt_dimensions);
?>
<div class="mb-3">
<label for="val1" class="form-label"><?=_('Value 1')?></label>
<input type="text" class="form-control" id="val1" name="val1">
</div>
<div class="mb-3">
<label for="val2" class="form-label"><?=_('Value 2')?></label>
<input type="text" class="form-control" id="val1" name="val2">
</div>
<div class="mb-3">
<label for="val3" class="form-label"><?=_('Value 3')?></label>
<input type="text" class="form-control" id="val3" name="val3">
</div>
<?php
form_create_select('unit', _('Unit'), $opt_unit);
form_create_select('accuracy', _('Accuracy'), $opt_accuracy);
form_create_select('eid', _('Equipment'), $opt_equipment);
?>
<div class="container-fluid px-0 my-3">
<button type="submit" name="submit[insert]" class="btn btn-primary"><?=_('Save')?></button>
<a href="<?=$g_scriptname?>" class="btn btn-secondary"><?=_('Back')?></a>
</div>
</form>
<?php
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT mname, nval, val1, val2, val3, unit, accuracy, note "
. "FROM measurement "
. "WHERE mid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$measurement = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('Edit measurement'), "</h2>\n";
?>
<form method="post">
<input type="hidden" name="id" value="<?=$id?>">
<div class="mb-3">
<label for="mname" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="name" name="mname" value="<?=$measurement->mname ?>">
</div>
<div class="mb-3">
<label for="note" class="form-label"><?=_('Note')?></label>
<input type="text" class="form-control" id="note" name="note" value="<?=$measurement->note ?>">
</div>
<?php
form_create_select('nval', _('Dimensions'), $opt_dimensions, $measurement->nval);
?>
<div class="mb-3">
<label for="val1"><?=_('Value 1')?></label>
<input type="number" min="0" class="form-control" id="val1" name="val1" value="<?=$measurement->val1 ?>">
</div>
<div class="mb-3">
<label for="val2"><?=_('Value 2')?></label>
<input type="number" min="0" class="form-control" id="val2" name="val2" value="<?=$measurement->val2 ?>">
</div>
<div class="mb-3">
<label for="val3"><?=_('Value 3')?></label>
<input type="number" min="0" class="form-control" id="val3" name="val3" value="<?=$measurement->val3 ?>">
</div>
<?php
form_create_select('unit', _('Unit'), $opt_unit, $measurement->unit);
form_create_select('accuracy', _('Accuracy'), $opt_accuracy, $measurement->accuracy);
form_create_select('eid', _('Equipment'), $opt_equipment, $measurement->eid);
form_edit_buttons($g_scriptname, $id);
echo "</form>\n";
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
else:
// ========== ERROR UNKNOWN VARIANT ===========================================
echo '<p>', _('Unknown function call: Please report to system development!'), "</p>\n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
include 'footer.php';