2 // Copyright (C) 2007 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 include_once("../globals.php");
10 include_once("$srcdir/patient.inc");
12 $template_dir = "$webserver_root/custom/letter_templates";
14 $patdata = sqlQuery("SELECT " .
15 "p.fname, p.mname, p.lname, p.pubpid " .
16 "FROM patient_data AS p " .
17 "WHERE p.pid = '$pid' LIMIT 1");
19 $alertmsg = ''; // anything here pops up in an alert box
21 // If the Generate button was clicked...
22 if ($_POST['form_generate']) {
23 $form_pid = $_POST['form_pid'];
24 $form_from = $_POST['form_from'];
25 $form_to = $_POST['form_to'];
26 $form_date = $_POST['form_date'];
27 $form_template = $_POST['form_template'];
28 $form_format = $_POST['form_format'];
29 $form_body = $_POST['form_body'];
31 // Set variables that depend on the output format.
33 $mimetype = 'application/postscript';
35 if ($form_format == 'pdf') {
36 $mimetype = 'application/pdf';
37 $postprocess = ' | ps2pdf - -';
40 else if ($form_format == 'html') {
42 $mimetype = 'text/html';
44 else if ($form_format == 'rtf') {
46 $mimetype = 'application/rtf';
50 $frow = sqlQuery("SELECT * FROM users WHERE id = '$form_from'");
51 $trow = sqlQuery("SELECT * FROM users WHERE id = '$form_to'");
55 $datestr = date('j F Y', strtotime($form_date));
57 // Create the temporary data file and process it with enscript.
58 $tmpfn = tempnam("/tmp", "oemr_letter");
59 $tmpfh = fopen($tmpfn, "w");
61 $fh = fopen("$template_dir/$form_template", 'r');
62 while (!feof($fh)) $cpstring .= fread($fh, 8192);
64 $cpstring = str_replace('{DATE}' , $datestr, $cpstring);
65 $cpstring = str_replace('{FROM_FNAME}' , $frow['fname'], $cpstring);
66 $cpstring = str_replace('{FROM_LNAME}' , $frow['lname'], $cpstring);
67 $cpstring = str_replace('{FROM_MNAME}' , $frow['mname'], $cpstring);
68 $cpstring = str_replace('{FROM_STREET}', $frow['street'], $cpstring);
69 $cpstring = str_replace('{FROM_CITY}' , $frow['city'], $cpstring);
70 $cpstring = str_replace('{FROM_STATE}' , $frow['state'], $cpstring);
71 $cpstring = str_replace('{FROM_POSTAL}', $frow['zip'], $cpstring);
72 $cpstring = str_replace('{TO_FNAME}' , $trow['fname'] , $cpstring);
73 $cpstring = str_replace('{TO_LNAME}' , $trow['lname'], $cpstring);
74 $cpstring = str_replace('{TO_MNAME}' , $trow['mname'], $cpstring);
75 $cpstring = str_replace('{TO_STREET}' , $trow['street'], $cpstring);
76 $cpstring = str_replace('{TO_CITY}' , $trow['city'], $cpstring);
77 $cpstring = str_replace('{TO_STATE}' , $trow['state'], $cpstring);
78 $cpstring = str_replace('{TO_POSTAL}' , $trow['zip'], $cpstring);
79 $cpstring = str_replace('{MESSAGE}' , $form_body, $cpstring);
80 fwrite($tmpfh, $cpstring);
82 $tmp0 = passthru("cd $template_dir; enscript -M A4 -B -e^ " .
83 "--margins=54:54:54:18 --word-wrap -w $lang -o - '$tmpfn'$postprocess");
86 header("Pragma: public");
88 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
89 header("Content-Type: $mimetype");
90 header("Content-Length: " . ob_get_length());
91 header("Content-Disposition: inline; filename=letter.$form_format");
98 // This is the case where we display the form for data entry.
100 // Get the users list.
101 $ures = sqlStatement("SELECT id, fname, lname FROM users " .
102 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
103 "ORDER BY lname, fname");
107 while ($urow = sqlFetchArray($ures)) {
108 $tmp1 = " <option value='" . $urow['id'] . "'";
109 $tmp2 = ">" . $urow['lname'];
110 if ($urow['fname']) $tmp2 .= ", " . $urow['fname'];
111 $tmp2 .= "</option>\n";
112 $optto .= $tmp1 . $tmp2;
113 if ($urow['id'] == $_SESSION['authUserID']) $tmp1 .= " selected";
114 $optfrom .= $tmp1 . $tmp2;
120 <link rel
='stylesheet' href
='<?php echo $css_header ?>' type
='text/css'>
121 <title
><?
xl('Letter Generator','e'); ?
></title
>
126 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
128 <script type
="text/javascript" src
="../../library/topdialog.js"></script
>
129 <script type
="text/javascript" src
="../../library/dialog.js"></script
>
130 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
131 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
132 <script type
="text/javascript" src
="../../library/dynarch_calendar_en.js"></script
>
133 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
135 <script language
="JavaScript">
136 <?php
// require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>
141 <body
<?
echo $top_bg_line;?
> leftmargin
='0' topmargin
='0' marginwidth
='0'
142 marginheight
='0' onunload
='imclosing()'>
144 <!-- <form method
='post' action
='letter.php' onsubmit
='return top.restoreSession()'> -->
145 <form method
='post' action
='letter.php'>
147 <input type
='hidden' name
='form_pid' value
='<?php echo $pid ?>' />
151 <table border
='0' cellspacing
='8' width
='98%'>
154 <td colspan
='4' align
='center'>
156 <b
><?php
xl('Generate Letter regarding ','e'); echo $patdata['fname'] . " " .
157 $patdata['lname'] . " (" . $patdata['pubpid'] . ")" ?
></b
>
165 <?php
xl('From','e'); ?
>:
169 <select name
='form_from'>
170 <?php
echo $optfrom; ?
>
175 <?php
xl('Date','e'); ?
>:
179 <input type
='text' size
='10' name
='form_date' id
='form_date'
180 value
='<?php echo date('Y
-m
-d
'); ?>'
181 title
='<?php xl('yyyy
-mm
-dd date of this letter
','e
'); ?>'
182 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' />
183 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
184 id
='img_date' border
='0' alt
='[?]' style
='cursor:pointer'
185 title
='<?php xl('Click here to choose a date
','e
'); ?>' />
193 <?php
xl('To','e'); ?
>:
197 <select name
='form_to'>
198 <?php
echo $optto; ?
>
203 <?php
xl('Template','e'); ?
>:
207 <select name
='form_template'>
209 $tpldir = "$webserver_root/custom/letter_templates";
210 $dh = opendir($tpldir);
211 if (! $dh) die("Cannot read $tpldir");
212 while (false !== ($tfname = readdir($dh))) {
213 if (preg_match('/^(.*)\.t[a-z]*$/', $tfname, $matches)) {
214 echo " <option value='$tfname'>" . $matches[1] . "</option>\n";
237 <?php
xl('Format','e'); ?
>:
241 <select name
='form_format'>
242 <option value
='pdf'>PDF
</option
>
243 <option value
='ps'>PostScript
</option
>
251 <textarea name
='form_body' rows
='20' cols
='30' style
='width:100%'
252 title
='Enter body of letter here' /></textarea
>
258 <input type
='submit' name
='form_generate' value
='Generate'>
263 <script language
='JavaScript'>
264 Calendar
.setup({inputField
:"form_date", ifFormat
:"%Y-%m-%d", button
:"img_date"});