288 lines
9.6 KiB
PHP
288 lines
9.6 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 = _('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 ' ';
|
|
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');
|
|
db_exec_insert('dropdown', $p);
|
|
$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;
|
|
|
|
case 'delete':
|
|
// only deletable if not used anywhere!
|
|
// equipment.?
|
|
if ($v <= 0) {
|
|
$g_warning->Add(_('Selection lists are not deletable!'));
|
|
}
|
|
$g_message->Add(_('Delete not yet implemented'));
|
|
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 '<th></th>';
|
|
echo '</tr>';
|
|
echo '</thead>';
|
|
foreach ($sth->fetchAll() as $row) {
|
|
echo '<tr><td>', $row['ddval'];
|
|
echo '<a class="text-nowrap" title="', _('View'), '" href="', $g_scriptname, '?f=view&id=', $row['ddval'], '"><i class="bi bi-eye ms-2"></i></a>';
|
|
echo '</td>';
|
|
echo '<td>', $row['ddtext'], '</td>';
|
|
echo '<td>', $num[$row['ddval']] ?? 0, '</td>';
|
|
// form_action_buttons($g_scriptname, $row['ddval']);
|
|
echo "<td>";
|
|
echo '<a class="text-nowrap" title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['ddval'], '"><i class="bi bi-pencil ms-2"></i></a>';
|
|
echo "</td>\n";
|
|
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, flags 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>', _('Flags'), "</th>";
|
|
echo "<th></th>\n";
|
|
echo "</tr>\n";
|
|
echo "</thead>\n";
|
|
|
|
foreach ($sth->fetchAll() as $row) {
|
|
$flags = explode(',', $row['flags']);
|
|
$fixed = in_array('fixed', $flags);
|
|
echo '<tr>';
|
|
echo '<td>', $row['ddval'],'</td>';
|
|
echo '<td>', $row['ddtext'], '</td>';
|
|
echo '<td>', $row['ddshort'], '</td>';
|
|
if ($row['color']) {
|
|
echo '<td>', format_color($row['color']), '</td>';
|
|
} else {
|
|
echo '<td></td>';
|
|
}
|
|
echo '<td>', $row['sort'], '</td>';
|
|
echo '<td>', ($fixed ? '<i class="bi bi-lock"></i>' : ''), '</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>', format_color($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 ' ';
|
|
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';
|