Add notes to networks; move networks js to its own file

This commit is contained in:
Marcus Hill 2023-12-31 11:13:24 +00:00
parent 114a72424c
commit 1a1b4ee810
5 changed files with 77 additions and 65 deletions

View File

@ -9,8 +9,8 @@
</div>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="client_id" value="<?php echo $client_id; ?>">
<div class="modal-body bg-white">
<div class="modal-body bg-white">
<div class="form-group">
<label>Name <strong class="text-danger">*</strong></label>
<div class="input-group">
@ -30,7 +30,7 @@
<input type="text" class="form-control" inputmode="numeric" pattern="[0-9]*" name="vlan" placeholder="ex. 20">
</div>
</div>
<div class="form-group">
<label>Network <strong class="text-danger">*</strong></label>
<div class="input-group">
@ -40,14 +40,14 @@
<input type="text" class="form-control" name="network" placeholder="Network ex 192.168.1.0/24" required>
</div>
</div>
<div class="form-group">
<label>Gateway <strong class="text-danger">*</strong></label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-route"></i></span>
</div>
<input type="text" class="form-control" name="gateway" placeholder="ex 192.168.1.1" data-inputmask="'alias': 'ip'" data-mask required>
<input type="text" class="form-control" name="gateway" placeholder="ex 192.168.1.1" data-inputmask="'alias': 'ip'" data-mask required>
</div>
</div>
@ -61,6 +61,11 @@
</div>
</div>
<div class="form-group">
<label>Notes</label>
<textarea class="form-control" rows="3" placeholder="Enter some notes" name="notes"></textarea>
</div>
<div class="form-group">
<label>Location</label>
<div class="input-group">
@ -69,15 +74,15 @@
</div>
<select class="form-control select2" name="location">
<option value="">- Location -</option>
<?php
$sql = mysqli_query($mysqli, "SELECT * FROM locations WHERE location_archived_at IS NULL AND location_client_id = $client_id ORDER BY location_name ASC");
<?php
$sql = mysqli_query($mysqli, "SELECT * FROM locations WHERE location_archived_at IS NULL AND location_client_id = $client_id ORDER BY location_name ASC");
while ($row = mysqli_fetch_array($sql)) {
$location_id = intval($row['location_id']);
$location_name = nullable_htmlentities($row['location_name']);
?>
<option value="<?php echo $location_id; ?>"><?php echo $location_name; ?></option>
<?php
}
?>

View File

@ -10,8 +10,8 @@
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="network_id" id="editNetworkId" value="">
<input type="hidden" name="client_id" value="<?php echo $client_id; ?>">
<div class="modal-body bg-white">
<div class="modal-body bg-white">
<div class="form-group">
<label>Name <strong class="text-danger">*</strong></label>
<div class="input-group">
@ -31,7 +31,7 @@
<input type="text" class="form-control" inputmode="numeric" pattern="[0-9]*" id="editNetworkVlan" name="vlan" placeholder="ex. 20">
</div>
</div>
<div class="form-group">
<label>Network <strong class="text-danger">*</strong></label>
<div class="input-group">
@ -41,7 +41,7 @@
<input type="text" class="form-control" id="editNetworkCidr" name="network" placeholder="Network ex 192.168.1.0/24" required>
</div>
</div>
<div class="form-group">
<label>Gateway <strong class="text-danger">*</strong></label>
<div class="input-group">
@ -62,6 +62,11 @@
</div>
</div>
<div class="form-group">
<label>Notes</label>
<textarea class="form-control" rows="3" id="editNetworkNotes" name="notes" placeholder="Enter some notes"></textarea>
</div>
<div class="form-group">
<label>Location</label>
<div class="input-group">
@ -73,7 +78,7 @@
</select>
</div>
</div>
</div>
<div class="modal-footer bg-white">
<button type="submit" name="edit_network" class="btn btn-primary text-bold"><i class="fa fa-check mr-2"></i>Save</button>

View File

