Webgui fixed cables and fuses, parts of code reorganized

This commit is contained in:
thooge committed 2026-10-25 09:03:24 +01:00
1 parent 9d09b1e120
commit 5641e1706f
25 files changed
+497 -427

No files matched your search

+84
View File
@@ -10,6 +10,90 @@
// ========== GENERIC FUNCTIONS ===============================================
function db_exec_insert($table, $params) {
// $params must have keys corresponding to table fields prefixed with ':'
// e.g. $param[':field']
global $pdo;
global $g_error;
$keys = array_keys($params);
$fields = join(', ', array_map(function($s) { return ltrim($s, ':'); }, $keys));
$pnames = join(', ', $keys);
$sql = "INSERT INTO $table ($fields) VALUES ($pnames)";
// $sth = $pdo->prepare("INSERT INTO $table ($fields) VALUES ($pnames)");
$sth = $pdo->prepare($sql);
try {
$sth->execute($params);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
$g_error->Add($sql);
$g_error->Add(print_r($params, true));
return false;
}
return $pdo->LastInsertId();
}
function db_exec_update($table, $params, $pk) {
// primary key can be a single field name or multiple fields: $pk is an array
global $pdo;
global $g_error;
// remove primary key fields from list of fields to update
if (is_array($pk)) {
if (count($pk) == 1) {
$pk = $pk[0];
$fields = $params;
unset($fields[':'.$pk]);
$where = "$pk=:$pk";
} else {
$pkparams = array_map(fn($value) => ':' . $value, $pk);
$fields = array_diff_key($params, array_flip($pkparams));
$where = join(' AND ', array_map(fn($s) => "$s=:$s", $pk));
}
} else {
$fields = $params;
unset($fields[':'.$pk]);
$where = "$pk=:$pk";
}
$fieldlist = join(', ', array_map(fn($s) => ltrim($s, ':') . '=' . $s, array_keys($fields)));
$sql = "UPDATE $table SET $fieldlist WHERE $where";
$sth = $pdo->prepare($sql);
try {
$sth->execute($params);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
$g_error->Add($sql);
$g_error->Add(print_r($params, true));
return false;
}
return true;
}
function db_exec_delete($table, $condition, $params) {
// WIP untested!
// condition contains the where part of the sql statement
// params are values for placeholders in condition
// returns the number of deleted records
global $pdo;
global $g_error;
// convert params to array if single value is given
if (!is_array($params)) {
$params = [$params];
}
$sql = "DELETE FROM $table";
if (!empty($condition)) {
$sql .= ' WHERE ' . $condition;
}
$sth = $pdo->prepare($sql);
try {
$sth->execute($params);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
$g_error->Add($sql);
$g_error->Add(print_r($params, true));
return -1;
}
return $sth->rowCount();
}
function db_get_setting($sno, $userid, $autocreate=false, $default=NULL) {
global $pdo;
global $g_error;
+256
View File
@@ -0,0 +1,256 @@
<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024-2026 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************
*/
// ========== FORM FUNCTIONS ==================================================
function form_get_action() {
if (!isset($_POST['submit'])) {
if (isset($_GET['f'])) {
$submit = $_GET['f'];
// strip parameters
/* $pos = strpos($submit, '?');
if ($pos > 2) {
$submit = substr($submit, 0, $pos);
} */
} else {
$submit = NULL;
}
} else {
$submit = $_POST['submit'];
}
if (is_array($submit)) {
$submit = key($submit);
}
return strtolower($submit);
}
function form_tag_assignment($script, $id, $tags) {
echo '<tr>';
echo '<th>', _('Tags'), "</th>\n";
echo '<td>', $tags, '</td>';
echo "</tr>\n<tr>";
echo '<th>', _('Tag assignment'), "</th>\n";
echo '<td><form method="post" action="', $script, '">', "\n";
echo '<div class="form-floating">';
echo '<input type="hidden" name="id" value="', $id .'">', "\n";
echo '<label>', _('(Separate by comma)'), '</label>', "\n";
echo '<input type="text" maxlength="80" class="form-control" name="tagstring" value="', $tags, '">', "\n";
echo '<input type="submit">', "\n";
echo "</div></form></td></td>\n";
echo "</tr>";
}
// ========== FORM FIELDS =====================================================
function form_create_numeric($fieldname, $label, $min=NULL, $max=NULL, $step=NULL, $value=NULL) {
// TODO WIP
// step = any float
// step = 0.01 float
// step = 1 integer
echo '<div class="mb-3">', "\n";
echo '<input type="number"';
echo ">\n";
echo "</div>\n";
}
function form_create_text($fieldname, $label, $maxlen=NULL, $value=NULL) {
// create generic text entry with assigned label
// TODO required, errortext
echo '<div class="mb-3">', "\n";
echo '<label for="', $fieldname, '" class="form-label">', $label, "</label>\n";
echo '<input type="text" class="form-control" id="', $fieldname, '" name="', $fieldname, '"';
if ($maxlen !== NULL) {
echo ' maxlength="', $maxlen, '"';
}
if ($value !== NULL) {
echo ' value="', h($value), '"';
}
echo ">\n";
echo "</div>\n";
}
function form_create_textarea($fieldname, $label, $rows=3, $maxlen=NULL, $value=NULL) {
echo '<div class="mb-3">', "\n";
echo '<label for="', $fieldname, '" class="form-label">', $label, "</label>\n";
echo '<textarea class="form-control" id="', $fieldname, '" name="', $fieldname, '"';
echo ' rows="', $rows, '"';
if ($maxlen !== NULL) {
echo ' maxlength="', $maxlen, '"';
}
echo '>';
if ($value !== NULL) {
echo h($value);
}
echo "</textarea>\n";
echo "</div>\n";
}
function form_create_color($fieldname, $label, $value=NULL) {
// value has to be 6char hex
echo '<div class="mb-3">', "\n";
echo '<label for="', $fieldname, '" class="form-label">', $label, "</label>\n";
echo '<input type="color" class="form-control" id="', $fieldname, '" name="', $fieldname, '"';
if ($value !== NULL) {
echo ' value="#', $value, '"';
}
echo ">\n";
echo "</div>\n";
}
function form_create_select($fieldname, $label, $optlist, $selval=NULL, $multiple=FALSE) {
echo '<div class="mb-3">', "\n";
echo '<label for="', $fieldname, '">', $label, "</label>\n";
echo '<select class="form-select" name="', $fieldname,'" id="', $fieldname, $multiple ? ' multiple' : '', '">', "\n";
foreach ($optlist as $k => $v) {
echo '<option value="', $k, '"';
if (isset($selval) and ($k == $selval)) {
echo ' selected';
}
echo '>', $v, "</option>\n";
}
echo "</select>\n";
echo "</div>\n";
}
function form_create_option($fieldname, $label) {
// TBD
}
function form_create_check($fieldname, $label, $checked) {
// create single checkbox with assigned label
echo '<div class="mb-3">', "\n";
echo '<input type="checkbox" class="form-check-input" name="', $fieldname, '" id="', $fieldname, '"';
if ($checked) {
echo ' checked';
}
echo ">\n";
echo '<label for="', $fieldname, '">', $label, "</label>\n";
echo "</div>\n";
}
function form_create_checks($fieldname, $label, $optlist, $selected = []) {
// create multiple combined checkboxes with title
echo '<fieldset class="mb-3">', "\n";
echo '<legend class="fs-6 mb-1 border-bottom pb-1">', $label, "</legend>\n";
echo '<div class="d-flex flex-wrap gap-3">';
foreach ($optlist as $k => $v) {
echo '<div class="form-check">', "\n";
$checked = in_array($k, $selected, true) ? ' checked' : '';
echo '<label class="form-check-label">';
echo '<input type="checkbox" class="form-check-input" name="', $fieldname, '[', $k, ']"', $checked, '>';
echo $v;
echo '</label>';
echo "</div>\n";
}
echo "</div>\n";
echo "</fieldset>\n";
}
// ========== FORM BUTTONS ====================================================
function form_add_button($script, $id=NULL, $idname='id') {
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<a href="', $script, '?f=add';
if (isset($id)) {
echo '&'.$idname.'='.$id;
}
echo '" class="btn btn-primary" role="button">', _('Add'), "</a>\n";
echo "</div>\n";
}
function form_view_buttons($script, $id, $backlink=NULL) {
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<a href="', $script, '?f=edit&id=', $id, '" class="btn btn-primary" role="button">', _('Edit'), "</a>\n";
echo '&nbsp;';
echo '<a href="', $script, '?f=del&id=', $id, '" class="btn btn-secondary" role="button">', _('Delete'), "</a>\n";
echo '&nbsp;';
echo '<a href="', $backlink ?? $script, '" class="btn btn-secondary">', _('Back'), "</a>\n";
echo "</div>\n";
}
function form_new_buttons($script) {
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<button type="submit" name="submit[insert]" class="btn btn-primary">', _('Save'), "</button>\n";
echo '<a href="', $script, '" class="btn btn-secondary">', _('Back'), "</a>\n";
echo "</div>\n";
}
function form_edit_buttons($script, $id) {
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<button type="submit" name="submit[update]" class="btn btn-primary">', _('Save'), "</button>\n";
echo '<a href="', $script, '?f=view&id=', $id, '" class="btn btn-secondary">', _('Back'), "</a>\n";
echo "</div>\n";
}
function form_delete_buttons($script, $id, $token) {
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<form method="post" action="', $script, '">', "\n";
echo '<input type="hidden" name="id" value="', $id .'">', "\n";
echo '<input type="hidden" name="token" value="', $token, '">', "\n";
echo '<button type="submit" name="submit[delete]" class="btn btn-primary">', _('Delete'), "</button>\n";
echo '<a href="', $script, '?f=view&id=', $id, '" class="btn btn-secondary">', _('Back'), "</a>\n";
echo "</form>\n";
echo "</div>\n";
}
// ========== FILTER FIELDS ===================================================
function filter_create_text($fieldname, $label, $value, $size=NULL, $maxlength=NULL) {
echo '<div class="d-flex flex-nowrap gap-2 align-items-start">', "\n";
echo '<label for="', $fieldname,'">', $label,'</label>', "\n";
echo '<input class="form-control" type="text" name="', $fieldname, '"';
if (isset($size)) {
echo ' size="', $size,'"';
}
if (isset($maxlength)) {
echo ' maxlength="', $maxlength, '"';
}
echo ' value="', h($value), '">', "\n";
echo "</div>\n";
}
function filter_create_select($fieldname, $label, $optlist, $selval=NULL, $multiple=FALSE) {
// - Wenn multiple gesetzt ist, muß selval ein array sein
// ansonsten ein einfacher String
/* if ($multiple) {
if (!is_array($selval)) {
}
} */
echo '<div class="d-flex flex-nowrap gap-2 align-items-start">', "\n";
echo '<label for="', $fieldname,'">', $label,'</label>', "\n";
echo '<select class="form-select" id="', $fieldname, '" name="', $fieldname, '"', $multiple ? ' multiple' : '', ">\n";
foreach ($optlist as $k => $v) {
echo '<option value="', $k, '"';
if ($k == $selval) {
echo ' selected';
}
echo '>', $v, '</option>', "\n";
}
echo "</select>\n";
echo "</div>\n";
}
function filter_create_checks($fieldname, $label, $optlist, $selected = []) {
// TODO WIP
echo '<fieldset class="mb-3">', "\n";
echo '<legend class="fs-6 mb-1 border-bottom pb-1">', $label, "</legend>\n";
echo '<div class="d-flex flex-wrap gap-3">', "\n";
foreach ($optlist as $k => $v) {
echo '<div class="form-check">', "\n";
$checked = in_array($k, $selected, true) ? ' checked' : '';
echo '<label class="form-check-label">', "\n";
echo '<input type="checkbox" class="form-check-input" name="', $fieldname, '[', $k, ']"', $checked, ">\n";
echo $v, "\n";
echo "</label>\n";
echo "</div>\n";
}
echo "</div>\n";
echo "</fieldset>\n";
}