migrated ubiquitous libraries to composer autoloader (#421)
[openemr.git] / library / classes / ORDataObject.class.php
blobf76e7f0dc09afd5919db1173a319118c3ccfe1d3
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 __construct() {
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));
35 //modified 01-2010 by BGM to centralize to formdata.inc.php
36 // have place several debug statements to allow standardized testing over next several months
37 if (!is_array($val)) {
38 //DEBUG LINE - error_log("ORDataObject persist before strip: ".$val, 0);
39 $val = strip_escape_custom($val);
40 //DEBUG LINE - error_log("ORDataObject persist after strip: ".$val, 0);
43 if (in_array($field,$pkeys) && empty($val)) {
44 $last_id = generate_id();
45 call_user_func(array(&$this,"set_".$field),$last_id);
46 $val = $last_id;
49 if (!empty($val)) {
50 //echo "s: $field to: $val <br>";
52 //modified 01-2010 by BGM to centralize to formdata.inc.php
53 // have place several debug statements to allow standardized testing over next several months
54 $sql .= " `" . $field . "` = '" . add_escape_custom(strval($val)) ."',";
55 //DEBUG LINE - error_log("ORDataObject persist after escape: ".add_escape_custom(strval($val)), 0);
56 //DEBUG LINE - error_log("ORDataObject persist after escape and then stripslashes test: ".stripslashes(add_escape_custom(strval($val))), 0);
57 //DEBUG LINE - error_log("ORDataObject original before the escape and then stripslashes test: ".strval($val), 0);
62 if (strrpos($sql,",") == (strlen($sql) -1)) {
63 $sql = substr($sql,0,(strlen($sql) -1));
66 //echo "<br>sql is: " . $sql . "<br /><br>";
67 sqlQuery($sql);
68 return true;
71 function populate() {
72 $sql = "SELECT * from " . $this->_prefix . $this->_table . " WHERE id = '" . add_escape_custom(strval($this->id)) . "'";
73 $results = sqlQuery($sql);
74 if (is_array($results)) {
75 foreach ($results as $field_name => $field) {
76 $func = "set_" . $field_name;
77 //echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br>";
78 if (is_callable(array($this,$func))) {
80 if (!empty($field)) {
81 //echo "s: $field_name to: $field <br>";
82 call_user_func(array(&$this,$func),$field);
90 function populate_array($results) {
91 if (is_array($results)) {
92 foreach ($results as $field_name => $field) {
93 $func = "set_" . $field_name;
94 //echo "f: $field m: $func status: " . (is_callable(array($this,$func))? "yes" : "no") . "<br>";
95 if (is_callable(array($this,$func))) {
97 if (!empty($field)) {
98 //echo "s: $field_name to: $field <br>";
99 call_user_func(array(&$this,$func),$field);
108 * Helper function that loads enumerations from the data as an array, this is also efficient
109 * because it uses psuedo-class variables so that it doesnt have to do database work for each instance
111 * @param string $field_name name of the enumeration in this objects table
112 * @param boolean $blank optional value to include a empty element at position 0, default is true
113 * @return array array of values as name to index pairs found in the db enumeration of this field
115 function _load_enum($field_name,$blank = true) {
116 if (!empty($GLOBALS['static']['enums'][$this->_table][$field_name])
117 && is_array($GLOBALS['static']['enums'][$this->_table][$field_name])
118 && !empty($this->_table)) {
120 return $GLOBALS['static']['enums'][$this->_table][$field_name];
122 else {
123 $cols = $this->_db->MetaColumns($this->_table);
124 if ($cols && !$cols->EOF) {
125 //why is there a foreach here? at some point later there will be a scheme to autoload all enums
126 //for an object rather than 1x1 manually as it is now
127 foreach($cols as $col) {
128 if ($col->name == $field_name && $col->type == "enum") {
129 for($idx=0;$idx<count($col->enums);$idx++)
131 $col->enums[$idx]=str_replace("'","",$col->enums[$idx]);
133 $enum = $col->enums;
134 //for future use
135 //$enum[$col->name] = $enum_types[1];
138 array_unshift($enum," ");
140 //keep indexing consistent whether or not a blank is present
141 if (!$blank) {
142 unset($enum[0]);
144 $enum = array_flip($enum);
145 $GLOBALS['static']['enums'][$this->_table][$field_name] = $enum;
147 return $enum;
151 function _utility_array($obj_ar,$reverse=false,$blank=true, $name_func="get_name", $value_func="get_id") {
152 $ar = array();
153 if ($blank) {
154 $ar[0] = " ";
156 if (!is_array($obj_ar)) return $ar;
157 foreach($obj_ar as $obj) {
158 $ar[$obj->$value_func()] = $obj->$name_func();
160 if ($reverse) {
161 $ar = array_flip($ar);
163 return $ar;
166 } // end of ORDataObject