Merge pull request #4 from Terranom674/fix/dialog-validation-09611

0.9.6.11: Dialog bei Fehler offen halten und Felder markieren
This commit is contained in:
Terranom674
2026-08-19 13:59:01 +02:00
committed by GitHub
3 changed files with 170 additions and 2 deletions

View File

@@ -78,6 +78,89 @@
+ '<button type="button" class="buttonLike" data-remove-storage>Entfernen</button></div>';
}
function clearValidation(form) {
[].slice.call(form.querySelectorAll('[aria-invalid="true"]')).forEach(function (field) {
field.removeAttribute('aria-invalid');
field.style.borderColor = '';
field.style.boxShadow = '';
});
[].slice.call(form.querySelectorAll('[data-edit-v2-field-error]')).forEach(function (node) { node.remove(); });
var box = form.querySelector('[data-edit-v2-error-summary]');
if (box) box.remove();
}
function fieldNodes(form, fieldName) {
return [].slice.call(form.querySelectorAll('[name="'+fieldName.replace(/"/g, '\\"')+'"]'));
}
function showValidation(form, data) {
clearValidation(form);
var message = (data && data.message) ? data.message : 'Die Verbindung konnte nicht gespeichert werden.';
var summary = document.createElement('div');
summary.dataset.editV2ErrorSummary = '1';
summary.className = 'bratonien-main-cache__warning';
summary.style.margin = '0 0 1rem';
summary.style.padding = '.75rem';
summary.style.border = '1px solid currentColor';
summary.innerHTML = '<strong>Speichern fehlgeschlagen:</strong> '+escapeHtml(message);
form.insertBefore(summary, form.firstChild);
var first = null;
var fields = data && Array.isArray(data.fields) ? data.fields : [];
fields.forEach(function (fieldName) {
fieldNodes(form, fieldName).forEach(function (field) {
field.setAttribute('aria-invalid', 'true');
field.style.borderColor = '#d65a5a';
field.style.boxShadow = '0 0 0 1px #d65a5a';
if (!first) first = field;
var note = document.createElement('span');
note.dataset.editV2FieldError = '1';
note.className = 'bratonien-main-cache__warning';
note.style.display = 'block';
note.style.marginTop = '.25rem';
note.textContent = message;
field.insertAdjacentElement('afterend', note);
});
});
if (first) {
first.scrollIntoView({block:'center', behavior:'smooth'});
window.setTimeout(function () { first.focus(); }, 150);
} else {
summary.scrollIntoView({block:'center', behavior:'smooth'});
}
}
function submitEditor(form, editDialog) {
clearValidation(form);
if (!form.reportValidity()) return;
var submit = form.querySelector('button[type="submit"]');
if (submit) submit.disabled = true;
var body = new FormData(form);
fetch('plugins/bratonien_tools/nc-connector-edit-save.php', {
method:'POST',
credentials:'same-origin',
cache:'no-store',
headers:{'Accept':'application/json'},
body:body
}).then(function (response) {
return response.json().then(function (data) {
if (!response.ok || !data.ok) {
showValidation(form, data);
return;
}
editDialog.close();
window.location.reload();
});
}).catch(function (error) {
showValidation(form, {message:error.message || String(error), fields:[]});
}).finally(function () {
if (submit) submit.disabled = false;
});
}
function openEditor(id) {
var editDialog = dialog();
var content = editDialog.querySelector('[data-edit-v2-content]');
@@ -132,7 +215,7 @@
+ '<label class="bratonien-label">Maximale Wartezeit (Sek.)</label><input name="nc_max_wait_seconds" type="number" min="60" value="'+escapeHtml(legacy.max_wait_seconds)+'">'
+ '<label class="bratonien-label">Vollprüfung nach (Sek.)</label><input name="nc_full_sync_seconds" type="number" min="300" value="'+escapeHtml(legacy.full_sync_seconds)+'">'
+ '</div></details>'
+ '<div class="bratonien-actions" style="margin-top:1rem"><button class="buttonLike" type="submit" name="bratonien_tool" value="nc_connector_update_local">Änderungen prüfen und speichern</button><button class="buttonLike" type="button" data-edit-v2-cancel>Abbrechen</button></div>'
+ '<div class="bratonien-actions" style="margin-top:1rem"><button class="buttonLike" type="submit">Änderungen prüfen und speichern</button><button class="buttonLike" type="button" data-edit-v2-cancel>Abbrechen</button></div>'
+ '</form>';
var form = content.querySelector('[data-edit-v2-form]');
@@ -146,6 +229,11 @@
var row = remove.closest('[data-storage-row]');
if (row) row.remove();
});
form.addEventListener('submit', function (event) {
event.preventDefault();
event.stopPropagation();
submitEditor(form, editDialog);
});
}).catch(function (error) {
content.innerHTML = '<p class="bratonien-main-cache__warning"><strong>Bearbeiten nicht möglich:</strong> '+escapeHtml(error.message || String(error))+'</p>';
});

