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, 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.
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));
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");
63 $fh = fopen("$template_dir/$form_template", 'r');
64 while (!feof($fh)) $cpstring .= fread($fh, 8192);
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);
95 $tmp0 = passthru("cd $template_dir; enscript -M A4 -B -e^ " .
96 "--margins=54:54:54:18 --word-wrap -w $lang -o - '$tmpfn'$postprocess");
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");
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");
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";
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";
146 <link rel
='stylesheet' href
='<?php echo $css_header ?>' type
='text/css'>
147 <title
><?
xl('Letter Generator','e'); ?
></title
>
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
;
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);
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 ?>' />
193 <table border
='0' cellspacing
='8' width
='98%'>
196 <td colspan
='4' align
='center'>
198 <b
><?php
xl('Generate Letter regarding ','e'); echo $patdata['fname'] . " " .
199 $patdata['lname'] . " (" . $patdata['pubpid'] . ")" ?
></b
>
207 <?php
xl('From','e'); ?
>:
211 <select name
='form_from'>
212 <?php
echo $optfrom; ?
>
217 <?php
xl('Date','e'); ?
>:
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
'); ?>' />
235 <?php
xl('Specialty','e'); ?
>:
239 <select name
='form_specialty' onchange
='newspecialty()'>
240 <?php
echo $optspec; ?
>
245 <?php
xl('Template','e'); ?
>:
249 <select name
='form_template'>
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";
271 <?php
xl('To','e'); ?
>:
275 <select name
='form_to'>
276 <?php
echo $optto; ?
>
281 <?php
xl('Format','e'); ?
>:
285 <select name
='form_format'>
286 <option value
='pdf'>PDF
</option
>
287 <option value
='ps'>PostScript
</option
>
295 <textarea name
='form_body' rows
='20' cols
='30' style
='width:100%'
296 title
='Enter body of letter here' /></textarea
>
302 <input type
='submit' name
='form_generate' value
='Generate'>
307 <script language
='JavaScript'>
308 Calendar
.setup({inputField
:"form_date", ifFormat
:"%Y-%m-%d", button
:"img_date"});