From aa2dcc027dbb934a1d412d36758598ba4dd2bd2c Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Thu, 23 Jan 2025 00:17:09 +0200 Subject: [PATCH] Makes required fields for hosts and agents --- app/pages/config.php | 4 +- app/templates/config-jilo.php | 71 +++++++++++++++++++++++++++++------ 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/app/pages/config.php b/app/pages/config.php index 560370e..9e2d8d3 100644 --- a/app/pages/config.php +++ b/app/pages/config.php @@ -65,7 +65,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $newHost = [ 'address' => $_POST['address'], 'platform_id' => $_POST['platform'], - 'name' => $_POST['name'], + 'name' => empty($_POST['name']) ? $_POST['address'] : $_POST['name'], ]; $result = $hostObject->addHost($newHost); if ($result === true) { @@ -79,7 +79,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $updatedHost = [ 'id' => $host_id, 'address' => $_POST['address'], - 'name' => $_POST['name'], + 'name' => empty($_POST['name']) ? $_POST['address'] : $_POST['name'], ]; $result = $hostObject->editHost($platform_id, $updatedHost); if ($result === true) { diff --git a/app/templates/config-jilo.php b/app/templates/config-jilo.php index 3538211..c13c14b 100644 --- a/app/templates/config-jilo.php +++ b/app/templates/config-jilo.php @@ -127,14 +127,14 @@
-
Host "" (#) in platform ""
+
Host "" (#) in platform ""
Host description
-
+
DNS name or IP
@@ -148,7 +148,7 @@ + placeholder="(defaults to DNS name or IP)">
@@ -233,7 +233,7 @@ @@ -759,6 +759,16 @@ $(function() { const platformId = hostActions.data('platform-id'); const card = hostActions.closest('.card'); + // Get form inputs + const hostAddress = card.find('input[name="address"]'); + + // Validate required fields + if (!hostAddress.val().trim()) { + showValidationTooltip(hostAddress, 'Please enter a DNS name or IP address'); + hostAddress.focus(); + return; + } + // Collect form data const formData = new FormData(); formData.append('item', 'host'); @@ -793,10 +803,10 @@ $(function() { .then(data => { if (data.success) { // Update view mode with new values - const name = card.find('input[name="name"]').val() || '(no description)'; + const name = card.find('input[name="name"]').val() || card.find('input[name="address"]').val(); const address = card.find('input[name="address"]').val(); - const viewContent = card.find('.host-view-mode:not(.btn)').first(); - viewContent.html( + + card.find('.host-view-mode:not(.btn)').first().html( `
Host description
@@ -823,10 +833,10 @@ $(function() { .catch(error => { console.error('Error:', error); // Since we know the save might work despite JSON errors, update UI anyway - const name = card.find('input[name="name"]').val() || '(no description)'; + const name = card.find('input[name="name"]').val() || card.find('input[name="name"]').val(); const address = card.find('input[name="address"]').val(); - const viewContent = card.find('.host-view-mode:not(.btn)').first(); - viewContent.html( + + card.find('.host-view-mode:not(.btn)').first().html( `
Host description
@@ -876,6 +886,16 @@ $(function() { const hostId = agentActions.data('host-id'); const row = agentActions.closest('tr'); + // Get form inputs + const agentUrl = row.find('input[name="url"]'); + + // Validate required fields + if (!agentUrl.val().trim()) { + showValidationTooltip(agentUrl, 'Please enter an endpoint URL'); + agentUrl.focus(); + return; + } + // Collect form data const formData = new FormData(); formData.append('item', 'agent'); @@ -987,7 +1007,9 @@ $(function() { agents.forEach(agent => { const agentType = agent.querySelector('td:first-child span'); const agentName = agentType ? agentType.textContent.trim() : 'Unknown agent'; - warningText += `
  • Agent: ${agentName}
  • `; + const agentUrl = agent.querySelector('td:nth-child(2) .agent-view-mode'); + const url = agentUrl ? agentUrl.textContent.trim() : 'Unknown URL'; + warningText += `
  • Agent ${agentName} at ${url}
  • `; }); warningText += ''; } @@ -1029,10 +1051,13 @@ $(function() { const agents = hostCard.querySelectorAll('.agent-details'); if (agents.length > 0) { warningText += '
      '; + agents.forEach(agent => { const agentType = agent.querySelector('td:first-child span'); const agentName = agentType ? agentType.textContent.trim() : 'Unknown agent'; - warningText += `
    • Agent: ${agentName}
    • `; + const agentUrl = agent.querySelector('td:nth-child(2) .agent-view-mode'); + const url = agentUrl ? agentUrl.textContent.trim() : 'Unknown URL'; + warningText += `
    • Agent ${agentName} at ${url}
    • `; }); warningText += '
    '; document.getElementById('deleteHostButton').disabled = true; @@ -1086,6 +1111,28 @@ $(function() { // Initialize tooltips $('[data-toggle="tooltip"]').tooltip(); + + // Helper function to show validation tooltip + function showValidationTooltip(input, message) { + const tooltip = $(input) + .tooltip({ + title: message, + placement: 'top', + trigger: 'manual', + template: '' + }) + .tooltip('show'); + + // Hide tooltip when input changes + input.one('input', function() { + $(this).tooltip('dispose'); + }); + + // Hide tooltip after 3 seconds + setTimeout(() => { + $(input).tooltip('dispose'); + }, 3000); + } }); // Show add platform modal