dump db version
[openemr.git] / ccr / createCCR.php
blob9ac59d7291cd609d48afd8ea8c5a31bfdc88e64d
1 <?php
2 /**
3 * CCR Script.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Garden State Health Systems <http://www.gshsys.com/>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2010 Garden State Health Systems <http://www.gshsys.com/>
10 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 // check if using the patient portal
16 //(if so, then use the portal authorization)
17 if (isset($_GET['portal_auth']) || isset($_GET['portal_auth_two'])) {
18 if (isset($_GET['portal_auth'])) {
19 $landingpage = "../patients/index.php";
20 } else { // isset($_GET['portal_auth_two'])
21 $landingpage = "../portal/index.php";
24 session_start();
25 if (isset($_SESSION['pid']) && (isset($_SESSION['patient_portal_onsite']) || isset($_SESSION['patient_portal_onsite_two']))) {
26 $pid = $_SESSION['pid'];
27 $ignoreAuth=true;
28 global $ignoreAuth;
29 } else {
30 session_destroy();
31 header('Location: '.$landingpage.'?w');
32 exit;
36 require_once(dirname(__FILE__) . "/../interface/globals.php");
37 require_once(dirname(__FILE__) . "/../library/sql-ccr.inc");
38 require_once(dirname(__FILE__) . "/uuid.php");
39 require_once(dirname(__FILE__) . "/transmitCCD.php");
40 require_once(dirname(__FILE__) . "/../custom/code_types.inc.php");
42 use PHPMailer\PHPMailer\PHPMailer;
44 function createCCR($action, $raw = "no", $requested_by = "")
47 $authorID = getUuid();
48 $patientID = getUuid();
49 $sourceID = getUuid();
50 $oemrID = getUuid();
52 $result = getActorData();
53 while ($res = sqlFetchArray($result[2])) {
54 ${"labID{$res['id']}"} = getUuid();
57 $ccr = new DOMDocument('1.0', 'UTF-8');
58 $e_styleSheet = $ccr->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="stylesheet/ccr.xsl"');
59 $ccr->appendChild($e_styleSheet);
61 $e_ccr = $ccr->createElementNS('urn:astm-org:CCR', 'ContinuityOfCareRecord');
62 $ccr->appendChild($e_ccr);
64 /////////////// Header
66 require_once("createCCRHeader.php");
67 $e_Body = $ccr->createElement('Body');
68 $e_ccr->appendChild($e_Body);
70 /////////////// Problems
72 $e_Problems = $ccr->createElement('Problems');
73 require_once("createCCRProblem.php");
74 $e_Body->appendChild($e_Problems);
76 /////////////// Alerts
78 $e_Alerts = $ccr->createElement('Alerts');
79 require_once("createCCRAlerts.php");
80 $e_Body->appendChild($e_Alerts);
82 ////////////////// Medication
84 $e_Medications = $ccr->createElement('Medications');
85 require_once("createCCRMedication.php");
86 $e_Body->appendChild($e_Medications);
88 ///////////////// Immunization
90 $e_Immunizations = $ccr->createElement('Immunizations');
91 require_once("createCCRImmunization.php");
92 $e_Body->appendChild($e_Immunizations);
95 /////////////////// Results
97 $e_Results = $ccr->createElement('Results');
98 require_once("createCCRResult.php");
99 $e_Body->appendChild($e_Results);
102 /////////////////// Procedures
104 //$e_Procedures = $ccr->createElement('Procedures');
105 //require_once("createCCRProcedure.php");
106 //$e_Body->appendChild($e_Procedures);
108 //////////////////// Footer
110 // $e_VitalSigns = $ccr->createElement('VitalSigns');
111 // $e_Body->appendChild($e_VitalSigns);
113 /////////////// Actors
115 $e_Actors = $ccr->createElement('Actors');
116 require_once("createCCRActor.php");
117 $e_ccr->appendChild($e_Actors);
119 if ($action=="generate") {
120 gnrtCCR($ccr, $raw, $requested_by);
123 if ($action == "viewccd") {
124 viewCCD($ccr, $raw, $requested_by);
128 function gnrtCCR($ccr, $raw = "no", $requested_by = "")
130 global $pid;
132 $ccr->preserveWhiteSpace = false;
133 $ccr->formatOutput = true;
135 if ($raw == "yes") {
136 // simply send the xml to a textarea (nice debugging tool)
137 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
138 echo $ccr->saveXml();
139 echo "</textarea>";
140 return;
141 } else if ($raw == "hybrid") {
142 // send a file that contains a hybrid file of the raw xml and the xsl stylesheet
143 createHybridXML($ccr);
144 } else if ($raw == "pure") {
145 // send a zip file that contains a separate xml data file and xsl stylesheet
146 if (! (class_exists('ZipArchive'))) {
147 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
148 return;
151 $tempDir = $GLOBALS['temporary_files_dir'];
152 $zipName = $tempDir . "/" . getReportFilename() . "-ccr.zip";
153 if (file_exists($zipName)) {
154 unlink($zipName);
157 $zip = new ZipArchive();
158 if (!($zip)) {
159 displayError(xl("ERROR: Unable to Create Zip Archive."));
160 return;
163 if ($zip->open($zipName, ZIPARCHIVE::CREATE)) {
164 $zip->addFile("stylesheet/ccr.xsl", "stylesheet/ccr.xsl");
165 $xmlName = $tempDir . "/" . getReportFilename() . "-ccr.xml";
166 if (file_exists($xmlName)) {
167 unlink($xmlName);
170 $ccr->save($xmlName);
171 $zip->addFile($xmlName, basename($xmlName));
172 $zip->close();
173 header("Pragma: public");
174 header("Expires: 0");
175 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
176 header("Content-Type: application/force-download");
177 header("Content-Length: " . filesize($zipName));
178 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
179 header("Content-Description: File Transfer");
180 readfile($zipName);
181 unlink($zipName);
182 unlink($xmlName);
183 exit(0);
184 } else {
185 displayError(xl("ERROR: Unable to Create Zip Archive."));
186 return;
188 } else if (substr($raw, 0, 4)=="send") {
189 $recipient = trim(stripslashes(substr($raw, 5)));
190 $result=transmitCCD($ccr, $recipient, $requested_by, "CCR");
191 echo htmlspecialchars($result, ENT_NOQUOTES);
192 return;
193 } else {
194 header("Content-type: application/xml");
195 echo $ccr->saveXml();
199 function viewCCD($ccr, $raw = "no", $requested_by = "")
201 global $pid;
203 $ccr->preserveWhiteSpace = false;
204 $ccr->formatOutput = true;
206 if (file_exists(dirname(__FILE__) .'/generatedXml')) {
207 $ccr->save(dirname(__FILE__) . '/generatedXml/ccrForCCD.xml');
210 $xmlDom = new DOMDocument();
211 $xmlDom->loadXML($ccr->saveXML());
213 $ccr_ccd = new DOMDocument();
214 $ccr_ccd->load(dirname(__FILE__) .'/ccd/ccr_ccd.xsl');
216 $xslt = new XSLTProcessor();
217 $xslt->importStylesheet($ccr_ccd);
219 $ccd = new DOMDocument();
220 $ccd->preserveWhiteSpace = false;
221 $ccd->formatOutput = true;
223 $ccd->loadXML($xslt->transformToXML($xmlDom));
225 if (file_exists(dirname(__FILE__) .'/generatedXml')) {
226 $ccd->save(dirname(__FILE__) . '/generatedXml/ccdDebug.xml');
229 if ($raw == "yes") {
230 // simply send the xml to a textarea (nice debugging tool)
231 echo "<textarea rows='35' cols='500' style='width:95%' readonly>";
232 echo $ccd->saveXml();
233 echo "</textarea>";
234 return;
237 if ($raw == "pure") {
238 // send a zip file that contains a separate xml data file and xsl stylesheet
239 if (! (class_exists('ZipArchive'))) {
240 displayError(xl("ERROR: Missing ZipArchive PHP Module"));
241 return;
244 $tempDir = $GLOBALS['temporary_files_dir'];
245 $zipName = $tempDir . "/" . getReportFilename() . "-ccd.zip";
246 if (file_exists($zipName)) {
247 unlink($zipName);
250 $zip = new ZipArchive();
251 if (!($zip)) {
252 displayError(xl("ERROR: Unable to Create Zip Archive."));
253 return;
256 if ($zip->open($zipName, ZIPARCHIVE::CREATE)) {
257 $zip->addFile("stylesheet/cda.xsl", "stylesheet/cda.xsl");
258 $xmlName = $tempDir . "/" . getReportFilename() . "-ccd.xml";
259 if (file_exists($xmlName)) {
260 unlink($xmlName);
263 $e_styleSheet = $ccd->createProcessingInstruction(
264 'xml-stylesheet',
265 'type="text/xsl" href="stylesheet/cda.xsl"'
267 $ccd->insertBefore($e_styleSheet, $ccd->firstChild);
268 $ccd->save($xmlName);
269 $zip->addFile($xmlName, basename($xmlName));
270 $zip->close();
271 header("Pragma: public");
272 header("Expires: 0");
273 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
274 header("Content-Type: application/force-download");
275 header("Content-Length: " . filesize($zipName));
276 header("Content-Disposition: attachment; filename=" . basename($zipName) . ";");
277 header("Content-Description: File Transfer");
278 readfile($zipName);
279 unlink($zipName);
280 unlink($xmlName);
281 exit(0);
282 } else {
283 displayError(xl("ERROR: Unable to Create Zip Archive."));
284 return;
288 if (substr($raw, 0, 4)=="send") {
289 $recipient = trim(stripslashes(substr($raw, 5)));
290 $result=transmitCCD($ccd, $recipient, $requested_by);
291 echo htmlspecialchars($result, ENT_NOQUOTES);
292 return;
295 $ss = new DOMDocument();
296 $ss->load(dirname(__FILE__) ."/stylesheet/cda.xsl");
298 $xslt->importStyleSheet($ss);
300 $html = $xslt->transformToXML($ccd);
302 echo $html;
306 function sourceType($ccr, $uuid)
309 $e_Source = $ccr->createElement('Source');
311 $e_Actor = $ccr->createElement('Actor');
312 $e_Source->appendChild($e_Actor);
314 $e_ActorID = $ccr->createElement('ActorID', $uuid);
315 $e_Actor->appendChild($e_ActorID);
317 return $e_Source;
321 function displayError($message)
323 echo '<script type="text/javascript">alert("' . addslashes($message) . '");</script>';
327 function createHybridXML($ccr)
330 // save the raw xml
331 $main_xml = $ccr->saveXml();
333 // save the stylesheet
334 $main_stylesheet = file_get_contents('stylesheet/ccr.xsl');
336 // replace stylesheet link in raw xml file
337 $substitute_string = '<?xml-stylesheet type="text/xsl" href="#style1"?>
338 <!DOCTYPE ContinuityOfCareRecord [
339 <!ATTLIST xsl:stylesheet id ID #REQUIRED>
342 $replace_string = '<?xml-stylesheet type="text/xsl" href="stylesheet/ccr.xsl"?>';
343 $main_xml = str_replace($replace_string, $substitute_string, $main_xml);
345 // remove redundant xml declaration from stylesheet
346 $replace_string = '<?xml version="1.0" encoding="UTF-8"?>';
347 $main_stylesheet = str_replace($replace_string, '', $main_stylesheet);
349 // embed the stylesheet in the raw xml file
350 $replace_string ='<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">';
351 $main_stylesheet = $replace_string.$main_stylesheet;
352 $main_xml = str_replace($replace_string, $main_stylesheet, $main_xml);
354 // insert style1 id into the stylesheet parameter
355 $substitute_string = 'xsl:stylesheet id="style1" exclude-result-prefixes';
356 $replace_string = 'xsl:stylesheet exclude-result-prefixes';
357 $main_xml = str_replace($replace_string, $substitute_string, $main_xml);
359 // prepare the filename to use
360 // LASTNAME-FIRSTNAME-PID-DATESTAMP-ccr.xml
361 $main_filename = getReportFilename()."-ccr.xml";
363 // send the output as a file to the user
364 header("Content-type: text/xml");
365 header("Content-Disposition: attachment; filename=" . $main_filename . "");
366 echo $main_xml;
369 if ($_POST['ccrAction']) {
370 $raw=$_POST['raw'];
371 /* If transmit requested, fail fast if the recipient address fails basic validation */
372 if (substr($raw, 0, 4)=="send") {
373 $send_to = trim(stripslashes(substr($raw, 5)));
374 if (!PHPMailer::ValidateAddress($send_to)) {
375 echo(htmlspecialchars(xl('Invalid recipient address. Please try again.'), ENT_QUOTES));
376 return;
379 createCCR($_POST['ccrAction'], $raw, $_POST['requested_by']);
380 } else {
381 createCCR($_POST['ccrAction'], $raw);