Files
YMS/webgui/tag.php
T

302 lines
9.5 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 = _('Tags');
$id = gpc_get_int($_REQUEST, 'id', 0);
// ========== 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['txt'] = gpc_get_string($_POST, 'flt_txt');
db_save_filter($flt, 210);
$action = ACT_DEFAULT;
break;
case 'freset':
db_clear_filter(210);
$action = ACT_DEFAULT;
break;
case 'insert':
$p[':tagname'] = gpc_get_string($_POST, 'tagname');;
$id = db_exec_insert('tag', $p);
$action = ACT_VIEW;
break;
case 'update':
$p[':tagid'] = $id;
$p[':tagname'] = gpc_get_string($_POST, 'tagname');
$p[':color'] = gpc_get_color($_POST, 'color');
$p[':description'] = gpc_get_string($_POST, 'description');
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 =======================================
// load filter from db
$flt = db_get_filter($user->id, 210, ['txt']);
echo "<h1>$pagetitle</h1>\n";
// db_save_taglist('inv', 1, 'apple, banana , , , orange, ddd');
// 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">
<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
$sql = "SELECT tagid, tagname, color, description "
. "FROM tag "
. "ORDER BY tagname";
$sth = $pdo->query($sql);
$res = $sth->fetchAll();
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>\n";
echo '<th>#</th>';
echo '<th>', _('Tag'), "</th>";
echo '<th>', _('Description'), "</th>";
echo '<th>', _('Color'), "</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['tagid'], '"><i class="bi bi-eye ms-2"></i></a>', "\n";
echo "</td>\n";
echo "<td>", $row['tagname'], "</td>\n";
echo "<td>", $row['description'], "</td>\n";
echo "<td>", format_color($row['color']), "</td>\n";
echo "<td>";
// Edit current record button
echo "<td>";
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['tagid'], '"><i class="bi-pencil"></i></a>', "\n";
echo "</td>\n";
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 Tag'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<div class="mb-3">
<label for="tagname" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="tagname" name="tagname" required autofocus>
</div>
<div class="mb-3">
<label for="note" class="form-label"><?=_('Description')?></label>
<input type="text" class="form-control" id="description" name="description">
</div>
<?php
form_new_buttons($g_scriptname);
echo "</form>\n";
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
$sql = "SELECT tagname, description, color "
. "FROM tag "
. "WHERE tagid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$tag = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('View Tag'), "</h2>\n";
echo '<table class="table">', "\n";
echo '<tr><th style="width:20%">', _('Tag'),"</th><td>", $tag->tagname, "</td></tr>\n";
echo '<tr><th>', _('Color'),"</th><td>", format_color($tag->color), "</td></tr>\n";
echo '<tr><th>', _('Description'),"</th><td>", $tag->description, "</td></tr>\n";
echo "</table>\n";
form_view_buttons($g_scriptname, $id);
echo '<h3>', _('Tag usage'), "</h3>\n";
$sql = "SELECT objid, objtype FROM tagref WHERE tagid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
echo '<p>';
if ($sth->rowCount() > 0) {
$res = $sth->fetchAll();
foreach ($res as $row) {
// select object name or description depending on objecttype
// TODO function db_get_tag_target($objtype, $objid);
/*switch ($row['objtype']) {
case 'equip':
$sql = "SELECT ename FROM equipment WHERE eid=?";
$target = "equipment.php";
break;
case 'inv':
$sql = "SELECT invname FROM inventory WHERE invid=?";
$target = "inventory.php";
break;
case 'prov':
$sql = "SELECT provname FROM provisions WHERE provvid=?";
$target = "provisions.php";
break;
case 'doc':
$sql = "SELECT title FROM document WHERE docid=?";
$target = "documents.php";
break;
case 'proj':
$sql = "SELECT projname FROM project WHERE projid=?";
$target = "projects.php";
break;
case 'task':
$sql = "SELECT taskname FROM task WHERE taskid=?";
$target = "task.php";
break;
case 'maint':
$sql = "SELECT activities FROM maintenance WHERE maintid=?";
$target = 'maintenance.php';
break;
}
$sth = $pdo->prepare($sql);
$sth->execute([$row['objid']]);
$desc = $sth->fetchColumn(); */
[$target, $desc] = db_get_tag_target($row['objtype'], $row['objid']);
echo '<a href="', $target, '?f=view&id=', $row['objid'], '">', $desc, ' (', $row['objid'], '/', $row['objtype'], ")</a>\n";
}
} else {
echo _('Tag is not used anywhere');
}
echo "</p>\n";
echo '<h3>', _('Related tags'), "</h3>\n";
echo '<p>', _('Not yet implemented'), "</p>\n";
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT tagname, description, color "
. "FROM tag "
. "WHERE tagid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$tag = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('Edit tag'), "</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"><?=_('Tag')?></label>
<input type="text" class="form-control" id="tagname" name="tagname" value="<?=$tag->tagname ?>">
</div>
<div class="mb-3">
<label for="note" class="form-label"><?=_('Description')?></label>
<input type="text" class="form-control" id="description" name="description" value="<?=$tag->description ?>">
</div>
<div class="mb-3">
<label for="color" class="form-label"><?=_('Color')?></label>
<input type="color" name="color" id="color" class="form-control" value="#<?=$tag->color ?>">
</div>
<?php
form_edit_buttons($g_scriptname, $id);
echo "</form>\n";
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
echo '<h2>', _('Delete tag'), "</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="', [$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';