fix: Update patient_tracker.php (#6595)
[openemr.git] / library / classes / PhoneNumber.class.php
blob7a1cfb007ae45daa287a99a237f1b42e796d477a
1 <?php
3 /**
4 * phone number class for smarty templates
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author duhlman
9 * @author Stephen Waite <stephen.waite@cmsvt.com>
10 * @copyright Copyright (c) duhlman
11 * @copyright Copyright (c) 2021 Stephen Waite <stephen.waite@cmsvt.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 define("TYPE_HOME", 1);
16 define("TYPE_WORK", 2);
17 define("TYPE_CELL", 3);
18 define("TYPE_EMERGENCY", 4);
19 define("TYPE_FAX", 5);
22 /**
23 * class Address
27 use OpenEMR\Common\ORDataObject\ORDataObject;
29 class PhoneNumber extends ORDataObject
31 var $id;
32 var $foreign_id;
33 var $country_code;
34 var $area_code;
35 var $prefix;
36 var $number;
37 var $type;
38 var $type_array = array("","Home", "Work", "Cell" , "Emergency" , "Fax");
40 /**
41 * Constructor sets all attributes to their default value
43 function __construct($id = "", $foreign_id = "")
45 $this->id = $id;
46 $this->foreign_id = $foreign_id;
47 $this->country_code = "+1";
48 $this->prefix = "";
49 $this->number = "";
50 $this->type = TYPE_HOME;
51 $this->_table = "phone_numbers";
52 if ($id != "") {
53 $this->populate();
57 static function factory_phone_numbers($foreign_id = "")
59 $sqlArray = array();
61 if (empty($foreign_id)) {
62 $foreign_id_sql = " like '%'";
63 } else {
64 $foreign_id_sql = " = ?";
65 $sqlArray[] = strval($foreign_id);
68 $phone_numbers = array();
69 $p = new PhoneNumber();
70 $sql = "SELECT id FROM " . escape_table_name($p->_table) . " WHERE foreign_id " . $foreign_id_sql . " ORDER BY type";
71 //echo $sql . "<bR />";
72 $results = sqlQ($sql, $sqlArray);
73 //echo "sql: $sql";
74 while ($row = sqlFetchArray($results)) {
75 $phone_numbers[] = new PhoneNumber($row['id']);
78 return $phone_numbers;
81 function set_id($id)
83 $this->id = $id;
86 function get_id()
88 return $this->id;
91 function foreign_id($id)
93 $this->foreign_id = $id;
96 function get_foreign_id()
98 return $this->foreign_id;
101 function set_country_code($ccode)
103 $this->country_code = $ccode;
106 function get_country_code()
108 return $this->country_code;
110 function set_area_code($acode)
112 $this->area_code = $acode;
115 function get_area_code()
117 return $this->area_code;
120 function set_number($num)
122 $this->number = $num;
125 function get_number()
127 return $this->number;
131 function set_type($type)
133 $this->type = $type;
136 function get_type()
138 return $this->type;
141 function set_prefix($prefix)
143 $this->prefix = $prefix;
146 function get_prefix()
148 return $this->prefix;
151 function get_phone_display()
153 if (is_numeric($this->area_code) && is_numeric($this->prefix) && is_numeric($this->number)) {
154 // return "(" . $this->area_code . ") " . $this->prefix . "-" . $this->number;
155 return $this->area_code . "-" . $this->prefix . "-" . $this->number;
158 return "";
161 function set_phone($num)
163 if (strlen($num) == 10 && is_numeric($num)) {
164 $this->area_code = substr($num, 0, 3);
165 $this->prefix = substr($num, 3, 3);
166 $this->number = substr($num, 6, 4);
167 } elseif (strlen($num) == 12) {
168 $nums = explode("-", $num);
169 if (count($nums) == 3) {
170 $this->area_code = $nums[0];
171 $this->prefix = $nums[1];
172 $this->number = $nums[2];
174 } elseif (strlen($num) == 14 && substr($num, 0, 1) == "(") {
175 $nums[0] = substr($num, 1, 3);
176 $nums[1] = substr($num, 6, 3);
177 $nums[2] = substr($num, 10, 4);
179 foreach ($nums as $n) {
180 if (!is_numeric($n)) {
181 return false;
185 if (count($nums) == 3) {
186 $this->area_code = $nums[0];
187 $this->prefix = $nums[1];
188 $this->number = $nums[2];
190 } else {
191 $this->area_code = '';
192 $this->prefix = '';
193 $this->number = '';
197 function toString($html = false)
199 $string .= "\n"
200 . "ID: " . $this->id . "\n"
201 . "FID: " . $this->foreign_id . "\n"
202 . $this->country_code . " (" . $this->area_code . ") " . $this->prefix . "-" . $this->number . " " . $this->type_array[$this->type];
203 if ($html) {
204 return nl2br($string);
205 } else {
206 return $string;
210 function persist($fid = "")
212 if (!empty($fid)) {
213 $this->foreign_id = $fid;
216 parent::persist();