Inserted missing semi-colons. Worked without them, but they should be there.
[openemr.git] / interface / patient_file / letter.php
blob59a53380750b54195116a69bb805d5f509001b25
1 <?php
2 // Copyright (C) 2007 Rod Roark <rod@sunsetsystems.com>
3 //
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, p.DOB " .
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.
32 $lang = 'PostScript';
33 $mimetype = 'application/postscript';
34 $postprocess = '';
35 if ($form_format == 'pdf') {
36 $mimetype = 'application/pdf';
37 $postprocess = ' | ps2pdf - -';
39 /****
40 else if ($form_format == 'html') {
41 $lang = 'html';
42 $mimetype = 'text/html';
44 else if ($form_format == 'rtf') {
45 $lang = 'rtf';
46 $mimetype = 'application/rtf';
48 ****/
50 $frow = sqlQuery("SELECT * FROM users WHERE id = '$form_from'");
51 $trow = sqlQuery("SELECT * FROM users WHERE id = '$form_to'");
53 ob_start();
55 $datestr = date('j F Y', strtotime($form_date));
56 $from_title = $frow['title'] ? $frow['title'] . ' ' : '';
57 $to_title = $trow['title'] ? $trow['title'] . ' ' : '';
59 // Create the temporary data file and process it with enscript.
60 $tmpfn = tempnam("/tmp", "oemr_letter");
61 $tmpfh = fopen($tmpfn, "w");
62 $cpstring = '';
63 $fh = fopen("$template_dir/$form_template", 'r');
64 while (!feof($fh)) $cpstring .= fread($fh, 8192);
65 fclose($fh);
66 $cpstring = str_replace('{DATE}' , $datestr, $cpstring);
67 $cpstring = str_replace('{FROM_TITLE}' , $from_title, $cpstring);
68 $cpstring = str_replace('{FROM_FNAME}' , $frow['fname'], $cpstring);
69 $cpstring = str_replace('{FROM_LNAME}' , $frow['lname'], $cpstring);
70 $cpstring = str_replace('{FROM_MNAME}' , $frow['mname'], $cpstring);
71 $cpstring = str_replace('{FROM_STREET}' , $frow['street'], $cpstring);
72 $cpstring = str_replace('{FROM_CITY}' , $frow['city'], $cpstring);
73 $cpstring = str_replace('{FROM_STATE}' , $frow['state'], $cpstring);
74 $cpstring = str_replace('{FROM_POSTAL}' , $frow['zip'], $cpstring);
75 $cpstring = str_replace('{FROM_VALEDICTORY}', $frow['valedictory'], $cpstring);
76 $cpstring = str_replace('{FROM_PHONECELL}' , $frow['phonecell'], $cpstring);
77 $cpstring = str_replace('{TO_TITLE}' , $to_title, $cpstring);
78 $cpstring = str_replace('{TO_FNAME}' , $trow['fname'], $cpstring);
79 $cpstring = str_replace('{TO_LNAME}' , $trow['lname'], $cpstring);
80 $cpstring = str_replace('{TO_MNAME}' , $trow['mname'], $cpstring);
81 $cpstring = str_replace('{TO_STREET}' , $trow['street'], $cpstring);
82 $cpstring = str_replace('{TO_CITY}' , $trow['city'], $cpstring);
83 $cpstring = str_replace('{TO_STATE}' , $trow['state'], $cpstring);
84 $cpstring = str_replace('{TO_POSTAL}' , $trow['zip'], $cpstring);
85 $cpstring = str_replace('{TO_VALEDICTORY}' , $trow['valedictory'], $cpstring);
86 $cpstring = str_replace('{TO_FAX}' , $trow['fax'], $cpstring);
87 $cpstring = str_replace('{TO_ORGANIZATION}' , $trow['organization'], $cpstring);
88 $cpstring = str_replace('{PT_FNAME}' , $patdata['fname'], $cpstring);
89 $cpstring = str_replace('{PT_LNAME}' , $patdata['lname'], $cpstring);
90 $cpstring = str_replace('{PT_MNAME}' , $patdata['mname'], $cpstring);
91 $cpstring = str_replace('{PT_DOB}' , $patdata['DOB'], $cpstring);
92 $cpstring = str_replace('{MESSAGE}' , $form_body, $cpstring);
93 fwrite($tmpfh, $cpstring);
94 fclose($tmpfh);
95 $tmp0 = passthru("cd $template_dir; enscript -M A4 -B -e^ " .
96 "--margins=54:54:54:18 --word-wrap -w $lang -o - '$tmpfn'$postprocess");
97 unlink($tmpfn);
99 header("Pragma: public");
100 header("Expires: 0");
101 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
102 header("Content-Type: $mimetype");
103 header("Content-Length: " . ob_get_length());
104 header("Content-Disposition: inline; filename=letter.$form_format");
106 ob_end_flush();
108 exit;
111 // This is the case where we display the form for data entry.
113 // Get the users list.
114 $ures = sqlStatement("SELECT id, fname, lname, specialty FROM users " .
115 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
116 "ORDER BY lname, fname");
117 $i = 0;
118 $optfrom = '';
119 $optto = '';
120 $ulist = "var ulist = new Array();\n";
121 while ($urow = sqlFetchArray($ures)) {
122 $uname = $urow['lname'];
123 if ($urow['fname']) $uname .= ", " . $urow['fname'];
124 $tmp1 = " <option value='" . $urow['id'] . "'";
125 $tmp2 = ">$uname</option>\n";
126 $optto .= $tmp1 . $tmp2;
127 if ($urow['id'] == $_SESSION['authUserID']) $tmp1 .= " selected";
128 $optfrom .= $tmp1 . $tmp2;
129 $ulist .= "ulist[$i] = '" . addslashes($uname) . "|" .
130 $urow['id'] . "|" . addslashes($urow['specialty']) . "';\n";
131 ++$i;
134 // Get the unique specialties.
135 $sres = sqlStatement("SELECT DISTINCT specialty FROM users " .
136 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
137 "ORDER BY specialty");
138 $optspec = "<option value='All'>All</option>\n";
139 while ($srow = sqlFetchArray($sres)) {
140 $optspec .= " <option value='" . $srow['specialty'] . "'>" .
141 $srow['specialty'] . "</option>\n";
144 <html>
145 <head>
146 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
147 <title><? xl('Letter Generator','e'); ?></title>
149 <style>
150 </style>
152 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
154 <script type="text/javascript" src="../../library/topdialog.js"></script>
155 <script type="text/javascript" src="../../library/dialog.js"></script>
156 <script type="text/javascript" src="../../library/textformat.js"></script>
157 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
158 <script type="text/javascript" src="../../library/dynarch_calendar_en.js"></script>
159 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
161 <script language="JavaScript">
162 <?php echo $ulist; ?>
164 // React to selection of a specialty. This rebuilds the "to" users list
165 // with users having that specialty, or all users if "All" is selected.
166 function newspecialty() {
167 var f = document.forms[0];
168 var s = f.form_specialty.value;
169 var theopts = f.form_to.options;
170 theopts.length = 0;
171 var j = 0;
172 for (var i = 0; i < ulist.length; ++i) {
173 tmp = ulist[i].split("|");
174 if (s != 'All' && s != tmp[2]) continue;
175 theopts[j++] = new Option(tmp[0], tmp[1], false, false);
179 </script>
181 </head>
183 <body <?echo $top_bg_line;?> leftmargin='0' topmargin='0' marginwidth='0'
184 marginheight='0' onunload='imclosing()'>
186 <!-- <form method='post' action='letter.php' onsubmit='return top.restoreSession()'> -->
187 <form method='post' action='letter.php'>
189 <input type='hidden' name='form_pid' value='<?php echo $pid ?>' />
191 <center>
193 <table border='0' cellspacing='8' width='98%'>
195 <tr>
196 <td colspan='4' align='center'>
197 &nbsp;<br>
198 <b><?php xl('Generate Letter regarding ','e'); echo $patdata['fname'] . " " .
199 $patdata['lname'] . " (" . $patdata['pubpid'] . ")" ?></b>
200 <br>&nbsp;
201 </td>
202 </tr>
204 <tr>
206 <td>
207 <?php xl('From','e'); ?>:
208 </td>
210 <td>
211 <select name='form_from'>
212 <?php echo $optfrom; ?>
213 </select>
214 </td>
216 <td>
217 <?php xl('Date','e'); ?>:
218 </td>
220 <td>
221 <input type='text' size='10' name='form_date' id='form_date'
222 value='<?php echo date('Y-m-d'); ?>'
223 title='<?php xl('yyyy-mm-dd date of this letter','e'); ?>'
224 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
225 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
226 id='img_date' border='0' alt='[?]' style='cursor:pointer'
227 title='<?php xl('Click here to choose a date','e'); ?>' />
228 </td>
230 </tr>
232 <tr>
234 <td>
235 <?php xl('Specialty','e'); ?>:
236 </td>
238 <td>
239 <select name='form_specialty' onchange='newspecialty()'>
240 <?php echo $optspec; ?>
241 </select>
242 </td>
244 <td>
245 <?php xl('Template','e'); ?>:
246 </td>
248 <td>
249 <select name='form_template'>
250 <?php
251 $tpldir = "$webserver_root/custom/letter_templates";
252 $dh = opendir($tpldir);
253 if (! $dh) die("Cannot read $tpldir");
254 while (false !== ($tfname = readdir($dh))) {
255 if (preg_match('/^(.*)\.t[a-z]*$/', $tfname, $matches)) {
256 echo " <option value='$tfname'>" . $matches[1] . "</option>\n";
259 closedir($dh);
261 </select>
262 </td>
264 </tr>
266 </tr>
268 <tr>
270 <td>
271 <?php xl('To','e'); ?>:
272 </td>
274 <td>
275 <select name='form_to'>
276 <?php echo $optto; ?>
277 </select>
278 </td>
280 <td>
281 <?php xl('Format','e'); ?>:
282 </td>
284 <td>
285 <select name='form_format'>
286 <option value='pdf'>PDF</option>
287 <option value='ps'>PostScript</option>
288 </select>
289 </td>
291 </tr>
293 <tr>
294 <td colspan='4'>
295 <textarea name='form_body' rows='20' cols='30' style='width:100%'
296 title='Enter body of letter here' /></textarea>
297 </td>
298 </tr>
300 </table>
302 <input type='submit' name='form_generate' value='Generate'>
304 </center>
305 </form>
307 <script language='JavaScript'>
308 Calendar.setup({inputField:"form_date", ifFormat:"%Y-%m-%d", button:"img_date"});
309 </script>
311 </body>
312 </html>