more organization of autoloaded files (#424)
[openemr.git] / library / classes / ORDataObject.class.php
blob0f6ecf5ea9addef8386de227819d5105fad087a2
1 <?php
2 require_once("Patient.class.php");
3 require_once("Person.class.php");
4 require_once("Provider.class.php");
5 require_once("Pharmacy.class.php");
7 /**
8 * class ORDataObject
12 class ORDataObject {
13 var $_prefix;
14 var $_table;
15 var $_db;
17 function __construct() {
18 $this->_db = $GLOBALS['adodb']['db'];
21 function persist() {
22 $sql = "REPLACE INTO " . $_prefix . $this->_table . " SET ";
23 //echo "<br><br>";
24 $fields = sqlListFields($this->_table);
25 $db = get_db();
26 $pkeys = $db->MetaPrimaryKeys($this->_table);
28 foreach ($fields as $field) {
29 $func = "get_" . $field;
30 //echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br>";
31 if (is_callable(array($this,$func))) {
32 $val = call_user_func(array($this,$func));
34 //modified 01-2010 by BGM to centralize to formdata.inc.php
35 // have place several debug statements to allow standardized testing over next several months
36 if (!is_array($val)) {
37 //DEBUG LINE - error_log("ORDataObject persist before strip: ".$val, 0);
38 $val = strip_escape_custom($val);
39 //DEBUG LINE - error_log("ORDataObject persist after strip: ".$val, 0);
42 if (in_array($field,$pkeys) && empty($val)) {
43 $last_id = generate_id();
44 call_user_func(array(&$this,"set_".$field),$last_id);
45 $val = $last_id;
48 if (!empty($val)) {
49 //echo "s: $field to: $val <br>";
51 //modified 01-2010 by BGM to centralize to formdata.inc.php
52 // have place several debug statements to allow standardized testing over next several months
53 $sql .= " `" . $field . "` = '" . add_escape_custom(strval($val)) ."',";
54 //DEBUG LINE - error_log("ORDataObject persist after escape: ".add_escape_custom(strval($val)), 0);
55 //DEBUG LINE - error_log("ORDataObject persist after escape and then stripslashes test: ".stripslashes(add_escape_custom(strval($val))), 0);
56 //DEBUG LINE - error_log("ORDataObject original before the escape and then stripslashes test: ".strval($val), 0);
61 if (strrpos($sql,",") == (strlen($sql) -1)) {
62 $sql = substr($sql,0,(strlen($sql) -1));
65 //echo "<br>sql is: " . $sql . "<br /><br>";
66 sqlQuery($sql);
67 return true;
70 function populate() {
71 $sql = "SELECT * from " . $this->_prefix . $this->_table . " WHERE id = '" . add_escape_custom(strval($this->id)) . "'";
72 $results = sqlQuery($sql);
73 if (is_array($results)) {
74 foreach ($results as $field_name => $field) {
75 $func = "set_" . $field_name;
76 //echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br>";
77 if (is_callable(array($this,$func))) {
79 if (!empty($field)) {
80 //echo "s: $field_name to: $field <br>";
81 call_user_func(array(&$this,$func),$field);
89 function populate_array($results) {
90 if (is_array($results)) {
91 foreach ($results as $field_name => $field) {
92 $func = "set_" . $field_name;
93 //echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br>";
94 if (is_callable(array($this,$func))) {
96 if (!empty($field)) {
97 //echo "s: $field_name to: $field <br>";
98 call_user_func(array(&$this,$func),$field);
107 * Helper function that loads enumerations from the data as an array, this is also efficient
108 * because it uses psuedo-class variables so that it doesnt have to do database work for each instance
110 * @param string $field_name name of the enumeration in this objects table
111 * @param boolean $blank optional value to include a empty element at position 0, default is true
112 * @return array array of values as name to index pairs found in the db enumeration of this field
114 function _load_enum($field_name,$blank = true) {
115 if (!empty($GLOBALS['static']['enums'][$this->_table][$field_name])
116 && is_array($GLOBALS['static']['enums'][$this->_table][$field_name])
117 && !empty($this->_table)) {
119 return $GLOBALS['static']['enums'][$this->_table][$field_name];
121 else {
122 $cols = $this->_db->MetaColumns($this->_table);
123 if ($cols && !$cols->EOF) {
124 //why is there a foreach here? at some point later there will be a scheme to autoload all enums
125 //for an object rather than 1x1 manually as it is now
126 foreach($cols as $col) {
127 if ($col->name == $field_name && $col->type == "enum") {
128 for($idx=0;$idx<count($col->enums);$idx++)
130 $col->enums[$idx]=str_replace("'","",$col->enums[$idx]);
132 $enum = $col->enums;
133 //for future use
134 //$enum[$col->name] = $enum_types[1];
137 array_unshift($enum," ");
139 //keep indexing consistent whether or not a blank is present
140 if (!$blank) {
141 unset($enum[0]);
143 $enum = array_flip($enum);
144 $GLOBALS['static']['enums'][$this->_table][$field_name] = $enum;
146 return $enum;
150 function _utility_array($obj_ar,$reverse=false,$blank=true, $name_func="get_name", $value_func="get_id") {
151 $ar = array();
152 if ($blank) {
153 $ar[0] = " ";
155 if (!is_array($obj_ar)) return $ar;
156 foreach($obj_ar as $obj) {
157 $ar[$obj->$value_func()] = $obj->$name_func();
159 if ($reverse) {
160 $ar = array_flip($ar);
162 return $ar;
165 } // end of ORDataObject