Merge pull request #899 from stephenwaite/admin_fix
[openemr.git] / ccr / createCCR.php
blob4101e1e3d4400c4fe90017fbcf5b525262076a51
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
26 // check if using the patient portal
27 //(if so, then use the portal authorization)
28 if (isset($_GET['portal_auth']) || isset($_GET['portal_auth_two'])) {
29 if (isset($_GET['portal_auth'])) {
30 $landingpage = "../patients/index.php";
32 else { // isset($_GET['portal_auth_two'])
33 $landingpage = "../portal/index.php";
35 session_start();
36 if ( isset($_SESSION['pid']) && (isset($_SESSION['patient_portal_onsite']) || isset($_SESSION['patient_portal_onsite_two']))) {
37 $pid = $_SESSION['pid'];
38 $ignoreAuth=true;
39 global $ignoreAuth;
41 else {
42 session_destroy();
43 header('Location: '.$landingpage.'?w');
44 exit;
48 require_once(dirname(__FILE__) . "/../interface/globals.php");
49 require_once(dirname(__FILE__) . "/../library/sql-ccr.inc");
50 require_once(dirname(__FILE__) . "/uuid.php");
51 require_once(dirname(__FILE__) . "/transmitCCD.php");
52 require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
54 function createCCR($action,$raw="no",$requested_by=""){
56 $authorID = getUuid();
57 $patientID = getUuid();
58 $sourceID = getUuid();
59 $oemrID = getUuid();
61 $result = getActorData();
62 while($res = sqlFetchArray($result[2])){
63 ${"labID{$res['id']}"} = getUuid();
66 $ccr = new DOMDocument('1.0','UTF-8');
67 $e_styleSheet = $ccr->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="stylesheet/ccr.xsl"');
68 $ccr->appendChild($e_styleSheet);
70 $e_ccr = $ccr->createElementNS('urn:astm-org:CCR', 'ContinuityOfCareRecord');
71 $ccr->appendChild($e_ccr);
73 /////////////// Header
75 require_once("createCCRHeader.php");
76 $e_Body = $ccr->createElement('Body');
77 $e_ccr->appendChild($e_Body);
79 /////////////// Problems
81 $e_Problems = $ccr->createElement('Problems');
82 require_once("createCCRProblem.php");
83 $e_Body->appendChild($e_Problems);
85 /////////////// Alerts
87 $e_Alerts = $ccr->createElement('Alerts');
88 require_once("createCCRAlerts.php");
89 $e_Body->appendChild($e_Alerts);
91 ////////////////// Medication
93 $e_Medications = $ccr->createElement('Medications');
94 require_once("createCCRMedication.php");
95 $e_Body->appendChild($e_Medications);
97 ///////////////// Immunization
99 $e_Immunizations = $ccr->createElement('Immunizations');
100 require_once("createCCRImmunization.php");
101 $e_Body->appendChild($e_Immunizations);
104 /////////////////// Results
106 $e_Results = $ccr->createElement('Results');
107 require_once("createCCRResult.php");
108 $e_Body->appendChild($e_Results);
111 /////////////////// Procedures
113 //$e_Procedures = $ccr->createElement('Procedures');
114 //require_once("createCCRProcedure.php");
115 //$e_Body->appendChild($e_Procedures);
117 //////////////////// Footer
119 // $e_VitalSigns = $ccr->createElement('VitalSigns');
120 // $e_Body->appendChild($e_VitalSigns);
122 /////////////// Actors
124 $e_Actors = $ccr->createElement('Actors');
125 require_once("createCCRActor.php");
126 $e_ccr->appendChild($e_Actors);
128 if ($action=="generate"){
129 gnrtCCR($ccr,$raw,$requested_by);
132 if($action == "viewccd"){
133 viewCCD($ccr,$raw,$requested_by);
137 function gnrtCCR($ccr,$raw="no",$requested_by=""){
138 global $pid;
140 $ccr->preserveWhiteSpace = false;
141 $ccr->formatOutput = true;
143 if ($raw == "yes") {
144 // simply send the xml to a textarea (nice debugging tool)
145 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
146 echo $ccr->saveXml();
147 echo "</textarea>";
148 return;
151 else if ($raw == "hybrid") {
152 // send a file that contains a hybrid file of the raw xml and the xsl stylesheet
153 createHybridXML($ccr);
156 else if ($raw == "pure") {
157 // send a zip file that contains a separate xml data file and xsl stylesheet
158 if (! (class_exists('ZipArchive')) ) {
159 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
160 return;
162 $tempDir = $GLOBALS['temporary_files_dir'];
163 $zipName = $tempDir . "/" . getReportFilename() . "-ccr.zip";
164 if (file_exists($zipName)) {
165 unlink($zipName);
167 $zip = new ZipArchive();
168 if (!($zip)) {
169 displayError(xl("ERROR: Unable to Create Zip Archive."));
170 return;
172 if ( $zip->open($zipName, ZIPARCHIVE::CREATE) ) {
173 $zip->addFile("stylesheet/ccr.xsl", "stylesheet/ccr.xsl");
174 $xmlName = $tempDir . "/" . getReportFilename() . "-ccr.xml";
175 if (file_exists($xmlName)) {
176 unlink($xmlName);
178 $ccr->save($xmlName);
179 $zip->addFile($xmlName, basename($xmlName) );
180 $zip->close();
181 header("Pragma: public");
182 header("Expires: 0");
183 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
184 header("Content-Type: application/force-download");
185 header("Content-Length: " . filesize($zipName));
186 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
187 header("Content-Description: File Transfer");
188 readfile($zipName);
189 unlink($zipName);
190 unlink($xmlName);
191 exit(0);
193 else {
194 displayError(xl("ERROR: Unable to Create Zip Archive."));
195 return;
199 else if (substr($raw,0,4)=="send") {
200 $recipient = trim(stripslashes(substr($raw,5)));
201 $result=transmitCCD($ccr,$recipient,$requested_by,"CCR");
202 echo htmlspecialchars($result,ENT_NOQUOTES);
203 return;
206 else {
207 header("Content-type: application/xml");
208 echo $ccr->saveXml();
213 function viewCCD($ccr,$raw="no",$requested_by=""){
214 global $pid;
216 $ccr->preserveWhiteSpace = false;
217 $ccr->formatOutput = true;
219 $ccr->save(dirname(__FILE__) .'/generatedXml/ccrForCCD.xml');
221 $xmlDom = new DOMDocument();
222 $xmlDom->loadXML($ccr->saveXML());
224 $ccr_ccd = new DOMDocument();
225 $ccr_ccd->load(dirname(__FILE__) .'/ccd/ccr_ccd.xsl');
227 $xslt = new XSLTProcessor();
228 $xslt->importStylesheet($ccr_ccd);
230 $ccd = new DOMDocument();
231 $ccd->preserveWhiteSpace = false;
232 $ccd->formatOutput = true;
234 $ccd->loadXML($xslt->transformToXML($xmlDom));
236 $ccd->save(dirname(__FILE__) .'/generatedXml/ccdDebug.xml');
238 if ($raw == "yes") {
239 // simply send the xml to a textarea (nice debugging tool)
240 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
241 echo $ccd->saveXml();
242 echo "</textarea>";
243 return;
246 if ($raw == "pure") {
247 // send a zip file that contains a separate xml data file and xsl stylesheet
248 if (! (class_exists('ZipArchive')) ) {
249 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
250 return;
252 $tempDir = $GLOBALS['temporary_files_dir'];
253 $zipName = $tempDir . "/" . getReportFilename() . "-ccd.zip";
254 if (file_exists($zipName)) {
255 unlink($zipName);
257 $zip = new ZipArchive();
258 if (!($zip)) {
259 displayError(xl("ERROR: Unable to Create Zip Archive."));
260 return;
262 if ( $zip->open($zipName, ZIPARCHIVE::CREATE) ) {
263 $zip->addFile("stylesheet/cda.xsl", "stylesheet/cda.xsl");
264 $xmlName = $tempDir . "/" . getReportFilename() . "-ccd.xml";
265 if (file_exists($xmlName)) {
266 unlink($xmlName);
268 $e_styleSheet = $ccd->createProcessingInstruction('xml-stylesheet',
269 'type="text/xsl" href="stylesheet/cda.xsl"');
270 $ccd->insertBefore($e_styleSheet,$ccd->firstChild);
271 $ccd->save($xmlName);
272 $zip->addFile($xmlName, basename($xmlName) );
273 $zip->close();
274 header("Pragma: public");
275 header("Expires: 0");
276 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
277 header("Content-Type: application/force-download");
278 header("Content-Length: " . filesize($zipName));
279 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
280 header("Content-Description: File Transfer");
281 readfile($zipName);
282 unlink($zipName);
283 unlink($xmlName);
284 exit(0);
286 else {
287 displayError(xl("ERROR: Unable to Create Zip Archive."));
288 return;
292 if (substr($raw,0,4)=="send") {
293 $recipient = trim(stripslashes(substr($raw,5)));
294 $result=transmitCCD($ccd,$recipient,$requested_by);
295 echo htmlspecialchars($result,ENT_NOQUOTES);
296 return;
299 $ss = new DOMDocument();
300 $ss->load(dirname(__FILE__) ."/stylesheet/cda.xsl");
302 $xslt->importStyleSheet($ss);
304 $html = $xslt->transformToXML($ccd);
306 echo $html;
311 function sourceType($ccr, $uuid){
313 $e_Source = $ccr->createElement('Source');
315 $e_Actor = $ccr->createElement('Actor');
316 $e_Source->appendChild($e_Actor);
318 $e_ActorID = $ccr->createElement('ActorID',$uuid);
319 $e_Actor->appendChild($e_ActorID);
321 return $e_Source;
325 function displayError($message) {
326 echo '<script type="text/javascript">alert("' . addslashes($message) . '");</script>';
330 function createHybridXML($ccr) {
332 // save the raw xml
333 $main_xml = $ccr->saveXml();
335 // save the stylesheet
336 $main_stylesheet = file_get_contents('stylesheet/ccr.xsl');
338 // replace stylesheet link in raw xml file
339 $substitute_string = '<?xml-stylesheet type="text/xsl" href="#style1"?>
340 <!DOCTYPE ContinuityOfCareRecord [
341 <!ATTLIST xsl:stylesheet id ID #REQUIRED>
344 $replace_string = '<?xml-stylesheet type="text/xsl" href="stylesheet/ccr.xsl"?>';
345 $main_xml = str_replace($replace_string,$substitute_string,$main_xml);
347 // remove redundant xml declaration from stylesheet
348 $replace_string = '<?xml version="1.0" encoding="UTF-8"?>';
349 $main_stylesheet = str_replace($replace_string,'',$main_stylesheet);
351 // embed the stylesheet in the raw xml file
352 $replace_string ='<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">';
353 $main_stylesheet = $replace_string.$main_stylesheet;
354 $main_xml = str_replace($replace_string,$main_stylesheet,$main_xml);
356 // insert style1 id into the stylesheet parameter
357 $substitute_string = 'xsl:stylesheet id="style1" exclude-result-prefixes';
358 $replace_string = 'xsl:stylesheet exclude-result-prefixes';
359 $main_xml = str_replace($replace_string,$substitute_string,$main_xml);
361 // prepare the filename to use
362 // LASTNAME-FIRSTNAME-PID-DATESTAMP-ccr.xml
363 $main_filename = getReportFilename()."-ccr.xml";
365 // send the output as a file to the user
366 header("Content-type: text/xml");
367 header("Content-Disposition: attachment; filename=" . $main_filename . "");
368 echo $main_xml;
371 if($_POST['ccrAction']) {
372 $raw=$_POST['raw'];
373 /* If transmit requested, fail fast if the recipient address fails basic validation */
374 if (substr($raw,0,4)=="send") {
375 $send_to = trim(stripslashes(substr($raw,5)));
376 if (!PHPMailer::ValidateAddress($send_to)) {
377 echo(htmlspecialchars( xl('Invalid recipient address. Please try again.'), ENT_QUOTES));
378 return;
380 createCCR($_POST['ccrAction'],$raw,$_POST['requested_by']);
381 } else {
382 createCCR($_POST['ccrAction'],$raw);