@ -37,7 +37,7 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
</a>
</div>
</div>
</div>
</div>
<div class="card-body">
@ -162,54 +162,7 @@ require_once "client_network_export_modal.php";
?>
<script>
function populateNetworkEditModal(client_id, network_id) {
// Send a GET request to post.php as post.php?network_get_json_details=true&client_id=NUM&network_id=NUM
jQuery.get(
"ajax.php",
{network_get_json_details: 'true', client_id: client_id, network_id: network_id},
function(data) {
// If we get a response from post.php, parse it as JSON
const response = JSON.parse(data);
// Access the network (only one!) and locations (possibly multiple)
const network = response.network[0];
const locations = response.locations;
// Populate the network modal fields
document.getElementById("editNetworkHeader").innerText = network.network_name;
document.getElementById("editNetworkId").value = network_id;
document.getElementById("editNetworkName").value = network.network_name;
document.getElementById("editNetworkVlan").value = network.network_vlan;
document.getElementById("editNetworkCidr").value = network.network;
document.getElementById("editNetworkGw").value = network.network_gateway;
document.getElementById("editNetworkDhcp").value = network.network_dhcp_range;
// Select the location dropdown
var locationDropdown = document.getElementById("editNetworkLocation");
// Clear location dropdown
var i, L = locationDropdown.options.length -1;
for(i = L; i >= 0; i--) {
locationDropdown.remove(i);
}
locationDropdown[locationDropdown.length] = new Option('- Location -', '0');
// Populate location dropdown
locations.forEach(location => {
if (parseInt(location.location_id) == parseInt(network.network_location_id)) {
locationDropdown[locationDropdown.length] = new Option(location.location_name, location.location_id, true, true);
}
else{
locationDropdown[locationDropdown.length] = new Option(location.location_name, location.location_id);
}
});
}
);
}
</script>
<script src="js/network_edit_modal.js"></script>
<?php
require_once "footer.php";

47
js/network_edit_modal.js Normal file
View File

@ -0,0 +1,47 @@
function populateNetworkEditModal(client_id, network_id) {
// Send a GET request to post.php as post.php?network_get_json_details=true&client_id=NUM&network_id=NUM
jQuery.get(
"ajax.php",
{network_get_json_details: 'true', client_id: client_id, network_id: network_id},
function(data) {
// If we get a response from post.php, parse it as JSON
const response = JSON.parse(data);
// Access the network (only one!) and locations (possibly multiple)
const network = response.network[0];
const locations = response.locations;
// Populate the network modal fields
document.getElementById("editNetworkHeader").innerText = network.network_name;
document.getElementById("editNetworkId").value = network_id;
document.getElementById("editNetworkName").value = network.network_name;
document.getElementById("editNetworkVlan").value = network.network_vlan;
document.getElementById("editNetworkCidr").value = network.network;
document.getElementById("editNetworkGw").value = network.network_gateway;
document.getElementById("editNetworkDhcp").value = network.network_dhcp_range;
document.getElementById("editNetworkNotes").value = network.network_notes;
// Select the location dropdown
var locationDropdown = document.getElementById("editNetworkLocation");
// Clear location dropdown
var i, L = locationDropdown.options.length -1;
for(i = L; i >= 0; i--) {
locationDropdown.remove(i);
}
locationDropdown[locationDropdown.length] = new Option('- Location -', '0');
// Populate location dropdown
locations.forEach(location => {
if (parseInt(location.location_id) == parseInt(network.network_location_id)) {
locationDropdown[locationDropdown.length] = new Option(location.location_name, location.location_id, true, true);
}
else{
locationDropdown[locationDropdown.length] = new Option(location.location_name, location.location_id);
}
});
}
);
}

View File

@ -14,9 +14,10 @@ if (isset($_POST['add_network'])) {
$network = sanitizeInput($_POST['network']);
$gateway = sanitizeInput($_POST['gateway']);
$dhcp_range = sanitizeInput($_POST['dhcp_range']);
$notes = sanitizeInput($_POST['notes']);
$location_id = intval($_POST['location']);
mysqli_query($mysqli,"INSERT INTO networks SET network_name = '$name', network_vlan = $vlan, network = '$network', network_gateway = '$gateway', network_dhcp_range = '$dhcp_range', network_location_id = $location_id, network_client_id = $client_id");
mysqli_query($mysqli,"INSERT INTO networks SET network_name = '$name', network_vlan = $vlan, network = '$network', network_gateway = '$gateway', network_dhcp_range = '$dhcp_range', network_notes = '$notes', network_location_id = $location_id, network_client_id = $client_id");
$network_id = mysqli_insert_id($mysqli);
@ -39,10 +40,11 @@ if (isset($_POST['edit_network'])) {
$network = sanitizeInput($_POST['network']);
$gateway = sanitizeInput($_POST['gateway']);
$dhcp_range = sanitizeInput($_POST['dhcp_range']);
$notes = sanitizeInput($_POST['notes']);
$location_id = intval($_POST['location']);
$client_id = intval($_POST['client_id']);
mysqli_query($mysqli,"UPDATE networks SET network_name = '$name', network_vlan = $vlan, network = '$network', network_gateway = '$gateway', network_dhcp_range = '$dhcp_range', network_location_id = $location_id WHERE network_id = $network_id");
mysqli_query($mysqli,"UPDATE networks SET network_name = '$name', network_vlan = $vlan, network = '$network', network_gateway = '$gateway', network_dhcp_range = '$dhcp_range', network_notes = '$notes', network_location_id = $location_id WHERE network_id = $network_id");
//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Network', log_action = 'Modify', log_description = '$session_name modified network $name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $network_id");