minor improvement to tabs style
[openemr.git] / library / parse_patient_xml.php
blob68f383db67d2f8dc3f4282ed97c2d7d21319bcd9
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){
29 $res = array();
30 $xml = new DOMDocument;
31 $xml->loadXML($content);
32 $xpath = new DOMXpath($xml);
33 $rootNamespace = $xml->lookupNamespaceUri($xml->namespaceURI);
34 $xpath->registerNamespace('x',$rootNamespace);
35 foreach($field_mapping as $skey=>$sval){
36 $path = preg_replace("/\/([a-zA-Z])/","/x:$1",$skey);
37 $elements = $xpath->query($path);
38 if(!is_null($elements)){
39 $ele_cnt = 1;
40 foreach($elements as $element){
41 foreach($sval as $field => $innerpath){
42 $ipath = preg_replace(array("/^([a-zA-Z])/","/\/([a-zA-Z])/"),array("x:$1","/x:$1"),$innerpath);
43 $val = $xpath->query($ipath, $element)->item(0)->textContent;
44 if($val){
45 $field_details = explode(':',$field);
46 $res[$field_details[0]][$ele_cnt][$field_details[1]] = $val;
49 $ele_cnt++;
53 return $res;
56 function insert_ccr_into_audit_data($var){
57 $audit_master_id_to_delete = $var['audit_master_id_to_delete'];
58 $approval_status = $var['approval_status'];
59 $type = $var['type'];
60 $ip_address = $var['ip_address'];
61 $field_name_value_array = $var['field_name_value_array'];
62 $entry_identification_array = $var['entry_identification_array'];
63 if($audit_master_id_to_delete){
64 $qry = "DELETE from audit_details WHERE audit_master_id=?";
65 sqlStatement($qry,array($audit_master_id_to_delete));
66 $qry = "DELETE from audit_master WHERE id=?";
67 sqlStatement($qry,array($audit_master_id_to_delete));
69 $master_query = "INSERT INTO audit_master SET pid = ?,approval_status = ?,ip_address = ?,type = ?";
70 $audit_master_id = sqlInsert($master_query,array(0,$approval_status,$ip_address,$type));
71 $detail_query = "INSERT INTO `audit_details` (`table_name`, `field_name`, `field_value`, `audit_master_id`, `entry_identification`) VALUES ";
72 $detail_query_array = '';
73 foreach($field_name_value_array as $key=>$val){
74 foreach($field_name_value_array[$key] as $cnt=>$field_details){
75 foreach($field_details as $field_name=>$field_value){
76 $detail_query .= "(? ,? ,? ,? ,?),";
77 $detail_query_array[] = $key;
78 $detail_query_array[] = trim($field_name);
79 $detail_query_array[] = trim($field_value);
80 $detail_query_array[] = $audit_master_id;
81 $detail_query_array[] = trim($entry_identification_array[$key][$cnt]);
85 $detail_query = substr($detail_query, 0, -1);
86 $detail_query = $detail_query.';';
87 sqlInsert($detail_query,$detail_query_array);
90 function insert_patient($audit_master_id){
91 $prow = sqlQuery("SELECT IFNULL(MAX(pid)+1,1) AS pid FROM patient_data");
92 $pid = $prow['pid'];
93 $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));
94 $tablecnt = sqlNumRows($res);
95 while($row = sqlFetchArray($res)){
96 $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']));
97 $table = $row['table_name'];
98 $newdata = array();
99 while($rowfield = sqlFetchArray($resfield)){
100 if($table == 'patient_data'){
101 if($rowfield['field_name'] == 'DOB'){
102 $newdata['patient_data'][$rowfield['field_name']] = substr($rowfield['field_value'],0,10);
103 }else{
104 $newdata['patient_data'][$rowfield['field_name']] = $rowfield['field_value'];
106 }elseif($table == 'lists1'){
107 $newdata['lists1'][$rowfield['field_name']] = $rowfield['field_value'];
108 }elseif($table == 'lists2'){
109 $newdata['lists2'][$rowfield['field_name']] = $rowfield['field_value'];
110 }elseif($table == 'prescriptions'){
111 $newdata['prescriptions'][$rowfield['field_name']] = $rowfield['field_value'];
112 }elseif($table == 'immunizations'){
113 $newdata['immunizations'][$rowfield['field_name']] = $rowfield['field_value'];
114 }elseif($table == 'procedure_result'){
115 $newdata['procedure_result'][$rowfield['field_name']] = $rowfield['field_value'];
116 }elseif($table == 'procedure_type'){
117 $newdata['procedure_type'][$rowfield['field_name']] = $rowfield['field_value'];
118 }elseif($table == 'misc_address_book'){
119 $newdata['misc_address_book'][$rowfield['field_name']] = $rowfield['field_value'];
120 }elseif($table == 'documents'){
121 $newdata['documents'][$rowfield['field_name']] = $rowfield['field_value'];
124 if($table == 'patient_data'){
125 updatePatientData($pid,$newdata['patient_data'],true);
126 }elseif($table == 'lists1'){
127 sqlInsert("INSERT INTO lists(".
128 "pid,diagnosis,activity".
129 ") VALUES (".
130 "'".add_escape_custom($pid)."',".
131 "'".add_escape_custom($newdata['lists1']['diagnosis'])."',".
132 "'".add_escape_custom($newdata['lists1']['activity'])."')"
134 }elseif($table == 'lists2'){
135 sqlInsert("INSERT INTO lists(".
136 "pid,date,type,title,diagnosis,reaction".
137 ") VALUES (".
138 "'".add_escape_custom($pid)."',".
139 "'".add_escape_custom($newdata['lists2']['date'])."',".
140 "'".add_escape_custom($newdata['lists2']['type'])."',".
141 "'".add_escape_custom($newdata['lists2']['title'])."',".
142 "'".add_escape_custom($newdata['lists2']['diagnosis'])."',".
143 "'".add_escape_custom($newdata['lists2']['reaction'])."')"
145 }elseif($table == 'prescriptions'){
146 sqlInsert("INSERT INTO prescriptions(".
147 "patient_id,date_added,active,drug,size,form,quantity".
148 ") VALUES (".
149 "'".add_escape_custom($pid)."',".
150 "'".add_escape_custom($newdata['prescriptions']['date_added'])."',".
151 "'".add_escape_custom($newdata['prescriptions']['active'])."',".
152 "'".add_escape_custom($newdata['prescriptions']['drug'])."',".
153 "'".add_escape_custom($newdata['prescriptions']['size'])."',".
154 "'".add_escape_custom($newdata['prescriptions']['form'])."',".
155 "'".add_escape_custom($newdata['prescriptions']['quantity'])."')"
157 }elseif($table == 'immunizations'){
158 sqlInsert("INSERT INTO immunizations(".
159 "patient_id,administered_date,note".
160 ") VALUES (".
161 "'".add_escape_custom($pid)."',".
162 "'".add_escape_custom($newdata['immunizations']['administered_date'])."',".
163 "'".add_escape_custom($newdata['immunizations']['note'])."')"
165 }elseif($table == 'procedure_result'){
166 /*sqlInsert("INSERT INTO procedure_result(".
167 "date,result,abnormal".
168 ") VALUES (".
169 "'".add_escape_custom($newdata['procedure_result']['date'])."',".
170 "'".add_escape_custom($newdata['procedure_result']['result'])."',".
171 "'".add_escape_custom($newdata['procedure_result']['abnormal'])."')"
172 );*/
173 }elseif($table == 'procedure_type'){
174 /*sqlInsert("INSERT INTO procedure_type(".
175 "name".
176 ") VALUES (".
177 "'".add_escape_custom($newdata['procedure_type']['name'])."')"
178 );*/
179 }elseif($table == 'misc_address_book'){
180 sqlInsert("INSERT INTO misc_address_book(".
181 "lname,fname,street,city,state,zip,phone".
182 ") VALUES (".
183 "'".add_escape_custom($newdata['misc_address_book']['lname'])."',".
184 "'".add_escape_custom($newdata['misc_address_book']['fname'])."',".
185 "'".add_escape_custom($newdata['misc_address_book']['street'])."',".
186 "'".add_escape_custom($newdata['misc_address_book']['city'])."',".
187 "'".add_escape_custom($newdata['misc_address_book']['state'])."',".
188 "'".add_escape_custom($newdata['misc_address_book']['zip'])."',".
189 "'".add_escape_custom($newdata['misc_address_book']['phone'])."')"
191 }elseif($table == 'documents'){
192 sqlQuery("UPDATE documents SET foreign_id = ? WHERE id =? ",array($pid,$newdata['documents']['id']));
195 sqlQuery("UPDATE audit_master SET approval_status=2 WHERE id=?",array($audit_master_id));
198 function createAuditArray($am_id,$table_name){
199 if(strpos($table_name,',')){
200 $tables = explode(',',$table_name);
201 $arr = array($am_id);
202 $table_qry = "";
203 for($i=0;$i<count($tables);$i++){
204 $table_qry .= "?,";
205 array_unshift($arr,$tables[$i]);
207 $table_qry = substr($table_qry,0,-1);
208 $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)
209 WHERE am.id = ? AND am.type = 11 AND am.approval_status = 1 ORDER BY ad.entry_identification,ad.field_name",$arr);
210 }else{
211 $query = sqlStatement("SELECT * FROM audit_master am LEFT JOIN audit_details ad ON ad.audit_master_id = am.id AND ad.table_name = ?
212 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));
214 $result = array();
215 while($res = sqlFetchArray($query)){
216 $result[$table_name][$res['entry_identification']][$res['field_name']] = $res['field_value'];
218 return $result;
221 function insertApprovedData($data){
222 $patient_data_fields = '';
223 $patient_data_values = array();
224 foreach($data as $key=>$val){
225 if(substr($key,-4) == '-sel'){
226 if(is_array($val)){
227 for($i=0;$i<count($val);$i++){
228 if($val[$i] == 'insert'){
229 if(substr($key,0,-4) == 'lists1'){
230 if($_REQUEST['lists1-activity'][$i] == 'Active'){
231 $activity = 1;
232 }elseif($_REQUEST['lists1-activity'][$i] == 'Inactive'){
233 $activity = 0;
235 $query = "INSERT INTO lists (pid,diagnosis,activity) VALUES (?,?,?)";
236 sqlQuery($query,array($_REQUEST['pid'],$_REQUEST['lists1-diagnosis'][$i],$activity));
237 }elseif(substr($key,0,-4) == 'lists2'){
238 $query = "INSERT INTO lists (pid,date,type,title,diagnosis,reaction) VALUES (?,?,?,?,?,?)";
239 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]));
240 }elseif(substr($key,0,-4) == 'prescriptions'){
241 if($_REQUEST['prescriptions-active'][$i] == 'Active'){
242 $active = 1;
243 }elseif($_REQUEST['prescriptions-active'][$i] == 'Inactive'){
244 $active = 0;
246 $query = "INSERT INTO prescriptions (patient_id,date_added,active,drug,size,form,quantity) VALUES (?,?,?,?,?,?,?)";
247 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]));
248 }elseif(substr($key,0,-4) == 'immunizations'){
249 $query = "INSERT INTO immunizations (patient_id,administered_date,note) VALUES (?,?,?)";
250 sqlQuery($query,array($_REQUEST['pid'],$_REQUEST['immunizations-administered_date'][$i],$_REQUEST['immunizations-note'][$i]));
251 }elseif(substr($key,0,-4) == 'procedure_result'){
252 //$query = "INSERT INTO procedure_type (name) VALUES (?)";
253 //sqlQuery($query,array($_REQUEST['procedure_type-name'][$i]));
254 //$query = "INSERT INTO procedure_result (date,result,abnormal) VALUES (?,?,?)";
255 //sqlQuery($query,array($_REQUEST['procedure_result-date'][$i],$active,$_REQUEST['procedure_result-abnormal'][$i]));
257 }elseif($val[$i] == 'update'){
258 if(substr($key,0,-4) == 'lists1'){
259 if($_REQUEST['lists1-activity'][$i] == 'Active'){
260 $activity = 1;
261 }elseif($_REQUEST['lists1-activity'][$i] == 'Inactive'){
262 $activity = 0;
264 $query = "UPDATE lists SET diagnosis=?,activity=? WHERE pid=? AND diagnosis=?";
265 sqlQuery($query,array($_REQUEST['lists1-diagnosis'][$i],$activity,$_REQUEST['pid'],$_REQUEST['lists1-old-diagnosis'][$i]));
269 }else{
270 if(substr($key,0,12) == 'patient_data'){
271 if($val == 'update'){
272 $var_name = substr($key,0,-4);
273 $field_name = substr($var_name,13);
274 $patient_data_fields .= $field_name.'=?,';
275 array_push($patient_data_values,$_REQUEST[$var_name]);
281 if(count($patient_data_values) > 0){
282 array_push($patient_data_values,$_REQUEST['pid']);
283 $patient_data_fields = substr($patient_data_fields,0,-1);
284 $query = "UPDATE patient_data SET $patient_data_fields WHERE pid=?";
285 sqlQuery($query,$patient_data_values);
287 sqlQuery("UPDATE documents SET foreign_id = ? WHERE id =? ",array($_REQUEST['pid'],$_REQUEST['doc_id']));