mysql 8 fixes (#1639)
[openemr.git] / library / classes / ORDataObject.class.php
blob21b8812e445f3753e011f8388a0f954429f12c89
1 <?php
3 /**
4 * class ORDataObject
6 */
8 class ORDataObject
10 var $_prefix;
11 var $_table;
12 var $_db;
14 function __construct()
16 $this->_db = $GLOBALS['adodb']['db'];
19 function persist()
21 $sql = "REPLACE INTO " . $this->_prefix . $this->_table . " SET ";
22 //echo "<br><br>";
23 $fields = sqlListFields($this->_table);
24 $db = get_db();
25 $pkeys = $db->MetaPrimaryKeys($this->_table);
27 foreach ($fields as $field) {
28 $func = "get_" . $field;
29 //echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br>";
30 if (is_callable(array($this,$func))) {
31 $val = call_user_func(array($this,$func));
33 //modified 01-2010 by BGM to centralize to formdata.inc.php
34 // have place several debug statements to allow standardized testing over next several months
35 if (!is_array($val)) {
36 //DEBUG LINE - error_log("ORDataObject persist before strip: ".$val, 0);
37 $val = strip_escape_custom($val);
38 //DEBUG LINE - error_log("ORDataObject persist after strip: ".$val, 0);
41 if (in_array($field, $pkeys) && empty($val)) {
42 $last_id = generate_id();
43 call_user_func(array(&$this,"set_".$field), $last_id);
44 $val = $last_id;
47 if (!empty($val)) {
48 //echo "s: $field to: $val <br>";
50 //modified 01-2010 by BGM to centralize to formdata.inc.php
51 // have place several debug statements to allow standardized testing over next several months
52 $sql .= " `" . $field . "` = '" . add_escape_custom(strval($val)) ."',";
53 //DEBUG LINE - error_log("ORDataObject persist after escape: ".add_escape_custom(strval($val)), 0);
54 //DEBUG LINE - error_log("ORDataObject persist after escape and then stripslashes test: ".stripslashes(add_escape_custom(strval($val))), 0);
55 //DEBUG LINE - error_log("ORDataObject original before the escape and then stripslashes test: ".strval($val), 0);
60 if (strrpos($sql, ",") == (strlen($sql) -1)) {
61 $sql = substr($sql, 0, (strlen($sql) -1));
64 //echo "<br>sql is: " . $sql . "<br /><br>";
65 sqlQuery($sql);
66 return true;
69 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))) {
78 if (!empty($field)) {
79 //echo "s: $field_name to: $field <br>";
80 call_user_func(array(&$this,$func), $field);
87 function populate_array($results)
89 if (is_array($results)) {
90 foreach ($results as $field_name => $field) {
91 $func = "set_" . $field_name;
92 //echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br>";
93 if (is_callable(array($this,$func))) {
94 if (!empty($field)) {
95 //echo "s: $field_name to: $field <br>";
96 call_user_func(array(&$this,$func), $field);
104 * Helper function that loads enumerations from the data as an array, this is also efficient
105 * because it uses psuedo-class variables so that it doesnt have to do database work for each instance
107 * @param string $field_name name of the enumeration in this objects table
108 * @param boolean $blank optional value to include a empty element at position 0, default is true
109 * @return array array of values as name to index pairs found in the db enumeration of this field
111 function _load_enum($field_name, $blank = true)
113 if (!empty($GLOBALS['static']['enums'][$this->_table][$field_name])
114 && is_array($GLOBALS['static']['enums'][$this->_table][$field_name])
115 && !empty($this->_table)) {
116 return $GLOBALS['static']['enums'][$this->_table][$field_name];
117 } else {
118 $cols = $this->_db->MetaColumns($this->_table);
119 if ($cols && !$cols->EOF) {
120 //why is there a foreach here? at some point later there will be a scheme to autoload all enums
121 //for an object rather than 1x1 manually as it is now
122 foreach ($cols as $col) {
123 if ($col->name == $field_name && $col->type == "enum") {
124 for ($idx=0; $idx<count($col->enums); $idx++) {
125 $col->enums[$idx]=str_replace("'", "", $col->enums[$idx]);
128 $enum = $col->enums;
129 //for future use
130 //$enum[$col->name] = $enum_types[1];
134 array_unshift($enum, " ");
136 //keep indexing consistent whether or not a blank is present
137 if (!$blank) {
138 unset($enum[0]);
141 $enum = array_flip($enum);
142 $GLOBALS['static']['enums'][$this->_table][$field_name] = $enum;
145 return $enum;
149 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] = " ";
156 if (!is_array($obj_ar)) {
157 return $ar;
160 foreach ($obj_ar as $obj) {
161 $ar[$obj->$value_func()] = $obj->$name_func();
164 if ($reverse) {
165 $ar = array_flip($ar);
168 return $ar;
170 } // end of ORDataObject