diff --git a/api/BusinessLogic/Emails/EmailTemplateParser.php b/api/BusinessLogic/Emails/EmailTemplateParser.php index 563a993e..1961ee88 100644 --- a/api/BusinessLogic/Emails/EmailTemplateParser.php +++ b/api/BusinessLogic/Emails/EmailTemplateParser.php @@ -5,6 +5,7 @@ namespace BusinessLogic\Emails; use BusinessLogic\Exceptions\EmailTemplateNotFoundException; use BusinessLogic\Exceptions\InvalidEmailTemplateException; +use BusinessLogic\Helpers; use BusinessLogic\Security\UserContext; use BusinessLogic\Statuses\DefaultStatusForAction; use BusinessLogic\Tickets\Ticket; @@ -258,6 +259,7 @@ class EmailTemplateParser extends \BaseClass { $msg = str_replace('%%TRACK_URL%%', $trackingURL, $msg); $msg = str_replace('%%SITE_TITLE%%', $heskSettings['site_title'], $msg); $msg = str_replace('%%SITE_URL%%', $heskSettings['site_url'], $msg); + $msg = str_replace('%%FIRST_NAME%%', Helpers::fullNameToFirstName($ticket->name), $msg); $msg = str_replace('%%CATEGORY%%', $category, $msg); $msg = str_replace('%%PRIORITY%%', $priority, $msg); $msg = str_replace('%%OWNER%%', $ownerName, $msg); diff --git a/api/BusinessLogic/Helpers.php b/api/BusinessLogic/Helpers.php index ea92bc5c..7a0fb87d 100644 --- a/api/BusinessLogic/Helpers.php +++ b/api/BusinessLogic/Helpers.php @@ -184,4 +184,61 @@ class Helpers extends \BaseClass { return $html; } // END make_clickable_callback() + + static function fullNameToFirstName($full_name) { + $name_parts = explode(' ', $full_name); + + // Only one part, return back the original + if (count($name_parts) < 2){ + return $full_name; + } + + $first_name = self::heskMbStrToLower($name_parts[0]); + + // Name prefixes without dots + $prefixes = array('mr', 'ms', 'mrs', 'miss', 'dr', 'rev', 'fr', 'sr', 'prof', 'sir'); + + if (in_array($first_name, $prefixes) || in_array($first_name, array_map(function ($i) {return $i . '.';}, $prefixes))) { + if(isset($name_parts[2])) { + // Mr James Smith -> James + $first_name = $name_parts[1]; + } else { + // Mr Smith (no first name given) + return $full_name; + } + } + + // Detect LastName, FirstName + if (self::heskMbSubstr($first_name, -1, 1) == ',') { + if (count($name_parts) == 2) { + $first_name = $name_parts[1]; + } else { + return $full_name; + } + } + + // If the first name doesn't have at least 3 chars, return the original + if(self::heskMbStrlen($first_name) < 3) { + return $full_name; + } + + // Return the name with first character uppercase + return self::heskUcfirst($first_name); + } + + static function heskMbStrToLower($in) { + return function_exists('mb_strtolower') ? mb_strtolower($in) : strtolower($in); + } + + static function heskMbStrlen($in) { + return function_exists('mb_strlen') ? mb_strlen($in, 'UTF-8') : strlen($in); + } + + static function heskMbSubstr($in, $start, $length) { + return function_exists('mb_substr') ? mb_substr($in, $start, $length, 'UTF-8') : substr($in, $start, $length); + } + + static function heskUcfirst($in) { + return function_exists('mb_convert_case') ? mb_convert_case($in, MB_CASE_TITLE, 'UTF-8') : ucfirst($in); + } } \ No newline at end of file diff --git a/inc/email_functions.inc.php b/inc/email_functions.inc.php index 7be13062..7f539ac4 100644 --- a/inc/email_functions.inc.php +++ b/inc/email_functions.inc.php @@ -782,6 +782,7 @@ function hesk_processMessage($msg, $ticket, $is_admin, $is_ticket, $just_message $msg = str_replace('%%TRACK_URL%%', $trackingURL, $msg); $msg = str_replace('%%SITE_TITLE%%', $hesk_settings['site_title'], $msg); $msg = str_replace('%%SITE_URL%%', $hesk_settings['site_url'], $msg); + $msg = str_replace('%%FIRST_NAME%%',hesk_full_name_to_first_name($ticket['name']),$msg); if (isset($ticket['message'])) { // If HTML is enabled, let's unescape everything, and call html2text. @@ -863,6 +864,7 @@ function hesk_processMessage($msg, $ticket, $is_admin, $is_ticket, $just_message $msg = str_replace('%%ID%%', $ticket['id'], $msg); $msg = str_replace('%%TIME_WORKED%%', $ticket['time_worked'] ,$msg); $msg = str_replace('%%LAST_REPLY_BY%%',$ticket['last_reply_by'] ,$msg); + $msg = str_replace('%%FIRST_NAME%%',hesk_full_name_to_first_name($ticket['name']),$msg); /* All custom fields */ for ($i=1; $i<=50; $i++) {