View File

@@ -1,7 +1,7 @@
<?php
/*
Plugin Name: Bratonien Tools
Version: 0.9.6.10
Version: 0.9.6.11
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
Author: Bratonien

View File

@@ -0,0 +1,80 @@
<?php
$piwigo_root = realpath(dirname(__DIR__, 2));
if ($piwigo_root === false)
{
http_response_code(500);
exit;
}
define('PHPWG_ROOT_PATH', rtrim($piwigo_root, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR);
include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store, max-age=0');
function bratonien_tools_nc_edit_json($status, array $payload)
{
http_response_code((int)$status);
echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
if (!defined('BRATONIEN_TOOLS_PATH'))
{
bratonien_tools_nc_edit_json(404, array('ok'=>false, 'message'=>'Bratonien Tools ist nicht aktiv.', 'fields'=>array()));
}
if (!function_exists('is_admin') || !is_admin())
{
bratonien_tools_nc_edit_json(403, array('ok'=>false, 'message'=>'Administratorrechte erforderlich.', 'fields'=>array()));
}
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST')
{
bratonien_tools_nc_edit_json(405, array('ok'=>false, 'message'=>'Nur POST ist erlaubt.', 'fields'=>array()));
}
try
{
check_pwg_token();
require_once(BRATONIEN_TOOLS_PATH.'include/tool_registry.inc.php');
$result = bratonien_tools_nc_connector_update_local_friendly();
bratonien_tools_nc_edit_json(200, array(
'ok'=>true,
'message'=>(string)($result['message'] ?? 'Verbindung wurde gespeichert.'),
'fields'=>array(),
));
}
catch (Throwable $e)
{
$message = trim($e->getMessage());
if ($message === '') $message = 'Die Verbindung konnte nicht gespeichert werden.';
$fields = array();
$add = function($name) use (&$fields)
{
if (!in_array($name, $fields, true)) $fields[] = $name;
};
if (stripos($message, 'Name') !== false && stripos($message, 'Datenbankname') === false) $add('connection_name');
if (stripos($message, 'Datenbank-Server') !== false) $add('nc_host');
if (stripos($message, 'Datenbank-Port') !== false) $add('nc_port');
if (stripos($message, 'Datenbankname') !== false) $add('nc_database');
if (stripos($message, 'Reader-Benutzer') !== false) $add('nc_user');
if (stripos($message, 'Datenbankpasswort') !== false) $add('nc_db_password');
if (stripos($message, 'Storage-ID') !== false) $add('nc_storage_id[]');
if (stripos($message, 'lokaler Pfad') !== false || stripos($message, 'Speicherort') !== false) $add('nc_local_mount[]');
if (stripos($message, 'Galerieordner') !== false) $add('nc_gallery_root');
if (stripos($message, 'Datenbankansichten') !== false || stripos($message, 'Source-View') !== false) $add('nc_source_view');
if (stripos($message, 'Datenbankansichten') !== false || stripos($message, 'Activity-View') !== false) $add('nc_activity_view');
if (stripos($message, 'Nextcloud-Adresse') !== false || stripos($message, 'Unter dieser Adresse') !== false) $add('nc_nextcloud_url');
if (stripos($message, 'Nextcloud-Benutzer') !== false || stripos($message, 'Benutzername oder Passwort') !== false || stripos($message, 'Benutzerzugang') !== false) $add('nc_nextcloud_user');
if (stripos($message, 'Nextcloud-Passwort') !== false || stripos($message, 'Benutzername oder Passwort') !== false || stripos($message, 'Benutzerzugang') !== false) $add('nc_nextcloud_password');
if (stripos($message, 'API-Schluessel-ID') !== false || stripos($message, 'API-Key') !== false) $add('nc_connection_api_key_id');
if (stripos($message, 'API-Geheimnis') !== false || stripos($message, 'API-Key') !== false) $add('nc_connection_api_key_secret');
bratonien_tools_nc_edit_json(422, array(
'ok'=>false,
'message'=>$message,
'fields'=>$fields,
));
}