Files
YMS/webgui/fuse.php
T

296 lines
9.7 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024-2026 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
require 'globals.inc';
require 'lib/gpc.inc';
$pagetitle = _('Fuses');
$id = gpc_get_int($_REQUEST, 'id', 0);
$opt_ftype = db_get_options(7);
$opt_location = db_get_options(10);
// ========== 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 'filter':
$flt = array();
$flt['location'] = gpc_get_int($_POST, 'flt_location');
$flt['txt'] = gpc_get_string($_POST, 'flt_txt');
db_save_filter($flt, 212);
$action = ACT_DEFAULT;
break;
case 'freset':
db_clear_filter(212);
$action = ACT_DEFAULT;
break;
case 'insert':
$p[':vid'] = $user->vid;
$p[':fnumber'] = gpc_get_int($_POST, 'fnumber');;
$p[':ftype'] = gpc_get_int($_POST, 'ftype');;
$p[':current'] = gpc_get_int($_POST, 'current');;
$p[':location'] = gpc_get_int($_POST, 'location');;
$p[':description'] = gpc_get_string($_POST, 'description');
$id = db_exec_insert('fuse', $p);
$action = ACT_VIEW;
break;
case 'update':
$p[':fuseid'] = $id;
$p[':fnumber'] = gpc_get_string($_POST, 'fuseid');
$p[':current'] = gpc_get_float($_POST, 'current');
$p[':cableid'] = gpc_get_int($_POST, 'cableid');
$p[':eid'] = gpc_get_int($_POST, 'id');
$p[':weight'] = min(gpc_get_float($_POST, 'weight'), 9.99); // limit max value
$p[':location'] = gpc_get_int($_POST, 'location');
$p[':description'] = gpc_get_string($_POST, 'description');
$p[':remarks'] = gpc_get_string($_POST, 'remarks');
db_exec_update('tag', $p, 'tagid');
$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;
}
// tagref have to be empty
// TODO
$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 =======================================
// lookup tables
$opt_ftype_short = db_get_options(7, '', true);
// load filter from db
$flt = db_get_filter($user->id, 212);
$w = array('vid=:vid');
$p = array(':vid' => $user->vid);
if ($flt->location == -1) {
$w[] = 'location IS NULL';
} elseif ($flt->location > 0) {
$w[] = 'location=:location';
$p[':location'] = $flt->location;
}
if (strlen($flt->txt) > 1) {
$w[] = 'description LIKE :txt';
$p[':txt'] = '%'.$flt->txt.'%';
}
$where = join(' AND ', $w);
$order = ' ORDER BY fnumber';
echo "<h1>$pagetitle</h1>\n";
// Filter
?>
<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 d-flex flex-wrap gap-3">
<?php
filter_create_select('flt_location', _('Location'), $g_opt_all + $g_opt_none + $opt_location, $flt->location);
?>
<div class="d-flex flex-nowrap gap-2">
<label for="flt_txt"><?=_('Description')?></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 fuseid, fnumber, ftype, location, current, description "
. "FROM fuse";
$sql .= ' WHERE ' . $where;
$sql .= $order;
$sth = $pdo->prepare($sql);
$sth->execute($p);
$res = $sth->fetchAll();
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>\n";
echo '<th>#</th>';
echo '<th>', pgettext('number', 'Number'), "</th>";
echo '<th>', _('Type'), "</th>";
echo '<th>', _('Current'), "</th>";
echo '<th>', _('Location'), "</th>";
echo '<th>', _('Description'), "</th>";
echo "<th></th>";
echo "</tr>\n";
echo "</thead>\n";
$i = 1;
foreach ($res as $row) {
echo "<tr>\n";
echo '<td>', $i++;
echo '<a class="text-nowrap" title="', _('View'), '" href="', $g_scriptname, '?f=view&id=', $row['fuseid'], '"><i class="bi bi-eye ms-2"></i></a>', "\n";
echo "</td>\n";
echo "<td>F", $row['fnumber'], "</td>\n";
echo "<td>",$opt_ftype_short[$row['ftype']], "</td>\n";
echo "<td>", $row['current'], "A</td>\n";
echo "<td>", $opt_location[$row['location']], "</td>\n";
echo "<td>", $row['description'], "</td>\n";
// Edit current record button
echo "<td>";
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['fusevid'], '"><i class="bi bi-pencil"></i></a>', "\n";
echo "</td>\n";
echo "</tr>\n";
}
// Table summary
table_rec_summary($i0, $i, $numrows, 6);
echo "</table>\n";
form_add_button($g_scriptname);
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
echo '<h2>', _('Add fuse'), "</h2>\n";
// calc next free fuse number for convenience
$sql = "SELECT max(fnumber) FROM fuse WHERE vid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$user->vid]);
$fnumber = $sth->fetchColumn() + 1;
?>
<form method="post" action="<?=$g_scriptname?>">
<div class="mb-3">
<label for="fnumber" class="form-label"><?=_('Fuse number')?></label>
<input type="text" class="form-control" id="fnumber" name="fnumber" value="<?=$fnumber?>" required>
</div>
<div class="mb-3">
<label for="note" class="form-label"><?=_('Description')?></label>
<input type="text" class="form-control" id="description" name="description" autofocus>
</div>
<?php form_create_select('ftype', _('Type'), $opt_ftype); ?>
<div class="mb-3">
<label for="current" class="form-label"><?=_('Current, Ampere')?></label>
<input type="text" class="form-control" id="current" name="current" required>
</div>
<?php form_create_select('location', _('Location'), $g_opt_none + $opt_location); ?>
<?php
form_new_buttons($g_scriptname);
echo "</form>\n";
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
$sql = "SELECT fnumber, ftype, current, cableid, eid, location, description, remarks "
. "FROM fuse "
. "WHERE fuseid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$fuse = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('View fuse'), "</h2>\n";
echo '<table class="table">', "\n";
echo '<tr><th style="width:20%">', _('Fuse'),"</th><td>F", $fuse->fnumber, "</td></tr>\n";
echo '<tr><th>', _('Description'),"</th><td>", $fuse->description, "</td></tr>\n";
echo '<tr><th>', _('Type'),"</th><td>", $opt_ftype[$fuse->ftype], "</td></tr>\n";
echo '<tr><th>', _('Current'),"</th><td>", $fuse->current, "A</td></tr>\n";
echo '<tr><th>', _('Location'),"</th><td>", $opt_location[$fuse->location], "</td></tr>\n";
echo '<tr><th>', _('Cable'),"</th><td>", $fuse->cableid, "</td></tr>\n";
echo '<tr><th>', _('Equipment'),"</th><td>", $fuse->eid, "</td></tr>\n";
echo '<tr><th>', _('Remarks'),"</th><td>", nl2br($fuse->remarks), "</td></tr>\n";
echo "</table>\n";
form_view_buttons($g_scriptname, $id);
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT fnumber, description "
. "FROM fuse "
. "WHERE fuseid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$fuse = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('Edit fuse'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<div class="mb-3">
<label for="mname" class="form-label"><?=pgettext('number', 'Number')?></label>
<input type="text" class="form-control" id="fnumber" name="fnumber" value="<?=$fuse->fnumber ?>">
</div>
<div class="mb-3">
<label for="note" class="form-label"><?=_('Description')?></label>
<input type="text" class="form-control" id="description" name="description" value="<?=$fuse->description ?>">
</div>
<?php
form_edit_buttons($g_scriptname, $id);
echo "</form>\n";
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
echo '<h2>', _('Delete fuse'), "</h2>\n";
$sql = "SELECT fusename, description FROM fuse WHERE fuseid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
if ($sth->rowCount() > 0) {
foreach ($sth->fetchAll() as $row) {
echo '<a href="', $link[$row['objtype']], '?f=view&id=', $row['objid'], '">', $row['objid'], '/', $row['objtype'], "</a>\n";
}
} else {
echo _('Fuse is not used anywhere');
$_SESSION['token'] = bin2hex(random_bytes(8));
form_delete_buttons($g_scriptname, $id, $_SESSION['token']);
}
else:
// ========== ERROR UNKNOWN VARIANT ===========================================
echo '<p>', _('Unknown function call: Please report to system development!'), "</p>\n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
include 'footer.php';