diff --git a/ajax.php b/ajax.php index 98003749..d52ad331 100644 --- a/ajax.php +++ b/ajax.php @@ -344,6 +344,45 @@ if (isset($_GET['quote_get_json_details'])) { } +/* + * Returns sorted list of active clients + */ +if (isset($_GET['get_active_clients'])) { + + $client_sql = mysqli_query( + $mysqli, + "SELECT client_id, client_name FROM clients + WHERE client_archived_at IS NULL + ORDER BY client_accessed_at DESC" + ); + + while ($row = mysqli_fetch_array($client_sql)) { + $response['clients'][] = $row; + } + + echo json_encode($response); +} + +/* + * Returns ordered list of active contacts for a specified client + */ +if (isset($_GET['get_client_contacts'])) { + $client_id = intval($_GET['client_id']); + + $contact_sql = mysqli_query( + $mysqli, + "SELECT contact_id, contact_name FROM contacts + WHERE contacts.contact_archived_at IS NULL AND contact_client_id = $client_id + ORDER BY contact_important DESC, contact_name" + ); + + while ($row = mysqli_fetch_array($contact_sql)) { + $response['contacts'][] = $row; + } + + echo json_encode($response); +} + /* * Dynamic TOTP for client login page * When provided with a TOTP secret, returns a 6-digit code diff --git a/js/ticket_change_client.js b/js/ticket_change_client.js new file mode 100644 index 00000000..dbb91a81 --- /dev/null +++ b/js/ticket_change_client.js @@ -0,0 +1,95 @@ +/* + * LISTENERS + */ + +// Modal loaded listener - populate client select +changeClientModalLoad = document.getElementById('clientChangeTicketModalLoad'); +changeClientModalLoad.addEventListener('click', function() { + populateChangeClientModal_Clients(); +}) + +// Client selected listener - populate contact select +// We seem to have to use jQuery to listen for events, as the client input is a select2 component? +clientSelectDropdown = document.getElementById("changeClientSelect"); +$(clientSelectDropdown).on('select2:select', function (e) { + console.log("Fired contacts listener!!!!!!") + let client_id = $(this).find(':selected').val(); + populateChangeClientModal_Contacts(client_id); +}); + + +/* + * FUNCTIONS + */ + +// Populate client list function +function populateChangeClientModal_Clients() { + + // Get current client ID + let current_client_id = document.getElementById("client_id").value; + + // Send a GET request to ajax.php as ajax.php?get_active_clients=true + jQuery.get( + "ajax.php", + {get_active_clients: 'true'}, + function(data) { + + // If we get a response from ajax.php, parse it as JSON + const response = JSON.parse(data); + + // Access the data for clients (multiple) + const clients = response.clients; + + // Client dropdown already defined in listeners as clientSelectDropdown + + // Clear dropdown + var i, L = clientSelectDropdown.options.length -1; + for (i = L; i >= 0; i--) { + clientSelectDropdown.remove(i); + } + clientSelectDropdown[clientSelectDropdown.length] = new Option('- Client -', '0'); + + // Populate dropdown + clients.forEach(client => { + if (parseInt(current_client_id) !== parseInt(client.client_id)) { + // Show clients returned (excluding the current client ID - we can't change a ticket client to itself) + clientSelectDropdown[clientSelectDropdown.length] = new Option(client.client_name, client.client_id); + } + }); + + } + ); +} + +// Populate client contact function (after a client is selected) +function populateChangeClientModal_Contacts(client_id) { + // Send a GET request to ajax.php as ajax.php?get_client_contacts=true&client_id=NUM + jQuery.get( + "ajax.php", + {get_client_contacts: 'true', client_id: client_id}, + function(data) { + + // If we get a response from ajax.php, parse it as JSON + const response = JSON.parse(data); + + // Access the data for contacts (multiple) + const contacts = response.contacts; + + // Contacts dropdown + var contactSelectDropdown = document.getElementById("changeContactSelect"); + + // Clear Category dropdown + var i, L = contactSelectDropdown.options.length -1; + for (i = L; i >= 0; i--) { + contactSelectDropdown.remove(i); + } + contactSelectDropdown[contactSelectDropdown.length] = new Option('- Contact -', '0'); + + // Populate dropdown + contacts.forEach(contact => { + contactSelectDropdown[contactSelectDropdown.length] = new Option(contact.contact_name, contact.contact_id); + }); + + } + ); +} diff --git a/post.php b/post.php index eecaf58c..8517f256 100644 --- a/post.php +++ b/post.php @@ -6500,6 +6500,29 @@ if(isset($_POST['merge_ticket'])){ } +if(isset($_POST['change_client_ticket'])){ + + validateTechRole(); + + $ticket_id = intval($_POST['ticket_id']); + $client_id = intval($_POST['new_client_id']); + $contact_id = intval($_POST['new_contact_id']); + + // Set any/all existing replies to internal + mysqli_query($mysqli, "UPDATE ticket_replies SET ticket_reply_type = 'Internal' WHERE ticket_reply_ticket_id = $ticket_id"); + + // Update ticket client & contact + mysqli_query($mysqli, "UPDATE tickets SET ticket_client_id = $client_id, ticket_contact_id = $contact_id WHERE ticket_id = $ticket_id LIMIT 1"); + + //Logging + mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Ticket Reply', log_action = 'Modify', log_description = '$session_name modified ticket - client changed', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client_id, log_user_id = $session_user_id, log_entity_id = $ticket_id"); + + $_SESSION['alert_message'] = "Ticket client updated"; + + header("Location: " . $_SERVER["HTTP_REFERER"]); + +} + if(isset($_GET['close_ticket'])){ validateTechRole(); diff --git a/ticket.php b/ticket.php index 7cefb9ca..8f430834 100644 --- a/ticket.php +++ b/ticket.php @@ -233,6 +233,9 @@ if (isset($_GET['ticket_id'])) { Merge + + Change Client +
@@ -265,7 +268,7 @@ if (isset($_GET['ticket_id'])) {