Files
YMS/webgui/documents.php
T

789 lines
27 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 = _('Documents');
$id = gpc_get_int($_REQUEST, 'id', 0);
$page = gpc_get_int($_REQUEST, 'p', 1);
$opt_doctype = db_load_enum('document', 'doctype', true, true);
asort($opt_doctype);
$opt_reftype = db_load_enum('docref', 'reftype', true, true);
asort($opt_reftype);
// ========== 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['doctype'] = gpc_get_enum($_POST, 'flt_doctype', array_keys($opt_doctype));
$flt['reftype'] = gpc_get_enum($_POST, 'flt_reftype', array_merge(array_keys($opt_reftype), ['none']));
$flt['txt'] = gpc_get_string($_POST, 'flt_txt');
db_save_filter($flt, 209);
$action = ACT_DEFAULT;
break;
case 'freset':
db_clear_filter(209);
$action = ACT_DEFAULT;
break;
case 'insert':
// WIP New standalone document
// debug $g_message->Add(print_r($_FILES, true));
// check for possible errors
if (!isset($_FILES['files']['error']) || is_array($_FILES['files']['error'])) {
$g_error->Add(_("Invalid file upload parameters."));
$action = ACT_ADD;
break;
}
if (($_FILES['files']['error'] == UPLOAD_ERR_INI_SIZE) || ($_FILES['files']['error'] == UPLOAD_ERR_FORM_SIZE)) {
$g_error->Add(_("Exceeded filesize limit."));
$action = ACT_ADD;
break;
}
if (empty($_FILES['files']['tmp_name']) ) {
$g_warning->Add(_('No file for upload submitted. Try again by selecting a file first.'));
$action = ACT_ADD;
break;
}
$nerr = 0;
$file_name = $_FILES['files']['name'];
$file_tmp = $_FILES['files']['tmp_name'];
$file_type = $_FILES['files']['type'];
$file_size = $_FILES['files']['size'];
$file_ext = strtolower(end(explode('.', $_FILES['files']['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));
$action = ACT_ADD;
break;
};
if ($file_type != $file_mimetype) {
$g_warning->Add(sprintf(_("Mimetype mismatch: '%s'."), $file_type));
}
// TODO for images check maximum size and scale down if needed
// ok let's go
$file_hash = md5_file($file_tmp);
$file_timestamp = date('Y-m-d H:i:s', filemtime($file_tmp));
// check if the file already exists, in which case issue
// a notice and display the existing document
$sth = $pdo->prepare("SELECT docid FROM document WHERE hash=?");
$sth->execute([$file_hash]);
$id = $sth->fetchColumn();
if ($id) {
$g_warning->Add(_('Document already exists!'));
$action = ACT_VIEW;
break;
}
if ($file_size > $g_doc_maxsize) {
$g_error->Add(sprintf(_('File to big: %s.%s'), $file_name, $file_type));
break;
}
switch ($file_mimetype) {
case 'application/pdf':
$p[':doctype'] = 'generic';
$p[':pages'] = pdf_get_pagecount($file_tmp);
$prefix = 'doc';
break;
case 'image/svg+xml':
$p[':doctype'] = 'drawing';
$prefix = 'drw';
break;
case 'image/png':
case 'image/jpeg':
$p[':doctype'] = 'picture';
$prefix = 'pic';
break;
}
$p[':mimetype'] = $file_mimetype;
$p[':filename'] = $file_name;
$p[':extension'] = $file_ext;
$p[':hash'] = $file_hash;
$p[':doctime'] = $file_timestamp;
$p[':docsize'] = $file_size;
$p[':title'] = gpc_get_string($_POST, 'title');
$p[':remarks'] = gpc_get_string($_POST, 'remarks');
$id = db_exec_insert('document', $p);
$file_techname = sprintf('%s/%s-%06d.%s', $g_doc_basepath, $prefix, $id, $file_ext);
move_uploaded_file($file_tmp, $file_techname);
$action = ACT_VIEW;
break;
case 'update':
$p[':docid'] = $id;
$p[':doctype'] = gpc_get_string($_POST, 'doctype');
$p[':filename'] = gpc_get_string($_POST, 'filename');
$p[':title'] = gpc_get_string($_POST, 'title', 40);
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
db_exec_update('document', $p, 'docid');
$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']);
// get document details
$sql = "SELECT doctype, extension FROM document WHERE docid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$doc = $sth->fetch(PDO::FETCH_OBJ);
// clean thumbs
$thumbdir = $g_doc_basepath . '/thumbs/';
$pattern = sprintf('%s-%06d*', $g_doc_typemap[$doc->doctype], $id);
$g_success->Add("Remove thumbs: type=$doc->doctype pattern=$pattern");
foreach (glob($thumbdir . $pattern) as $f) {
unlink($f);
$g_success->Add($f);
}
// remove references
$nref = db_exec_delete('docref', 'docid=?', $id);
if ($nref > 0) {
$g_success->Add(sprintf(_('Removed %d references'), $nref));
}
// remove document
$docfilename = sprintf('%s/%s-%06d.%s', $g_doc_basepath, $g_doc_typemap[$doc->doctype], $id, $doc->extension);
unlink($docfilename);
$g_success->Add(sprintf(_('Deleted document %s'), $docfilename));
// remove record from database
$sth = $pdo->prepare("DELETE FROM document WHERE docid=?");
try {
$sth->execute([$id]);
} catch (PDOexception $e) {
$g_error->Add(sprintf(_('SQL-Error: %s'), $e->getMessage()));
$action = ACT_DELETE;
break;
}
$action = ACT_DEFAULT;
break;
case 'addref':
$opt_reftype = db_load_enum('docref', 'reftype');
$p[':docid'] = $id;
$p[':refid'] = gpc_get_int($_POST, 'refid');
$p[':reftype'] = gpc_get_enum($_POST, 'reftype', $opt_reftype, 'equipment');
/* $sql = "INSERT INTO docref "
. " (docid, refid, reftype) "
. "VALUES "
. " (:docid, :refid, :reftype)";
$sth = $pdo->prepare($sql);
$sth->bindValue(':docid', $id, PDO::PARAM_INT);
$sth->bindValue(':refid', $refid, PDO::PARAM_INT);
$sth->bindValue(':reftype', $reftype, PDO::PARAM_STR);
$sth->execute(); */
db_exec_insert('docref', $p);
$action = ACT_VIEW;
break;
case 'delref':
// remove only link, not critical so get without token is ok
$drid = gpc_get_int($_GET, 'drid');
$sql = "DELETE FROM docref WHERE drid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$drid]);
$g_message->Add(_('Removed document reference'));
$action = ACT_VIEW;
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, 209);
// $w = array('(vid=:vid OR vid IS NULL)');
// $p = array(':vid' => $user->vid);
$w = array();
$p = array();
if (isset($flt->doctype) and $flt->doctype != 'all') {
$w[] = 'doctype=:doctype';
$p[':doctype'] = $flt->doctype;
}
if (isset($flt->reftype) and $flt->reftype != 'all') {
if ($flt->reftype == 'none') {
$w[] = 'reftype IS NULL';
} else {
$w[] = 'reftype=:reftype';
$p[':reftype'] = $flt->reftype;
}
}
if (strlen($flt->txt) > 1) {
$w[] = '(filename LIKE :txt OR title LIKE :txt OR remarks LIKE :txt)';
$p[':txt'] = '%'.$flt->txt.'%';
}
$where = join(' AND ', $w);
$order = ' ORDER BY docid';
/*
WIP
Attention: Do not count duplicate, so first select docid from different sources
an as laststep get a distinct list of docids to count.
Documents with no reference at all
SELECT docid FROM document LEFT OUTER JOIN docref USING (docid) WHERE docref.drid IS NULL;
Documents for current vessel
SELECT docid FROM document LEFT OUTER JOIN docref USING (docid) WHERE docref.reftype='vessel' AND docref.refid=:vid
Documents for equipment in current vessel
SELECT DISTINCT d.docid AS docid FROM document AS d
LEFT OUTER JOIN docref AS r USING (docid) LEFT OUTER JOIN equipment AS e ON (r.refid=e.eid)
WHERE r.reftype='equipment' AND e.vid=:vid
*/
// get total record count for pagination and limit
$sql = "SELECT COUNT(DISTINCT docid) FROM document LEFT OUTER JOIN docref USING (docid)";
if ($where) $sql .= ' WHERE ' . $where;
$sth = $pdo->prepare($sql);
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', $g_rows_pp);
$lastpage = ceil($numrows/$rows_pp);
$sql = "SELECT d.docid, d.doctype, d.filename, d.title, COUNT(r.drid) AS refcount "
. "FROM document AS d LEFT OUTER JOIN docref AS r USING (docid)";
if ($where) $sql .= ' WHERE ' . $where;
$sql .= " GROUP BY d.docid, d.doctype, d.filename, d.title";
if ($order) $sql .= $order;
// if pagination:
$sql .= ' LIMIT ' . ($page - 1) * $g_rows_pp . ',' . $g_rows_pp;
$sth = $pdo->prepare($sql);
$sth->execute($p);
$res = $sth->fetchAll();
// Filter
$opt_special = array(
'all' => _('&horbar; all &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">
<div class="d-flex flex-nowrap gap-2">
<label for="flt_doctype">Type</label>
<select class="form-select" id="flt_doctype" name="flt_doctype">
<?php
foreach ($opt_special + $opt_doctype as $k => $v) {
echo '<option value="', $k,'"';
if ($k == $flt->doctype) {
echo ' selected';
}
echo '>', $v, '</option>';
}
?>
</select>
</div>
<div class="d-flex flex-nowrap gap-2">
<label for="flt_reftype"><?=_('Reference')?></label>
<select class="form-select" id="flt_reftype" name="flt_reftype">
<?php
$opt_special['none'] = _('&horbar; none &horbar;');
foreach ($opt_special + $opt_reftype as $k => $v) {
echo '<option value="', $k,'"';
if ($k == $flt->reftype) {
echo ' selected';
}
echo '>', $v, '</option>';
}
?>
</select>
</div>
<div class="d-flex flex-nowrap gap-2">
<label for="flt_txt">Text</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
// 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>", _('Type'), "</th>";
echo "<th>", _('Filename'), "</th>";
echo "<th>", _('Title'), "</th>";
echo "<th>", _('Links'), "</th>";
echo "<th>&nbsp;</th>";
echo "</tr>\n";
echo "</thead>\n";
$i = ($page - 1) * $rows_pp;
$i0 = $i + 1;
foreach ($res as $row) {
$i++; // record number
echo "<tr>\n";
echo "<td>", $i;
echo '<a title="', _('View'), '" href="', $g_scriptname, '?f=view&id=', $row['docid'], '"><i class="bi-eye ms-2"></i></a>', "\n";
echo "</td>\n";
echo "<td>", $opt_doctype[$row['doctype']], "</td>\n";
echo "<td>", $row['filename'], "</td>\n";
echo "<td>", $row['title'], "</td>\n";
echo "<td>", $row['refcount'], "</td>\n";
// Edit current record button
echo "<td>";
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['docid'], '"><i class="bi-pencil"></i></a>', "\n";
echo "</td>\n";
echo "</tr>\n";
}
// Table summary
echo "<tfoot>\n";
echo '<tr><td colspan="6">', sprintf(_('Records %d to %d of %d'), $i0, $i, $numrows), '</td></tr>', "\n";
echo "</tfoot>\n";
echo "</table>\n";
echo $pagination;
form_add_button($g_scriptname);
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
echo '<h2>', _('Add Document'), "</h2>\n";
?>
<form method="post" enctype="multipart/form-data" action="<?=$g_scriptname?>">
<div class="mb-3">
<label for="title" class="form-label"><?=_('Title')?></label>
<input type="text" class="form-control" id="title" name="title" value="<?=$title ?? ''?>">
</div>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=$remarks ?? ''?></textarea>
</div>
<div class="mb-3">
<label for="files" class="form-label"><?=_('Document')?></label>
<input class="form-control" type="file" id="files" name="files" onchange="preview()">
<img id="frame" src="" class="img-fluid">
<button type="button" class="btn btn-secondary" onclick="clearimg();return false;"><?=_('Clear selection')?></button>
<script>
function preview() {
var images = ["image/png", "image/jpeg", "image/svg+xml"];
console.log(event.target.files[0].type);
if (images.indexOf(event.target.files[0].type) > -1) {
frame.src = URL.createObjectURL(event.target.files[0]);
} else {
frame.src = "";
}
}
function clearimg() {
document.getElementById('files').value = '';
frame.src = '';
}
</script>
</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 =====================================
/* mutool: preview of page thumbs */
$sql = "SELECT docid, doctype, mimetype, filename, extension, hash,"
. " docsize, doctime, title, pages, remarks, roleaccess "
. "FROM document "
. "WHERE docid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$document = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('View Document'), "</h2>\n";
echo '<div class="row">', "\n";
echo '<div class="col">', "\n";
echo '<table class="table table-borderless">', "\n";
echo '<tr><th scope="row" style="width:20%">', _('Document type'),"</th><td>", get_enum($document->doctype), "</td></tr>\n";
if ($document->doctype == 'picture') {
$techname = sprintf('pic-%06d.%s', $document->docid, $document->extension);
echo '<tr><th scope="row">', _('Preview'), '</th><td>';
// echo '<a href="dl.php?id=', $document->docid, '" target="_blank">';
echo '<div class="image-gallery">';
echo '<a href="#" data-bs-toggle="modal" data-bs-target="#imageModal">';
// echo '<img width="', $g_thumb_size, '" src="dl.php?id=', $document->docid, '&t=s" alt="', $document->title, '">';
echo '<img width="', $g_thumb_size, '" src="dl.php?id=', $document->docid, '" alt="', $document->title, '">';
echo '</a>';
echo '</div>'; // gallery
?>
<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">
<?=$document->title;?>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<img src="dl.php?id=<?=$document->docid ?>" class="preview-image">
</div>
</div>
</div>
</div>
<?php
// echo '</a>';
echo "</td></tr>\n";
echo '<th scope="row">Image extents</th>';
[$width, $height] = getimagesize($g_doc_basepath . '/' . $techname);
echo '<td>', $width, 'x', $height, 'px';
echo "</td></tr>";
} elseif (($document->doctype == 'drawing')) {
// TODO preview with popup
$techname = sprintf('drw-%06d.%s', $document->docid, $document->extension);
echo '<tr><th scope="row">', _('Drawing'), '</th>';
echo '<td><img src="dl.php?id=', $document->docid, '" alt="', $document->title, '"></td></tr>', "\n";
} elseif (in_array($document->doctype, ['generic', 'manual', 'invoice'])) {
$techname = sprintf('doc-%06d.%s', $document->docid, $document->extension);
if ($document->mimetype == 'application/pdf') {
// read page count from pdf file:
// $pdfpages = pdf_get_pagecount("$g_doc_basepath/$techname");
echo '<tr><th scope="row">', _('PDF'), '</th><td>';
// Download link
echo '<a href="dl.php?id=', $document->docid, '">', $document->title ?? _('Download'), '</a> (', sprintf(_('%d Pages'), $document->pages), ')';
echo "</td></tr>\n";
}
}
if ($document->docsize == 0) {
// update get current size in db
$document->docsize = file_fix_filesize($document->docid, "$g_doc_basepath/$techname");
}
echo '<tr><th scope="row">', _('Mimetype'),"</th><td>", $document->mimetype, "</td></tr>\n";
echo '<tr><th scope="row">', _('Filename'),"</th><td>", $document->filename, "</td></tr>\n";
echo '<tr><th scope="row">', _('File size'),"</th><td>", format_filesize($document->docsize), "</td></tr>\n";
echo '<tr><th scope="row">', _('Upload date'),"</th><td>", $document->doctime, "</td></tr>\n";
echo '<tr><th scope="row">', _('Title'),"</th><td>", $document->title, "</td></tr>\n";
echo '<tr><th scope="row">', _('Remarks'),"</th><td>", $document->remarks, "</td></tr>\n";
echo "</table>\n";
echo "</div>\n", '<div class="col text-center">', "\n"; // column break
if ($document->mimetype == 'application/pdf') {
echo '<img id="preview" src="dl.php?id=', $document->docid, '&p=', $page, '&t=l" alt="PDF-Preview">', "\n";
if ($document->pages > 1) {
$pagination = get_pagination($g_scriptname, 'f=view&id='.$id, $page, $document->pages, true);
echo $pagination;
// $pagination;
}
}
echo "</div>\n</div>\n"; // end of columns
form_view_buttons($g_scriptname, $id);
echo '<h3>', _('Document references'), "</h3>\n";
$references = array();
// Vessel
$vids = array();
$sql = "SELECT drid, reftype, refid, vesselname AS target "
. "FROM docref INNER JOIN vessel ON (docref.reftype='vessel' AND docref.refid=vessel.vid) "
. "WHERE docid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
foreach ($sth->fetchAll() as $row) {
$references[] = $row;
$vids[] = $row['refid'];
}
// Equipment
$eids = array();
$sql = "SELECT drid, reftype, refid, ename AS target "
. "FROM docref INNER JOIN equipment ON (docref.reftype='equipment' AND docref.refid=equipment.eid) "
. "WHERE docid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
foreach ($sth->fetchAll() as $row) {
$references[] = $row;
$eids[] = $row['refid'];
}
// Companies
$compids = array();
$sql = "SELECT drid, reftype, refid, compname AS target "
. "FROM docref INNER JOIN company ON (docref.reftype='company' AND docref.refid=company.compid) "
. "WHERE docid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
foreach ($sth->fetchAll() as $row) {
$references[] = $row;
$compids[] = $row['refid'];
}
echo '<table class="table table-sm">', "\n";
echo "<thead>\n";
echo "<tr>";
echo "<th>#</th>\n";
echo '<th style="width:20%">' . _('Type') . "</th>";
echo "<th>" . _('Used by') . "</th>";
echo "<td></td>";
echo "</tr>\n";
echo "</thead>\n";
$i = 0;
foreach ($references as $ref) {
$i++;
echo "<tr>";
echo "<td>", $i;
if ($ref['reftype'] == 'equipment') {
echo '<a title="', _('View'), '" href="equipment.php?f=view&id=', $ref['refid'],'"><i class="bi-eye ms-2"></i></a>';
} elseif ($ref['reftype'] == 'company') {
echo '<a title="', _('View'), '" href="company.php?f=view&id=', $ref['refid'],'"><i class="bi-eye ms-2"></i></a>';
}
echo "</td>";
echo "<td>", get_enum($ref['reftype']), "</td>";
echo "<td>", $ref['target'], "</td>";
echo "<td>";
echo '<a title="', _('Remove reference'), '" href="', $g_scriptname, '?f=delref&id=', $id, '&drid=', $ref['drid'], '"><i class="bi-trash"></i></a>';
echo "</td>";
echo "</tr>\n";
}
echo "</table>\n";
// New references
echo '<h4>', _('Add new reference'), "</h4>\n";
// enum('vessel','box','equipment','project','task','user','company')
//
// Equipment options
// Do not include equipment that has already been referenced in the list!
$opt_equipment = array();
$sql = "SELECT eid, ename FROM equipment WHERE vid=? ORDER BY ename";
$sth = $pdo->prepare($sql);
$sth->execute([$user->vid]);
foreach ($sth->fetchAll() as $row) {
if (!in_array($row['eid'], $eids)) {
$opt_equipment[$row['eid']] = $row['ename'];
}
}
?>
<form class="mb-3" method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<input type="hidden" name="reftype" value="equipment">
<label for="eid"><?=_('Equipment')?></label>
<select name="refid" id="eid">
<?php
foreach ($opt_equipment as $k => $v) {
echo '<option value="', $k, '">', $v, "</option>\n";
}
?>
</select>
<button class="btn btn-sm btn-secondary" name="submit[addref]">Referenzieren</button>
</form>
<?php
// Company options
// Do not include compamies that have already been referenced in this list
$opt_company = array();
$sql = "SELECT compid, compname FROM company ORDER BY compname";
$sth = $pdo->prepare($sql);
$sth->execute();
foreach ($sth->fetchAll() as $row) {
if (!in_array($row['compid'], $compids)) {
$opt_company[$row['compid']] = $row['compname'];
}
}
?>
<form class="mb-3" method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<input type="hidden" name="reftype" value="company">
<label for="compid"><?=_('Company')?></label>
<select name="refid" id="compid">
<?php
foreach ($opt_company as $k => $v) {
echo '<option value="', $k, '">', $v, "</option>\n";
}
?>
</select>
<button class="btn btn-sm btn-secondary" name="submit[addref]">Referenzieren</button>
</form>
<?php
// Vessel options
// Do not include vessel that has already been referenced in the list!
$opt_vessel = array();
$sql = "SELECT vid, vesselname FROM vessel ORDER BY vesselname";
$sth = $pdo->query($sql);
foreach ($sth->fetchAll() as $row) {
if (!in_array($row['vid'], $vids)) {
$opt_vessel[$row['vid']] = $row['vesselname'];
}
}
?>
<form class="mb-3" method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<input type="hidden" name="reftype" value="vessel">
<label for="vid"><?=_('Vessel')?></label>
<select name="refid" id="vid">
<?php
foreach ($opt_vessel as $k => $v) {
echo '<option value="', $k, '">', $v, "</option>\n";
}
?>
</select>
<button class="btn btn-sm btn-secondary" name="submit[addref]">Referenzieren</button>
</form>
<?php
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
echo '<h2>', _('Edit Document'), "</h2>\n";
$sql = "SELECT docid, doctype, mimetype, filename, extension, hash, title, remarks "
. "FROM document "
. "WHERE docid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$document = $sth->fetch(PDO::FETCH_OBJ);
if ($document->doctype == 'picture') {
echo '<img width="', $g_thumb_size, '" src="dl.php?id=', $document->docid, '">';
}
?>
<form method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<div class="mb-3">
<label for="title" class="form-label"><?=_('Filename')?></label>
<input type="text" class="form-control" id="filename" name="filename" value="<?=$document->filename ?>">
</div>
<div class="mb-3">
<label for="title" class="form-label"><?=_('Title')?></label>
<input type="text" class="form-control" id="title" name="title" value="<?=$document->title ?>">
</div>
<?php
if ($document->mimetype == 'application/pdf') {
echo '<div class="mb-3">', "\n";
$opt_doctype = array(
'generic' => _('Generic'),
'manual' => _('Manual'),
'invoice' => _('Invoice')
) ;
form_create_select('doctype', _('Document type'), $opt_doctype, $document->doctype);
echo "</div>";
} else {
echo '<input type="hidden" name="doctype" value="', $document->doctype, '">', "\n";
}
?>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=$document->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 ==========================================
$sql = "SELECT docid, doctype, mimetype, filename, extension, title "
. "FROM document "
. "WHERE docid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$document = $sth->fetch(PDO::FETCH_OBJ);
$sql = "SELECT COUNT(*) FROM docref WHERE docid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$nref = $sth->fetchColumn();
echo '<h2>', _('Delete Document'), "</h2>\n";
echo '<p>', sprintf(_('Record no. %d'), $id), "</p>\n";
echo '<p>Dokument: ', $document->filename, "</p>\n";
echo '<p>Title: ', $document->title, "</p>\n";
echo '<p>References: ', $nref, "<p>\n";
echo '<p>', _('Deleting a document item is final. There is no way back. Only delete if you are absolute sure.'), "</p>\n";
$_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';