Highway to PSR2
[openemr.git] / ccr / createCCR.php
blobeb2fe4acf3e3b66323bb4e6dda43215e81928f3e
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";
31 } else { // isset($_GET['portal_auth_two'])
32 $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;
40 } else {
41 session_destroy();
42 header('Location: '.$landingpage.'?w');
43 exit;
47 require_once(dirname(__FILE__) . "/../interface/globals.php");
48 require_once(dirname(__FILE__) . "/../library/sql-ccr.inc");
49 require_once(dirname(__FILE__) . "/uuid.php");
50 require_once(dirname(__FILE__) . "/transmitCCD.php");
51 require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
53 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 = "")
139 global $pid;
141 $ccr->preserveWhiteSpace = false;
142 $ccr->formatOutput = true;
144 if ($raw == "yes") {
145 // simply send the xml to a textarea (nice debugging tool)
146 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
147 echo $ccr->saveXml();
148 echo "</textarea>";
149 return;
150 } else if ($raw == "hybrid") {
151 // send a file that contains a hybrid file of the raw xml and the xsl stylesheet
152 createHybridXML($ccr);
153 } else if ($raw == "pure") {
154 // send a zip file that contains a separate xml data file and xsl stylesheet
155 if (! (class_exists('ZipArchive'))) {
156 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
157 return;
160 $tempDir = $GLOBALS['temporary_files_dir'];
161 $zipName = $tempDir . "/" . getReportFilename() . "-ccr.zip";
162 if (file_exists($zipName)) {
163 unlink($zipName);
166 $zip = new ZipArchive();
167 if (!($zip)) {
168 displayError(xl("ERROR: Unable to Create Zip Archive."));
169 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);
179 $ccr->save($xmlName);
180 $zip->addFile($xmlName, basename($xmlName));
181 $zip->close();
182 header("Pragma: public");
183 header("Expires: 0");
184 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
185 header("Content-Type: application/force-download");
186 header("Content-Length: " . filesize($zipName));
187 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
188 header("Content-Description: File Transfer");
189 readfile($zipName);
190 unlink($zipName);
191 unlink($xmlName);
192 exit(0);
193 } else {
194 displayError(xl("ERROR: Unable to Create Zip Archive."));
195 return;
197 } else if (substr($raw, 0, 4)=="send") {
198 $recipient = trim(stripslashes(substr($raw, 5)));
199 $result=transmitCCD($ccr, $recipient, $requested_by, "CCR");
200 echo htmlspecialchars($result, ENT_NOQUOTES);
201 return;
202 } else {
203 header("Content-type: application/xml");
204 echo $ccr->saveXml();
208 function viewCCD($ccr, $raw = "no", $requested_by = "")
210 global $pid;
212 $ccr->preserveWhiteSpace = false;
213 $ccr->formatOutput = true;
215 $ccr->save(dirname(__FILE__) .'/generatedXml/ccrForCCD.xml');
217 $xmlDom = new DOMDocument();
218 $xmlDom->loadXML($ccr->saveXML());
220 $ccr_ccd = new DOMDocument();
221 $ccr_ccd->load(dirname(__FILE__) .'/ccd/ccr_ccd.xsl');
223 $xslt = new XSLTProcessor();
224 $xslt->importStylesheet($ccr_ccd);
226 $ccd = new DOMDocument();
227 $ccd->preserveWhiteSpace = false;
228 $ccd->formatOutput = true;
230 $ccd->loadXML($xslt->transformToXML($xmlDom));
232 $ccd->save(dirname(__FILE__) .'/generatedXml/ccdDebug.xml');
234 if ($raw == "yes") {
235 // simply send the xml to a textarea (nice debugging tool)
236 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
237 echo $ccd->saveXml();
238 echo "</textarea>";
239 return;
242 if ($raw == "pure") {
243 // send a zip file that contains a separate xml data file and xsl stylesheet
244 if (! (class_exists('ZipArchive'))) {
245 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
246 return;
249 $tempDir = $GLOBALS['temporary_files_dir'];
250 $zipName = $tempDir . "/" . getReportFilename() . "-ccd.zip";
251 if (file_exists($zipName)) {
252 unlink($zipName);
255 $zip = new ZipArchive();
256 if (!($zip)) {
257 displayError(xl("ERROR: Unable to Create Zip Archive."));
258 return;
261 if ($zip->open($zipName, ZIPARCHIVE::CREATE)) {
262 $zip->addFile("stylesheet/cda.xsl", "stylesheet/cda.xsl");
263 $xmlName = $tempDir . "/" . getReportFilename() . "-ccd.xml";
264 if (file_exists($xmlName)) {
265 unlink($xmlName);
268 $e_styleSheet = $ccd->createProcessingInstruction(
269 'xml-stylesheet',
270 'type="text/xsl" href="stylesheet/cda.xsl"'
272 $ccd->insertBefore($e_styleSheet, $ccd->firstChild);
273 $ccd->save($xmlName);
274 $zip->addFile($xmlName, basename($xmlName));
275 $zip->close();
276 header("Pragma: public");
277 header("Expires: 0");
278 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
279 header("Content-Type: application/force-download");
280 header("Content-Length: " . filesize($zipName));
281 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
282 header("Content-Description: File Transfer");
283 readfile($zipName);
284 unlink($zipName);
285 unlink($xmlName);
286 exit(0);
287 } else {
288 displayError(xl("ERROR: Unable to Create Zip Archive."));
289 return;
293 if (substr($raw, 0, 4)=="send") {
294 $recipient = trim(stripslashes(substr($raw, 5)));
295 $result=transmitCCD($ccd, $recipient, $requested_by);
296 echo htmlspecialchars($result, ENT_NOQUOTES);
297 return;
300 $ss = new DOMDocument();
301 $ss->load(dirname(__FILE__) ."/stylesheet/cda.xsl");
303 $xslt->importStyleSheet($ss);
305 $html = $xslt->transformToXML($ccd);
307 echo $html;
311 function sourceType($ccr, $uuid)
314 $e_Source = $ccr->createElement('Source');
316 $e_Actor = $ccr->createElement('Actor');
317 $e_Source->appendChild($e_Actor);
319 $e_ActorID = $ccr->createElement('ActorID', $uuid);
320 $e_Actor->appendChild($e_ActorID);
322 return $e_Source;
326 function displayError($message)
328 echo '<script type="text/javascript">alert("' . addslashes($message) . '");</script>';
332 function createHybridXML($ccr)
335 // save the raw xml
336 $main_xml = $ccr->saveXml();
338 // save the stylesheet
339 $main_stylesheet = file_get_contents('stylesheet/ccr.xsl');
341 // replace stylesheet link in raw xml file
342 $substitute_string = '<?xml-stylesheet type="text/xsl" href="#style1"?>
343 <!DOCTYPE ContinuityOfCareRecord [
344 <!ATTLIST xsl:stylesheet id ID #REQUIRED>
347 $replace_string = '<?xml-stylesheet type="text/xsl" href="stylesheet/ccr.xsl"?>';
348 $main_xml = str_replace($replace_string, $substitute_string, $main_xml);
350 // remove redundant xml declaration from stylesheet
351 $replace_string = '<?xml version="1.0" encoding="UTF-8"?>';
352 $main_stylesheet = str_replace($replace_string, '', $main_stylesheet);
354 // embed the stylesheet in the raw xml file
355 $replace_string ='<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">';
356 $main_stylesheet = $replace_string.$main_stylesheet;
357 $main_xml = str_replace($replace_string, $main_stylesheet, $main_xml);
359 // insert style1 id into the stylesheet parameter
360 $substitute_string = 'xsl:stylesheet id="style1" exclude-result-prefixes';
361 $replace_string = 'xsl:stylesheet exclude-result-prefixes';
362 $main_xml = str_replace($replace_string, $substitute_string, $main_xml);
364 // prepare the filename to use
365 // LASTNAME-FIRSTNAME-PID-DATESTAMP-ccr.xml
366 $main_filename = getReportFilename()."-ccr.xml";
368 // send the output as a file to the user
369 header("Content-type: text/xml");
370 header("Content-Disposition: attachment; filename=" . $main_filename . "");
371 echo $main_xml;
374 if ($_POST['ccrAction']) {
375 $raw=$_POST['raw'];
376 /* If transmit requested, fail fast if the recipient address fails basic validation */
377 if (substr($raw, 0, 4)=="send") {
378 $send_to = trim(stripslashes(substr($raw, 5)));
379 if (!PHPMailer::ValidateAddress($send_to)) {
380 echo(htmlspecialchars(xl('Invalid recipient address. Please try again.'), ENT_QUOTES));
381 return;
384 createCCR($_POST['ccrAction'], $raw, $_POST['requested_by']);
385 } else {
386 createCCR($_POST['ccrAction'], $raw);