Added access controls for encounter categories
[openemr.git] / ccr / createCCR.php
blob5b7ecd6d506d2edd24bdf320075d5bc9724efcdf
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="")
57 $authorID = getUuid();
58 $patientID = getUuid();
59 $sourceID = getUuid();
60 $oemrID = getUuid();
62 $result = getActorData();
63 while($res = sqlFetchArray($result[2])){
64 ${"labID{$res['id']}"} = getUuid();
67 $ccr = new DOMDocument('1.0','UTF-8');
68 $e_styleSheet = $ccr->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="stylesheet/ccr.xsl"');
69 $ccr->appendChild($e_styleSheet);
71 $e_ccr = $ccr->createElementNS('urn:astm-org:CCR', 'ContinuityOfCareRecord');
72 $ccr->appendChild($e_ccr);
74 /////////////// Header
76 require_once("createCCRHeader.php");
77 $e_Body = $ccr->createElement('Body');
78 $e_ccr->appendChild($e_Body);
80 /////////////// Problems
82 $e_Problems = $ccr->createElement('Problems');
83 require_once("createCCRProblem.php");
84 $e_Body->appendChild($e_Problems);
86 /////////////// Alerts
88 $e_Alerts = $ccr->createElement('Alerts');
89 require_once("createCCRAlerts.php");
90 $e_Body->appendChild($e_Alerts);
92 ////////////////// Medication
94 $e_Medications = $ccr->createElement('Medications');
95 require_once("createCCRMedication.php");
96 $e_Body->appendChild($e_Medications);
98 ///////////////// Immunization
100 $e_Immunizations = $ccr->createElement('Immunizations');
101 require_once("createCCRImmunization.php");
102 $e_Body->appendChild($e_Immunizations);
105 /////////////////// Results
107 $e_Results = $ccr->createElement('Results');
108 require_once("createCCRResult.php");
109 $e_Body->appendChild($e_Results);
112 /////////////////// Procedures
114 //$e_Procedures = $ccr->createElement('Procedures');
115 //require_once("createCCRProcedure.php");
116 //$e_Body->appendChild($e_Procedures);
118 //////////////////// Footer
120 // $e_VitalSigns = $ccr->createElement('VitalSigns');
121 // $e_Body->appendChild($e_VitalSigns);
123 /////////////// Actors
125 $e_Actors = $ccr->createElement('Actors');
126 require_once("createCCRActor.php");
127 $e_ccr->appendChild($e_Actors);
129 if ($action=="generate"){
130 gnrtCCR($ccr,$raw,$requested_by);
133 if($action == "viewccd"){
134 viewCCD($ccr,$raw,$requested_by);
138 function gnrtCCR($ccr,$raw="no",$requested_by="")
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 if (substr($raw,0,4)=="send") {
202 $recipient = trim(stripslashes(substr($raw,5)));
203 $result=transmitCCD($ccr,$recipient,$requested_by,"CCR");
204 echo htmlspecialchars($result,ENT_NOQUOTES);
205 return;
208 else {
209 header("Content-type: application/xml");
210 echo $ccr->saveXml();
215 function viewCCD($ccr,$raw="no",$requested_by="")
217 global $pid;
219 $ccr->preserveWhiteSpace = false;
220 $ccr->formatOutput = true;
222 $ccr->save(dirname(__FILE__) .'/generatedXml/ccrForCCD.xml');
224 $xmlDom = new DOMDocument();
225 $xmlDom->loadXML($ccr->saveXML());
227 $ccr_ccd = new DOMDocument();
228 $ccr_ccd->load(dirname(__FILE__) .'/ccd/ccr_ccd.xsl');
230 $xslt = new XSLTProcessor();
231 $xslt->importStylesheet($ccr_ccd);
233 $ccd = new DOMDocument();
234 $ccd->preserveWhiteSpace = false;
235 $ccd->formatOutput = true;
237 $ccd->loadXML($xslt->transformToXML($xmlDom));
239 $ccd->save(dirname(__FILE__) .'/generatedXml/ccdDebug.xml');
241 if ($raw == "yes") {
242 // simply send the xml to a textarea (nice debugging tool)
243 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
244 echo $ccd->saveXml();
245 echo "</textarea>";
246 return;
249 if ($raw == "pure") {
250 // send a zip file that contains a separate xml data file and xsl stylesheet
251 if (! (class_exists('ZipArchive')) ) {
252 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
253 return;
255 $tempDir = $GLOBALS['temporary_files_dir'];
256 $zipName = $tempDir . "/" . getReportFilename() . "-ccd.zip";
257 if (file_exists($zipName)) {
258 unlink($zipName);
260 $zip = new ZipArchive();
261 if (!($zip)) {
262 displayError(xl("ERROR: Unable to Create Zip Archive."));
263 return;
265 if ( $zip->open($zipName, ZIPARCHIVE::CREATE) ) {
266 $zip->addFile("stylesheet/cda.xsl", "stylesheet/cda.xsl");
267 $xmlName = $tempDir . "/" . getReportFilename() . "-ccd.xml";
268 if (file_exists($xmlName)) {
269 unlink($xmlName);
271 $e_styleSheet = $ccd->createProcessingInstruction('xml-stylesheet',
272 'type="text/xsl" href="stylesheet/cda.xsl"');
273 $ccd->insertBefore($e_styleSheet,$ccd->firstChild);
274 $ccd->save($xmlName);
275 $zip->addFile($xmlName, basename($xmlName) );
276 $zip->close();
277 header("Pragma: public");
278 header("Expires: 0");
279 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
280 header("Content-Type: application/force-download");
281 header("Content-Length: " . filesize($zipName));
282 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
283 header("Content-Description: File Transfer");
284 readfile($zipName);
285 unlink($zipName);
286 unlink($xmlName);
287 exit(0);
289 else {
290 displayError(xl("ERROR: Unable to Create Zip Archive."));
291 return;
295 if (substr($raw,0,4)=="send") {
296 $recipient = trim(stripslashes(substr($raw,5)));
297 $result=transmitCCD($ccd,$recipient,$requested_by);
298 echo htmlspecialchars($result,ENT_NOQUOTES);
299 return;
302 $ss = new DOMDocument();
303 $ss->load(dirname(__FILE__) ."/stylesheet/cda.xsl");
305 $xslt->importStyleSheet($ss);
307 $html = $xslt->transformToXML($ccd);
309 echo $html;
314 function sourceType($ccr, $uuid)
317 $e_Source = $ccr->createElement('Source');
319 $e_Actor = $ccr->createElement('Actor');
320 $e_Source->appendChild($e_Actor);
322 $e_ActorID = $ccr->createElement('ActorID',$uuid);
323 $e_Actor->appendChild($e_ActorID);
325 return $e_Source;
329 function displayError($message)
331 echo '<script type="text/javascript">alert("' . addslashes($message) . '");</script>';
335 function createHybridXML($ccr)
338 // save the raw xml
339 $main_xml = $ccr->saveXml();
341 // save the stylesheet
342 $main_stylesheet = file_get_contents('stylesheet/ccr.xsl');
344 // replace stylesheet link in raw xml file
345 $substitute_string = '<?xml-stylesheet type="text/xsl" href="#style1"?>
346 <!DOCTYPE ContinuityOfCareRecord [
347 <!ATTLIST xsl:stylesheet id ID #REQUIRED>
350 $replace_string = '<?xml-stylesheet type="text/xsl" href="stylesheet/ccr.xsl"?>';
351 $main_xml = str_replace($replace_string,$substitute_string,$main_xml);
353 // remove redundant xml declaration from stylesheet
354 $replace_string = '<?xml version="1.0" encoding="UTF-8"?>';
355 $main_stylesheet = str_replace($replace_string,'',$main_stylesheet);
357 // embed the stylesheet in the raw xml file
358 $replace_string ='<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">';
359 $main_stylesheet = $replace_string.$main_stylesheet;
360 $main_xml = str_replace($replace_string,$main_stylesheet,$main_xml);
362 // insert style1 id into the stylesheet parameter
363 $substitute_string = 'xsl:stylesheet id="style1" exclude-result-prefixes';
364 $replace_string = 'xsl:stylesheet exclude-result-prefixes';
365 $main_xml = str_replace($replace_string,$substitute_string,$main_xml);
367 // prepare the filename to use
368 // LASTNAME-FIRSTNAME-PID-DATESTAMP-ccr.xml
369 $main_filename = getReportFilename()."-ccr.xml";
371 // send the output as a file to the user
372 header("Content-type: text/xml");
373 header("Content-Disposition: attachment; filename=" . $main_filename . "");
374 echo $main_xml;
377 if($_POST['ccrAction']) {
378 $raw=$_POST['raw'];
379 /* If transmit requested, fail fast if the recipient address fails basic validation */
380 if (substr($raw,0,4)=="send") {
381 $send_to = trim(stripslashes(substr($raw,5)));
382 if (!PHPMailer::ValidateAddress($send_to)) {
383 echo(htmlspecialchars( xl('Invalid recipient address. Please try again.'), ENT_QUOTES));
384 return;
386 createCCR($_POST['ccrAction'],$raw,$_POST['requested_by']);
387 } else {
388 createCCR($_POST['ccrAction'],$raw);