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