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['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");
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);
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);
91 else if ($_POST['formaction'] == "loadtemplate" && $_POST['form_template'] != "") {
93 $fh = fopen("$template_dir/".$_POST['form_template'], 'r');
94 while (!feof($fh)) $bodytext.= fread($fh, 8192);
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'];
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);
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'];
121 // read the saved file back
122 $fh = fopen("$template_dir/".$_POST['form_template'], 'r');
123 while (!feof($fh)) $bodytext.= fread($fh, 8192);
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");
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";
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";
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
;
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
;
203 var br
= ((txtarea
.selectionStart || txtarea
.selectionStart
== '0') ?
204 "ff" : (document
.selection ?
"ie" : false ) );
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
;
219 var range
= document
.selection
.createRange();
220 range
.moveStart ('character', -txtarea
.value
.length
);
221 range
.moveStart ('character', strPos
);
222 range
.moveEnd ('character', 0);
225 else if (br
== "ff") {
226 txtarea
.selectionStart
= strPos
;
227 txtarea
.selectionEnd
= strPos
;
230 txtarea
.scrollTop
= scrollPos
;
233 function insertAtCursor(myField
, myValue
) {
235 if (document
.selection
) {
237 sel
= document
.selection
.createRange();
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
)
246 + myField
.value
.substring(endPos
, myField
.value
.length
);
248 myField
.value +
= myValue
;
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 ?>' />
266 <table border
='0' cellspacing
='8' width
='98%'>
269 <td colspan
='4' align
='center'>
271 <b
><?php
xl('Generate Letter regarding ','e'); echo $patdata['fname'] . " " .
272 $patdata['lname'] . " (" . $patdata['pubpid'] . ")" ?
></b
>
280 <?php
xl('From','e'); ?
>:
284 <select name
='form_from'>
285 <?php
echo $optfrom; ?
>
290 <?php
xl('Date','e'); ?
>:
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
'); ?>' />
308 <?php
xl('Specialty','e'); ?
>:
312 <select name
='form_specialty' onchange
='newspecialty()'>
313 <?php
echo $optspec; ?
>
318 <?php
xl('Template','e'); ?
>:
322 <select name
="form_template" id
="form_template">
323 <option value
="">(none
)</option
>
325 $tpldir = "$webserver_root/custom/letter_templates";
326 $dh = opendir($tpldir);
327 if (! $dh) die("Cannot read $tpldir");
328 while (false !== ($tfname = readdir($dh))) {
330 if (preg_match("/^\./", $tfname)) { continue; }
331 echo "<option value=".$tfname;
332 if ($tfname == $_POST['form_template']) echo " SELECTED";
349 <?php
xl('To','e'); ?
>:
353 <select name
='form_to'>
354 <?php
echo $optto; ?
>
359 <?php
xl('Format','e'); ?
>:
363 <select name
='form_format'>
364 <option value
='pdf'>PDF
</option
>
365 <option value
='ps'>PostScript
</option
>
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>
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>
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
'>
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">
422 <input type="button" class="savenewtemplate" value="Save new template">
423 <input type="button" class="cancelnewtemplate" value="Cancel">
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.");
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?")) {
501 $
("#formaction").val("savetemplate");
502 $
("#theform").submit();