letter generator
[openemr.git] / interface / patient_file / letter.php
blob8453d4e743101b5f7746dde223ff4b24c9fbdfc2
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 " .
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));
57 // Create the temporary data file and process it with enscript.
58 $tmpfn = tempnam("/tmp", "oemr_letter");
59 $tmpfh = fopen($tmpfn, "w");
60 $cpstring = '';
61 $fh = fopen("$template_dir/$form_template", 'r');
62 while (!feof($fh)) $cpstring .= fread($fh, 8192);
63 fclose($fh);
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);
81 fclose($tmpfh);
82 $tmp0 = passthru("cd $template_dir; enscript -M A4 -B -e^ " .
83 "--margins=54:54:54:18 --word-wrap -w $lang -o - '$tmpfn'$postprocess");
84 unlink($tmpfn);
86 header("Pragma: public");
87 header("Expires: 0");
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");
93 ob_end_flush();
95 exit;
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");
105 $optfrom = '';
106 $optto = '';
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;
118 <html>
119 <head>
120 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
121 <title><? xl('Letter Generator','e'); ?></title>
123 <style>
124 </style>
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"); ?>
137 </script>
139 </head>
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 ?>' />
149 <center>
151 <table border='0' cellspacing='8' width='98%'>
153 <tr>
154 <td colspan='4' align='center'>
155 &nbsp;<br>
156 <b><?php xl('Generate Letter regarding ','e'); echo $patdata['fname'] . " " .
157 $patdata['lname'] . " (" . $patdata['pubpid'] . ")" ?></b>
158 <br>&nbsp;
159 </td>
160 </tr>
162 <tr>
164 <td>
165 <?php xl('From','e'); ?>:
166 </td>
168 <td>
169 <select name='form_from'>
170 <?php echo $optfrom; ?>
171 </select>
172 </td>
174 <td>
175 <?php xl('Date','e'); ?>:
176 </td>
178 <td>
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'); ?>' />
186 </td>
188 </tr>
190 <tr>
192 <td>
193 <?php xl('To','e'); ?>:
194 </td>
196 <td>
197 <select name='form_to'>
198 <?php echo $optto; ?>
199 </select>
200 </td>
202 <td>
203 <?php xl('Template','e'); ?>:
204 </td>
206 <td>
207 <select name='form_template'>
208 <?php
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";
217 closedir($dh);
219 </select>
220 </td>
222 </tr>
224 </tr>
226 <tr>
228 <td>
229 &nbsp;
230 </td>
232 <td>
233 &nbsp;
234 </td>
236 <td>
237 <?php xl('Format','e'); ?>:
238 </td>
240 <td>
241 <select name='form_format'>
242 <option value='pdf'>PDF</option>
243 <option value='ps'>PostScript</option>
244 </select>
245 </td>
247 </tr>
249 <tr>
250 <td colspan='4'>
251 <textarea name='form_body' rows='20' cols='30' style='width:100%'
252 title='Enter body of letter here' /></textarea>
253 </td>
254 </tr>
256 </table>
258 <input type='submit' name='form_generate' value='Generate'>
260 </center>
261 </form>
263 <script language='JavaScript'>
264 Calendar.setup({inputField:"form_date", ifFormat:"%Y-%m-%d", button:"img_date"});
265 </script>
267 </body>
268 </html>