some assorted updates
[openemr.git] / library / classes / ORDataObject.class.php
blob930103ae813767e2b4ebf9e5e328b55121f2fe5d
1 <?php
2 require_once (dirname(__FILE__) ."/../sql.inc");
3 require_once("Patient.class.php");
4 require_once("Person.class.php");
5 require_once("Provider.class.php");
6 require_once("Pharmacy.class.php");
8 /**
9 * class ORDataObject
13 class ORDataObject {
14 var $_prefix;
15 var $_table;
16 var $_db;
18 function ORDataObject() {
19 $this->_db = $GLOBALS['adodb']['db'];
22 function persist() {
23 $sql = "REPLACE INTO " . $_prefix . $this->_table . " SET ";
24 //echo "<br><br>";
25 $fields = sqlListFields($this->_table);
26 $db = get_db();
27 $pkeys = $db->MetaPrimaryKeys($this->_table);
29 foreach ($fields as $field) {
30 $func = "get_" . $field;
31 //echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br>";
32 if (is_callable(array($this,$func))) {
33 $val = call_user_func(array($this,$func));
34 if ((get_magic_quotes_gpc() || get_magic_quotes_runtime()) && !is_array($val)) {
35 $val = stripslashes($val);
37 if (in_array($field,$pkeys) && empty($val)) {
38 $last_id = generate_id();
39 call_user_func(array(&$this,"set_".$field),$last_id);
40 $val = $last_id;
43 if (!empty($val)) {
44 //echo "s: $field to: $val <br>";
45 $sql .= " `" . $field . "` = '" . mysql_real_escape_string(strval($val)) ."',";
50 if (strrpos($sql,",") == (strlen($sql) -1)) {
51 $sql = substr($sql,0,(strlen($sql) -1));
54 //echo "<br>sql is: " . $sql . "<br /><br>";
55 sqlQuery($sql);
56 return true;
59 function populate() {
60 $sql = "SELECT * from " . $this->_prefix . $this->_table . " WHERE id = '" . mysql_real_escape_string(strval($this->id)) . "'";
61 $results = sqlQuery($sql);
62 if (is_array($results)) {
63 foreach ($results as $field_name => $field) {
64 $func = "set_" . $field_name;
65 //echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br>";
66 if (is_callable(array($this,$func))) {
68 if (!empty($field)) {
69 //echo "s: $field_name to: $field <br>";
70 call_user_func(array(&$this,$func),$field);
78 function populate_array($results) {
79 if (is_array($results)) {
80 foreach ($results as $field_name => $field) {
81 $func = "set_" . $field_name;
82 //echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br>";
83 if (is_callable(array($this,$func))) {
85 if (!empty($field)) {
86 //echo "s: $field_name to: $field <br>";
87 call_user_func(array(&$this,$func),$field);
95 /**
96 * Helper function that loads enumerations from the data as an array, this is also efficient
97 * because it uses psuedo-class variables so that it doesnt have to do database work for each instance
99 * @param string $field_name name of the enumeration in this objects table
100 * @param boolean $blank optional value to include a empty element at position 0, default is true
101 * @return array array of values as name to index pairs found in the db enumeration of this field
103 function _load_enum($field_name,$blank = true) {
104 if (!empty($GLOBALS['static']['enums'][$this->_table][$field_name])
105 && is_array($GLOBALS['static']['enums'][$this->_table][$field_name])
106 && !empty($this->_table)) {
108 return $GLOBALS['static']['enums'][$this->_table][$field_name];
110 else {
111 $cols = $this->_db->MetaColumns($this->_table);
112 if ($cols && !$cols->EOF) {
113 //why is there a foreach here? at some point later there will be a scheme to autoload all enums
114 //for an object rather than 1x1 manually as it is now
115 foreach($cols as $col) {
116 if ($col->name == $field_name && substr($col->type,0,4) == "enum") {
117 preg_match_all("|[\'](.*)[\']|U",$col->type,$enum_types);
118 //position 1 is where preg_match puts the matches sans the delimiters
119 $enum = $enum_types[1];
120 //for future use
121 //$enum[$col->name] = $enum_types[1];
124 array_unshift($enum," ");
126 //keep indexing consistent whether or not a blank is present
127 if (!$blank) {
128 unset($enum[0]);
130 $enum = array_flip($enum);
131 $GLOBALS['static']['enums'][$this->_table][$field_name] = $enum;
133 return $enum;
137 function _utility_array($obj_ar,$reverse=false,$blank=true, $name_func="get_name", $value_func="get_id") {
138 $ar = array();
139 if ($blank) {
140 $ar[0] = " ";
142 if (!is_array($obj_ar)) return $ar;
143 foreach($obj_ar as $obj) {
144 $ar[$obj->$value_func()] = $obj->$name_func();
146 if ($reverse) {
147 $ar = array_flip($ar);
149 return $ar;
152 } // end of ORDataObject