Interim autoloaded library/classes via composer classmap, take 4. (#422)
[openemr.git] / library / ajax / ccr_import_ajax.php
blob29058e166305d792180c45512b3af9453ff0250b
1 <?php
2 /**
3 * library/ccr_import_ajax.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
26 //SANITIZE ALL ESCAPES
27 $sanitize_all_escapes=true;
30 //STOP FAKE REGISTER GLOBALS
31 $fake_register_globals=false;
34 require_once(dirname(__FILE__) . "/../../interface/globals.php");
35 require_once(dirname(__FILE__) . "/../parse_patient_xml.php");
37 if($_REQUEST["ccr_ajax"] == "yes"){
38 $doc_id = $_REQUEST["document_id"];
39 $d = new Document($doc_id);
40 $url = $d->get_url();
41 $storagemethod = $d->get_storagemethod();
42 $couch_docid = $d->get_couch_docid();
43 $couch_revid = $d->get_couch_revid();
44 if($storagemethod == 1){
45 $couch = new CouchDB();
46 $data = array($GLOBALS['couchdb_dbase'],$couch_docid);
47 $resp = $couch->retrieve_doc($data);
48 $content = $resp->data;
49 if($content=='' && $GLOBALS['couchdb_log']==1){
50 $log_content = date('Y-m-d H:i:s')." ==> Retrieving document\r\n";
51 $log_content = date('Y-m-d H:i:s')." ==> URL: ".$url."\r\n";
52 $log_content .= date('Y-m-d H:i:s')." ==> CouchDB Document Id: ".$couch_docid."\r\n";
53 $log_content .= date('Y-m-d H:i:s')." ==> CouchDB Revision Id: ".$couch_revid."\r\n";
54 $log_content .= date('Y-m-d H:i:s')." ==> Failed to fetch document content from CouchDB.\r\n";
55 $log_content .= date('Y-m-d H:i:s')." ==> Will try to download file from HardDisk if exists.\r\n\r\n";
56 $this->document_upload_download_log($d->get_foreign_id(),$log_content);
57 die(xlt("File retrieval from CouchDB failed"));
59 $content = base64_decode($content);
60 }else{
61 $url = preg_replace("|^(.*)://|","",$url);
62 $from_all = explode("/",$url);
63 $from_filename = array_pop($from_all);
64 $from_pathname_array = array();
65 for ($i=0;$i<$d->get_path_depth();$i++) {
66 $from_pathname_array[] = array_pop($from_all);
68 $from_pathname_array = array_reverse($from_pathname_array);
69 $from_pathname = implode("/",$from_pathname_array);
70 $temp_url = $GLOBALS['OE_SITE_DIR'] . '/documents/' . $from_pathname . '/' . $from_filename;
71 if (!file_exists($temp_url)) {
72 echo xlt('The requested document is not present at the expected location on the filesystem or there are not sufficient permissions to access it') . '.' . $temp_url;
73 }else{
74 $content = file_get_contents($temp_url);
77 //fields to which the corresponding elements are to be inserted
78 //format - level 1 key is the main tag in the XML eg:- //Problems or //Problems/Problem according to the content in the XML.
79 //level 2 key is 'table name:field name' and level 2 value is the sub tag under the main tag given in level 1 key
80 //eg:- 'Type/Text' if the XML format is '//Problems/Problem/Type/Text' or 'id/@extension' if it is an attribute
81 //level 2 key can be 'table name:#some value' for checking whether a particular tag exits in the XML section
82 $field_mapping = array(
83 '//Problems/Problem' => array(
84 'lists1:diagnosis' => 'Description/Code/Value',
85 'lists1:comments' => 'CommentID',
86 'lists1:activity' => 'Status/Text',
88 '//Alerts/Alert' => array(
89 'lists2:type' => 'Type/Text',
90 'lists2:diagnosis' => 'Description/Code/Value',
91 'lists2:date' => 'Agent/EnvironmentalAgents/EnvironmentalAgent/DateTime/ExactDateTime',
92 'lists2:title' => 'Agent/EnvironmentalAgents/EnvironmentalAgent/Description/Text',
93 'lists2:reaction' => 'Reaction/Description/Text',
95 '//Medications/Medication' => array(
96 'prescriptions:date_added' => 'DateTime/ExactDateTime',
97 'prescriptions:active' => 'Status/Text',
98 'prescriptions:drug' => 'Product/ProductName/Text',
99 'prescriptions:size' => 'Product/Strength/Value',
100 'prescriptions:unit' => 'Product/Strength/Units/Unit',
101 'prescriptions:form' => 'Product/Form/Text',
102 'prescriptions:quantity' => 'Quantity/Value',
103 'prescriptions:note' => 'PatientInstructions/Instruction/Text',
104 'prescriptions:refills' => 'Refills/Refill/Number',
106 '//Immunizations/Immunization' => array(
107 'immunizations:administered_date' => 'DateTime/ExactDateTime',
108 'immunizations:note' => 'Directions/Direction/Description/Text',
110 '//Results/Result' => array(
111 'procedure_result:date' => 'DateTime/ExactDateTime',
112 'procedure_type:name' => 'Test/Description/Text',
113 'procedure_result:result' => 'Test/TestResult/Value',
114 'procedure_result:range' => 'Test/NormalResult/Normal/Value',
115 'procedure_result:abnormal' => 'Test/Flag/Text',
117 '//Actors/Actor' => array(
118 'patient_data:fname' => 'Person/Name/CurrentName/Given',
119 'patient_data:lname' => 'Person/Name/CurrentName/Family',
120 'patient_data:DOB' => 'Person/DateOfBirth/ExactDateTime',
121 'patient_data:sex' => 'Person/Gender/Text',
122 'patient_data:abname' => 'InformationSystem/Name',
123 'patient_data:#Type' => 'InformationSystem/Type',
124 'patient_data:pubpid' => 'IDs/ID',
125 'patient_data:street' => 'Address/Line1',
126 'patient_data:city' => 'Address/City',
127 'patient_data:state' => 'Address/State',
128 'patient_data:postal_code' => 'Address/PostalCode',
129 'patient_data:phone_contact' => 'Telephone/Value',
132 if(!empty($content)){
133 $var = array();
134 $res = parseXmlStream($content,$field_mapping);
135 $var = array(
136 'approval_status' => 1,
137 'type' => 11,
138 'ip_address' => $_SERVER['REMOTE_ADDR'],
140 foreach($res as $sections=>$details){
141 foreach($details as $cnt=>$vals){
142 foreach($vals as $key=>$val){
143 if(array_key_exists('#Type',$res[$sections][$cnt])){
144 if($key == 'postal_code'){
145 $var['field_name_value_array']['misc_address_book'][$cnt]['zip'] = $val;
146 }elseif($key == 'phone_contact'){
147 $var['field_name_value_array']['misc_address_book'][$cnt]['phone'] = $val;
148 }elseif($key == 'abname'){
149 $values = explode(' ',$val);
150 if($values[0]){
151 $var['field_name_value_array']['misc_address_book'][$cnt]['lname'] = $values[0];
153 if($values[1]){
154 $var['field_name_value_array']['misc_address_book'][$cnt]['fname'] = $values[1];
156 }else{
157 $var['field_name_value_array']['misc_address_book'][$cnt][$key] = $val;
159 $var['entry_identification_array']['misc_address_book'][$cnt] = $cnt;
160 }else{
161 if($sections == 'lists1' && $key == 'activity'){
162 if($val == 'Active'){
163 $val = 1;
164 }else{
165 $val = 0;
168 if($sections == 'lists2' && $key == 'type'){
169 if(strpos($val,"-")){
170 $vals = explode("-",$val);
171 $val = $vals[0];
172 }else{
173 $val = "";
176 if($sections == 'prescriptions' && $key == 'active'){
177 if($val == 'Active'){
178 $val = 1;
179 }else{
180 $val = 0;
183 $var['field_name_value_array'][$sections][$cnt][$key] = $val;
184 $var['entry_identification_array'][$sections][$cnt] = $cnt;
187 if(array_key_exists('#Type',$var['field_name_value_array']['misc_address_book'][$cnt])){
188 unset($var['field_name_value_array']['misc_address_book'][$cnt]['#Type']);
192 $var['field_name_value_array']['documents'][0]['id'] = $doc_id;
193 insert_ccr_into_audit_data($var);
194 $d->update_imported($doc_id);
195 echo xlt('Successfully Imported the details. Please approve the patient from the Pending Approval Screen').'.';
196 }else{
197 exit(xlt('Could not read the file'));
199 exit;