Moved patient picture, if present, to top.
[openemr.git] / interface / patient_file / letter.php
blob9bcb2692f902f75ac9ded510ee41e203bedce723
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['formaction']=="generate") {
23 // documentation for ezpdf is here --> http://www.ros.co.nz/pdf/
24 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
25 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
26 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
27 ,$GLOBALS['oer_config']['prescriptions']['bottom']
28 ,$GLOBALS['oer_config']['prescriptions']['left']
29 ,$GLOBALS['oer_config']['prescriptions']['right']
31 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
32 //if(!empty($this->pconfig['logo'])) {
33 // $pdf->ezImage($this->pconfig['logo'],"","","none","left");
34 //}
36 $form_pid = $_POST['form_pid'];
37 $form_from = $_POST['form_from'];
38 $form_to = $_POST['form_to'];
39 $form_date = $_POST['form_date'];
40 $form_template = $_POST['form_template'];
41 $form_format = $_POST['form_format'];
42 $form_body = $_POST['form_body'];
44 $frow = sqlQuery("SELECT * FROM users WHERE id = '$form_from'");
45 $trow = sqlQuery("SELECT * FROM users WHERE id = '$form_to'");
47 $datestr = date('j F Y', strtotime($form_date));
48 $from_title = $frow['title'] ? $frow['title'] . ' ' : '';
49 $to_title = $trow['title'] ? $trow['title'] . ' ' : '';
51 $cpstring = $_POST['form_body'];
53 //$fh = fopen("$template_dir/$form_template", 'r');
54 //while (!feof($fh)) $cpstring .= fread($fh, 8192);
55 //fclose($fh);
57 $cpstring = str_replace('{DATE}' , $datestr, $cpstring);
58 $cpstring = str_replace('{FROM_TITLE}' , $from_title, $cpstring);
59 $cpstring = str_replace('{FROM_FNAME}' , $frow['fname'], $cpstring);
60 $cpstring = str_replace('{FROM_LNAME}' , $frow['lname'], $cpstring);
61 $cpstring = str_replace('{FROM_MNAME}' , $frow['mname'], $cpstring);
62 $cpstring = str_replace('{FROM_STREET}' , $frow['street'], $cpstring);
63 $cpstring = str_replace('{FROM_CITY}' , $frow['city'], $cpstring);
64 $cpstring = str_replace('{FROM_STATE}' , $frow['state'], $cpstring);
65 $cpstring = str_replace('{FROM_POSTAL}' , $frow['zip'], $cpstring);
66 $cpstring = str_replace('{FROM_VALEDICTORY}', $frow['valedictory'], $cpstring);
67 $cpstring = str_replace('{FROM_PHONECELL}' , $frow['phonecell'], $cpstring);
68 $cpstring = str_replace('{TO_TITLE}' , $to_title, $cpstring);
69 $cpstring = str_replace('{TO_FNAME}' , $trow['fname'], $cpstring);
70 $cpstring = str_replace('{TO_LNAME}' , $trow['lname'], $cpstring);
71 $cpstring = str_replace('{TO_MNAME}' , $trow['mname'], $cpstring);
72 $cpstring = str_replace('{TO_STREET}' , $trow['street'], $cpstring);
73 $cpstring = str_replace('{TO_CITY}' , $trow['city'], $cpstring);
74 $cpstring = str_replace('{TO_STATE}' , $trow['state'], $cpstring);
75 $cpstring = str_replace('{TO_POSTAL}' , $trow['zip'], $cpstring);
76 $cpstring = str_replace('{TO_VALEDICTORY}' , $trow['valedictory'], $cpstring);
77 $cpstring = str_replace('{TO_FAX}' , $trow['fax'], $cpstring);
78 $cpstring = str_replace('{TO_ORGANIZATION}' , $trow['organization'], $cpstring);
79 $cpstring = str_replace('{PT_FNAME}' , $patdata['fname'], $cpstring);
80 $cpstring = str_replace('{PT_LNAME}' , $patdata['lname'], $cpstring);
81 $cpstring = str_replace('{PT_MNAME}' , $patdata['mname'], $cpstring);
82 $cpstring = str_replace('{PT_DOB}' , $patdata['DOB'], $cpstring);
83 //$cpstring = str_replace('{MESSAGE}' , $form_body, $cpstring);
85 $pdf->ezText($cpstring, 12);
87 $pdf->ezStream();
88 exit;
91 else if ($_POST['formaction'] == "loadtemplate" && $_POST['form_template'] != "") {
92 $bodytext = "";
93 $fh = fopen("$template_dir/".$_POST['form_template'], 'r');
94 while (!feof($fh)) $bodytext.= fread($fh, 8192);
95 fclose($fh);
97 else if ($_POST['formaction'] == "newtemplate" && $_POST['newtemplatename'] != "") {
98 // attempt to save the template
99 $fh = fopen("$template_dir/".$_POST['newtemplatename'], 'w');
100 if (! fwrite($fh, $_POST['form_body'])) {
101 echo "Error while writing to file ".$template_dir."/".$_POST['newtemplatename'];
102 die;
104 fclose($fh);
106 // read the saved file back
107 $_POST['form_template'] = $_POST['newtemplatename'];
108 $fh = fopen("$template_dir/".$_POST['form_template'], 'r');
109 while (!feof($fh)) $bodytext.= fread($fh, 8192);
110 fclose($fh);
112 else if ($_POST['formaction'] == "savetemplate" && $_POST['form_template'] != "") {
113 // attempt to save the template
114 $fh = fopen("$template_dir/".$_POST['form_template'], 'w');
115 if (! fwrite($fh, $_POST['form_body'])) {
116 echo "Error while writing to file ".$template_dir."/".$_POST['form_template'];
117 die;
119 fclose($fh);
121 // read the saved file back
122 $fh = fopen("$template_dir/".$_POST['form_template'], 'r');
123 while (!feof($fh)) $bodytext.= fread($fh, 8192);
124 fclose($fh);
127 // This is the case where we display the form for data entry.
129 // Get the users list.
130 $ures = sqlStatement("SELECT id, fname, lname, specialty FROM users " .
131 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
132 "ORDER BY lname, fname");
133 $i = 0;
134 $optfrom = '';
135 $optto = '';
136 $ulist = "var ulist = new Array();\n";
137 while ($urow = sqlFetchArray($ures)) {
138 $uname = $urow['lname'];
139 if ($urow['fname']) $uname .= ", " . $urow['fname'];
140 $tmp1 = " <option value='" . $urow['id'] . "'";
141 $tmp2 = ">$uname</option>\n";
142 $optto .= $tmp1 . $tmp2;
143 if ($urow['id'] == $_SESSION['authUserID']) $tmp1 .= " selected";
144 $optfrom .= $tmp1 . $tmp2;
145 $ulist .= "ulist[$i] = '" . addslashes($uname) . "|" .
146 $urow['id'] . "|" . addslashes($urow['specialty']) . "';\n";
147 ++$i;
150 // Get the unique specialties.
151 $sres = sqlStatement("SELECT DISTINCT specialty FROM users " .
152 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
153 "ORDER BY specialty");
154 $optspec = "<option value='All'>All</option>\n";
155 while ($srow = sqlFetchArray($sres)) {
156 $optspec .= " <option value='" . $srow['specialty'] . "'>" .
157 $srow['specialty'] . "</option>\n";
161 <html>
162 <head>
163 <?php html_header_show();?>
164 <title><?php xl('Letter Generator','e'); ?></title>
166 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
167 <link rel='stylesheet' href='<?php echo $css_header ?>' type='text/css'>
169 <!-- supporting javascript code -->
170 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.js"></script>
172 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/topdialog.js"></script>
173 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js"></script>
174 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/textformat.js"></script>
175 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.js"></script>
176 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar_en.js"></script>
177 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar_setup.js"></script>
179 <script language="JavaScript">
180 <?php echo $ulist; ?>
182 // React to selection of a specialty. This rebuilds the "to" users list
183 // with users having that specialty, or all users if "All" is selected.
184 function newspecialty() {
185 var f = document.forms[0];
186 var s = f.form_specialty.value;
187 var theopts = f.form_to.options;
188 theopts.length = 0;
189 var j = 0;
190 for (var i = 0; i < ulist.length; ++i) {
191 tmp = ulist[i].split("|");
192 if (s != 'All' && s != tmp[2]) continue;
193 theopts[j++] = new Option(tmp[0], tmp[1], false, false);
198 // insert text into a textarea where the cursor is
199 function insertAtCaret(areaId,text) {
200 var txtarea = document.getElementById(areaId);
201 var scrollPos = txtarea.scrollTop;
202 var strPos = 0;
203 var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
204 "ff" : (document.selection ? "ie" : false ) );
205 if (br == "ie") {
206 txtarea.focus();
207 var range = document.selection.createRange();
208 range.moveStart ('character', -txtarea.value.length);
209 strPos = range.text.length;
211 else if (br == "ff") strPos = txtarea.selectionStart;
213 var front = (txtarea.value).substring(0,strPos);
214 var back = (txtarea.value).substring(strPos,txtarea.value.length);
215 txtarea.value=front+text+back;
216 strPos = strPos + text.length;
217 if (br == "ie") {
218 txtarea.focus();
219 var range = document.selection.createRange();
220 range.moveStart ('character', -txtarea.value.length);
221 range.moveStart ('character', strPos);
222 range.moveEnd ('character', 0);
223 range.select();
225 else if (br == "ff") {
226 txtarea.selectionStart = strPos;
227 txtarea.selectionEnd = strPos;
228 txtarea.focus();
230 txtarea.scrollTop = scrollPos;
233 function insertAtCursor(myField, myValue) {
234 //IE support
235 if (document.selection) {
236 myField.focus();
237 sel = document.selection.createRange();
238 sel.text = myValue;
240 //MOZILLA/NETSCAPE support
241 else if (myField.selectionStart || myField.selectionStart == '0') {
242 var startPos = myField.selectionStart;
243 var endPos = myField.selectionEnd;
244 myField.value = myField.value.substring(0, startPos)
245 + myValue
246 + myField.value.substring(endPos, myField.value.length);
247 } else {
248 myField.value += myValue;
253 </script>
255 </head>
257 <body class="body_top" onunload='imclosing()'>
259 <!-- <form method='post' action='letter.php' onsubmit='return top.restoreSession()'> -->
260 <form method='post' action='letter.php' id="theform" name="theform">
261 <input type="hidden" name="formaction" id="formaction" value="">
262 <input type='hidden' name='form_pid' value='<?php echo $pid ?>' />
264 <center>
266 <table border='0' cellspacing='8' width='98%'>
268 <tr>
269 <td colspan='4' align='center'>
270 &nbsp;<br>
271 <b><?php xl('Generate Letter regarding ','e'); echo $patdata['fname'] . " " .
272 $patdata['lname'] . " (" . $patdata['pubpid'] . ")" ?></b>
273 <br>&nbsp;
274 </td>
275 </tr>
277 <tr>
279 <td>
280 <?php xl('From','e'); ?>:
281 </td>
283 <td>
284 <select name='form_from'>
285 <?php echo $optfrom; ?>
286 </select>
287 </td>
289 <td>
290 <?php xl('Date','e'); ?>:
291 </td>
293 <td>
294 <input type='text' size='10' name='form_date' id='form_date'
295 value='<?php echo date('Y-m-d'); ?>'
296 title='<?php xl('yyyy-mm-dd date of this letter','e'); ?>'
297 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />
298 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
299 id='img_date' border='0' alt='[?]' style='cursor:pointer'
300 title='<?php xl('Click here to choose a date','e'); ?>' />
301 </td>
303 </tr>
305 <tr>
307 <td>
308 <?php xl('Specialty','e'); ?>:
309 </td>
311 <td>
312 <select name='form_specialty' onchange='newspecialty()'>
313 <?php echo $optspec; ?>
314 </select>
315 </td>
317 <td>
318 <?php xl('Template','e'); ?>:
319 </td>
321 <td>
322 <select name="form_template" id="form_template">
323 <option value="">(none)</option>
324 <?php
325 $tpldir = "$webserver_root/custom/letter_templates";
326 $dh = opendir($tpldir);
327 if (! $dh) die("Cannot read $tpldir");
328 while (false !== ($tfname = readdir($dh))) {
329 // skip dot-files
330 if (preg_match("/^\./", $tfname)) { continue; }
331 echo "<option value=".$tfname;
332 if ($tfname == $_POST['form_template']) echo " SELECTED";
333 echo ">";
334 echo $tfname;
335 echo "</option>";
337 closedir($dh);
339 </select>
340 </td>
342 </tr>
344 </tr>
346 <tr>
348 <td>
349 <?php xl('To','e'); ?>:
350 </td>
352 <td>
353 <select name='form_to'>
354 <?php echo $optto; ?>
355 </select>
356 </td>
358 <td>
359 <?php xl('Format','e'); ?>:
360 </td>
362 <td>
363 <select name='form_format'>
364 <option value='pdf'>PDF</option>
365 <option value='ps'>PostScript</option>
366 </select>
367 </td>
369 </tr>
371 <tr>
372 <td colspan='4'>
373 <div id="letter_toolbar" class='text' style="width: 100%; background-color: #ddd; padding: 5px; margin: 0px;">
374 Insert special field:
375 <select id="letter_field">
376 <option value="">- Choose -</option>
377 <option value="{DATE}">Today's Date</option>
378 <option value="{FROM_TITLE}">FROM - Title</option>
379 <option value="{FROM_FNAME}">FROM - First name</option>
380 <option value="{FROM_MNAME}">FROM - Middle name</option>
381 <option value="{FROM_LNAME}">FROM - Last name</option>
382 <option value="{FROM_STREET}">FROM - Street</option>
383 <option value="{FROM_CITY}">FROM - City</option>
384 <option value="{FROM_STATE}">FROM - State</option>
385 <option value="{FROM_POSTAL}">FROM - Postal Code</option>
386 <option value="{FROM_VALEDICTORY}">FROM - Valedictory</option>
387 <option value="{FROM_PHONECELL}">FROM - Cell Phone</option>
388 <option value="{TO_TITLE}">TO - Title</option>
389 <option value="{TO_FNAME}">TO - First name</option>
390 <option value="{TO_MNAME}">TO - Middle name</option>
391 <option value="{TO_LNAME}">TO - Last name</option>
392 <option value="{TO_STREET}">TO - Street</option>
393 <option value="{TO_CITY}">TO - City</option>
394 <option value="{TO_STATE}">TO - State</option>
395 <option value="{TO_POSTAL}">TO - Postal Code</option>
396 <option value="{TO_VALEDICTORY}">TO - Valedictory</option>
397 <option value="{TO_ORGANIZATION}">TO - Organization</option>
398 <option value="{TO_FAX}">TO - Fax number</option>
399 <option value="{PT_FNAME}">PATIENT - First name</option>
400 <option value="{PT_MNAME}">PATIENT - Middle name</option>
401 <option value="{PT_LNAME}">PATIENT - Last name</option>
402 <option value="{PT_DOB}">PATIENT - Date of birth</option>
403 </select>
404 </div>
405 <textarea name='form_body' id="form_body" rows='20' cols='30' style='width:100%'
406 title='Enter body of letter here' /><?php echo $bodytext; ?></textarea>
407 </td>
408 </tr>
410 </table>
412 <input type='button' class="addtemplate" value='Save as New '>
413 <input type='button' name='savetemplate' id="savetemplate" value='Save Changes'>
414 <input type='button' name='form_generate' id="form_generate" value='Generate Letter'>
416 </center>
418 <!-- template DIV that appears when user chooses to add a new letter template -->
419 <div id="newtemplatedetail" style="border: 1px solid black; padding: 3px; display: none; visibility: hidden; background-color: lightgrey;">
420 Template Name: <input type="textbox" size="20" maxlength="30" name="newtemplatename" id="newtemplatename">
421 <br>
422 <input type="button" class="savenewtemplate" value="Save new template">
423 <input type="button" class="cancelnewtemplate" value="Cancel">
424 </div>
426 </form>
427 </body>
429 <script language='JavaScript'>
430 Calendar.setup({inputField:"form_date", ifFormat:"%Y-%m-%d", button:"img_date"});
432 // jQuery stuff to make the page a little easier to use
434 $(document).ready(function(){
435 $("#form_generate").click(function() { $("#formaction").val("generate"); $("#theform").submit(); });
436 $("#form_template").change(function() { $("#formaction").val("loadtemplate"); $("#theform").submit(); });
438 $("#savetemplate").click(function() { SaveTemplate(this); });
440 $("#letter_field").change(function() { insertAtCursor(document.getElementById("form_body"), $(this).val()); $(this).attr("selectedIndex", "0"); });
442 $(".addtemplate").click(function() { AddTemplate(this); });
443 $(".savenewtemplate").click(function() { SaveNewTemplate(this); });
444 $(".deletetemplate").click(function() { DeleteTemplate(this); });
445 $(".cancelnewtemplate").click(function() { CancelNewTemplate(this); });
447 // display the 'new group' DIV
448 var AddTemplate = function(btnObj) {
449 // show the field details DIV
450 $('#newtemplatedetail').css('visibility', 'visible');
451 $('#newtemplatedetail').css('display', 'block');
452 $(btnObj).parent().append($("#newtemplatedetail"));
453 $('#newtemplatedetail > #newtemplatename').focus();
456 // save the new template
457 var SaveNewTemplate = function(btnObj) {
458 // the template name can only have letters, numbers, spaces and underscores
459 // AND it cannot start with a number
460 if ($("#newtemplatename").val().match(/^\d+/)) {
461 alert("Template names cannot start with numbers.");
462 return false;
464 var validname = $("#newtemplatename").val().replace(/[^A-za-z0-9]/g, "_"); // match any non-word characters and replace them
465 $("#newtemplatename").val(validname);
467 // submit the form to add a new field to a specific group
468 $("#formaction").val("newtemplate");
469 $("#theform").submit();
472 // actually delete a template file
474 var DeleteTemplate = function(btnObj) {
475 var parts = $(btnObj).attr("id");
476 var groupname = parts.replace(/^\d+/, "");
477 if (confirm("WARNING - This action cannot be undone.\n Are you sure you wish to delete the entire group named '"+groupname+"'?")) {
478 // submit the form to add a new field to a specific group
479 $("#formaction").val("deletegroup");
480 $("#deletegroupname").val(parts);
481 $("#theform").submit();
486 // just hide the new template DIV
487 var CancelNewTemplate = function(btnObj) {
488 // hide the field details DIV
489 $('#newtemplatedetail').css('visibility', 'hidden');
490 $('#newtemplatedetail').css('display', 'none');
491 // reset the new group values to a default
492 $('#newtemplatedetail > #newtemplatename').val("");
496 // save the template, overwriting the older version
497 var SaveTemplate = function(btnObj) {
498 if (! confirm("You are about to permanently replace the existing template. Are you sure you wish to continue?")) {
499 return false;
501 $("#formaction").val("savetemplate");
502 $("#theform").submit();
506 </script>
508 </html>