Files
YMS/webgui/equipment.php
T

710 lines
26 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024-2026 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
require 'globals.inc';
require 'lib/gpc.inc';
require 'lib/fileutils.inc';
$pagetitle = _('Equipment');
$id = gpc_get_int($_REQUEST, 'id', 0);
$opt_supplier = db_get_opt_supp($g_opt_unknown);
$opt_manufacturer = db_get_opt_manuf($g_opt_unknown);
$opt_ecat = db_get_options(4); // Equipment categories
$opt_unit = db_get_options(8);
// ========== 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['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');
$allowed = db_load_enum('equipment', 'flags');
$flt['flags'] = gpc_get_set($_POST, 'flt_flags', $allowed);
db_save_filter($flt, 200);
$action = ACT_DEFAULT;
break;
case 'freset':
db_clear_filter(200);
$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':
$p[':eid'] = $id;
$p[':ename'] = gpc_get_string($_POST, 'ename');
$p[':shortname'] = gpc_get_string($_POST, 'shortname', 20);
$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;
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;
}
unset($_SESSION['token']);
$sth = $pdo->prepare("DELETE FROM docref WHERE reftype='equipment' AND refid=?");
try {
$sth->execute([$id]);
} catch (PDOException $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
$refcount = $sth->rowCount();
$sth = $pdo->prepare("DELETE FROM equipment WHERE eid=?");
try {
$sth->execute([$id]);
} catch (PDOException $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
$g_message->Add(sprintf(_('Deleted equipment no. %d'), $id));
if ($refcount > 0) {
$g_message->Add(sprintf(_('%d document links were removed'), $refcount));
}
$action = ACT_DEFAULT;
break;
case 'upload':
$action = ACT_VIEW;
if (!isset($_FILES['files'])) {
$g_warning->Add(_('No files for upload submitted'));
break;
}
$extensions = ['jpg', 'png'];
$all_files = count($_FILES ["files"]["tmp_name"]);
$nerr = 0;
for ($i = 0; $i < $all_files; $i++) {
$file_name = $_FILES['files']['name'][$i];
$file_tmp = $_FILES['files']['tmp_name'][$i];
$file_type = $_FILES['files']['type'][$i];
$file_size = $_FILES['files']['size'][$i];
$file_ext = strtolower(end(explode('.', $_FILES['files']['name'][$i])));
// $file = $g_doc_basepath . '/' . $file_name;
$file_mimetype = mime_content_type($file_tmp);
if (!in_array($file_mimetype, $g_doc_mimetypes)) {
$g_error->Add(sprintf(_("Mimetype '%s' not allowed for upload."), $file_mimetype));
$nerr += 1;
}
if (!in_array($file_ext, $extensions)) {
$g_error->Add(_('Filetype not allowed for upload:') . ' ' . $file_type);
$nerr += 1;
}
if ($file_size > $g_doc_maxsize) {
$g_error->Add(sprintf(_('File to big: %s.%s'), $file_name, $file_type));
$nerr += 1;
}
if ($nerr > 0) {
break;
}
// scale down if dimensions exceed limits
$img_maxx= $pdo->query("SELECT valint FROM settings WHERE userid=0 AND sno=20")->fetchColumn();
$img_maxy= $pdo->query("SELECT valint FROM settings WHERE userid=0 AND sno=21")->fetchColumn();
[$width, $height] = getimagesize($file_tmp);
$factor = min($img_maxx / $width, $img_maxy / $height);
if ($factor < 1) {
$width = (int)($width * $factor);
$height = (int)($height * $factor);
$g_message->add(sprintf(_('Image scaled down to %dx%d'), $width, $height));
if ($file_mimetype == 'image/jpeg') {
$orig = imagecreatefromjpeg($file_tmp);
$scaled = imagescale($orig, $width, $height, IMG_BICUBIC);
imagejpeg($scaled, $file_tmp, 85);
} else if ($file_mimetype == 'image/png') {
$orig = imagecreatefrompng($file_tmp);
if (!$orig) {
$g_error->Add("imagecreatefrompng failed");
break;
}
$scaled = imagescale($orig, $width, $height, IMG_BICUBIC);
if (!$scaled) {
$g_error->Add("imagescale failed");
break;
}
imagepng($scaled, $file_tmp, 9);
} else {
$g_error->Add(sprintf(_('Internal error: Unknown mimetype %s!'), $file_mimetype));
break;
}
$file_size = filesize($file_tmp);
}
// check if storage limit is exceeded
if (dir_get_size($g_doc_basepath) + $file_size > $g_storage_limit * 1024 * 1024) {
$g_error->Add(_('Storage limit exceeded. Image not uploaded!'));
break;
}
$file_hash = md5_file($file_tmp);
$file_timestamp = date('Y-m-d H:i:s', filemtime($file_tmp));
// check whether the file already exists, in this case issue
// a message and just create a link to the already known file
$sql = "SELECT docid FROM document WHERE hash=?";
$sth = $pdo->prepare($sql);
$sth->execute([$file_hash]);
$row = $sth->fetch();
if (!$row) {
$sql = "INSERT INTO document"
. " (doctype, filename, extension, hash, doctime, docsize, mimetype) "
. "VALUES"
. " (?, ?, ?, ?, ?, ?, ?)";
$sth = $pdo->prepare($sql);
$sth->execute(['picture', $file_name, $file_ext, $file_hash, $file_timestamp, $file_size, $file_mimetype]);
$docid = $pdo->lastInsertId();
$file_techname = file_get_docname($docid, 'picture', $file_ext);
move_uploaded_file($file_tmp, $g_doc_basepath . '/' . $file_techname);
} else {
// A document record definitely exists here, now just
// connect it to the selected equipment
$docid = $row['docid'];
}
$sql = "INSERT INTO docref (docid, refid, reftype) VALUES (?, ?, 'equipment')";
$sth = $pdo->prepare($sql);
// Reference may already exist!
try {
$sth->execute([$docid, $id]);
} catch (PDOException $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
$g_error->Add($sql);
// $g_error->Add(print_r($params, true));
}
} // for
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 =======================================
page_caption_search($pagetitle);
$flt = db_get_filter($user->id, 200, ['cat','manuf','supp','txt','flags']);
$sort = array(
1 => 'ename',
2 => 'ename DESC'
);
$w = array('vid=:vid');
$p = array(':vid' => $user->vid);
if ($flt->cat > 0) {
$w[] = 'ecat=:ecat';
$p[':ecat'] = $flt->cat;
} elseif ($flt->cat == -1) {
$w[] = 'ecat IS NULL';
}
if ($flt->manuf > 0) {
$w[] = 'manufacturer=:manuf';
$p[':manuf'] = $flt->manuf;
} elseif ($flt->manuf == -1) {
$w[] = 'manufacturer IS NULL';
}
if ($flt->supp > 0) {
$w[] = 'supplier=:supp';
$p[':supp'] = $flt->supp;
} elseif ($flt->supp == -1) {
$w[] = 'supplier IS NULL';
}
if (strlen($flt->txt) > 1) {
$w[] = '(ename LIKE :txt OR model LIKE :txt OR remarks LIKE :txt)';
$p[':txt'] = '%'.$flt->txt.'%';
}
// condition for multiple flags is OR
$parts = ["flags=''", 'flags IS NULL'];
foreach ($flt->flags as $f) {
$parts[] = "FIND_IN_SET('$f', flags)";
}
if (!empty($parts)) {
if (count($parts) > 1) {
$w[] = '(' . join(' OR ', $parts) . ')';
} else {
$w[] = $parts[0];
}
}
$where = join(' AND ', $w);
$order = ' ORDER BY ename';
// get total record count for pagination and limit
$sth = $pdo->prepare("SELECT COUNT(*) FROM equipment WHERE " . $where);
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);
$sql = "SELECT eid, ename, manufacturer, model, serial, remarks, flags,"
. " TIMESTAMPDIFF(MONTH, purchdate, NOW()) AS age_mon "
. "FROM equipment";
$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();
// Filter
/*$opt_special = array(
-2 => _('&horbar; all &horbar;'),
-1 => _('&horbar; none &horbar;')
); */
$opt_none_male = array(-1 => pgettext('male', '&horbar; none &horbar;'));
?>
<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_category', _('Category'), $g_opt_all + $g_opt_none + $opt_ecat, $flt->cat);
filter_create_select('flt_manuf', _('Manufacturer'), $g_opt_all + $opt_none_male + $opt_manufacturer, $flt->manuf);
filter_create_select('flt_supp', _('Supplier'), $g_opt_all + $opt_none_male + $opt_supplier, $flt->supp);
?>
<div class="d-flex flex-nowrap gap-2">
<label for="flt_txt"><?=_('Text')?></label>
<input type="text" class="form-control" name="flt_txt" maxlength="30" value="<?=$flt->txt ?>">
</div>
<div class="d-flex flex-nowrap gap-2">
<?php
$opt_flags = db_load_enum('equipment', 'flags', true, true, false);
filter_create_checks('flt_flags', _('Flags'), $opt_flags, $flt->flags ?? []);
?>
</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
// 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>#</th>';
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>&nbsp;</th>";
echo "</tr>\n";
echo "</thead>\n";
$i = ($page - 1) * $rows_pp;
$i0 = $i + 1;
foreach ($res as $row) {
// calc nice age display
if (!isset($row['age_mon'])) {
$age = '';
} elseif ($row['age_mon'] < 12) {
$age = sprintf(_('%dm'), $row['age_mon']);
} else {
$age = sprintf(_('%dy'), intdiv($row['age_mon'], 12));
}
$i++; // record number
echo "<tr>\n";
echo "<td>", $i;
echo '<a class="text-nowrap" title="', _('View'), '" href="', $g_scriptname, '?f=view&id=', $row['eid'], '"><i class="bi bi-eye ms-2"></i></a>', "\n";
echo "</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";
// Edit current record button
echo "<td>";
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['eid'], '"><i class="bi-pencil"></i></a>', "\n";
echo "</td>\n";
echo "</tr>\n";
}
?>
<tfoot>
<tr><td colspan="8"><?php printf(_('Records %d to %d of %d'), $i0, $i, $numrows); ?></td></tr>
</tfoot>
</table>
<?php
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?>">
<div class="mb-3">
<label for="ename" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="ename" name="ename">
</div>
<?php
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>
<input type="text" class="form-control" id="model" name="model">
</div>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3" maxlength="150"></textarea>
</div>
<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 =====================================
$sql = "SELECT eid, ename, model, serial, purchdate, price, weight, "
. " supplier, manufacturer, ecat, shortname, remarks, flags "
. "FROM equipment "
. "WHERE eid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$equipment = $sth->fetch(PDO::FETCH_OBJ);
echo "<h2>", $equipment->ename, "</h2>\n";
// Bootstrap grid
echo '<div class="container">';
echo '<div class="row">';
echo '<div class="col">';
echo '<table class="table">', "\n";
echo '<tr><th>', _('Short name'),"</th><td>", $equipment->shortname, "</td></tr>\n";
echo '<tr><th>', _('Manufacturer'),"</th><td>", $opt_manufacturer[$equipment->manufacturer];
echo '&nbsp;<a title="', _('View'), '" href="company.php?f=view&id=', $equipment->manufacturer, '"><i class="bi-eye"></i></a>';
echo "</td></tr>\n";
echo '<tr><th>', _('Model'),"</th><td>", $equipment->model, "</td></tr>\n";
echo '<tr><th>', _('Serial'),"</th><td>", $equipment->serial, "</td></tr>\n";
echo '<tr><th>', _('Weight'),"</th><td>", format_float($equipment->weight, 2, 'kg'), "</td></tr>\n";
echo '<tr><th>', _('Price'),"</th><td>", format_currency($equipment->price), "</td></tr>\n";
echo '<tr><th>', _('Purchase date'),"</th><td>", $equipment->purchdate, "</td></tr>\n";
echo '<tr><th>', _('Supplier'),"</th><td>", $equipment->supplier ? $opt_supplier[$equipment->supplier] : 'n/a', "</td></tr>\n";
echo '<tr><th>', _('Category'),"</th><td>", $opt_ecat[$equipment->ecat], "</td></tr>\n";
echo '<tr><th>', _('Remarks'),"</th><td>", nl2br($equipment->remarks), "</td></tr>\n";
echo '<tr><th>', _('Flags'),"</th><td>", $equipment->flags, "</td></tr>\n";
echo "</table>\n";
echo '</div>'; // Column break
echo '<div class="col">', "\n";
echo '<div class="card mb-2">', "\n";
echo '<div class="card-header">', _('Images and documents'), "</div>\n";
echo '<div class="card-body d-flex flex-wrap gap-3">', "\n";
$sql = "SELECT d.docid, d.doctype, d.title "
. "FROM docref AS r INNER JOIN document AS d using (docid) "
. "WHERE r.reftype='equipment' AND r.refid=? "
. "ORDER BY d.doctype";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$res = $sth->fetchAll();
// TODO hübsche Darstellung
$doccount = 0;
foreach ($res as $row) {
$doccount += 1;
echo '<div class="border rounded p-2 d-flex flex-column" style="width: 150px;">';
if ($row['doctype'] == 'picture') {
echo '<a href="dl.php?id=', $row['docid'], '">';
// echo '<img width="120" src="doc/pic-', $row['hash'], '.', $row['extension'],'">';
echo '<img class="img-fluid rounded mb-2" width="120" src="dl.php?id=', $row['docid'], '" alt="', $row['title'], '">';
echo "</a>\n";
// additionally direct download and go to document details page
echo '<div class="d-flex justify-content-between mt-auto">';
echo '<a href="documents.php?f=view&id=', $row['docid'], '" title="Download image"> <i class="bi bi-eye"></i></a>';
echo '<a href="dl.php?id=', $row['docid'], '" title="View document"><i class="bi bi-download"></i></a>';
echo '</div>';
} elseif ($row['doctype'] == 'drawing') {
echo "<p>Drawing:<br>", $row['title'], "<br>";
echo '<a href="dl.php?id=', $row['docid'], '">';
echo '<img width="180" src="dl.php?id=', $row['docid'], '">';
echo "</a>";
echo "</p>\n";
} elseif ($row['doctype'] == 'generic') {
echo '<a href="dl.php?id=', $row['docid'], '">';
echo '<img width="120" src="dl.php?id=', $row['docid'], '&t=s" alt="', $row['title'], '">';
// echo $row['title'];
echo "</a>\n";
} elseif ($row['doctype'] == 'manual') {
echo "<p>Manual:<br>";
echo '<a href="documents.php?f=view&id=', $row['docid'], '">';
echo $row['title'];
echo "</a></p>\n";
} else {
// unknown document type
echo "<span>Unknown document type:", $row['doctype'], "</span>\n";
}
echo "</div>\n";
}
if ($doccount == 0) {
echo _('No referenced documents'), "\n";
}
echo "</div>\n"; // card-body
echo "</div>\n"; // card
?>
<form method="post" enctype="multipart/form-data" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<div class="card">
<div class="card-header"><?=_('Add Image');?></div>
<div class="card-body">
<input type="file" name="files[]" multiple accept="image/*" capture="camera">
<input type="submit" name="submit[upload]" value="Upload">
</div>
</div>
</form>
<?php
echo '</div>'; // col
echo '</div>'; // row
echo '</div>'; // container
// Buttons at bottom of data area
form_view_buttons($g_scriptname, $id);
// Maintenance records
echo '<h3>', _('Maintenances'), "</h3>";
$sql = "SELECT maintid, series, activities FROM maintenance WHERE eid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
if ($sth->rowCount() > 0) {
echo "<ul>\n";
foreach ($sth->fetchAll() as $row) {
echo "<li>", $row['activities'], ' ';
echo '<a title="', _('View'), '" href="maintenance.php?f=view&id=', $row['maintid'], '"><i class="bi-eye"></i></a>';
echo "</li>\n";
}
echo "</ul>\n";
} else {
echo '<p>', _('No maintenance records found.'), "<p>\n";
}
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<a href="maintenance.php?f=add&id=', $id, '" class="btn btn-primary" role="button">';
echo _('Add maintenance'), "</a>\n";
echo "</div>\n";
// Measurement records
echo '<h3>', _('Measurements'), "</h3>";
$sql = "SELECT mid, mname, nval, val1, val2, val3, unit, accuracy FROM measurement WHERE eid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
if ($sth->rowCount() > 0) {
echo "<ul>\n";
foreach ($sth->fetchAll() as $row) {
echo "<li>", $row['mname'], ': ';
echo format_measurement($row['nval'], $opt_unit[$row['unit']], $row['val1'], $row['val2'], $row['val3']);
echo ' <a title="', _('View'), '" href="measurement.php?f=view&id=', $row['mid'], '"><i class="bi-eye"></i></a>';
echo "</li>\n";
}
echo "</ul>\n";
} else {
echo '<p>', _('No measurement records found.'), "<p>\n";
}
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<a href="measurement.php?f=add&id=', $id, '" class="btn btn-primary" role="button">';
echo _('Add measurement'), "</a>\n";
echo "</div>\n";
// Spare parts
echo '<h3>', _('Spare parts'), "</h3>";
$sql = "SELECT invid, invname FROM inventory WHERE eid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
if ($sth->rowCount() > 0) {
echo "<ul>\n";
foreach ($sth->fetchAll() as $row) {
echo "<li>", $row['invname'], ': ';
echo ' <a title="', _('View'), '" href="inventory.php?f=view&id=', $row['invid'], '"><i class="bi-eye"></i></a>';
echo "</li>\n";
}
echo "</ul>\n";
} else {
echo '<p>', _('No spare parts found.'), "<p>\n";
}
// Document preview
?>
<div class="modal fade" id="imageModal" tabindex="-1" aria-labelledby="imageModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered custom-image-modal">
<div class="modal-content">
<div class="modal-header">
TITLE
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
IMAGE
</div>
</div>
</div>
</div>
<?php
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT compid, compname FROM company WHERE comptype=1 ORDER BY compname";
$sth = $pdo->query($sql);
$supplier = array();
foreach ($sth->fetchAll() as $row) {
$supplier[$row['compid']] = $row['compname'];
}
$sql = "SELECT eid, ename, model, serial, weight, price, purchdate,"
. " supplier, manufacturer, ecat, shortname, remarks "
. "FROM equipment "
. "WHERE eid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$equipment = $sth->fetch(PDO::FETCH_OBJ);
?>
<h2><?=_('Edit Equipment')?></h2>
<form method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<div class="mb-3">
<label for="ename" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="ename" name="ename" value="<?=$equipment->ename ?>">
</div>
<div class="mb-3">
<label for="shortname" class="form-label"><?=_('Short name')?></label>
<input type="text" class="form-control" id="shortname" name="shortname" value="<?=$equipment->shortname ?>">
</div>
<?php form_create_select('manufacturer', _('Manufacturer'), $opt_manufacturer, $equipment->manufacturer); ?>
<div class="mb-3">
<label for="model" class="form-label"><?=_('Model')?></label>
<input type="text" class="form-control" id="model" name="model" value="<?=$equipment->model ?>">
</div>
<div class="mb-3">
<label for="serial" class="form-label"><?=_('Serial')?></label>
<input type="text" class="form-control" id="serial" name="serial" value="<?=$equipment->serial ?>">
</div>
<div class="mb-3">
<label for="weight" class="form-label"><?=_('Weight, kg')?></label>
<input type="text" class="form-control" id="weight" name="weight" value="<?=format_float($equipment->weight); ?>">
</div>
<div class="mb-3">
<label for="price" class="form-label"><?=sprintf(_('Price, %s'), $g_lconv['currency_symbol']); ?></label>
<input type="text" class="form-control" id="price" name="price" value="<?=format_float($equipment->price) ?>">
</div>
<div class="mb-3">
<label for="purchdate"><?=_('Purchase date')?></label>
<input type="date" class="form-control" id="startdate" name="purchdate" value="<?=$equipment->purchdate ?>">
</div>
<?php
form_create_select('supplier', _('Supplier'), $opt_supplier, $equipment->supplier);
form_create_select('category', _('Category'), $g_opt_unknown + $opt_ecat, $equipment->ecat);
?>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=$equipment->remarks ?></textarea>
</div>
<button type="submit" name="submit[update]" class="btn btn-primary"><?=_('Save')?></button>
<a href="<?=$g_scriptname?>?f=view&id=<?=$id?>" class="btn btn-secondary"><?=_('Back')?></a>
</form>
<?php
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
$_SESSION['token'] = bin2hex(random_bytes(8));
$sql = "SELECT ename, remarks FROM equipment WHERE eid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$equipment = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('Delete equipment'),"</h2>\n";
echo '<p>', sprintf(_('Record no. %d'), $id), "</p>\n";
echo '<p>Name: ', $equipment->ename, "</p>";
echo '<p>Remarks: ', $equipment->remarks, "</p>";
echo '<p>', _('Deleting an equipment item is final. There is no way back. Only delete if you are absolute sure.'), "</p>\n";
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';