2 // Copyright (C) 2006 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");
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";
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");
27 while ($crow = sqlFetchArray($cres)) {
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() {
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);
50 $msg .= "tiffcp returned $tmp2: $tmp0 ";
55 // If we are submitting...
57 if ($_POST['form_save']) {
58 $action_taken = false;
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.
71 // If the target filename exists, modify it until it doesn't.
73 while (is_file("$docdir/$filebase$ffmod$ffsuff")) {
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);
83 $info_msg .= "tiff2pdf returned $tmp2: $tmp0 ";
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" .
93 "'$newid', 'file_url', '$fsize', NOW(), 'file://$target', " .
94 "'application/pdf', $patient_id " .
97 $query = "INSERT INTO categories_to_documents ( " .
98 "category_id, document_id" .
100 "'$catid', '$newid' " .
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.
145 $tmpfn1 = tempnam("/tmp", "fax1");
146 $tmpfn2 = tempnam("/tmp", "fax2");
147 $tmph = fopen($tmpfn1, "w");
149 $fh = fopen("$webserver_root/custom/faxcover.txt", 'r');
150 while (!feof($fh)) $cpstring .= fread($fh, 8192);
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);
159 $tmp0 = exec("cd $webserver_root/custom; " . $GLOBALS['hylafax_enscript'] .
160 " -o $tmpfn2 $tmpfn1", $tmp1, $tmp2);
162 $info_msg .= "enscript returned $tmp2: $tmp0 ";
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'",
172 $info_msg .= "sendfax returned $tmp2: $tmp0 ";
176 $action_taken = true;
179 if ($_POST['form_cb_delete'] && !$info_msg) {
180 // Delete the tiff file.
184 if (is_dir($faxcache)) {
185 $dh = opendir($faxcache);
186 while (($tmp = readdir($dh)) !== false) {
187 if (is_file("$faxcache/$tmp")) unlink("$faxcache/$tmp");
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";
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");
230 <title
>Dispatch Received Document
</title
>
231 <link rel
=stylesheet href
='<? echo $css_header ?>' type
='text/css'>
235 body
, td
, input
, select
, textarea
{
236 font
-family
: Arial
, Helvetica
, sans
-serif
;
241 padding
: 0.2em
1em
1em
1em
;
251 border
-color
: #0000ff;
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
;
269 divstyle
.display
= 'block';
271 divstyle
.display
= 'none';
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!');
299 if (f
.form_cb_forward
.checked
) {
300 var s
= f
.form_fax
.value
;
302 alert('A fax number is required!');
306 for (var i
= 0; i
< s
.length
; ++i
) {
308 if (c
>= '0' && c
<= '9') {
311 else if (digcount
== 0 || c
!= '-') {
312 alert('Invalid character(s) in fax number!');
317 if (s
.charAt(0) < '2') {
318 alert('Local phone number starts with an invalid digit!');
322 else if (digcount
== 11) {
323 if (s
.charAt(0) != '1') {
324 alert('11-digit number must begin with 1!');
328 else if (digcount
== 10) {
329 if (s
.charAt(0) < '2') {
330 alert('10-digit number starts with an invalid digit!');
333 f
.form_fax
.value
= '1' + s
;
336 alert('Invalid number of digits in fax telephone number!');
341 if (f
.form_cb_copy
.checked || f
.form_cb_forward
.checked
) {
343 for (var i
= 0; i
< f
.elements
.length
; ++i
) {
344 if (f
.elements
[i
].name
== 'form_images[]' && f
.elements
[i
].checked
)
347 if (check_count
== 0) {
348 alert('No pages have been selected!');
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;'>
374 <td
class='itemtitle' width
='1%' nowrap
>Patient
</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' />
383 <td
class='itemtitle' nowrap
>Category
</td
>
385 <select name
='form_category' style
='width:100%'>
387 foreach ($categories as $catkey => $catname) {
388 echo " <option value='$catkey'";
389 echo ">$catname</option>\n";
396 <td
class='itemtitle' nowrap
>Filename
</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' />
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;'>
411 <td
class='itemtitle' width
='1%' nowrap
>Type
</td
>
413 <select name
='form_note_type' style
='width:100%'>
415 foreach ($patient_note_types as $value) {
416 echo " <option value='$value'";
417 echo ">$value</option>\n";
424 <td
class='itemtitle' width
='1%' nowrap
>To
</td
>
426 <select name
='form_note_to' style
='width:100%'>
428 while ($urow = sqlFetchArray($ures)) {
429 echo " <option value='" . $urow['username'] . "'";
430 echo ">" . $urow['lname'];
431 if ($urow['fname']) echo ", " . $urow['fname'];
435 <option value
=''>** <?
xl('Close','e'); ?
> **</option
>
440 <td
class='itemtitle' nowrap
>Message
</td
>
442 <textarea name
='form_note_message' rows
='3' cols
='30' style
='width:100%'
443 title
='Your comments' /></textarea
>
447 </div
><!-- end div_note
-->
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;'>
460 <td
class='itemtitle' width
='1%' nowrap
>From
</td
>
462 <input type
='text' size
='10' name
='form_from' style
='width:100%'
463 title
='Type your name here' />
467 <td
class='itemtitle' nowrap
>To
</td
>
469 <input type
='text' size
='10' name
='form_to' style
='width:100%'
470 title
='Type the recipient name here' />
474 <td
class='itemtitle' nowrap
>Fax
</td
>
476 <input type
='text' size
='10' name
='form_fax' style
='width:100%'
477 title
='The fax phone number to send this to' />
481 <td
class='itemtitle' nowrap
>Message
</td
>
483 <textarea name
='form_message' rows
='3' cols
='30' style
='width:100%'
484 title
='Your comments to include with this message' /></textarea
>
488 <td
class='itemtitle' nowrap
>Quality
</td
>
490 <input type
='radio' name
='form_finemode' value
='' />Normal
 
;
491 <input type
='radio' name
='form_finemode' value
='1' checked
/>Fine
 
;
495 </div
><!-- end div_forward
-->
497 <p
><input type
='checkbox' name
='form_cb_delete' value
='1' />
498 <b
>Delete Fax from Queue
</b
></p
>
502 <input type
='submit' name
='form_save' value
='OK' />
504 <input type
='button' value
='Cancel' onclick
='window.close()' />
507 <p
><br
/><b
>Please select the desired pages to copy
or forward
:</b
></p
>
511 $dh = opendir($faxcache);
512 if (! $dh) die("Cannot read $faxcache");
514 while (false !== ($jfname = readdir($dh))) {
515 if (preg_match("/^(.*)\.jpg/", $jfname, $matches)) {
517 $jfnamebase = $matches[1];
519 echo " <td valign='top'>\n";
520 echo " <img src='../../faxcache/$filebase/$jfname' />\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";