fix provided by mdsupport
[openemr.git] / library / parse_patient_xml.php
blob937dad96f896766d01191092999a3ed29034067e
1 <?php
2 /**
3 * library/parse_patient_xml.php Functions related to patient CCR/CCD/CCDA parsing.
5 * Functions related to patient CCR/CCD/CCDA parsing and insert/update to corresponding tables.
7 * Copyright (C) 2013 Z&H Consultancy Services Private Limited <sam@zhservices.com>
9 * LICENSE: This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
20 * @package OpenEMR
21 * @author Eldho Chacko <eldho@zhservices.com>
22 * @author Ajil P M <ajilpm@zhservices.com>
23 * @link http://www.open-emr.org
28 function parseXmlStream($content, $field_mapping)
30 $res = array();
31 $xml = new DOMDocument;
32 $xml->loadXML($content);
33 $xpath = new DOMXpath($xml);
34 $rootNamespace = $xml->lookupNamespaceUri($xml->namespaceURI);
35 $xpath->registerNamespace('x', $rootNamespace);
36 foreach ($field_mapping as $skey => $sval) {
37 $path = preg_replace("/\/([a-zA-Z])/", "/x:$1", $skey);
38 $elements = $xpath->query($path);
39 if (!is_null($elements)) {
40 $ele_cnt = 1;
41 foreach ($elements as $element) {
42 foreach ($sval as $field => $innerpath) {
43 $ipath = preg_replace(array("/^([a-zA-Z])/","/\/([a-zA-Z])/"), array("x:$1","/x:$1"), $innerpath);
44 $val = $xpath->query($ipath, $element)->item(0)->textContent;
45 if ($val) {
46 $field_details = explode(':', $field);
47 $res[$field_details[0]][$ele_cnt][$field_details[1]] = $val;
51 $ele_cnt++;
56 return $res;
59 function insert_ccr_into_audit_data($var)
61 $audit_master_id_to_delete = $var['audit_master_id_to_delete'];
62 $approval_status = $var['approval_status'];
63 $type = $var['type'];
64 $ip_address = $var['ip_address'];
65 $field_name_value_array = $var['field_name_value_array'];
66 $entry_identification_array = $var['entry_identification_array'];
67 if ($audit_master_id_to_delete) {
68 $qry = "DELETE from audit_details WHERE audit_master_id=?";
69 sqlStatement($qry, array($audit_master_id_to_delete));
70 $qry = "DELETE from audit_master WHERE id=?";
71 sqlStatement($qry, array($audit_master_id_to_delete));
74 $master_query = "INSERT INTO audit_master SET pid = ?,approval_status = ?,ip_address = ?,type = ?";
75 $audit_master_id = sqlInsert($master_query, array(0,$approval_status,$ip_address,$type));
76 $detail_query = "INSERT INTO `audit_details` (`table_name`, `field_name`, `field_value`, `audit_master_id`, `entry_identification`) VALUES ";
77 $detail_query_array = '';
78 foreach ($field_name_value_array as $key => $val) {
79 foreach ($field_name_value_array[$key] as $cnt => $field_details) {
80 foreach ($field_details as $field_name => $field_value) {
81 $detail_query .= "(? ,? ,? ,? ,?),";
82 $detail_query_array[] = $key;
83 $detail_query_array[] = trim($field_name);
84 $detail_query_array[] = trim($field_value);
85 $detail_query_array[] = $audit_master_id;
86 $detail_query_array[] = trim($entry_identification_array[$key][$cnt]);
91 $detail_query = substr($detail_query, 0, -1);
92 $detail_query = $detail_query.';';
93 sqlInsert($detail_query, $detail_query_array);
96 function insert_patient($audit_master_id)
98 $prow = sqlQuery("SELECT IFNULL(MAX(pid)+1,1) AS pid FROM patient_data");
99 $pid = $prow['pid'];
100 $res = sqlStatement("SELECT DISTINCT ad.table_name,entry_identification FROM audit_master as am,audit_details as ad WHERE am.id=ad.audit_master_id AND am.approval_status = '1' AND am.id=? AND am.type=11 ORDER BY ad.id", array($audit_master_id));
101 $tablecnt = sqlNumRows($res);
102 while ($row = sqlFetchArray($res)) {
103 $resfield = sqlStatement("SELECT * FROM audit_details WHERE audit_master_id=? AND table_name=? AND entry_identification=?", array($audit_master_id,$row['table_name'],$row['entry_identification']));
104 $table = $row['table_name'];
105 $newdata = array();
106 while ($rowfield = sqlFetchArray($resfield)) {
107 if ($table == 'patient_data') {
108 if ($rowfield['field_name'] == 'DOB') {
109 $newdata['patient_data'][$rowfield['field_name']] = substr($rowfield['field_value'], 0, 10);
110 } else {
111 $newdata['patient_data'][$rowfield['field_name']] = $rowfield['field_value'];
113 } elseif ($table == 'lists1') {
114 $newdata['lists1'][$rowfield['field_name']] = $rowfield['field_value'];
115 } elseif ($table == 'lists2') {
116 $newdata['lists2'][$rowfield['field_name']] = $rowfield['field_value'];
117 } elseif ($table == 'prescriptions') {
118 $newdata['prescriptions'][$rowfield['field_name']] = $rowfield['field_value'];
119 } elseif ($table == 'immunizations') {
120 $newdata['immunizations'][$rowfield['field_name']] = $rowfield['field_value'];
121 } elseif ($table == 'procedure_result') {
122 $newdata['procedure_result'][$rowfield['field_name']] = $rowfield['field_value'];
123 } elseif ($table == 'procedure_type') {
124 $newdata['procedure_type'][$rowfield['field_name']] = $rowfield['field_value'];
125 } elseif ($table == 'misc_address_book') {
126 $newdata['misc_address_book'][$rowfield['field_name']] = $rowfield['field_value'];
127 } elseif ($table == 'documents') {
128 $newdata['documents'][$rowfield['field_name']] = $rowfield['field_value'];
132 if ($table == 'patient_data') {
133 updatePatientData($pid, $newdata['patient_data'], true);
134 } elseif ($table == 'lists1') {
135 sqlInsert("INSERT INTO lists(".
136 "pid,diagnosis,activity".
137 ") VALUES (".
138 "'".add_escape_custom($pid)."',".
139 "'".add_escape_custom($newdata['lists1']['diagnosis'])."',".
140 "'".add_escape_custom($newdata['lists1']['activity'])."')");
141 } elseif ($table == 'lists2') {
142 sqlInsert("INSERT INTO lists(".
143 "pid,date,type,title,diagnosis,reaction".
144 ") VALUES (".
145 "'".add_escape_custom($pid)."',".
146 "'".add_escape_custom($newdata['lists2']['date'])."',".
147 "'".add_escape_custom($newdata['lists2']['type'])."',".
148 "'".add_escape_custom($newdata['lists2']['title'])."',".
149 "'".add_escape_custom($newdata['lists2']['diagnosis'])."',".
150 "'".add_escape_custom($newdata['lists2']['reaction'])."')");
151 } elseif ($table == 'prescriptions') {
152 sqlInsert("INSERT INTO prescriptions(".
153 "patient_id,date_added,active,drug,size,form,quantity".
154 ") VALUES (".
155 "'".add_escape_custom($pid)."',".
156 "'".add_escape_custom($newdata['prescriptions']['date_added'])."',".
157 "'".add_escape_custom($newdata['prescriptions']['active'])."',".
158 "'".add_escape_custom($newdata['prescriptions']['drug'])."',".
159 "'".add_escape_custom($newdata['prescriptions']['size'])."',".
160 "'".add_escape_custom($newdata['prescriptions']['form'])."',".
161 "'".add_escape_custom($newdata['prescriptions']['quantity'])."')");
162 } elseif ($table == 'immunizations') {
163 sqlInsert("INSERT INTO immunizations(".
164 "patient_id,administered_date,note".
165 ") VALUES (".
166 "'".add_escape_custom($pid)."',".
167 "'".add_escape_custom($newdata['immunizations']['administered_date'])."',".
168 "'".add_escape_custom($newdata['immunizations']['note'])."')");
169 } elseif ($table == 'procedure_result') {
170 /*sqlInsert("INSERT INTO procedure_result(".
171 "date,result,abnormal".
172 ") VALUES (".
173 "'".add_escape_custom($newdata['procedure_result']['date'])."',".
174 "'".add_escape_custom($newdata['procedure_result']['result'])."',".
175 "'".add_escape_custom($newdata['procedure_result']['abnormal'])."')"
176 );*/
177 } elseif ($table == 'procedure_type') {
178 /*sqlInsert("INSERT INTO procedure_type(".
179 "name".
180 ") VALUES (".
181 "'".add_escape_custom($newdata['procedure_type']['name'])."')"
182 );*/
183 } elseif ($table == 'misc_address_book') {
184 sqlInsert("INSERT INTO misc_address_book(".
185 "lname,fname,street,city,state,zip,phone".
186 ") VALUES (".
187 "'".add_escape_custom($newdata['misc_address_book']['lname'])."',".
188 "'".add_escape_custom($newdata['misc_address_book']['fname'])."',".
189 "'".add_escape_custom($newdata['misc_address_book']['street'])."',".
190 "'".add_escape_custom($newdata['misc_address_book']['city'])."',".
191 "'".add_escape_custom($newdata['misc_address_book']['state'])."',".
192 "'".add_escape_custom($newdata['misc_address_book']['zip'])."',".
193 "'".add_escape_custom($newdata['misc_address_book']['phone'])."')");
194 } elseif ($table == 'documents') {
195 sqlQuery("UPDATE documents SET foreign_id = ? WHERE id =? ", array($pid,$newdata['documents']['id']));
199 sqlQuery("UPDATE audit_master SET approval_status=2 WHERE id=?", array($audit_master_id));
202 function createAuditArray($am_id, $table_name)
204 if (strpos($table_name, ',')) {
205 $tables = explode(',', $table_name);
206 $arr = array($am_id);
207 $table_qry = "";
208 for ($i=0; $i<count($tables); $i++) {
209 $table_qry .= "?,";
210 array_unshift($arr, $tables[$i]);
213 $table_qry = substr($table_qry, 0, -1);
214 $query = sqlStatement("SELECT * FROM audit_master am LEFT JOIN audit_details ad ON ad.audit_master_id = am.id AND ad.table_name IN ($table_qry)
215 WHERE am.id = ? AND am.type = 11 AND am.approval_status = 1 ORDER BY ad.entry_identification,ad.field_name", $arr);
216 } else {
217 $query = sqlStatement("SELECT * FROM audit_master am LEFT JOIN audit_details ad ON ad.audit_master_id = am.id AND ad.table_name = ?
218 WHERE am.id = ? AND am.type = 11 AND am.approval_status = 1 ORDER BY ad.entry_identification,ad.field_name", array($table_name,$am_id));
221 $result = array();
222 while ($res = sqlFetchArray($query)) {
223 $result[$table_name][$res['entry_identification']][$res['field_name']] = $res['field_value'];
226 return $result;
229 function insertApprovedData($data)
231 $patient_data_fields = '';
232 $patient_data_values = array();
233 foreach ($data as $key => $val) {
234 if (substr($key, -4) == '-sel') {
235 if (is_array($val)) {
236 for ($i=0; $i<count($val); $i++) {
237 if ($val[$i] == 'insert') {
238 if (substr($key, 0, -4) == 'lists1') {
239 if ($_REQUEST['lists1-activity'][$i] == 'Active') {
240 $activity = 1;
241 } elseif ($_REQUEST['lists1-activity'][$i] == 'Inactive') {
242 $activity = 0;
245 $query = "INSERT INTO lists (pid,diagnosis,activity) VALUES (?,?,?)";
246 sqlQuery($query, array($_REQUEST['pid'],$_REQUEST['lists1-diagnosis'][$i],$activity));
247 } elseif (substr($key, 0, -4) == 'lists2') {
248 $query = "INSERT INTO lists (pid,date,type,title,diagnosis,reaction) VALUES (?,?,?,?,?,?)";
249 sqlQuery($query, array($_REQUEST['pid'],$_REQUEST['lists2-date'][$i],$_REQUEST['lists2-type'][$i],$_REQUEST['lists2-title'][$i],$_REQUEST['lists2-diagnosis'][$i],$_REQUEST['lists2-reaction'][$i]));
250 } elseif (substr($key, 0, -4) == 'prescriptions') {
251 if ($_REQUEST['prescriptions-active'][$i] == 'Active') {
252 $active = 1;
253 } elseif ($_REQUEST['prescriptions-active'][$i] == 'Inactive') {
254 $active = 0;
257 $query = "INSERT INTO prescriptions (patient_id,date_added,active,drug,size,form,quantity) VALUES (?,?,?,?,?,?,?)";
258 sqlQuery($query, array($_REQUEST['pid'],$_REQUEST['prescriptions-date_added'][$i],$active,$_REQUEST['prescriptions-drug'][$i],$_REQUEST['prescriptions-size'][$i],$_REQUEST['prescriptions-form'][$i],$_REQUEST['prescriptions-quantity'][$i]));
259 } elseif (substr($key, 0, -4) == 'immunizations') {
260 $query = "INSERT INTO immunizations (patient_id,administered_date,note) VALUES (?,?,?)";
261 sqlQuery($query, array($_REQUEST['pid'],$_REQUEST['immunizations-administered_date'][$i],$_REQUEST['immunizations-note'][$i]));
262 } elseif (substr($key, 0, -4) == 'procedure_result') {
263 //$query = "INSERT INTO procedure_type (name) VALUES (?)";
264 //sqlQuery($query,array($_REQUEST['procedure_type-name'][$i]));
265 //$query = "INSERT INTO procedure_result (date,result,abnormal) VALUES (?,?,?)";
266 //sqlQuery($query,array($_REQUEST['procedure_result-date'][$i],$active,$_REQUEST['procedure_result-abnormal'][$i]));
268 } elseif ($val[$i] == 'update') {
269 if (substr($key, 0, -4) == 'lists1') {
270 if ($_REQUEST['lists1-activity'][$i] == 'Active') {
271 $activity = 1;
272 } elseif ($_REQUEST['lists1-activity'][$i] == 'Inactive') {
273 $activity = 0;
276 $query = "UPDATE lists SET diagnosis=?,activity=? WHERE pid=? AND diagnosis=?";
277 sqlQuery($query, array($_REQUEST['lists1-diagnosis'][$i],$activity,$_REQUEST['pid'],$_REQUEST['lists1-old-diagnosis'][$i]));
281 } else {
282 if (substr($key, 0, 12) == 'patient_data') {
283 if ($val == 'update') {
284 $var_name = substr($key, 0, -4);
285 $field_name = substr($var_name, 13);
286 $patient_data_fields .= $field_name.'=?,';
287 array_push($patient_data_values, $_REQUEST[$var_name]);
294 if (count($patient_data_values) > 0) {
295 array_push($patient_data_values, $_REQUEST['pid']);
296 $patient_data_fields = substr($patient_data_fields, 0, -1);
297 $query = "UPDATE patient_data SET $patient_data_fields WHERE pid=?";
298 sqlQuery($query, $patient_data_values);
301 sqlQuery("UPDATE documents SET foreign_id = ? WHERE id =? ", array($_REQUEST['pid'],$_REQUEST['doc_id']));