f006fed399d569ed35f1a5e4e539d2c6c421bf13
[openemr.git] / ccr / createCCR.php
blobf006fed399d569ed35f1a5e4e539d2c6c421bf13
1 <?php
2 /**
3 * CCR Script.
5 * Copyright (C) 2010 Garden State Health Systems <http://www.gshsys.com/>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Garden State Health Systems <http://www.gshsys.com/>
20 * @link http://www.open-emr.org
24 //SANITIZE ALL ESCAPES
25 $sanitize_all_escapes=true;
28 //STOP FAKE REGISTER GLOBALS
29 $fake_register_globals=false;
32 // check if using the patient portal
33 //(if so, then use the portal authorization)
34 if (isset($_GET['portal_auth'])) {
35 $landingpage = "../patients/index.php";
36 session_start();
37 if ( isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite']) ) {
38 $pid = $_SESSION['pid'];
39 $ignoreAuth=true;
40 global $ignoreAuth;
42 else {
43 session_destroy();
44 header('Location: '.$landingpage.'?w');
45 exit;
49 require_once(dirname(__FILE__) . "/../interface/globals.php");
50 require_once(dirname(__FILE__) . "/../library/sql-ccr.inc");
51 require_once(dirname(__FILE__) . "/../library/classes/class.phpmailer.php");
52 require_once(dirname(__FILE__) . "/uuid.php");
53 require_once(dirname(__FILE__) . "/transmitCCD.php");
54 require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
56 function createCCR($action,$raw="no",$requested_by=""){
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,$requested_by);
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",$requested_by=""){
209 global $pid;
211 $ccr->preserveWhiteSpace = false;
212 $ccr->formatOutput = true;
214 $ccr->save(dirname(__FILE__) .'/generatedXml/ccrForCCD.xml');
216 $xmlDom = new DOMDocument();
217 $xmlDom->loadXML($ccr->saveXML());
219 $ccr_ccd = new DOMDocument();
220 $ccr_ccd->load(dirname(__FILE__) .'/ccd/ccr_ccd.xsl');
222 $xslt = new XSLTProcessor();
223 $xslt->importStylesheet($ccr_ccd);
225 $ccd = new DOMDocument();
226 $ccd->preserveWhiteSpace = false;
227 $ccd->formatOutput = true;
229 $ccd->loadXML($xslt->transformToXML($xmlDom));
231 $ccd->save(dirname(__FILE__) .'/generatedXml/ccdDebug.xml');
233 if ($raw == "yes") {
234 // simply send the xml to a textarea (nice debugging tool)
235 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
236 echo $ccd->saveXml();
237 echo "</textarea>";
238 return;
241 if ($raw == "pure") {
242 // send a zip file that contains a separate xml data file and xsl stylesheet
243 if (! (class_exists('ZipArchive')) ) {
244 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
245 return;
247 $tempDir = $GLOBALS['temporary_files_dir'];
248 $zipName = $tempDir . "/" . getReportFilename() . "-ccd.zip";
249 if (file_exists($zipName)) {
250 unlink($zipName);
252 $zip = new ZipArchive();
253 if (!($zip)) {
254 displayError(xl("ERROR: Unable to Create Zip Archive."));
255 return;
257 if ( $zip->open($zipName, ZIPARCHIVE::CREATE) ) {
258 $zip->addFile("stylesheet/cda.xsl", "stylesheet/cda.xsl");
259 $xmlName = $tempDir . "/" . getReportFilename() . "-ccd.xml";
260 if (file_exists($xmlName)) {
261 unlink($xmlName);
263 $e_styleSheet = $ccd->createProcessingInstruction('xml-stylesheet',
264 'type="text/xsl" href="stylesheet/cda.xsl"');
265 $ccd->insertBefore($e_styleSheet,$ccd->firstChild);
266 $ccd->save($xmlName);
267 $zip->addFile($xmlName, basename($xmlName) );
268 $zip->close();
269 header("Pragma: public");
270 header("Expires: 0");
271 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
272 header("Content-Type: application/force-download");
273 header("Content-Length: " . filesize($zipName));
274 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
275 header("Content-Description: File Transfer");
276 readfile($zipName);
277 unlink($zipName);
278 unlink($xmlName);
279 exit(0);
281 else {
282 displayError(xl("ERROR: Unable to Create Zip Archive."));
283 return;
287 if (substr($raw,0,4)=="send") {
288 $recipient = trim(stripslashes(substr($raw,5)));
289 $result=transmitCCD($ccd,$recipient,$requested_by);
290 echo htmlspecialchars($result,ENT_NOQUOTES);
291 return;
294 $ss = new DOMDocument();
295 $ss->load(dirname(__FILE__) ."/stylesheet/cda.xsl");
297 $xslt->importStyleSheet($ss);
299 $html = $xslt->transformToXML($ccd);
301 echo $html;
306 function sourceType($ccr, $uuid){
308 $e_Source = $ccr->createElement('Source');
310 $e_Actor = $ccr->createElement('Actor');
311 $e_Source->appendChild($e_Actor);
313 $e_ActorID = $ccr->createElement('ActorID',$uuid);
314 $e_Actor->appendChild($e_ActorID);
316 return $e_Source;
320 function displayError($message) {
321 echo '<script type="text/javascript">alert("' . addslashes($message) . '");</script>';
325 function createHybridXML($ccr) {
327 // save the raw xml
328 $main_xml = $ccr->saveXml();
330 // save the stylesheet
331 $main_stylesheet = file_get_contents('stylesheet/ccr.xsl');
333 // replace stylesheet link in raw xml file
334 $substitute_string = '<?xml-stylesheet type="text/xsl" href="#style1"?>
335 <!DOCTYPE ContinuityOfCareRecord [
336 <!ATTLIST xsl:stylesheet id ID #REQUIRED>
339 $replace_string = '<?xml-stylesheet type="text/xsl" href="stylesheet/ccr.xsl"?>';
340 $main_xml = str_replace($replace_string,$substitute_string,$main_xml);
342 // remove redundant xml declaration from stylesheet
343 $replace_string = '<?xml version="1.0" encoding="UTF-8"?>';
344 $main_stylesheet = str_replace($replace_string,'',$main_stylesheet);
346 // embed the stylesheet in the raw xml file
347 $replace_string ='<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">';
348 $main_stylesheet = $replace_string.$main_stylesheet;
349 $main_xml = str_replace($replace_string,$main_stylesheet,$main_xml);
351 // insert style1 id into the stylesheet parameter
352 $substitute_string = 'xsl:stylesheet id="style1" exclude-result-prefixes';
353 $replace_string = 'xsl:stylesheet exclude-result-prefixes';
354 $main_xml = str_replace($replace_string,$substitute_string,$main_xml);
356 // prepare the filename to use
357 // LASTNAME-FIRSTNAME-PID-DATESTAMP-ccr.xml
358 $main_filename = getReportFilename()."-ccr.xml";
360 // send the output as a file to the user
361 header("Content-type: text/xml");
362 header("Content-Disposition: attachment; filename=" . $main_filename . "");
363 echo $main_xml;
366 if($_POST['ccrAction']) {
367 $raw=$_POST['raw'];
368 /* If transmit requested, fail fast if the recipient address fails basic validation */
369 if (substr($raw,0,4)=="send") {
370 $send_to = trim(stripslashes(substr($raw,5)));
371 if (!PHPMailer::ValidateAddress($send_to)) {
372 echo(htmlspecialchars( xl('Invalid recipient address. Please try again.'), ENT_QUOTES));
373 return;
375 createCCR($_POST['ccrAction'],$raw,$_POST['requested_by']);
376 } else {
377 createCCR($_POST['ccrAction'],$raw);