dolibarr 24.0.0-beta
phone.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Open-Dsi <support@open-dsi.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
33function dol_parse_phone($phone)
34{
35 $phone = trim((string) $phone);
36 if ($phone === '') {
37 return array('code' => '', 'number' => '');
38 }
39
40 if (strpos($phone, '+') === 0) {
41 $pos = strpos($phone, ' ');
42 if ($pos !== false) {
43 return array(
44 'code' => substr($phone, 0, $pos),
45 'number' => substr($phone, $pos + 1)
46 );
47 }
48 // Phone starts with + but has no space: try to keep it as-is in code
49 return array('code' => '', 'number' => $phone);
50 }
51
52 return array('code' => '', 'number' => $phone);
53}
54
68function dol_build_phone($db, $code, $number)
69{
70 $number = preg_replace('/[\s\-\.\‍(\‍)]/', '', trim((string) $number));
71 $code = trim((string) $code);
72
73 if ($number === '') {
74 return '';
75 }
76
77 if ($code !== '') {
78 // Strip national trunk prefix (e.g. leading "0" for France)
79 $sql = "SELECT trunk_prefix FROM ".$db->prefix()."c_country";
80 $sql .= " WHERE phone_code = ".((int) ltrim($code, '+'));
81 $sql .= " AND trunk_prefix IS NOT NULL AND trunk_prefix != ''";
82 $sql .= " LIMIT 1";
83 $resql = $db->query($sql);
84 if ($resql) {
85 $obj = $db->fetch_object($resql);
86 if ($obj && isset($obj->trunk_prefix) && $obj->trunk_prefix !== '' && $obj->trunk_prefix !== null) {
87 $prefix = $obj->trunk_prefix;
88 if (strpos($number, $prefix) === 0) {
89 $number = substr($number, strlen($prefix));
90 }
91 }
92 $db->free($resql);
93 }
94 }
95
96 if ($code !== '') {
97 return $code.' '.$number;
98 }
99
100 return $number;
101}
102
110function dol_get_trunk_prefix($db, $phone_code)
111{
112 $phone_code = trim((string) $phone_code);
113 if ($phone_code === '') {
114 return '';
115 }
116
117 $sql = "SELECT trunk_prefix FROM ".$db->prefix()."c_country";
118 $sql .= " WHERE phone_code = ".((int) ltrim($phone_code, '+'));
119 $sql .= " AND trunk_prefix IS NOT NULL AND trunk_prefix != ''";
120 $sql .= " LIMIT 1";
121 $resql = $db->query($sql);
122 if ($resql) {
123 $obj = $db->fetch_object($resql);
124 if ($obj && isset($obj->trunk_prefix) && $obj->trunk_prefix !== '' && $obj->trunk_prefix !== null) {
125 $db->free($resql);
126 return $obj->trunk_prefix;
127 }
128 $db->free($resql);
129 }
130
131 return '';
132}
133
142{
143 $country_id = (int) $country_id;
144 if ($country_id <= 0) {
145 return '';
146 }
147
148 $sql = "SELECT phone_code FROM ".$db->prefix()."c_country WHERE rowid = ".((int) $country_id);
149 $resql = $db->query($sql);
150 if ($resql) {
151 $obj = $db->fetch_object($resql);
152 if ($obj && !empty($obj->phone_code)) {
153 $db->free($resql);
154 return '+'.$obj->phone_code;
155 }
156 $db->free($resql);
157 }
158
159 return '';
160}
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_get_trunk_prefix($db, $phone_code)
Get the national trunk prefix for a phone code.
dol_build_phone($db, $code, $number)
Build a normalized phone string from code and number parts.
Definition phone.lib.php:68
dol_parse_phone($phone)
Parse a stored phone number into country code and number parts.
Definition phone.lib.php:33
dol_get_phone_code_from_country($db, $country_id)
Get the phone calling code for a country.