workaround for fopen() bug in some versions of php
[openemr.git] / controllers / C_Prescription.class.php
blob145af72277b616a3bfba91a39a222a860b39c29a
1 <?php
3 require_once ($GLOBALS['fileroot'] . "/library/classes/Controller.class.php");
4 require_once($GLOBALS['fileroot'] ."/library/classes/Prescription.class.php");
5 require_once($GLOBALS['fileroot'] ."/library/classes/Provider.class.php");
6 require_once($GLOBALS['fileroot'] ."/library/classes/RXList.class.php");
8 class C_Prescription extends Controller {
10 var $template_mod;
11 var $pconfig;
13 function C_Prescription($template_mod = "general") {
14 parent::Controller();
15 $this->template_mod = $template_mod;
16 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
17 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
18 $this->assign("STYLE", $GLOBALS['style']);
19 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
22 function default_action() {
23 $this->assign("prescription",$this->prescriptions[0]);
24 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
27 function edit_action($id = "",$patient_id="",$p_obj = null) {
29 if ($p_obj != null && get_class($p_obj) == "prescription") {
30 $this->prescriptions[0] = $p_obj;
32 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
33 $this->prescriptions[0] = new Prescription($id);
36 if (!empty($patient_id)) {
37 $this->prescriptions[0]->set_patient_id($patient_id);
39 $this->default_action();
42 function list_action($id,$sort = "") {
43 if (empty($id)) {
44 $this->function_argument_error();
45 exit;
47 if (!empty($sort)) {
48 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
50 else {
51 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
53 //print_r(Prescription::prescriptions_factory($id));
54 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
57 function block_action($id,$sort = "") {
58 if (empty($id)) {
59 $this->function_argument_error();
60 exit;
62 if (!empty($sort)) {
63 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
65 else {
66 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
68 //print_r(Prescription::prescriptions_factory($id));
69 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
72 function lookup_action() {
73 $this->do_lookup();
74 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
77 function edit_action_process() {
78 if ($_POST['process'] != "true")
79 return;
80 //print_r($_POST);
82 $this->prescriptions[0] = new Prescription($_POST['id']);
83 parent::populate_object($this->prescriptions[0]);
84 //echo $this->prescriptions[0]->toString(true);
85 $this->prescriptions[0]->persist();
86 $_POST['process'] = "";
87 return $this->send_action($this->prescriptions[0]->id);
90 function send_action($id) {
91 $_POST['process'] = "true";
92 if(empty($id)) {
93 $this->function_argument_error();
96 $this->assign("prescription",new Prescription($id));
97 $this->_state = false;
98 return $this->fetch($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_send.html");
101 function send_action_process($id) {
102 $dummy = ""; // Added by Rod to avoid run-time warnings
103 if ($_POST['process'] != "true")
104 return;
105 if(empty($id)) {
106 $this->function_argument_error();
108 $p = new Prescription($id);
109 switch ($_POST['submit']) {
111 case "Print":
112 // The following statement added by Rod.
113 // Looking at Controller.class.php, it appears that _state is set to false
114 // to indicate that no further HTML is to be generated.
115 $this->_state = false; // Added by Rod - see Controller.class.php
116 return $this->_print_prescription($p, $dummy);
117 break;
118 case "Email":
119 return $this->_email_prescription($p,$_POST['email_to']);
120 break;
121 case "Fax":
122 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
123 //$this->assign("process_result","No fax server is currently setup.");
124 return $this->_fax_prescription($p,$_POST['fax_to']);
125 break;
126 case "Auto Send":
127 $pharmacy_id = $_POST['pharmacy_id'];
128 //echo "auto sending to : " . $_POST['pharmacy_id'];
129 $phar = new Pharmacy($_POST['pharmacy_id']);
130 //print_r($phar);
131 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
132 return $this->_print_prescription($p, $dummy);
134 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
135 $email = $phar->get_email();
136 if (!empty($email)) {
137 return $this->_email_prescription($p,$phar->get_email());
139 //else print it
141 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
142 $faxNum= $phar->get_fax();
143 if(!empty($faxNum)) {
144 Return $this->_fax_prescription ($p,$faxNum);
146 // return $this->assign("process_result","No fax server is currently setup.");
147 // else default is printing,
149 //the pharmacy has no default or default is print
150 return $this->_print_prescription($p, $dummy);
153 return;
157 function _print_prescription($p, & $toFile) {
158 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
159 $pdf =& new Cezpdf("LETTER");
160 $pdf->ezSetMargins(80,30,90,30);
161 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
163 if(!empty($this->pconfig['logo'])) {
164 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
166 $pdf->ezText($p->get_prescription_display(),10);
167 if(!empty($this->pconfig['signature'])) {
168 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
170 if(!empty($toFile))
172 $toFile = $pdf->ezOutput();
174 else
176 $pdf->ezStream();
177 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
179 return;
182 function _email_prescription($p,$email) {
183 if (empty($email)) {
184 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
185 return;
187 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
188 $mail = new PHPMailer();
189 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
190 //this is a temporary config item until the rest of the per practice billing settings make their way in
191 $mail->From = $GLOBALS['practice_return_email_path'];
192 $mail->FromName = $p->provider->get_name_display();
193 $mail->isMail();
194 $mail->Host = "localhost";
195 $mail->Mailer = "mail";
196 $text_body = $p->get_prescription_display();
197 $mail->Body = $text_body;
198 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
199 $mail->AddAddress($email);
200 if($mail->Send()) {
201 $this->assign("process_result","Email was successfully sent to: " . $email);
202 return;
204 else {
205 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
206 return;
210 function do_lookup() {
211 if ($_POST['process'] != "true")
212 return;
213 $list = array();
214 if (!empty($_POST['drug'])) {
215 $list = @RxList::get_list($_POST['drug']);
217 if (is_array($list)) {
218 $list = array_flip($list);
219 $this->assign("drug_options",$list);
220 $this->assign("drug_values",array_keys($list));
222 else {
223 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
225 //print_r($_POST);
226 //$this->assign("PROCESS","");
228 $_POST['process'] = "";
231 function _fax_prescription($p,$faxNum)
233 $err = "Sent fax";
234 //strip - ,(, ), and ws
235 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
236 //validate the number
238 if(!empty($faxNum) && is_numeric($faxNum))
240 //get the sendfax command and execute it
241 $cmd = $this->pconfig['sendfax'];
242 // prepend any prefix to the fax number
243 $pref=$this->pconfig['prefix'];
244 $faxNum=$pref.$faxNum;
245 if(empty($cmd))
247 $err .= " Send fax not set in includes/config.php";
248 break;
250 else
252 //generate file to fax
253 $faxFile = "Failed";
254 $this->_print_prescription($p, $faxFile);
255 if(empty($faxFile))
257 $err .= " _print_prescription returned empty file";
258 break;
260 $fileName = dirname(__FILE__)."/../documents/".$p->get_id()
261 .$p->get_patient_id()."_fax_.pdf";
262 //print "filename is $fileName";
263 touch($fileName); // php bug
264 $handle = fopen($fileName,"w");
265 if(!$handle)
267 $err .= " Failed to open file $fileName to write fax to";
268 break;
270 if(fwrite($handle, $faxFile) === false)
272 $err .= " Failed to write data to $fileName";
273 break;
275 fclose($handle);
276 $args = " -n -d $faxNum $fileName";
277 //print "command is $cmd $args<br>";
278 exec($cmd . $args);
282 else
284 $err = "bad fax number passed to function";
286 if($err)
288 $this->assign("process_result",$err);