294 lines
11 KiB
PHP
294 lines
11 KiB
PHP
<?php
|
|
/******************************************************************************
|
|
* YMS - Yacht Management Software
|
|
* Copyright (C) 2024-2026 Thomas Hooge
|
|
*
|
|
* SPDX-License-Identifier: WTFPL
|
|
******************************************************************************/
|
|
|
|
require 'globals.inc';
|
|
require 'lib/db.inc';
|
|
require 'lib/gpc.inc';
|
|
require 'lib/form.inc';
|
|
|
|
$pagetitle = _('Cables');
|
|
|
|
$id = gpc_get_int($_REQUEST, 'id', 0);
|
|
|
|
$opt_cabletype = db_get_options(DD_CABLE_TYPE);
|
|
$opt_techname = db_get_options(DD_CABLE_TECHNAME);
|
|
$opt_cablecond = db_load_enum('cable', 'cablecond', true, true);
|
|
|
|
// ========== 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['type'] = gpc_get_int($_POST, 'flt_type');
|
|
$flt['txt'] = gpc_get_string($_POST, 'flt_txt');
|
|
db_save_filter($flt, 211);
|
|
$action = ACT_DEFAULT;
|
|
break;
|
|
|
|
case 'freset':
|
|
db_clear_filter(211);
|
|
$action = ACT_DEFAULT;
|
|
break;
|
|
|
|
case 'insert':
|
|
$p[':cablename'] = gpc_get_string($_POST, 'cablename');
|
|
$p[':length'] = gpc_get_float($_POST, 'length');
|
|
$p[':conductors'] = gpc_get_int($_POST, 'conductors');
|
|
$p[':xsection'] = gpc_get_float($_POST, 'xsection');
|
|
$id = db_exec_insert('cable', $p);
|
|
db_set_revision('table', TBL_CABLES);
|
|
$action = ACT_VIEW;
|
|
break;
|
|
|
|
case 'update':
|
|
$p[':cableid'] = $id;
|
|
$p[':cablename'] = gpc_get_string($_POST, 'cablename');
|
|
$p[':cabletype'] = gpc_get_int($_POST, 'cabletype');
|
|
$p[':techname'] = gpc_get_int($_POST, 'techname');
|
|
$p[':length'] = gpc_get_float($_POST, 'length');
|
|
$p[':conductors'] = gpc_get_int($_POST, 'conductors');
|
|
$p[':xsection'] = gpc_get_float($_POST, 'xsection');
|
|
$p[':diameter'] = gpc_get_float($_POST, 'diameter');
|
|
$p[':weight'] = gpc_get_float($_POST, 'weight');
|
|
$p[':color'] = gpc_get_color($_POST, 'color');
|
|
$p[':cablecond'] = gpc_get_string($_POST, 'cablecond');
|
|
$p[':remarks'] = gpc_get_string($_POST, 'remarks');
|
|
db_exec_update('cable', $p, 'cableid');
|
|
$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;
|
|
}
|
|
// 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 =======================================
|
|
|
|
// load filter from db
|
|
$flt = db_get_filter($user->id, 211, ['txt', 'type']);
|
|
|
|
$w = array('vid=:vid');
|
|
$p = array(':vid' => $user->vid);
|
|
|
|
if (strlen($flt->txt) > 1) {
|
|
$w[] = 'cablename LIKE :txt OR remarks LIKE :txt';
|
|
$p[':txt'] = '%'.$flt->txt.'%';
|
|
}
|
|
if ($flt->type == -1) {
|
|
$w[] = 'cabletype IS NULL';
|
|
} elseif ($flt->type > 0) {
|
|
$w[] = 'cabletype=:cabletype';
|
|
$p[':type'] = $flt->type;
|
|
}
|
|
|
|
$where = join(' AND ', $w);
|
|
$order = ' ORDER BY cablename';
|
|
|
|
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_type', _('Type'), $g_opt_all + $g_opt_none + $opt_cabletype, $flt->type);
|
|
filter_create_text('flt_txt', _('Text'), $flt->text, 30, 45);
|
|
?>
|
|
</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 cableid, cablename, cabletype, conductors, length, xsection, color, cablecond "
|
|
. "FROM cable ";
|
|
$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>', _('Cable'), "</th>";
|
|
echo '<th>', _('Name'), "</th>";
|
|
echo '<th>', _('Type'), "</th>";
|
|
echo '<th>', _('Conductors'), "</th>";
|
|
echo '<th>', _('Length'), "</th>";
|
|
echo '<th>', _('Cross section'), "</th>";
|
|
echo '<th>', _('Color'), "</th>";
|
|
echo '<th>', _('Condition'), "</th>";
|
|
echo "<th></th>";
|
|
echo "</tr>\n";
|
|
echo "</thead>\n";
|
|
$i = 0;
|
|
foreach ($res as $row) {
|
|
$i++; // record number
|
|
echo "<tr>\n";
|
|
echo '<td>', $i;
|
|
echo '<a class="text-nowrap" title="', _('View'), '" href="', $g_scriptname, '?f=view&id=', $row['cableid'], '"><i class="bi bi-eye ms-2"></i></a>', "\n";
|
|
echo "</td>\n";
|
|
echo "<td>", $row['cableid'], "</td>\n";
|
|
echo "<td>", $row['cablename'], "</td>\n";
|
|
echo "<td>", $row['cabletype'], "</td>\n";
|
|
echo "<td>", $row['conductors'], "</td>\n";
|
|
echo "<td>", $row['length'], "m</td>\n";
|
|
echo "<td>", $row['xsection'], "mm²</td>\n";
|
|
echo "<td>", format_color($row['color']), "</td>\n";
|
|
echo "<td>", get_enum($row['cablecond']), "</td>\n";
|
|
echo "<td>";
|
|
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['cableid'], '"><i class="bi-pencil"></i></a>', "\n";
|
|
echo "</td>\n";
|
|
echo "</tr>\n";
|
|
}
|
|
// Table summary
|
|
// table_rec_summary(1, $i-1, $i-1, 10);
|
|
echo "</table>\n";
|
|
|
|
form_add_button($g_scriptname);
|
|
|
|
elseif ($action == ACT_ADD):
|
|
// ========== VARIANT: add record =============================================
|
|
|
|
echo '<h2>', _('Add Cable'), "</h2>\n";
|
|
|
|
?>
|
|
<form method="post" action="<?=$g_scriptname?>">
|
|
<?php
|
|
form_create_text('cablename', _('Name'), 80, NULL, TRUE, TRUE);
|
|
form_create_numeric('length', _('Length, m'), step: 0.01);
|
|
form_create_select('cabletype', _('Type'), $g_opt_none + $opt_cabletype);
|
|
form_create_numeric('number', _('Number of conductors'), NULL, 1, 99, 1);
|
|
form_create_numeric('xsection', _('Cross section, mm²'), NULL, 0, 999, 0.1);
|
|
|
|
form_new_buttons($g_scriptname);
|
|
echo "</form>\n";
|
|
|
|
elseif ($action == ACT_VIEW):
|
|
// ========== VARIANT: view single record =====================================
|
|
|
|
$sql = "SELECT cablename, cabletype, techname, length, conductors, diameter, xsection,"
|
|
. " weight, color, supplier, manufacturer, cablecond, remarks "
|
|
. "FROM cable "
|
|
. "WHERE cableid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
$cable = $sth->fetch(PDO::FETCH_OBJ);
|
|
|
|
echo '<h2>', _('View cable'), "</h2>\n";
|
|
|
|
echo '<table class="table">', "\n";
|
|
echo '<tr><th style="width:20%">', _('Cable'),"</th><td>", $cable->cablename, "</td></tr>\n";
|
|
echo '<tr><th>', _('Type'),"</th><td>", $opt_cabletype[$cable->cabletype], "</td></tr>\n";
|
|
echo '<tr><th>', _('Techname'),"</th><td>", $opt_techname[$cable->techname], "</td></tr>\n";
|
|
echo '<tr><th>', _('Length'),"</th><td>", format_float($cable->length, 1, 'm'), "</td></tr>\n";
|
|
echo '<tr><th>', _('Conductors'),"</th><td>", $cable->conductors, "</td></tr>\n";
|
|
echo '<tr><th>', _('Diameter'),"</th><td>", format_float($cable->diameter, 1, 'mm'), "</td></tr>\n";
|
|
echo '<tr><th>', _('Cross section'),"</th><td>", format_float($cable->xsection, 1, 'mm²'), "</td></tr>\n";
|
|
echo '<tr><th>', _('Weight'),"</th><td>", format_float($cable->weight, 2, 'kg/m'), "</td></tr>\n";
|
|
echo '<tr><th>', _('Color'),"</th><td>", format_color($cable->color), "</td></tr>\n";
|
|
echo '<tr><th>', _('Condition'),"</th><td>", $opt_cablecond[$cable->cablecond], "</td></tr>\n";
|
|
echo "<tr><th>", _('Remarks'), "</th><td>", h($cable->remarks, br:true), "</td></tr>\n";
|
|
echo "</table>\n";
|
|
|
|
form_view_buttons($g_scriptname, $id);
|
|
|
|
elseif ($action == ACT_EDIT):
|
|
// ========== VARIANT: edit single record =====================================
|
|
|
|
$sql = "SELECT cablename, cabletype, techname, length, conductors, diameter, xsection,"
|
|
. " weight, color, supplier, manufacturer, cablecond, remarks "
|
|
. "FROM cable "
|
|
. "WHERE cableid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
$cable = $sth->fetch(PDO::FETCH_OBJ);
|
|
|
|
echo '<h2>', _('Edit cable'), "</h2>\n";
|
|
?>
|
|
<form method="post" action="<?=$g_scriptname?>">
|
|
<input type="hidden" name="id" value="<?=$id?>">
|
|
<?php
|
|
form_create_text('cablename', _('Name'), 80, $cable->cablename, TRUE, TRUE);
|
|
form_create_select('cabletype', _('Type'), $g_opt_none + $opt_cabletype, $cable->cabletype);
|
|
form_create_select('techname', _('Techname'), $g_opt_none + $opt_techname, $cable->techname);
|
|
form_create_numeric('length', _('Length, m'), format_float($cable->length, 1), 0, 9999, 0.01);
|
|
form_create_numeric('number', _('Number of conductors'), $cable->conductors, 1, 99, 1);
|
|
form_create_numeric('diameter', ('Diameter, mm'), format_float($cable->diameter, 1), 0, 999, 0.1);
|
|
form_create_numeric('xsection', _('Cross section, mm²'), format_float($cable->xsection, 1), 0, 999, 0.1);
|
|
form_create_numeric('weight', _('Weight, kg/m'), format_float($cable->weight, 2), 0, 9999, 0.01);
|
|
form_create_color('color', _('Color'), $cable->color);
|
|
form_create_select('cablecond', _('Condition'), $opt_cablecond, $cable->cablecond);
|
|
form_create_textarea('remarks', _('Remarks'), 3, 150, $cable->remarks);
|
|
|
|
form_edit_buttons($g_scriptname, $id);
|
|
echo "</form>\n";
|
|
|
|
elseif ($action == ACT_DELETE):
|
|
// ========== VARIANT: delete record ==========================================
|
|
|
|
echo '<h2>', _('Delete cable'), "</h2>\n";
|
|
|
|
$sql = "SELECT objid, objtype FROM tagref WHERE tagid=?";
|
|
$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 _('Tag 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';
|