improved cover sheet and a couple of other fax-sending tweaks
[openemr.git] / interface / fax / fax_dispatch.php
blobafbdad716a1c48b27d50ce32e30d394b913ab759
1 <?
2 // Copyright (C) 2006 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");
11 include_once("$srcdir/pnotes.inc");
13 $filename = escapeshellcmd($_GET['file']);
14 $filepath = $GLOBALS['hylafax_basedir'] . '/recvq/' . $filename;
15 $filebase = basename("/$filename", '.tif');
16 $faxcache = "$webserver_root/faxcache/$filebase";
18 $info_msg = "";
20 // This function builds an array of document categories recursively.
21 // Kittens are the children of cats, you know. :-)
23 function getKittens($catid, $catstring, &$categories) {
24 $cres = sqlStatement("SELECT id, name FROM categories " .
25 "WHERE parent = $catid ORDER BY name");
26 $childcount = 0;
27 while ($crow = sqlFetchArray($cres)) {
28 ++$childcount;
29 getKittens($crow['id'], ($catstring ? "$catstring / " : "") .
30 ($catid ? $crow['name'] : ''), $categories);
32 // If no kitties, then this is a leaf node and should be listed.
33 if (!$childcount) $categories[$catid] = $catstring;
36 // This merges the tiff files for the selected pages into one tiff file.
38 function mergeTiffs() {
39 global $faxcache;
40 $msg = '';
41 $inames = '';
42 $tmp1 = array();
43 $tmp2 = 0;
44 foreach ($_POST['form_images'] as $inbase) {
45 $inames .= ' ' . escapeshellarg("$inbase.tif");
47 if (!$inames) die("Internal error - no pages were selected!");
48 $tmp0 = exec("cd '$faxcache'; tiffcp $inames temp.tif", $tmp1, $tmp2);
49 if ($tmp2) {
50 $msg .= "tiffcp returned $tmp2: $tmp0 ";
52 return $msg;
55 // If we are submitting...
57 if ($_POST['form_save']) {
58 $action_taken = false;
59 $tmp1 = array();
60 $tmp2 = 0;
62 if ($_POST['form_cb_copy']) {
63 $patient_id = (int) $_POST['form_pid'];
64 if (!$patient_id) die('Internal error - patient ID was not provided!');
65 // Compute the name of the target directory and make sure it exists.
66 $docdir = "$webserver_root/documents/$patient_id";
67 exec("mkdir -p '$docdir'");
68 // Compute a target filename that does not yet exist.
69 $ffmod = '';
70 $ffsuff = '.pdf';
71 // If the target filename exists, modify it until it doesn't.
72 $count = 0;
73 while (is_file("$docdir/$filebase$ffmod$ffsuff")) {
74 ++$count;
75 $ffmod = "_$count";
77 $target = "$docdir/$filebase$ffmod$ffsuff";
79 // Create the target PDF.
80 $info_msg .= mergeTiffs();
81 $tmp0 = exec("tiff2pdf -p letter -o '$target' '$faxcache/temp.tif'", $tmp1, $tmp2);
82 if ($tmp2) {
83 $info_msg .= "tiff2pdf returned $tmp2: $tmp0 ";
85 else {
86 $newid = generate_id();
87 $fsize = filesize($target);
88 $catid = (int) $_POST['form_category'];
89 // Update the database.
90 $query = "INSERT INTO documents ( " .
91 "id, type, size, date, url, mimetype, foreign_id" .
92 " ) VALUES ( " .
93 "'$newid', 'file_url', '$fsize', NOW(), 'file://$target', " .
94 "'application/pdf', $patient_id " .
95 ")";
96 sqlStatement($query);
97 $query = "INSERT INTO categories_to_documents ( " .
98 "category_id, document_id" .
99 " ) VALUES ( " .
100 "'$catid', '$newid' " .
101 ")";
102 sqlStatement($query);
104 // If we are posting a note...
105 if ($_POST['form_cb_note']) {
106 // Build note text in a way that identifies the new document.
107 // See pnotes_full.php which uses this to auto-display the document.
108 $note = "$filebase$ffmod$ffsuff";
109 for ($tmp = $catid; $tmp;) {
110 $catrow = sqlQuery("SELECT name, parent FROM categories WHERE id = '$tmp'");
111 $note = $catrow['name'] . "/$note";
112 $tmp = $catrow['parent'];
114 $note = "New scanned document $newid: $note";
115 $form_note_message = trim($_POST['form_note_message']);
116 if (get_magic_quotes_gpc()) $form_note_message = stripslashes($form_note_message);
117 if ($form_note_message) $note .= "\n" . $form_note_message;
118 // addPnote() will do its own addslashes().
119 addPnote($_POST['form_pid'], $note, $userauthorized, '1',
120 $_POST['form_note_type'], $_POST['form_note_to']);
123 $action_taken = true;
127 if ($_POST['form_cb_forward']) {
128 $form_from = trim($_POST['form_from']);
129 $form_to = trim($_POST['form_to']);
130 $form_fax = trim($_POST['form_fax']);
131 $form_message = trim($_POST['form_message']);
132 $form_finemode = $_POST['form_finemode'] ? '-m' : '-l';
134 if (get_magic_quotes_gpc()) {
135 $form_from = stripslashes($form_from);
136 $form_to = stripslashes($form_to);
137 $form_message = stripslashes($form_message);
140 // Generate a cover page using enscript. This can be a cool thing
141 // to do, as enscript is very powerful.
143 $tmp1 = array();
144 $tmp2 = 0;
145 $tmpfn1 = tempnam("/tmp", "fax1");
146 $tmpfn2 = tempnam("/tmp", "fax2");
147 $tmph = fopen($tmpfn1, "w");
148 $cpstring = '';
149 $fh = fopen("$webserver_root/custom/faxcover.txt", 'r');
150 while (!feof($fh)) $cpstring .= fread($fh, 8192);
151 fclose($fh);
152 $cpstring = str_replace('{CURRENT_DATE}' , date('F j, Y'), $cpstring);
153 $cpstring = str_replace('{SENDER_NAME}' , $form_from , $cpstring);
154 $cpstring = str_replace('{RECIPIENT_NAME}', $form_to , $cpstring);
155 $cpstring = str_replace('{RECIPIENT_FAX}' , $form_fax , $cpstring);
156 $cpstring = str_replace('{MESSAGE}' , $form_message , $cpstring);
157 fwrite($tmph, $cpstring);
158 fclose($tmph);
159 $tmp0 = exec("cd $webserver_root/custom; " . $GLOBALS['hylafax_enscript'] .
160 " -o $tmpfn2 $tmpfn1", $tmp1, $tmp2);
161 if ($tmp2) {
162 $info_msg .= "enscript returned $tmp2: $tmp0 ";
164 unlink($tmpfn1);
166 // Send the fax as the cover page followed by the selected pages.
167 $info_msg .= mergeTiffs();
168 $tmp0 = exec("sendfax -n $form_finemode -d " .
169 escapeshellarg($form_fax) . " $tmpfn2 '$faxcache/temp.tif'",
170 $tmp1, $tmp2);
171 if ($tmp2) {
172 $info_msg .= "sendfax returned $tmp2: $tmp0 ";
174 unlink($tmpfn2);
176 $action_taken = true;
179 if ($_POST['form_cb_delete'] && !$info_msg) {
180 // Delete the tiff file.
181 unlink($filepath);
183 // Erase its cache.
184 if (is_dir($faxcache)) {
185 $dh = opendir($faxcache);
186 while (($tmp = readdir($dh)) !== false) {
187 if (is_file("$faxcache/$tmp")) unlink("$faxcache/$tmp");
189 closedir($dh);
190 rmdir($faxcache);
193 $action_taken = true;
196 if (!$action_taken && !$info_msg)
197 $info_msg = 'You did not choose any actions.';
199 // Close this window and refresh the fax list.
200 echo "<html>\n<body>\n<script language='JavaScript'>\n";
201 if ($info_msg) echo " alert('$info_msg');\n";
202 echo " if (!opener.closed && opener.refreshme) opener.refreshme();\n";
203 echo " window.close();\n";
204 echo "</script>\n</body>\n</html>\n";
205 exit();
208 // If we get this far then we are displaying the form.
210 // If the image cache does not yet exist for this fax, build it.
211 if (! is_dir($faxcache)) {
212 $tmp0 = exec("mkdir -p '$faxcache'", $tmp1, $tmp2);
213 if ($tmp2) die("mkdir returned $tmp2: $tmp0");
214 $tmp0 = exec("cd '$faxcache'; tiffsplit '$filepath'", $tmp1, $tmp2);
215 if ($tmp2) die("tiffsplit returned $tmp2: $tmp0");
216 $tmp0 = exec("cd '$faxcache'; mogrify -resize 600x776 -format jpg *.tif", $tmp1, $tmp2);
217 if ($tmp2) die("mogrify returned $tmp2: $tmp0");
220 // Get the categories list.
221 $categories = array();
222 getKittens(0, '', $categories);
224 // Get the users list.
225 $ures = sqlStatement("SELECT username, fname, lname FROM users " .
226 "ORDER BY lname, fname");
228 <html>
229 <head>
230 <title>Dispatch Received Document</title>
231 <link rel=stylesheet href='<? echo $css_header ?>' type='text/css'>
233 <style>
235 body, td, input, select, textarea {
236 font-family: Arial, Helvetica, sans-serif;
237 font-size: 10pt;
240 body {
241 padding: 0.2em 1em 1em 1em;
244 .itemtitle {
245 font-weight: bold;
248 div.section {
249 border: solid;
250 border-width: 1px;
251 border-color: #0000ff;
252 margin-left: 2em;
253 padding: 1em;
256 </style>
258 <script type="text/javascript" src="../../library/topdialog.js"></script>
259 <script type="text/javascript" src="../../library/dialog.js"></script>
260 <script type="text/javascript" src="../../library/textformat.js"></script>
262 <script language="JavaScript">
264 var mypcc = '<? echo $GLOBALS['phone_country_code'] ?>';
266 function divclick(cb, divid) {
267 var divstyle = document.getElementById(divid).style;
268 if (cb.checked) {
269 divstyle.display = 'block';
270 } else {
271 divstyle.display = 'none';
273 return true;
276 // This is for callback by the find-patient popup.
277 function setpatient(pid, lname, fname, dob) {
278 var f = document.forms[0];
279 f.form_patient.value = lname + ', ' + fname;
280 f.form_pid.value = pid;
283 // This invokes the find-patient popup.
284 function sel_patient() {
285 dlgopen('../main/calendar/find_patient_popup.php', '_blank', 500, 400);
288 // Check for errors when the form is submitted.
289 function validate() {
290 var f = document.forms[0];
292 if (f.form_cb_copy.checked) {
293 if (! f.form_pid.value) {
294 alert('You have not selected a patient!');
295 return false;
299 if (f.form_cb_forward.checked) {
300 var s = f.form_fax.value;
301 if (! s) {
302 alert('A fax number is required!');
303 return false;
305 var digcount = 0;
306 for (var i = 0; i < s.length; ++i) {
307 var c = s.charAt(i);
308 if (c >= '0' && c <= '9') {
309 ++digcount;
311 else if (digcount == 0 || c != '-') {
312 alert('Invalid character(s) in fax number!');
313 return false;
316 if (digcount == 7) {
317 if (s.charAt(0) < '2') {
318 alert('Local phone number starts with an invalid digit!');
319 return false;
322 else if (digcount == 11) {
323 if (s.charAt(0) != '1') {
324 alert('11-digit number must begin with 1!');
325 return false;
328 else if (digcount == 10) {
329 if (s.charAt(0) < '2') {
330 alert('10-digit number starts with an invalid digit!');
331 return false;
333 f.form_fax.value = '1' + s;
335 else {
336 alert('Invalid number of digits in fax telephone number!');
337 return false;
341 if (f.form_cb_copy.checked || f.form_cb_forward.checked) {
342 var check_count = 0;
343 for (var i = 0; i < f.elements.length; ++i) {
344 if (f.elements[i].name == 'form_images[]' && f.elements[i].checked)
345 ++check_count;
347 if (check_count == 0) {
348 alert('No pages have been selected!');
349 return false;
353 return true;
356 </script>
358 </head>
360 <body <?echo $top_bg_line;?> onunload='imclosing()'>
362 <center><h2>Dispatch Received Document</h2></center>
364 <form method='post' name='theform' action='fax_dispatch.php?file=<? echo $filename ?>'
365 onsubmit='return validate()'>
367 <p><input type='checkbox' name='form_cb_copy' value='1'
368 onclick='return divclick(this,"div_copy");' />
369 <b>Copy Pages to Patient Chart</b></p>
371 <div id='div_copy' class='section' style='display:none;'>
372 <table>
373 <tr>
374 <td class='itemtitle' width='1%' nowrap>Patient</td>
375 <td>
376 <input type='text' size='10' name='form_patient' style='width:100%'
377 value=' (Click to select)' onclick='sel_patient()'
378 title='Click to select patient' readonly />
379 <input type='hidden' name='form_pid' value='0' />
380 </td>
381 </tr>
382 <tr>
383 <td class='itemtitle' nowrap>Category</td>
384 <td>
385 <select name='form_category' style='width:100%'>
386 <?php
387 foreach ($categories as $catkey => $catname) {
388 echo " <option value='$catkey'";
389 echo ">$catname</option>\n";
392 </select>
393 </td>
394 </tr>
395 <tr>
396 <td class='itemtitle' nowrap>Filename</td>
397 <td>
398 <input type='text' size='10' name='form_filename' style='width:100%'
399 value='<? echo $filename ?>'
400 title='Name for this document in the patient chart' />
401 </td>
402 </tr>
403 <tr>
404 <td colspan='2' style='padding-top:0.5em;'>
405 <input type='checkbox' name='form_cb_note' value='1'
406 onclick='return divclick(this,"div_note");' />
407 <b>Create Patient Note</b>
408 <div id='div_note' class='section' style='display:none;margin-top:0.5em;'>
409 <table>
410 <tr>
411 <td class='itemtitle' width='1%' nowrap>Type</td>
412 <td>
413 <select name='form_note_type' style='width:100%'>
414 <?php
415 foreach ($patient_note_types as $value) {
416 echo " <option value='$value'";
417 echo ">$value</option>\n";
420 </select>
421 </td>
422 </tr>
423 <tr>
424 <td class='itemtitle' width='1%' nowrap>To</td>
425 <td>
426 <select name='form_note_to' style='width:100%'>
427 <?php
428 while ($urow = sqlFetchArray($ures)) {
429 echo " <option value='" . $urow['username'] . "'";
430 echo ">" . $urow['lname'];
431 if ($urow['fname']) echo ", " . $urow['fname'];
432 echo "</option>\n";
435 <option value=''>** <? xl('Close','e'); ?> **</option>
436 </select>
437 </td>
438 </tr>
439 <tr>
440 <td class='itemtitle' nowrap>Message</td>
441 <td>
442 <textarea name='form_note_message' rows='3' cols='30' style='width:100%'
443 title='Your comments' /></textarea>
444 </td>
445 </tr>
446 </table>
447 </div><!-- end div_note -->
448 </td>
449 </tr>
450 </table>
451 </div><!-- end div_copy -->
453 <p><input type='checkbox' name='form_cb_forward' value='1'
454 onclick='return divclick(this,"div_forward");' />
455 <b>Forward Pages via Fax</b></p>
457 <div id='div_forward' class='section' style='display:none;'>
458 <table>
459 <tr>
460 <td class='itemtitle' width='1%' nowrap>From</td>
461 <td>
462 <input type='text' size='10' name='form_from' style='width:100%'
463 title='Type your name here' />
464 </td>
465 </tr>
466 <tr>
467 <td class='itemtitle' nowrap>To</td>
468 <td>
469 <input type='text' size='10' name='form_to' style='width:100%'
470 title='Type the recipient name here' />
471 </td>
472 </tr>
473 <tr>
474 <td class='itemtitle' nowrap>Fax</td>
475 <td>
476 <input type='text' size='10' name='form_fax' style='width:100%'
477 title='The fax phone number to send this to' />
478 </td>
479 </tr>
480 <tr>
481 <td class='itemtitle' nowrap>Message</td>
482 <td>
483 <textarea name='form_message' rows='3' cols='30' style='width:100%'
484 title='Your comments to include with this message' /></textarea>
485 </td>
486 </tr>
487 <tr>
488 <td class='itemtitle' nowrap>Quality</td>
489 <td>
490 <input type='radio' name='form_finemode' value='' />Normal &nbsp;
491 <input type='radio' name='form_finemode' value='1' checked />Fine &nbsp;
492 </td>
493 </tr>
494 </table>
495 </div><!-- end div_forward -->
497 <p><input type='checkbox' name='form_cb_delete' value='1' />
498 <b>Delete Fax from Queue</b></p>
500 <center>
502 <input type='submit' name='form_save' value='OK' />
503 &nbsp; &nbsp;
504 <input type='button' value='Cancel' onclick='window.close()' />
505 </p>
507 <p><br /><b>Please select the desired pages to copy or forward:</b></p>
508 <table>
510 <?php
511 $dh = opendir($faxcache);
512 if (! $dh) die("Cannot read $faxcache");
513 $page = 0;
514 while (false !== ($jfname = readdir($dh))) {
515 if (preg_match("/^(.*)\.jpg/", $jfname, $matches)) {
516 ++$page;
517 $jfnamebase = $matches[1];
518 echo " <tr>\n";
519 echo " <td valign='top'>\n";
520 echo " <img src='../../faxcache/$filebase/$jfname' />\n";
521 echo " </td>\n";
522 echo " <td align='center' valign='top'>\n";
523 echo " <input type='checkbox' name='form_images[]' value='$jfnamebase' />\n";
524 echo " <br />$page\n";
525 echo " </td>\n";
526 echo " </tr>\n";
529 closedir($dh);
532 </table>
533 </center>
534 </form>
536 </body>
537 </html>