Highway to PSR2
[openemr.git] / library / classes / PhoneNumber.class.php
blob69abe6846a173ce577f51976d4866d6841aca524
1 <?php
2 /************************************************************************
3 phone_number.php - Copyright duhlman
5 /usr/share/apps/umbrello/headings/heading.php
7 This file was generated on %date% at %time%
8 The original location of this file is /home/duhlman/uml-generated-code/prescription.php
9 **************************************************************************/
11 define("TYPE_HOME", 1);
12 define("TYPE_WORK", 2);
13 define("TYPE_CELL", 3);
14 define("TYPE_EMERGENCY", 4);
15 define("TYPE_FAX", 5);
18 /**
19 * class Address
22 class PhoneNumber extends ORDataObject
24 var $id;
25 var $foreign_id;
26 var $country_code;
27 var $area_code;
28 var $prefix;
29 var $number;
30 var $type_array = array("","Home", "Work", "Cell" , "Emergency" , "Fax");
32 /**
33 * Constructor sets all Prescription attributes to their default value
35 function __construct($id = "", $foreign_id = "")
37 $this->id = $id;
38 $this->foreign_id = $foreign_id;
39 $this->country_code = "+1";
40 $this->prefix = "";
41 $this->number = "";
42 $this->type = TYPE_HOME;
43 $this->_table = "phone_numbers";
44 if ($id != "") {
45 $this->populate();
49 static function factory_phone_numbers($foreign_id = "")
51 if (empty($foreign_id)) {
52 $foreign_id= "like '%'";
53 } else {
54 $foreign_id= " = '" . add_escape_custom(strval($foreign_id)) . "'";
57 $phone_numbers = array();
58 $p = new PhoneNumber();
59 $sql = "SELECT id FROM " . $p->_table . " WHERE foreign_id " .$foreign_id . " ORDER BY type";
60 //echo $sql . "<bR />";
61 $results = sqlQ($sql);
62 //echo "sql: $sql";
63 while ($row = sqlFetchArray($results)) {
64 $phone_numbers[] = new PhoneNumber($row['id']);
67 return $phone_numbers;
70 function set_id($id)
72 $this->id = $id;
75 function get_id()
77 return $this->id;
80 function foreign_id($id)
82 $this->foreign_id = $id;
85 function get_foreign_id()
87 return $this->foreign_id;
90 function set_country_code($ccode)
92 $this->country_code = $ccode;
95 function get_country_code()
97 return $this->country_code;
99 function set_area_code($acode)
101 $this->area_code = $acode;
104 function get_area_code()
106 return $this->area_code;
109 function set_number($num)
111 $this->number = $num;
114 function get_number()
116 return $this->number;
120 function set_type($type)
122 $this->type = $type;
125 function get_type()
127 return $this->type;
130 function set_prefix($prefix)
132 $this->prefix = $prefix;
135 function get_prefix()
137 return $this->prefix;
140 function get_phone_display()
142 if (is_numeric($this->area_code) && is_numeric($this->prefix) && is_numeric($this->number)) {
143 // return "(" . $this->area_code . ") " . $this->prefix . "-" . $this->number;
144 return $this->area_code . "-" . $this->prefix . "-" . $this->number;
147 return "";
150 function set_phone($num)
152 if (strlen($num) == 10 && is_numeric($num)) {
153 $this->area_code = substr($num, 0, 3);
154 $this->prefix = substr($num, 3, 3);
155 $this->number = substr($num, 6, 4);
156 } elseif (strlen($num) == 12) {
157 $nums = explode("-", $num);
158 if (count($nums) == 3) {
159 $this->area_code = $nums[0];
160 $this->prefix = $nums[1];
161 $this->number = $nums[2];
163 } elseif (strlen($num) == 14 && substr($num, 0, 1) == "(") {
164 $nums[0] = substr($num, 1, 3);
165 $nums[1] = substr($num, 6, 3);
166 $nums[2] = substr($num, 10, 4);
168 foreach ($nums as $n) {
169 if (!is_numeric($n)) {
170 return false;
174 if (count($nums) == 3) {
175 $this->area_code = $nums[0];
176 $this->prefix = $nums[1];
177 $this->number = $nums[2];
182 function toString($html = false)
184 $string .= "\n"
185 . "ID: " . $this->id."\n"
186 . "FID: " . $this->foreign_id."\n"
187 . $this->country_code . " (" . $this->area_code . ") " . $this->prefix . "-" . $this->number . " " . $this->type_array[$this->type];
188 if ($html) {
189 return nl2br($string);
190 } else {
191 return $string;
195 function persist($fid = "")
197 if (!empty($fid)) {
198 $this->foreign_id = $fid;
201 parent::persist();
203 } // end of PhoneNumber
204 /*$p = new PhoneNumber(1);
205 echo $p->toString();
206 $p = new PhoneNumber(true);
208 $ps = PhoneNumber::factory_phone_numbers(55);
209 foreach($ps as $p) {
210 echo $p->toString(true);