From 218fd2dcdcb48821b6e03083f6f36883f92d12d4 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Tue, 25 Mar 2025 18:45:24 -0400 Subject: [PATCH] Update formatPhoneNumber php function to include other country formatting, updated phone inputs to incoude country code --- admin_settings_company.php | 21 ++- ajax/ajax_contact_edit.php | 28 ++-- ajax/ajax_location_edit.php | 30 ++-- ajax/ajax_vendor_edit.php | 10 +- contacts.php | 7 +- functions.php | 160 ++++++++++++++---- js/app.js | 16 -- modals/client_add_modal.php | 70 +++++--- modals/contact_add_modal.php | 24 ++- modals/location_add_modal.php | 24 ++- modals/vendor_add_modal.php | 304 +++++++++++++++++----------------- post/user/client.php | 9 +- post/user/contact.php | 4 +- post/user/contact_model.php | 4 +- post/user/vendor.php | 4 +- post/user/vendor_model.php | 1 + 16 files changed, 437 insertions(+), 279 deletions(-) diff --git a/admin_settings_company.php b/admin_settings_company.php index 035b62ea..3b9a3fc1 100644 --- a/admin_settings_company.php +++ b/admin_settings_company.php @@ -12,7 +12,8 @@ $company_address = nullable_htmlentities($row['company_address']); $company_city = nullable_htmlentities($row['company_city']); $company_state = nullable_htmlentities($row['company_state']); $company_zip = nullable_htmlentities($row['company_zip']); -$company_phone = formatPhoneNumber($row['company_phone']); +$company_phone_country_code = formatPhoneNumber($row['company_phone_country_code']); +$company_phone = nullable_htmlentities(formatPhoneNumber($row['company_phone'], $company_phone_country_code)); $company_email = nullable_htmlentities($row['company_email']); $company_website = nullable_htmlentities($row['company_website']); $company_logo = nullable_htmlentities($row['company_logo']); @@ -110,13 +111,18 @@ $company_initials = nullable_htmlentities(initials($company_name)); -
- -
-
- + +
+
+
+
+
+ +
+ + +
-
@@ -152,4 +158,3 @@ $company_initials = nullable_htmlentities(initials($company_name));
- + " placeholder="Code" maxlength="4"> +
- +
+ +
-
- -
-
- + +
+
+
+
+
+ +
+ " placeholder="Code" maxlength="4"> + +
-
diff --git a/ajax/ajax_location_edit.php b/ajax/ajax_location_edit.php index 5c1622bb..23098d95 100644 --- a/ajax/ajax_location_edit.php +++ b/ajax/ajax_location_edit.php @@ -14,9 +14,11 @@ $location_address = nullable_htmlentities($row['location_address']); $location_city = nullable_htmlentities($row['location_city']); $location_state = nullable_htmlentities($row['location_state']); $location_zip = nullable_htmlentities($row['location_zip']); -$location_phone = formatPhoneNumber($row['location_phone']); +$location_phone_country_code = nullable_htmlentities($row['location_phone_country_code']); +$location_phone = nullable_htmlentities(formatPhoneNumber($row['location_phone'], $location_phone_country_code)); $location_extension = formatPhoneNumber($row['location_extension']); -$location_fax = formatPhoneNumber($row['location_fax']); +$location_fax_country_code = nullable_htmlentities($row['location_fax_country_code']); +$location_fax = nullable_htmlentities(formatPhoneNumber($row['location_fax'], $location_fax_country_code)); $location_hours = nullable_htmlentities($row['location_hours']); $location_photo = nullable_htmlentities($row['location_photo']); $location_notes = nullable_htmlentities($row['location_notes']); @@ -207,22 +209,30 @@ ob_start();
- + " placeholder="Code" maxlength="4"> +
- +
+ +
-
- -
-
- + +
+
+
+
+
+ +
+ " placeholder="Code" maxlength="4"> + +
-
diff --git a/ajax/ajax_vendor_edit.php b/ajax/ajax_vendor_edit.php index 9f58b1de..a1a616f9 100644 --- a/ajax/ajax_vendor_edit.php +++ b/ajax/ajax_vendor_edit.php @@ -11,7 +11,8 @@ $vendor_name = nullable_htmlentities($row['vendor_name']); $vendor_description = nullable_htmlentities($row['vendor_description']); $vendor_account_number = nullable_htmlentities($row['vendor_account_number']); $vendor_contact_name = nullable_htmlentities($row['vendor_contact_name']); -$vendor_phone = formatPhoneNumber($row['vendor_phone']); +$vendor_phone_country_code = nullable_htmlentities($row['vendor_phone_country_code']); +$vendor_phone = nullable_htmlentities(formatPhoneNumber($row['contact_phone'], $vendor_phone_country_code)); $vendor_extension = nullable_htmlentities($row['vendor_extension']); $vendor_email = nullable_htmlentities($row['vendor_email']); $vendor_website = nullable_htmlentities($row['vendor_website']); @@ -126,12 +127,15 @@ ob_start();
- + " placeholder="Code" maxlength="4"> +
- +
+ +
diff --git a/contacts.php b/contacts.php index 44d3a5cf..c6c657e9 100644 --- a/contacts.php +++ b/contacts.php @@ -312,14 +312,15 @@ $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); } else { $contact_extension_display = "x$contact_extension"; } - $contact_phone = formatPhoneNumber($row['contact_phone']); + $contact_phone_country_code = nullable_htmlentities($row['contact_phone_country_code']); + $contact_phone = nullable_htmlentities(formatPhoneNumber($row['contact_phone'], $contact_phone_country_code)); if (empty($contact_phone)) { $contact_phone_display = ""; } else { $contact_phone_display = "
$contact_phone$contact_extension_display
"; } - - $contact_mobile = formatPhoneNumber($row['contact_mobile']); + $contact_mobile_country_code = nullable_htmlentities($row['contact_phone_country_code']); + $contact_mobile = nullable_htmlentities(formatPhoneNumber($row['contact_mobile'], $contact_mobile_country_code)); if (empty($contact_mobile)) { $contact_mobile_display = ""; } else { diff --git a/functions.php b/functions.php index 07124d75..293e9989 100644 --- a/functions.php +++ b/functions.php @@ -193,40 +193,144 @@ function truncate($text, $chars) { return $text . "..."; } -function formatPhoneNumber($phoneNumber) { - global $mysqli; +function formatPhoneNumber($phoneNumber, $country_code = '', $show_country_code = false) { - // Get Phone Mask Option - $phone_mask = mysqli_fetch_array(mysqli_query($mysqli, "SELECT config_phone_mask FROM settings WHERE company_id = 1"))[0]; + // Remove all non-digit characters + $digits = preg_replace('/\D/', '', $phoneNumber); + $formatted = ''; - if ($phone_mask == 0) { - return $phoneNumber; + switch ($country_code) { + case '1': // USA/Canada — (123) 456-7890 + if (strlen($digits) === 10) { + $formatted = '(' . substr($digits, 0, 3) . ') ' . substr($digits, 3, 3) . '-' . substr($digits, 6); + } + break; + + case '44': // UK — 07123 456 789 + if ($digits[0] === '0') $digits = substr($digits, 1); + if (strlen($digits) === 10) { + $formatted = '0' . substr($digits, 0, 4) . ' ' . substr($digits, 4, 3) . ' ' . substr($digits, 7); + } + break; + + case '61': // Australia — 0412 345 678 + if ($digits[0] === '0') $digits = substr($digits, 1); + if (strlen($digits) === 9) { + $formatted = '0' . substr($digits, 0, 4) . ' ' . substr($digits, 4, 3) . ' ' . substr($digits, 7); + } + break; + + case '91': // India — 91234 56789 + if (strlen($digits) === 10) { + $formatted = substr($digits, 0, 5) . ' ' . substr($digits, 5); + } + break; + + case '81': // Japan — 03-1234-5678 + if ($digits[0] === '0') $digits = substr($digits, 1); + if (strlen($digits) >= 9 && strlen($digits) <= 10) { + $formatted = '0' . substr($digits, 0, 2) . '-' . substr($digits, 2, 4) . '-' . substr($digits, 6); + } + break; + + case '49': // Germany — 030 12345678 + if ($digits[0] === '0') $digits = substr($digits, 1); + if (strlen($digits) >= 10) { + $formatted = '0' . substr($digits, 0, 3) . ' ' . substr($digits, 3); + } + break; + + case '33': // France — 01 23 45 67 89 + if ($digits[0] === '0') $digits = substr($digits, 1); + if (strlen($digits) === 9) { + $formatted = '0' . implode(' ', str_split($digits, 2)); + } + break; + + case '34': // Spain — 612 345 678 + if (strlen($digits) === 9) { + $formatted = substr($digits, 0, 3) . ' ' . substr($digits, 3, 3) . ' ' . substr($digits, 6); + } + break; + + case '39': // Italy — 312 345 6789 + if ($digits[0] === '0') $digits = substr($digits, 1); + $formatted = '0' . implode(' ', str_split($digits, 3)); + break; + + case '55': // Brazil — (11) 91234-5678 + if (strlen($digits) === 11) { + $formatted = '(' . substr($digits, 0, 2) . ') ' . substr($digits, 2, 5) . '-' . substr($digits, 7); + } + break; + + case '7': // Russia — 8 (912) 345-67-89 + if ($digits[0] === '8') $digits = substr($digits, 1); + if (strlen($digits) === 10) { + $formatted = '8 (' . substr($digits, 0, 3) . ') ' . substr($digits, 3, 3) . '-' . substr($digits, 6, 2) . '-' . substr($digits, 8); + } + break; + + case '86': // China — 138 0013 8000 + if (strlen($digits) === 11) { + $formatted = substr($digits, 0, 3) . ' ' . substr($digits, 3, 4) . ' ' . substr($digits, 7); + } + break; + + case '82': // South Korea — 010-1234-5678 + if (strlen($digits) === 11) { + $formatted = substr($digits, 0, 3) . '-' . substr($digits, 3, 4) . '-' . substr($digits, 7); + } + break; + + case '62': // Indonesia — 0812 3456 7890 + if ($digits[0] !== '0') $digits = '0' . $digits; + if (strlen($digits) === 12) { + $formatted = substr($digits, 0, 4) . ' ' . substr($digits, 4, 4) . ' ' . substr($digits, 8); + } + break; + + case '63': // Philippines — 0912 345 6789 + if (strlen($digits) === 11) { + $formatted = substr($digits, 0, 4) . ' ' . substr($digits, 4, 3) . ' ' . substr($digits, 7); + } + break; + + case '234': // Nigeria — 0801 234 5678 + if ($digits[0] !== '0') $digits = '0' . $digits; + if (strlen($digits) === 11) { + $formatted = substr($digits, 0, 4) . ' ' . substr($digits, 4, 3) . ' ' . substr($digits, 7); + } + break; + + case '27': // South Africa — 082 123 4567 + if (strlen($digits) >= 9 && strlen($digits) <= 10) { + $formatted = substr($digits, 0, 3) . ' ' . substr($digits, 3, 3) . ' ' . substr($digits, 6); + } + break; + + case '971': // UAE — 050 123 4567 + if (strlen($digits) === 9) { + $formatted = substr($digits, 0, 3) . ' ' . substr($digits, 3, 3) . ' ' . substr($digits, 6); + } + break; + + default: + // If no match, do nothing here and use fallback below + break; } - - $phoneNumber = $phoneNumber ? preg_replace('/[^0-9]/', '', $phoneNumber) : ""; - - if (strlen($phoneNumber) > 10) { - $countryCode = substr($phoneNumber, 0, strlen($phoneNumber) - 10); - $areaCode = substr($phoneNumber, -10, 3); - $nextThree = substr($phoneNumber, -7, 3); - $lastFour = substr($phoneNumber, -4, 4); - - $phoneNumber = '+' . $countryCode . ' (' . $areaCode . ') ' . $nextThree . '-' . $lastFour; - } else if (strlen($phoneNumber) == 10) { - $areaCode = substr($phoneNumber, 0, 3); - $nextThree = substr($phoneNumber, 3, 3); - $lastFour = substr($phoneNumber, 6, 4); - - $phoneNumber = '(' . $areaCode . ') ' . $nextThree . '-' . $lastFour; - } else if (strlen($phoneNumber) == 7) { - $nextThree = substr($phoneNumber, 0, 3); - $lastFour = substr($phoneNumber, 3, 4); - - $phoneNumber = $nextThree . '-' . $lastFour; + // Fallback if formatting failed + if (!$formatted && strlen($digits) >= 7) { + $formatted = substr($digits, 0, 3) . ' ' . substr($digits, 3, 3) . ' ' . substr($digits, 6); } - return $phoneNumber; + // Still no formatting? Use raw digits + if (!$formatted) { + $formatted = $digits ?: $phoneNumber; // Use original input if digits are empty + } + + return $show_country_code && $country_code ? "+$country_code $formatted" : $formatted; } function mkdirMissing($dir) { diff --git a/js/app.js b/js/app.js index 632c0d48..38285580 100644 --- a/js/app.js +++ b/js/app.js @@ -425,20 +425,4 @@ $(document).ready(function() { // Data Tables new DataTable('.dataTables'); - - // Initialize International Phone Input - const inputs = document.querySelectorAll('input[type="tel"]'); - - // Loop through all the selected inputs and initialize intlTelInput on each one - inputs.forEach(input => { - window.intlTelInput(input, { - initialCountry: "us", - strictMode: true, - showFlags: false, - separateDialCode: true, - nationalMode: false, - loadUtils: () => import("../plugins/intl-tel-input/js/utils.js") // for formatting/placeholders etc - }); - }); - }); \ No newline at end of file diff --git a/modals/client_add_modal.php b/modals/client_add_modal.php index 2d7001f9..a83d4cd9 100644 --- a/modals/client_add_modal.php +++ b/modals/client_add_modal.php @@ -199,23 +199,39 @@ -
- -
-
- + + +
+
+
+
+
+ +
+ + +
+
+
+
+
+
-
-
- -
-
- + +
+
+
+
+
+ +
+ + +
-
@@ -243,30 +259,38 @@
- +
-
+
- + +
-
- +
+
+ +
- -
-
-
- + +
+
+
+
+
+ +
+ + +
-
diff --git a/modals/contact_add_modal.php b/modals/contact_add_modal.php index e8ea5b3c..d267245d 100644 --- a/modals/contact_add_modal.php +++ b/modals/contact_add_modal.php @@ -101,22 +101,30 @@
- + +
- +
+ +
-
- -
-
- + +
+
+
+
+
+ +
+ + +
-
diff --git a/modals/location_add_modal.php b/modals/location_add_modal.php index 8aff7a8d..80fb697c 100644 --- a/modals/location_add_modal.php +++ b/modals/location_add_modal.php @@ -184,22 +184,30 @@
- + +
- +
+ +
-
- -
-
- + +
+
+
+
+
+ +
+ + +
-
diff --git a/modals/vendor_add_modal.php b/modals/vendor_add_modal.php index cae5d2ed..fc0f9154 100644 --- a/modals/vendor_add_modal.php +++ b/modals/vendor_add_modal.php @@ -1,165 +1,159 @@