First web gui prototype

This commit is contained in:
2024-04-09 10:26:32 +02:00
parent 0b534fa727
commit 793eda4bc4
39 changed files with 6760 additions and 0 deletions
+271
View File
@@ -0,0 +1,271 @@
<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
require 'globals.inc';
$pagetitle = _('Lists');
$id = gpc_get_int($_REQUEST, 'id', 0); // list id
$v = gpc_get_int($_REQUEST, 'v', -1); // list value id
function create_ddval_form($script, $ddid, $formtype, $row=NULL) {
// formtype: insert or update
$update = ($formtype == 'update');
echo '<form method="post" action="', $script, '">';
echo '<input type="hidden" name="id" value="', $ddid, '">';
if ($update) {
echo '<input type="hidden" name="v" value="', $row['ddval'], '">';
}
// text in one row
echo '<div class="form-floating">';
echo '<input type="text" name="ddtext" id="ddtext" class="form-control"';
if ($update) echo ' value="', $row['ddtext'],'"';
echo '><label for="ddtext">Description</label></div>', "\n";
// all other fields in secondary row
echo '<div class="row my-3">';
echo '<div class="col"><div class="form-floating">';
echo '<input type="text" name="ddshort" id="ddshort"class="form-control"';
if ($update) echo ' value="', $row['ddshort'],'"';
echo '><label for="ddshort">Short description</label></div></div>', "\n";
echo '<div class="col"><div class="form-floating">';
echo '<input type="color" name="color" id="color" class="form-control"';
if ($update) echo ' value="', $row['color'],'"';
echo '><label for="color">Color</label></div></div>', "\n";
echo '<div class="col"><div class="form-floating">';
echo '<input type="text" name="sort" id="sort" class="form-control"';
if ($update) echo ' value="', $row['sort'],'"';
echo '><label for="sort">Sort</label></div></div>', "\n";
echo '</div>'; // finish row
// buttons
echo '<button name="submit" value="', $formtype, '" class="btn btn-primary btn-sm" title="Save"><i class="bi-save"></i> Save</button>';
echo '&nbsp';
echo '<a href="', $script, '?f=edit&id=', $ddid, '" class="btn btn-secondary btn-sm" title="Cancel"><i class="bi-x-square"></i> Cancel</a>';
echo "</form>\n";
}
// ========== ACTIONS START ===================================================
switch ($submit = form_get_action()) {
case NULL: break;
case 'add': $v = -2; $action = ACT_EDIT; break;
case 'view': $action = ACT_VIEW; break;
case 'edit': $action = ACT_EDIT; break;
case 'del': $action = ACT_DELETE; break;
case 'insert':
$p[':ddid'] = gpc_get_int($_POST, 'id');
$sth = $pdo->prepare("SELECT MAX(ddval)+1 FROM dropdown WHERE ddid=?");
$sth->execute([$p[':ddid']]);
$p[':ddval'] = $sth->fetchColumn() ?? 1;
$p[':ddtext'] = gpc_get_string($_POST, 'ddtext');
$p[':ddshort'] = gpc_get_string($_POST, 'ddshort');
$p[':color'] = gpc_get_color($_POST, 'color');
$p[':sort'] = gpc_get_string($_POST, 'sort');
$keys = array_keys($p);
$fields = join(',', array_map(function($s) { return ltrim($s, ':'); }, $keys));
$pnames = join(',', $keys);
$sth = $pdo->prepare("INSERT INTO dropdown ($fields) VALUES ($pnames)");
try {
$sth->execute($p);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
$action = ACT_EDIT;
break;
case 'update':
$p[':ddtext'] = gpc_get_string($_POST, 'ddtext');
$p[':ddshort'] = gpc_get_string($_POST, 'ddshort');
$p[':color'] = gpc_get_color($_POST, 'color');
$p[':sort'] = gpc_get_string($_POST, 'sort');
$fields = join(',', array_map(function($s) { return ltrim($s, ':') . '=' . $s; }, array_keys($p)));
$sth = $pdo->prepare("UPDATE dropdown SET $fields WHERE ddid=:ddid AND ddval=:ddval");
$p[':ddid'] = gpc_get_int($_POST, 'id');
$p[':ddval'] = gpc_get_int($_POST, 'v');
try {
$sth->execute($p);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
unset($v);
$action = ACT_EDIT;
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 =======================================
// record counts
$num = array();
$sql = "SELECT ddid, COUNT(*) AS n FROM dropdown WHERE ddid>0 GROUP BY ddid";
$sth = $pdo->query($sql);
foreach ($sth->fetchAll(PDO::FETCH_NUM) as $row) {
$num[$row[0]] = $row[1];
}
$sql = "SELECT ddval, ddtext FROM dropdown WHERE ddid=0 AND ddval>0";
$sth = $pdo->query($sql);
echo "<h1>$pagetitle</h1>\n";
echo '<p>', _('Danger! Only adjust if it is clear what impact this will have!'), "<br>\n";
// <p>Achtung! Nur anpassen wenn klar ist was das für Auswirkungen hat!</p>
echo _('Adding values is usually not critical.'), "</p>\n";
// <p>Hinzufügen von Werten ist in der Regel unkritisch.</p>
echo '<table class="table">', "\n";
echo '<thead>';
echo '<tr>';
echo '<th>#</th>';
echo '<th>', _('Description'),'</th>';
echo '<th>', _('Number'),'</th>';
echo '<td></td>';
echo '</tr>';
echo '</thead>';
foreach ($sth->fetchAll() as $row) {
echo '<tr><td>', $row['ddval'],'</td>';
echo '<td>', $row['ddtext'], '</td>';
echo '<td>', $num[$row['ddval']] ?? 0, '</td>';
form_action_buttons($g_scriptname, $row['ddval']);
echo "</tr>\n";
}
echo "</table>\n";
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
$sth = $pdo->prepare("SELECT ddtext FROM dropdown WHERE ddid=0 AND ddval=?");
$sth->execute([$id]);
$listname = $sth->fetchColumn();
echo '<h2>', sprintf(_('View List: %s'), $listname), "</h2>\n";
$sql = "SELECT ddval, ddtext, ddshort, color, sort FROM dropdown WHERE ddid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
echo '<table class="table">';
echo "<thead>\n";
echo "<tr>\n";
echo '<th>#</th>';
echo '<th>', _('Name'), "</th>";
echo '<th>', _('Short'), "</th>";
echo '<th>', _('Color'), "</th>";
echo '<th>', _('Sort'), "</th>";
echo "<th></th>\n";
echo "</tr>\n";
echo "</thead>\n";
foreach ($sth->fetchAll() as $row) {
echo '<tr>';
echo '<td>', $row['ddval'],'</td>';
echo '<td>', $row['ddtext'], '</td>';
echo '<td>', $row['ddshort'], '</td>';
echo '<td>', $row['color'], '</td>';
echo '<td>', $row['sort'], '</td>';
echo "</tr>\n";
}
echo "</table>\n";
echo '<a href="', $g_scriptname, '" class="btn btn-secondary">', _('Back'), "</a>\n";
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sth = $pdo->prepare("SELECT ddtext FROM dropdown WHERE ddid=0 AND ddval=?");
$sth->execute([$id]);
$listname = $sth->fetchColumn();
echo '<h2>', sprintf(_('Edit List: %s'), $listname), "</h2>\n";
$sql = "SELECT ddval, ddtext, ddshort, color, sort FROM dropdown WHERE ddid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
echo '<table class="table">';
echo "<thead>\n";
echo "<tr>\n";
echo '<th>#</th>';
echo '<th>', _('Name'), "</th>";
echo '<th>', _('Short'), "</th>";
echo '<th>', _('Color'), "</th>";
echo '<th>', _('Sort'), "</th>";
echo "<th></th>\n";
echo "</tr>\n";
echo "</thead>\n";
foreach ($sth->fetchAll() as $row) {
echo '<tr>';
if ($row['ddval'] == $v) {
echo '<td>', $row['ddval'], '</td>';
echo '<td colspan="4">';
create_ddval_form($g_scriptname, $id, 'update', $row);
echo '</td>';
echo "<td></td>\n";
} else {
echo '<td>', $row['ddval'],'</td>';
echo '<td>', $row['ddtext'], '</td>';
echo '<td>', $row['ddshort'], '</td>';
echo '<td>', $row['color'], '</td>';
echo '<td>', $row['sort'], '</td>';
// Action links
echo '<td>';
echo '<a href="', $g_scriptname, '?f=edit&id=', $id, '&v=', $row['ddval'], '"><i class="bi-pencil"></i></a>';
echo '&nbsp;';
echo '<i class="bi-trash"></i>';
echo '</td>', "\n";
// TODO extend function to accept additional parameters
// form_action_buttons($g_scriptname, $row['ddid'];
}
echo "</tr>\n";
}
if ($v == -2) {
echo '<tr>';
echo '<td>(+)</td>';
echo '<td colspan="4">';
create_ddval_form($g_scriptname, $id, 'insert');
echo '</td>';
echo "<td></td>\n";
echo "</tr>";
}
echo "</table>\n";
echo '<a href="', $g_scriptname, '?f=add&id=', $id, '" class="btn btn-primary">', _('Add'), "</a>\n";
echo '<a href="', $g_scriptname, '" class="btn btn-secondary">', _('Back'), "</a>\n";
else:
// ========== ERROR UNKNOWN VARIANT ===========================================
echo '<p>', _('Unknown function call: Please report to system development!'), "</p>\n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
include 'footer.php';