Fix for proper form data handling.
[openemr.git] / library / classes / PhoneNumber.class.php
blob8172fc475151aa0535bf641065847b7881362b99
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);
17 require_once("ORDataObject.class.php");
19 /**
20 * class Address
23 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 PhoneNumber($id = "",$foreign_id = "") {
36 $this->id = $id;
37 $this->foreign_id = $foreign_id;
38 $this->country_code = "+1";
39 $this->prefix = "";
40 $this->number = "";
41 $this->type = TYPE_HOME;
42 $this->_table = "phone_numbers";
43 if ($id != "") {
44 $this->populate();
49 function factory_phone_numbers($foreign_id = "") {
50 if (empty($foreign_id)) {
51 $foreign_id= "like '%'";
53 else {
54 $foreign_id= " = '" . mysql_real_escape_string(strval($foreign_id)) . "'";
56 $phone_numbers = array();
57 $p = new PhoneNumber();
58 $sql = "SELECT id FROM " . $p->_table . " WHERE foreign_id " .$foreign_id . " ORDER BY type";
59 //echo $sql . "<bR />";
60 $results = sqlQ($sql);
61 //echo "sql: $sql";
62 while ($row = mysql_fetch_array($results) ) {
63 $phone_numbers[] = new PhoneNumber($row['id']);
65 return $phone_numbers;
68 function set_id ($id) {
69 $this->id = $id;
72 function get_id () {
73 return $this->id;
76 function foreign_id ($id) {
77 $this->foreign_id = $id;
80 function get_foreign_id () {
81 return $this->foreign_id;
84 function set_country_code ($ccode) {
85 $this->country_code = $ccode;
88 function get_country_code () {
89 return $this->country_code;
91 function set_area_code ($acode) {
92 $this->area_code = $acode;
95 function get_area_code () {
96 return $this->area_code;
99 function set_number ($num) {
100 $this->number = $num;
103 function get_number () {
104 return $this->number;
108 function set_type ($type) {
109 $this->type = $type;
112 function get_type () {
113 return $this->type;
116 function set_prefix ($prefix) {
117 $this->prefix = $prefix;
120 function get_prefix () {
121 return $this->prefix;
124 function get_phone_display() {
125 if (is_numeric($this->area_code) && is_numeric($this->prefix) && is_numeric($this->number)) {
126 // return "(" . $this->area_code . ") " . $this->prefix . "-" . $this->number;
127 return $this->area_code . "-" . $this->prefix . "-" . $this->number;
129 return "";
132 function set_phone($num) {
133 if (strlen($num) == 10 && is_numeric($num)) {
134 $this->area_code = substr ($num,0,3);
135 $this->prefix = substr ($num,3,3);
136 $this->number = substr ($num,6,4);
138 elseif (strlen($num) == 12) {
139 $nums = split("-",$num);
140 if (count($nums) == 3) {
141 $this->area_code = $nums[0];
142 $this->prefix = $nums[1];
143 $this->number = $nums[2];
146 elseif (strlen($num) == 14 && substr($num,0,1) == "(") {
147 $nums[0] = substr($num,1,3);
148 $nums[1] = substr($num,6,3);
149 $nums[2] = substr($num,10,4);
151 foreach ($nums as $n) {
152 if (!is_numeric($n)) {
153 return false;
157 if (count($nums) == 3) {
158 $this->area_code = $nums[0];
159 $this->prefix = $nums[1];
160 $this->number = $nums[2];
165 function toString($html = false) {
166 $string .= "\n"
167 . "ID: " . $this->id."\n"
168 . "FID: " . $this->foreign_id."\n"
169 . $this->country_code . " (" . $this->area_code . ") " . $this->prefix . "-" . $this->number . " " . $this->type_array[$this->type];
170 if ($html) {
171 return nl2br($string);
173 else {
174 return $string;
178 function persist($fid ="") {
179 if (!empty($fid)) {
180 $this->foreign_id = $fid;
182 parent::persist();
185 } // end of PhoneNumber
186 /*$p = new PhoneNumber(1);
187 echo $p->toString();
188 $p = new PhoneNumber(true);
190 $ps = PhoneNumber::factory_phone_numbers(55);
191 foreach($ps as $p) {
192 echo $p->toString(true);