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