minor comment fix in library/sql.inc
[openemr.git] / ccr / createCCR.php
blob662aee592d23c881dba6fe1e004cc5c9b8e82ce8
1 <?php
2 // ------------------------------------------------------------------------ //
3 // Garden State Health Systems //
4 // Copyright (c) 2010 gshsys.com //
5 // <http://www.gshsys.com/> //
6 // ------------------------------------------------------------------------ //
7 // This program is free software; you can redistribute it and/or modify //
8 // it under the terms of the GNU General Public License as published by //
9 // the Free Software Foundation; either version 2 of the License, or //
10 // (at your option) any later version. //
11 // //
12 // You may not change or alter any portion of this comment or credits //
13 // of supporting developers from this source code or any supporting //
14 // source code which is considered copyrighted (c) material of the //
15 // original comment or credit authors. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details. //
21 // //
22 // You should have received a copy of the GNU General Public License //
23 // along with this program; if not, write to the Free Software //
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
25 // ------------------------------------------------------------------------ //
27 //SANITIZE ALL ESCAPES
28 $sanitize_all_escapes=true;
31 //STOP FAKE REGISTER GLOBALS
32 $fake_register_globals=false;
35 // check if using the patient portal
36 //(if so, then use the portal authorization)
37 if (isset($_GET['portal_auth'])) {
38 $landingpage = "../patients/index.php";
39 session_start();
40 if ( isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite']) ) {
41 $pid = $_SESSION['pid'];
42 $ignoreAuth=true;
43 global $ignoreAuth;
45 else {
46 session_destroy();
47 header('Location: '.$landingpage.'?w');
48 exit;
52 require_once(dirname(__FILE__) . "/../interface/globals.php");
53 require_once(dirname(__FILE__) . "/../library/sql-ccr.inc");
54 require_once(dirname(__FILE__) . "/uuid.php");
56 function createCCR($action,$raw="no"){
58 $authorID = getUuid();
59 $patientID = getUuid();
60 $sourceID = getUuid();
61 $oemrID = getUuid();
63 $result = getActorData();
64 while($res = sqlFetchArray($result[2])){
65 ${"labID{$res['id']}"} = getUuid();
68 $ccr = new DOMDocument('1.0','UTF-8');
69 $e_styleSheet = $ccr->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="stylesheet/ccr.xsl"');
70 $ccr->appendChild($e_styleSheet);
72 $e_ccr = $ccr->createElementNS('urn:astm-org:CCR', 'ContinuityOfCareRecord');
73 $ccr->appendChild($e_ccr);
75 /////////////// Header
77 require_once("createCCRHeader.php");
78 $e_Body = $ccr->createElement('Body');
79 $e_ccr->appendChild($e_Body);
81 /////////////// Problems
83 $e_Problems = $ccr->createElement('Problems');
84 require_once("createCCRProblem.php");
85 $e_Body->appendChild($e_Problems);
87 /////////////// Alerts
89 $e_Alerts = $ccr->createElement('Alerts');
90 require_once("createCCRAlerts.php");
91 $e_Body->appendChild($e_Alerts);
93 ////////////////// Medication
95 $e_Medications = $ccr->createElement('Medications');
96 require_once("createCCRMedication.php");
97 $e_Body->appendChild($e_Medications);
99 ///////////////// Immunization
101 $e_Immunizations = $ccr->createElement('Immunizations');
102 require_once("createCCRImmunization.php");
103 $e_Body->appendChild($e_Immunizations);
106 /////////////////// Results
108 $e_Results = $ccr->createElement('Results');
109 require_once("createCCRResult.php");
110 $e_Body->appendChild($e_Results);
113 /////////////////// Procedures
115 //$e_Procedures = $ccr->createElement('Procedures');
116 //require_once("createCCRProcedure.php");
117 //$e_Body->appendChild($e_Procedures);
119 //////////////////// Footer
121 // $e_VitalSigns = $ccr->createElement('VitalSigns');
122 // $e_Body->appendChild($e_VitalSigns);
124 /////////////// Actors
126 $e_Actors = $ccr->createElement('Actors');
127 require_once("createCCRActor.php");
128 $e_ccr->appendChild($e_Actors);
130 if ($action=="generate"){
131 gnrtCCR($ccr,$raw);
134 if($action == "viewccd"){
135 viewCCD($ccr,$raw);
139 function gnrtCCR($ccr,$raw="no"){
140 global $pid;
142 $ccr->preserveWhiteSpace = false;
143 $ccr->formatOutput = true;
145 if ($raw == "yes") {
146 // simply send the xml to a textarea (nice debugging tool)
147 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
148 echo $ccr->saveXml();
149 echo "</textarea>";
150 return;
153 else if ($raw == "hybrid") {
154 // send a file that contains a hybrid file of the raw xml and the xsl stylesheet
155 createHybridXML($ccr);
158 else if ($raw == "pure") {
159 // send a zip file that contains a separate xml data file and xsl stylesheet
160 if (! (class_exists('ZipArchive')) ) {
161 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
162 return;
164 $tempDir = $GLOBALS['temporary_files_dir'];
165 $zipName = $tempDir . "/" . getReportFilename() . "-ccr.zip";
166 if (file_exists($zipName)) {
167 unlink($zipName);
169 $zip = new ZipArchive();
170 if (!($zip)) {
171 displayError(xl("ERROR: Unable to Create Zip Archive."));
172 return;
174 if ( $zip->open($zipName, ZIPARCHIVE::CREATE) ) {
175 $zip->addFile("stylesheet/ccr.xsl", "stylesheet/ccr.xsl");
176 $xmlName = $tempDir . "/" . getReportFilename() . "-ccr.xml";
177 if (file_exists($xmlName)) {
178 unlink($xmlName);
180 $ccr->save($xmlName);
181 $zip->addFile($xmlName, basename($xmlName) );
182 $zip->close();
183 header("Pragma: public");
184 header("Expires: 0");
185 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
186 header("Content-Type: application/force-download");
187 header("Content-Length: " . filesize($zipName));
188 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
189 header("Content-Description: File Transfer");
190 readfile($zipName);
191 unlink($zipName);
192 unlink($xmlName);
193 exit(0);
195 else {
196 displayError(xl("ERROR: Unable to Create Zip Archive."));
197 return;
201 else {
202 header("Content-type: application/xml");
203 echo $ccr->saveXml();
208 function viewCCD($ccr,$raw="no"){
210 $ccr->preserveWhiteSpace = false;
211 $ccr->formatOutput = true;
213 $ccr->save(dirname(__FILE__) .'/generatedXml/ccrForCCD.xml');
215 $xmlDom = new DOMDocument();
216 $xmlDom->loadXML($ccr->saveXML());
218 $ccr_ccd = new DOMDocument();
219 $ccr_ccd->load(dirname(__FILE__) .'/ccd/ccr_ccd.xsl');
221 $xslt = new XSLTProcessor();
222 $xslt->importStylesheet($ccr_ccd);
224 $ccd = new DOMDocument();
225 $ccd->preserveWhiteSpace = false;
226 $ccd->formatOutput = true;
228 $ccd->loadXML($xslt->transformToXML($xmlDom));
230 $ccd->save(dirname(__FILE__) .'/generatedXml/ccdDebug.xml');
232 if ($raw == "yes") {
233 // simply send the xml to a textarea (nice debugging tool)
234 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
235 echo $ccd->saveXml();
236 echo "</textarea>";
237 return;
240 $ss = new DOMDocument();
241 $ss->load(dirname(__FILE__) ."/stylesheet/cda.xsl");
243 $xslt->importStyleSheet($ss);
245 $html = $xslt->transformToXML($ccd);
247 echo $html;
253 function sourceType($ccr, $uuid){
255 $e_Source = $ccr->createElement('Source');
257 $e_Actor = $ccr->createElement('Actor');
258 $e_Source->appendChild($e_Actor);
260 $e_ActorID = $ccr->createElement('ActorID',$uuid);
261 $e_Actor->appendChild($e_ActorID);
263 return $e_Source;
267 function displayError($message) {
268 echo '<script type="text/javascript">alert("' . addslashes($message) . '");</script>';
272 function createHybridXML($ccr) {
274 // save the raw xml
275 $main_xml = $ccr->saveXml();
277 // save the stylesheet
278 $main_stylesheet = file_get_contents('stylesheet/ccr.xsl');
280 // replace stylesheet link in raw xml file
281 $substitute_string = '<?xml-stylesheet type="text/xsl" href="#style1"?>
282 <!DOCTYPE ContinuityOfCareRecord [
283 <!ATTLIST xsl:stylesheet id ID #REQUIRED>
286 $replace_string = '<?xml-stylesheet type="text/xsl" href="stylesheet/ccr.xsl"?>';
287 $main_xml = str_replace($replace_string,$substitute_string,$main_xml);
289 // remove redundant xml declaration from stylesheet
290 $replace_string = '<?xml version="1.0" encoding="UTF-8"?>';
291 $main_stylesheet = str_replace($replace_string,'',$main_stylesheet);
293 // embed the stylesheet in the raw xml file
294 $replace_string ='<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">';
295 $main_stylesheet = $replace_string.$main_stylesheet;
296 $main_xml = str_replace($replace_string,$main_stylesheet,$main_xml);
298 // insert style1 id into the stylesheet parameter
299 $substitute_string = 'xsl:stylesheet id="style1" exclude-result-prefixes';
300 $replace_string = 'xsl:stylesheet exclude-result-prefixes';
301 $main_xml = str_replace($replace_string,$substitute_string,$main_xml);
303 // prepare the filename to use
304 // LASTNAME-FIRSTNAME-PID-DATESTAMP-ccr.xml
305 $main_filename = getReportFilename()."-ccr.xml";
307 // send the output as a file to the user
308 header("Content-type: text/xml");
309 header("Content-Disposition: attachment; filename=" . $main_filename . "");
310 echo $main_xml;
313 if($_POST['ccrAction'])
315 createCCR($_POST['ccrAction'],$_POST['raw']);