From 827b880b2844f568bdfd9c0061f50fd0a1c10996 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Sun, 17 Sep 2023 20:08:41 -0400 Subject: [PATCH] Fixed edit scheduled ticket updated shoertenClient function to be even more intelligent --- functions.php | 49 +++++++++++++++++++----------- js/scheduled_tickets_edit_modal.js | 2 +- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/functions.php b/functions.php index 9922eef4..9a35bba2 100644 --- a/functions.php +++ b/functions.php @@ -705,25 +705,40 @@ function removeEmoji($text){ } function shortenClient($client) { - $client = trim($client); // Remove any leading/trailing spaces + // Pre-process by removing any non-alphanumeric characters except for certain punctuations. + $cleaned = preg_replace('/[^a-zA-Z0-9&]+/', ' ', $client); - // 1. If the client name is a single word and has three or more characters: - if (strpos($client, ' ') === false && strlen($client) >= 3) { - return substr($client, 0, 3); - } - - // 2. If the client name has multiple words: - $words = explode(' ', $client); + // Break into words. + $words = explode(' ', trim($cleaned)); + $shortened = ''; - foreach ($words as $word) { - $shortened .= $word[0]; + + // If there's only one word. + if (count($words) == 1) { + $word = $words[0]; + + if (strlen($word) <= 3) { + return strtoupper($word); + } + + // Prefer starting and ending characters. + $shortened = $word[0] . substr($word, -2); + } else { + // Less weightage to common words. + $commonWords = ['the', 'of', 'and']; + + foreach ($words as $word) { + if (!in_array(strtolower($word), $commonWords) || strlen($shortened) < 2) { + $shortened .= $word[0]; + } + } + + // If there are still not enough characters, take from the last word. + while (strlen($shortened) < 3 && !empty($word)) { + $shortened .= substr($word, 1, 1); + $word = substr($word, 1); + } } - // 3. If there are still not enough characters: - if (strlen($shortened) < 3) { - $remaining = 3 - strlen($shortened); - $shortened .= substr($client, strlen($shortened), $remaining); - } - - return strtoupper(substr($shortened, 0, 3)); // Return first three characters + return strtoupper(substr($shortened, 0, 3)); } \ No newline at end of file diff --git a/js/scheduled_tickets_edit_modal.js b/js/scheduled_tickets_edit_modal.js index 7194112f..3386147c 100644 --- a/js/scheduled_tickets_edit_modal.js +++ b/js/scheduled_tickets_edit_modal.js @@ -19,7 +19,7 @@ function populateScheduledTicketEditModal(client_id, ticket_id) { document.getElementById("editClientId").value = client_id; document.getElementById("editTicketSubject").value = ticket.scheduled_ticket_subject; document.getElementById("editTicketNextRun").value = ticket.scheduled_ticket_next_run; - $('#editTicketDetails').summernote('code', ticket.scheduled_ticket_details); + tinyMCE.get('editTicketDetails').setContent(ticket.scheduled_ticket_details); // Frequency dropdown