Eye module improvements and full support for innodb (#1776)
[openemr.git] / interface / forms / eye_mag / save.php
blob1bf629a71109bd984621dafe0a12588f6f2fc261
1 <?php
2 /**
3 * forms/eye_mag/save.php
5 * This saves the submitted data.
6 * Forms: new and updates
7 * User preferences for displaying the form as the user desires.
8 * Each time a form is used, layout choices auto-change preferences.
9 * Retrieves old records so the user can flip through old values within this form,
10 * ideally with the intent that the old data can be carried forward.
11 * Yeah, gotta write that carry forward stuff yet. Next week it'll be done?
12 * HTML5 Canvas images the user draws.
13 * For now we have one image per section
14 * I envision a user definable image they can upload to draw on and name such as
15 * A face image to draw injectable location/dosage for fillers or botulinum toxins.
16 * Ideally this concept when it comes to fruition will serve as a basis for any specialty image form
17 * to be used. Upload image, drop widget and save it...
19 * Copyright (C) 2016 Raymond Magauran <magauran@MedFetch.com>
21 * LICENSE: This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU Affero General Public License as
23 * published by the Free Software Foundation, either version 3 of the
24 * License, or (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU Affero General Public License for more details.
31 * You should have received a copy of the GNU Affero General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 * @package OpenEMR
35 * @author Ray Magauran <magauran@MedFetch.com>
36 * @link http://www.open-emr.org
40 $table_name = "form_eye_mag";
41 $form_name = "eye_mag";
42 $form_folder = "eye_mag";
45 require_once("../../globals.php");
47 require_once("$srcdir/html2pdf/vendor/autoload.php");
48 require_once("$srcdir/api.inc");
49 require_once("$srcdir/forms.inc");
50 require_once("php/" . $form_name . "_functions.php");
51 require_once($srcdir . "/../controllers/C_Document.class.php");
52 require_once($srcdir . "/documents.php");
53 require_once("$srcdir/patient.inc");
54 require_once("$srcdir/options.inc.php");
55 require_once("$srcdir/acl.inc");
56 require_once("$srcdir/lists.inc");
57 require_once("$srcdir/report.inc");
58 require_once("$srcdir/html2pdf/html2pdf.class.php");
60 use Mpdf\Mpdf;
62 $returnurl = 'encounter_top.php';
64 if (isset($_REQUEST['id'])) {
65 $id = $_REQUEST['id'];
68 if (!$id) {
69 $id = $_REQUEST['pid'];
72 $encounter = $_REQUEST['encounter'];
74 $AJAX_PREFS = $_REQUEST['AJAX_PREFS'];
75 if ($encounter == "" && !$id && !$AJAX_PREFS && (($_REQUEST['mode'] != "retrieve") or ($_REQUEST['mode'] == "show_PDF"))) {
76 echo "Sorry Charlie..."; //should lead to a database of errors for explanation.
77 exit;
80 /**
81 * Save/update the preferences
83 if ($_REQUEST['AJAX_PREFS']) {
84 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
85 VALUES
86 ('PREFS','VA','Vision',?,'RS','51',?,'1')";
87 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_VA']));
89 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
90 VALUES
91 ('PREFS','W','Current Rx',?,'W','52',?,'2')";
92 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_W']));
94 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
95 VALUES
96 ('PREFS','W_width','Detailed Rx',?,'W_width','80',?,'100')";
97 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_W_width']));
99 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
100 VALUES
101 ('PREFS','MR','Manifest Refraction',?,'MR','53',?,'3')";
102 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_MR']));
104 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
105 VALUES
106 ('PREFS','MR_width','Detailed MR',?,'MR_width','81',?,'110')";
107 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_W_width']));
109 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
110 VALUES
111 ('PREFS','CR','Cycloplegic Refraction',?,'CR','54',?,'4')";
112 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_CR']));
114 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
115 VALUES
116 ('PREFS','CTL','Contact Lens',?,'CTL','55',?,'5')";
117 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_CTL']));
119 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
120 VALUES
121 ('PREFS', 'VAX', 'Visual Acuities', ?, 'VAX','65', ?,'15')";
122 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_VAX']));
124 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
125 VALUES
126 ('PREFS', 'RXHX', 'Prior Refractions', ?, 'RXHX','65', ?,'115')";
127 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_RXHX']));
129 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
130 VALUES
131 ('PREFS','ADDITIONAL','Additional Data Points',?,'ADDITIONAL','56',?,'6')";
132 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_ADDITIONAL']));
134 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
135 VALUES
136 ('PREFS','CLINICAL','CLINICAL',?,'CLINICAL','57',?,'7')";
137 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_CLINICAL']));
139 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
140 VALUES
141 ('PREFS','IOP','Intraocular Pressure',?,'IOP','67',?,'17')";
142 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_IOP']));
144 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
145 VALUES
146 ('PREFS','EXAM','EXAM',?,'EXAM','58',?,'8')";
147 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_EXAM']));
149 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
150 VALUES
151 ('PREFS','CYLINDER','CYL',?,'CYL','59',?,'9')";
152 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_CYL']));
154 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
155 VALUES
156 ('PREFS','HPI_VIEW','HPI View',?,'HPI_VIEW','60',?,'10')";
157 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_HPI_VIEW']));
159 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
160 VALUES
161 ('PREFS','EXT_VIEW','External View',?,'EXT_VIEW','66',?,'16')";
162 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_EXT_VIEW']));
164 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
165 VALUES
166 ('PREFS','ANTSEG_VIEW','Anterior Segment View',?,'ANTSEG_VIEW','61',?,'11')";
167 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_ANTSEG_VIEW']));
169 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
170 VALUES
171 ('PREFS','RETINA_VIEW','Retina View',?,'RETINA_VIEW','62',?,'12')";
172 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_RETINA_VIEW']));
174 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
175 VALUES
176 ('PREFS','NEURO_VIEW','Neuro View',?,'NEURO_VIEW','63',?,'13')";
177 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_NEURO_VIEW']));
179 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
180 VALUES
181 ('PREFS','ACT_VIEW','ACT View',?,'ACT_VIEW','64',?,'14')";
182 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_ACT_VIEW']));
184 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
185 VALUES
186 ('PREFS','ACT_SHOW','ACT Show',?,'ACT_SHOW','65',?,'15')";
187 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_ACT_SHOW']));
189 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
190 VALUES
191 ('PREFS','HPI_RIGHT','HPI DRAW',?,'HPI_RIGHT','70',?,'16')";
192 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_HPI_RIGHT']));
194 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
195 VALUES
196 ('PREFS','PMH_RIGHT','PMH DRAW',?,'PMH_RIGHT','71',?,'17')";
197 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_PMH_RIGHT']));
198 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
199 VALUES
200 ('PREFS','EXT_RIGHT','EXT DRAW',?,'EXT_RIGHT','72',?,'18')";
201 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_EXT_RIGHT']));
202 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
203 VALUES
204 ('PREFS','ANTSEG_RIGHT','ANTSEG DRAW',?,'ANTSEG_RIGHT','73',?,'19')";
205 $result = sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_ANTSEG_RIGHT']));
207 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
208 VALUES
209 ('PREFS','RETINA_RIGHT','RETINA DRAW',?,'RETINA_RIGHT','74',?,'20')";
210 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_RETINA_RIGHT']));
211 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
212 VALUES
213 ('PREFS','NEURO_RIGHT','NEURO DRAW',?,'NEURO_RIGHT','75',?,'21')";
214 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_NEURO_RIGHT']));
215 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
216 VALUES
217 ('PREFS','IMPPLAN_RIGHT','IMPPLAN DRAW',?,'IMPPLAN_RIGHT','76',?,'22')";
218 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_IMPPLAN_RIGHT']));
219 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
220 VALUES
221 ('PREFS','PANEL_RIGHT','PMSFH Panel',?,'PANEL_RIGHT','77',?,'23')";
222 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_PANEL_RIGHT']));
223 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
224 VALUES
225 ('PREFS','KB_VIEW','KeyBoard View',?,'KB_VIEW','78',?,'24')";
226 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_KB']));
227 $query = "REPLACE INTO " . $table_name . "_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
228 VALUES
229 ('PREFS','TOOLTIPS','Toggle Tooltips',?,'TOOLTIPS','79',?,'25')";
230 sqlQuery($query, array($_SESSION['authId'], $_REQUEST['PREFS_TOOLTIPS']));
234 * ADD ANY NEW PREFERENCES above, and as a hidden field in the body.
237 /** <!-- End Preferences --> **/
240 * Create, update or retrieve a form and its values
242 if (!$pid) {
243 $pid = $_SESSION['pid'];
246 $userauthorized = $_SESSION['userauthorized'];
247 if ($encounter == "") {
248 $encounter = date("Ymd");
251 $form_id = $_REQUEST['form_id'];
252 $zone = $_REQUEST['zone'];
254 $providerID = findProvider($pid, $encounter);
255 if ($providerID == '0') {
256 $providerID = $userauthorized;//who is the default provider?
259 $providerNAME = getProviderName($providerID);
261 // The form is submitted to be updated or saved in some way.
262 // Give each instance of a form a uniqueID. If the form has no owner, update DB with this uniqueID.
263 // If the DB shows a uniqueID ie. an owner, and the save request uniqueID does not = the uniqueID in the DB,
264 // ask if the new user wishes to take ownership?
265 // If yes, any other's attempt to save fields/form are denied and the return code says you are not the owner...
266 if ($_REQUEST['unlock'] == '1') {
267 // we are releasing the form, by closing the page or clicking on ACTIVE FORM, so unlock it.
268 // if it's locked and they own it ($REQUEST[LOCKEDBY] == LOCKEDBY), they can unlock it
269 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from form_eye_locking WHERE ID=?";
270 $lock = sqlQuery($query, array($form_id));
271 if (($lock['LOCKED'] > '') ) { //&& ($_REQUEST['LOCKEDBY'] == $lock['LOCKEDBY'])) {
272 $query = "update form_eye_locking set LOCKED='',LOCKEDBY='' where id=?";
273 sqlQuery($query, array($form_id));
276 exit;
277 } elseif ($_REQUEST['acquire_lock'] == "1") {
278 //we are taking over the form's active state, others will go read-only
279 $query = "UPDATE form_eye_locking set LOCKED='1',LOCKEDBY=?,LOCKEDDATE=NOW() where id=?";//" and LOCKEDBY=?";
280 $result = sqlQuery($query, array($_REQUEST['uniqueID'], $form_id ));
281 exit;
282 } else {
283 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from form_eye_locking WHERE ID=?";
284 $lock = sqlQuery($query, array($form_id));
285 if (($lock['LOCKED']) && ($_REQUEST['uniqueID'] != $lock['LOCKEDBY'])) {
286 // This session not the owner or it is not new so it is locked
287 // Did the user send a demand to take ownership?
288 if ($lock['LOCKEDBY'] != $_REQUEST['ownership']) {
289 //tell them they are locked out by another user now
290 echo "Code 400";
291 // or return a JSON encoded string with current LOCK ID?
292 // echo "Sorry Charlie, you get nothing since this is locked... No save for you!";
293 exit;
294 } elseif ($lock['LOCKEDBY'] == $_REQUEST['ownership']) {
295 // then they are taking ownership - all others get locked...
296 // new LOCKEDBY becomes our uniqueID LOCKEDBY
297 $_REQUEST['LOCKED'] = '1';
298 $_REQUEST['LOCKEDBY'] = $_REQUEST['uniqueID'];
299 //update table
300 $query = "update form_eye_locking set LOCKED=?,LOCKEDBY=? where id=?";
301 sqlQuery($query, array('1', $_REQUEST['LOCKEDBY'], $form_id));
302 //go on to save what we want...
304 } elseif (!$lock['LOCKED']) { // it is not locked yet
305 $_REQUEST['LOCKED'] = '1';
306 $query = "update form_eye_locking set LOCKED=?,LOCKEDBY=?,LOCKEDDATE=NOW() where id=?";
307 sqlQuery($query, array('1', $_REQUEST['LOCKEDBY'], $form_id));
308 //go on to save what we want...
311 if (!$_REQUEST['LOCKEDBY']) {
312 $_REQUEST['LOCKEDBY'] = rand();
316 if ($_REQUEST["mode"] == "new") {
317 $base_array = array();
318 $newid = formSubmit('form_eye_base', '', $id, $userauthorized);
320 addForm($encounter, $form_name, $newid, $form_folder, $pid, $userauthorized);
321 //we need to poulate all the rest of $tables with an $newid and blank values...
322 $tables = array('form_eye_hpi','form_eye_ros','form_eye_vitals',
323 'form_eye_acuity','form_eye_refraction','form_eye_biometrics',
324 'form_eye_external', 'form_eye_antseg','form_eye_postseg',
325 'form_eye_neuro','form_eye_locking');
327 foreach ($tables as $table_name) {
328 $query = "INSERT INTO " . $table_name . " ('id','pid') VALUES (?,?)";
329 $result = sqlStatement($query, array($new_id,$pid));
332 } elseif ($_REQUEST["mode"] == "update") {
333 // The user has write privileges to work with...
335 if ($_REQUEST['action'] == "store_PDF") {
337 * We want to store/overwrite the current PDF version of this encounter's f
338 * Currently this is only called 'beforeunload', ie. when you finish the form
339 * In this current paradigm, anytime the form is opened, then closed, the PDF
340 * is overwritten. With esign implemented, the PDF should be locked. I suppose
341 * with esign the form can't even be opened so the only way to get to the PDF
342 * is through the Documents->Encounters links.
344 $query = "select id from categories where name = 'Encounters'";
345 $result = sqlStatement($query);
346 $ID = sqlFetchArray($result);
347 $category_id = $ID['id'];
348 $PDF_OUTPUT = '1';
350 $filename = $pid . "_" . $encounter . ".pdf";
351 $filepath = $GLOBALS['oer_config']['documents']['repository'] . $pid;
352 foreach (glob($filepath . '/' . $filename) as $file) {
353 unlink($file);
356 $sql = "DELETE from categories_to_documents where document_id IN (SELECT id from documents where documents.url like '%" . $filename . "')";
357 sqlQuery($sql);
358 $sql = "DELETE from documents where documents.url like '%" . $filename . "'";
359 sqlQuery($sql);
360 // We want to overwrite so only one PDF is stored per form/encounter
361 // $pdf = new HTML2PDF('P', 'Letter', 'en', array(5, 5, 5, 5) ); // add a little margin 5cm all around TODO: add to globals
363 /***********/
365 /*$pdf = new HTML2PDF(
366 $GLOBALS['pdf_layout'],
367 $GLOBALS['pdf_size'],
368 $GLOBALS['pdf_language'],
369 true, // default unicode setting is true
370 'UTF-8', // default encoding setting is UTF-8
371 array($GLOBALS['pdf_left_margin'],$GLOBALS['pdf_top_margin'],$GLOBALS['pdf_right_margin'],$GLOBALS['pdf_bottom_margin']),
372 $_SESSION['language_direction'] == 'rtl' ? true : false
373 );*/
374 $config_mpdf = array(
375 'tempDir' => $GLOBALS['MPDF_WRITE_DIR'],
376 'mode' => $GLOBALS['pdf_language'],
377 'format' => $GLOBALS['pdf_size'],
378 'default_font_size' => '9',
379 'default_font' => '',
380 'margin_left' => $GLOBALS['pdf_left_margin'],
381 'margin_right' => $GLOBALS['pdf_right_margin'],
382 'margin_top' => $GLOBALS['pdf_top_margin'],
383 'margin_bottom' => $GLOBALS['pdf_bottom_margin'],
384 'margin_header' => '',
385 'margin_footer' => '',
386 'orientation' => $GLOBALS['pdf_layout'],
387 'shrink_tables_to_fit' => 1,
388 'use_kwt' => true,
389 'keep_table_proportions' => true
391 $pdf = new mPDF($config_mpdf);
392 if ($_SESSION['language_direction'] == 'rtl') {
393 $pdf->SetDirectionality('rtl');
395 ob_start();
397 <link rel="stylesheet" href="<?php echo $webserver_root; ?>/interface/themes/style_pdf.css" type="text/css">
398 <div id="report_custom" style="width:100%;"> <!-- large outer DIV -->
399 <?php
400 echo report_header($pid);
401 include_once($GLOBALS['incdir'] . "/forms/eye_mag/report.php");
402 call_user_func($form_name . "_report", $pid, $form_encounter, $N, $form_id);
403 if ($printable) {
404 echo "" . xl('Signature') . ": _______________________________<br />";
407 </div> <!-- end of report_custom DIV -->
409 <?php
411 global $web_root, $webserver_root;
412 $content = ob_get_clean();
413 // Fix a nasty html2pdf bug - it ignores document root!
414 $i = 0;
415 $wrlen = strlen($web_root);
416 $wsrlen = strlen($webserver_root);
417 while (true) {
418 $i = stripos($content, " src='/", $i + 1);
419 if ($i === false) {
420 break;
423 if (substr($content, $i+6, $wrlen) === $web_root &&
424 substr($content, $i+6, $wsrlen) !== $webserver_root) {
425 $content = substr($content, 0, $i + 6) . $webserver_root . substr($content, $i + 6 + $wrlen);
428 // Below is for including style sheet for report specific styles. Left here for future use.
429 //$styles = file_get_contents('../css/report.css');
430 //$pdf->writeHTML($styles, 1);
431 //$pdf->writeHTML($content, 2);
433 $pdf->writeHTML($content, false); // false or zero works for both mPDF and HTML2PDF
434 $tmpdir = $GLOBALS['OE_SITE_DIR'] . '/documents/temp/'; // Best to get a known system temp directory to ensure a writable directory.
435 $temp_filename = $tmpdir . $filename;
436 $content_pdf = $pdf->Output($temp_filename, 'F');
437 $type = "application/pdf";
438 $size = filesize($temp_filename);
439 $return = addNewDocument($filename, $type, $temp_filename, 0, $size, $_SESSION['authUserID'], $pid, $category_id);
440 $doc_id = $return['doc_id'];
441 $sql = "UPDATE documents set encounter_id=? where id=?"; //link it to this encounter
442 sqlQuery($sql, array($encounter, $doc_id));
444 unlink($temp_filename);
446 exit();
449 // Store the IMPPLAN area. This is separate from the rest of the form
450 // It is in a separate table due to its one-to-many relationship with the form_id.
451 if ($_REQUEST['action'] == "store_IMPPLAN") {
452 $IMPPLAN = json_decode($_REQUEST['parameter'], true);
453 //remove what is there and replace it with this data.
454 $query = "DELETE from form_" . $form_folder . "_impplan where form_id=? and pid=?";
455 sqlQuery($query, array($form_id, $pid));
457 for ($i = 0; $i < count($IMPPLAN); $i++) {
458 $query = "INSERT IGNORE INTO form_" . $form_folder . "_impplan (form_id, pid, title, code, codetype, codedesc, codetext, plan, IMPPLAN_order, PMSFH_link) VALUES(?,?,?,?,?,?,?,?,?,?) ";
459 $response = sqlQuery($query, array($form_id, $pid, $IMPPLAN[$i]['title'], $IMPPLAN[$i]['code'], $IMPPLAN[$i]['codetype'], $IMPPLAN[$i]['codedesc'], $IMPPLAN[$i]['codetext'], $IMPPLAN[$i]['plan'], $i, $IMPPLAN[$i]['PMSFH_link']));
460 //if it is a duplicate then delete this from the array and return the array via json.
461 //or rebuild it from mysql
464 //Since we are potentially ignoring duplicates, build json IMPPLAN_items and return it to the user to rebuild IMP/Plan area
465 $IMPPLAN_items = build_IMPPLAN_items($pid, $form_id);
466 echo json_encode($IMPPLAN_items);
467 exit;
470 //change PCP/referring doc
471 if ($_REQUEST['action'] == 'docs') {
472 $query = "update patient_data set providerID=?,ref_providerID=? where pid =?";
473 sqlQuery($query, array($_REQUEST['pcp'], $_REQUEST['rDOC'], $pid));
475 if ($_REQUEST['pcp']) {
476 //return PCP's data to end user to update their form
477 $query = "SELECT * FROM users WHERE id =?";
478 $DOC1 = sqlQuery($query, array($_REQUEST['pcp']));
479 $DOCS['pcp']['name'] = $DOC1['fname'] . " " . $DOC1['lname'];
480 if ($DOC1['suffix']) {
481 $DOCS['pcp']['name'] .= ", " . $DOC1['suffix'];
483 $DOCS['pcp']['address'] = $DOC1['organization'] . "<br />" . $DOC1['street'] . "<br />" . $DOC1['city'] . ", " . $DOC1['state'] . " " . $DOC1['zip'] . "<br />";
484 $DOCS['pcp']['fax'] = $DOC1['fax'];
485 $DOCS['pcp']['phone'] = $DOC1['phonew1'];
487 // does the fax already exist?
488 $query = "SELECT * FROM form_taskman WHERE TO_ID=? AND PATIENT_ID=? AND ENC_ID=?";
489 $FAX_PCP = sqlQuery($query, array($_REQUEST['pcp'], $pid, $encounter));
490 if ($FAX_PCP['ID']) { //it is here already, make them print and manually fax it. Show icon
491 $DOCS['pcp']['fax_info'] = "&nbsp;&nbsp;
492 <span id='status_Fax_pcp'>
493 <a href='" . $webroot . "/controller.php?document&view&patient_id=" . $pid . "&doc_id=" . $FAX_PCP['DOC_ID'] . "'
494 target='_blank' title='" . xla('View the Summary Report sent via Fax Server on') . " " . $FAX_PCP['COMPLETED_DATE'] . ".'>
495 <i class='fa fa-file-pdf-o fa-fw'></i>
496 </a>
497 <i class='fa fa-repeat fa-fw' onclick=\"top . restoreSession(); create_task('" . attr($_REQUEST['pcp']) . "','Fax-resend','ref'); return false;\"></i>
498 </span>";
499 } else {
500 $DOCS['pcp']['fax_info'] = '
501 <a href="#" onclick="top.restoreSession(); create_task(\'' . attr($_REQUEST['pcp']) . '\',\'Fax\',\'pcp\'); return false;">
502 ' . text($DOC1['fax']) . '&nbsp;&nbsp;
503 <span id="status_Fax_pcp"><i class="fa fa-fax fa-fw"></i></span>
504 </a>';
508 if ($_REQUEST['rDOC']) {
509 //return referring Doc's data to end user to update their form
510 $query = "SELECT * FROM users WHERE id =?";
511 $DOC2 = sqlQuery($query, array($_REQUEST['rDOC']));
512 $DOCS['ref']['name'] = $DOC2['fname'] . " " . $DOC2['lname'];
513 if ($DOC2['suffix']) {
514 $DOCS['ref']['name'] .= ", " . $DOC2['suffix'];
516 if ($DOCS['ref']['address'] > '') {
517 $DOCS['ref']['address'] = $DOC2['organization'] . "<br />";
519 $DOCS['ref']['address'] .= $DOC2['street'] . "<br />" . $DOC2['city'] . ", " . $DOC2['state'] . " " . $DOC2['zip'] . "<br />";
520 $DOCS['ref']['fax'] = $DOC2['fax'];
521 $DOCS['ref']['phone'] = $DOC2['phonew1'];
523 // does the fax already exist?
524 $query = "SELECT * FROM form_taskman WHERE TO_ID=? AND PATIENT_ID=? AND ENC_ID=?";
525 $FAX_REF = sqlQuery($query, array($_REQUEST['rDOC'], $pid, $encounter));
526 if ($FAX_REF['ID'] > '') { //it is here already, make them print and manually fax it. Show icon
527 $DOCS['ref']['fax_info'] = text($DOC2['fax']) . "&nbsp;&nbsp;
528 <span id='status_Fax_ref'>
529 <a href='" . $webroot . "/controller.php?document&view&patient_id=" . $pid . "&doc_id=" . $FAX_REF['DOC_ID'] . "'
530 target='_blank' title='" . xla('View the Summary Report sent via Fax Server on') . " " . $FAX_REF['COMPLETED_DATE'] . ".'>
531 <i class='fa fa-file-pdf-o fa-fw'></i>
532 </a>
533 <i class='fa fa-repeat fa-fw' onclick=\"top . restoreSession(); create_task('" . attr($_REQUEST['rDOC']) . "','Fax-resend','ref'); return false;\"></i>
534 </span>";
535 } else {
536 $DOCS['ref']['fax_info'] = '
537 <a href="#" onclick="top.restoreSession(); create_task(\'' . attr($_REQUEST['rDOC']) . '\',\'Fax\',\'ref\'); return false;">
538 ' . text($DOC2['fax']) . '&nbsp;&nbsp;
539 <span id="status_Fax_ref"><i class="fa fa-fax fa-fw"></i></span>
540 </a>';
544 echo json_encode($DOCS);
545 exit;
548 /*** START CODE to DEAL WITH PMSFH/ISUUE_TYPES ****/
549 if ($_REQUEST['PMSFH_save'] == '1') {
550 if (!$PMSFH) {
551 $PMSFH = build_PMSFH($pid);
554 $issue = $_REQUEST['issue'];
555 $deletion = $_REQUEST['deletion'];
556 $form_save = $_REQUEST['form_save'];
557 $pid = $_SESSION['pid'];
558 $encounter = $_SESSION['encounter'];
559 $form_id = $_REQUEST['form_id'];
560 $form_type = $_REQUEST['form_type'];
561 $r_PMSFH = $_REQUEST['r_PMSFH'];
562 if ($deletion == 1) {
563 row_delete("issue_encounter", "list_id = '$issue'");
564 row_delete("lists", "id = '$issue'");
565 $PMSFH = build_PMSFH($pid);
566 send_json_values($PMSFH);
567 exit;
568 } else {
569 if ($form_type == 'ROS') { //ROS
570 $query = "UPDATE form_eye_ros set ROSGENERAL=?,ROSHEENT=?,ROSCV=?,ROSPULM=?,ROSGI=?,ROSGU=?,ROSDERM=?,ROSNEURO=?,ROSPSYCH=?,ROSMUSCULO=?,ROSIMMUNO=?,ROSENDOCRINE=?,ROSCOMMENTS=?.pid=? where id=?";
571 sqlStatement($query, array($_REQUEST['ROSGENERAL'], $_REQUEST['ROSHEENT'], $_REQUEST['ROSCV'], $_REQUEST['ROSPULM'], $_REQUEST['ROSGI'], $_REQUEST['ROSGU'], $_REQUEST['ROSDERM'], $_REQUEST['ROSNEURO'], $_REQUEST['ROSPSYCH'], $_REQUEST['ROSMUSCULO'], $_REQUEST['ROSIMMUNO'], $_REQUEST['ROSENDOCRINE'], $_REQUEST['ROSCOMMENTS'],$pid, $form_id));
572 $PMSFH = build_PMSFH($pid);
573 send_json_values($PMSFH);
574 exit;
575 } elseif ($form_type == 'SOCH') { //SocHx
576 $newdata = array();
577 $fres = sqlStatement("SELECT * FROM layout_options " .
578 "WHERE form_id = 'HIS' AND uor > 0 AND field_id != '' " .
579 "ORDER BY group_id, seq");
580 while ($frow = sqlFetchArray($fres)) {
581 $field_id = $frow['field_id'];
582 //get value only if field exist in $_POST (prevent deleting of field with disabled attribute)
583 if (isset($_POST["form_$field_id"])) {
584 $newdata[$field_id] = get_layout_form_value($frow);
587 //have to figure where to put comments in this next line for the rest of openemr
588 updateHistoryData($pid, $newdata);
589 if ($_REQUEST['marital_status'] > '') {
590 // have to match input with list_option for marital to not break openEMR
591 $query = "select * from list_options where list_id='marital'";
592 $fres = sqlStatement($query);
593 while ($frow = sqlFetchArray($fres)) {
594 if (($_REQUEST['marital_status'] == $frow['option_id']) || ($_REQUEST['marital_status'] == $frow['title'])) {
595 $status = $frow['option_id'];
596 $query = "UPDATE patient_data set status=? where pid=?";
597 sqlStatement($query, array($status, $pid));
602 if ($_REQUEST['occupation'] > '') {
603 $query = "UPDATE patient_data set occupation=? where pid=?";
604 sqlStatement($query, array($_REQUEST['occupation'], $pid));
607 $PMSFH = build_PMSFH($pid);
608 send_json_values($PMSFH);
609 exit;
610 } elseif ($form_type == 'FH') {
611 $query = "UPDATE history_data set
612 relatives_cancer=?,
613 relatives_diabetes=?,
614 relatives_high_blood_pressure=?,
615 relatives_heart_problems=?,
616 relatives_stroke=?,
617 relatives_epilepsy=?,
618 relatives_mental_illness=?,
619 relatives_suicide=?,
620 usertext11=?,
621 usertext12=?,
622 usertext13=?,
623 usertext14=?,
624 usertext15=?,
625 usertext16=?,
626 usertext17=?,
627 usertext18=? where pid=?";
628 $resFH = sqlStatement($query, array($_REQUEST['relatives_cancer'], $_REQUEST['relatives_diabetes'], $_REQUEST['relatives_high_blood_pressure'], $_REQUEST['relatives_heart_problems'], $_REQUEST['relatives_stroke'], $_REQUEST['relatives_epilepsy'], $_REQUEST['relatives_mental_illness'], $_REQUEST['relatives_suicide'], $_REQUEST['usertext11'], $_REQUEST['usertext12'], $_REQUEST['usertext13'], $_REQUEST['usertext14'], $_REQUEST['usertext15'], $_REQUEST['usertext16'], $_REQUEST['usertext17'], $_REQUEST['usertext18'], $pid));
629 $PMSFH = build_PMSFH($pid);
630 send_json_values($PMSFH);
631 exit;
632 } else {
633 if ($_REQUEST['form_title'] == '') {
634 return;
637 $subtype = '';
638 if ($form_type == "POH") {
639 $form_type = "medical_problem";
640 $subtype = "eye";
641 } elseif ($form_type == "PMH") {
642 $form_type = "medical_problem";
643 } elseif ($form_type == "Allergy") {
644 $form_type = "allergy";
645 } elseif ($form_type == "Surgery") {
646 $form_type = "surgery";
647 } elseif ($form_type == "POS") {
648 $form_type = "surgery";
649 $subtype = "eye";
650 } elseif ($form_type == "Medication") {
651 $form_type = "medication";
652 if ($_REQUEST['form_eye_subtype']) {
653 $subtype = "eye";
654 //we always want a default begin date
655 //if it is empty, fill it with today
656 if ($_REQUEST['form_begin'] == '') {
657 $_REQUEST['form_begin'] = date("Y-m-d");
661 if ($_REQUEST['form_begin'] == '') {
662 $_REQUEST['form_begin'] = $visit_date;
666 $i = 0;
667 $form_begin = DateToYYYYMMDD($_REQUEST['form_begin']);
668 $form_end = DateToYYYYMMDD($_REQUEST['form_end']);
671 * When adding an issue, see if the issue is already here.
672 * If so we need to update it. If not we are adding it.
673 * Check the PMSFH array first by title.
674 * If not present in PMSFH, check the DB to be sure.
676 foreach ($PMSFH[$form_type] as $item) {
677 if ($item['title'] == $_REQUEST['form_title']) {
678 $issue = $item['issue'];
682 if (!$issue) {
683 if ($subtype == '') {
684 $query = "SELECT id,pid from lists where title=? and type=? and pid=?";
685 $issue2 = sqlQuery($query, array($_REQUEST['form_title'], $form_type, $pid));
686 $issue = $issue2['id'];
687 } else {
688 $query = "SELECT id,pid from lists where title=? and type=? and pid=? and subtype=?";
689 $issue2 = sqlQuery($query, array($_REQUEST['form_title'], $form_type, $pid, $subtype));
690 $issue = $issue2['id'];
694 $issue = 0 + $issue;
695 if ($_REQUEST['form_reinjury_id'] == "") {
696 $form_reinjury_id = "0";
699 if ($_REQUEST['form_injury_grade'] == "") {
700 $form_injury_grade = "0";
703 if ($_REQUEST['form_outcome'] == '') {
704 $_REQUEST['form_outcome'] = '0';
707 if ($issue != '0') { //if this issue already exists we are updating it...
708 $query = "UPDATE lists SET " .
709 "type = '" . add_escape_custom($form_type) . "', " .
710 "title = '" . add_escape_custom($_REQUEST['form_title']) . "', " .
711 "comments = '" . add_escape_custom($_REQUEST['form_comments']) . "', " .
712 "begdate = " . QuotedOrNull($form_begin) . ", " .
713 "enddate = " . QuotedOrNull($form_end) . ", " .
714 "returndate = " . QuotedOrNull($form_return) . ", " .
715 "diagnosis = '" . add_escape_custom($_REQUEST['form_diagnosis']) . "', " .
716 "occurrence = '" . add_escape_custom($_REQUEST['form_occur']) . "', " .
717 "classification = '" . add_escape_custom($_REQUEST['form_classification']) . "', " .
718 "reinjury_id = '" . add_escape_custom($_REQUEST['form_reinjury_id']) . "', " .
719 "referredby = '" . add_escape_custom($_REQUEST['form_referredby']) . "', " .
720 "injury_grade = '" . add_escape_custom($_REQUEST['form_injury_grade']) . "', " .
721 "injury_part = '" . add_escape_custom($form_injury_part) . "', " .
722 "injury_type = '" . add_escape_custom($form_injury_type) . "', " .
723 "outcome = '" . add_escape_custom($_REQUEST['form_outcome']) . "', " .
724 "destination = '" . add_escape_custom($_REQUEST['form_destination']) . "', " .
725 "reaction ='" . add_escape_custom($_REQUEST['form_reaction']) . "', " .
726 "erx_uploaded = '0', " .
727 "modifydate = NOW(), " .
728 "subtype = '" . $subtype . "' " .
729 "WHERE id = '" . add_escape_custom($issue) . "'";
730 sqlStatement($query);
731 if ($text_type == "medication" && enddate != '') {
732 sqlStatement('UPDATE prescriptions SET '
733 . 'medication = 0 where patient_id = ? '
734 . " and upper(trim(drug)) = ? "
735 . ' and medication = 1', array($pid, strtoupper($_REQUEST['form_title'])));
737 } else {
738 $query = "INSERT INTO lists ( " .
739 "date, pid, type, title, activity, comments, " .
740 "begdate, enddate, returndate, " .
741 "diagnosis, occurrence, classification, referredby, user, " .
742 "groupname, outcome, destination,reaction,subtype " .
743 ") VALUES ( " .
744 "NOW(), ?,?,?,1,?," .
745 QuotedOrNull($form_begin) . ", " . QuotedOrNull($form_end) . ", " . QuotedOrNull($form_return) . ", " .
746 "?,?,?,?,?," .
747 "?,?,?,?,?)";
748 $issue = sqlInsert($query, array($pid, $form_type, $_REQUEST['form_title'], $_REQUEST['form_comments'],
749 $_REQUEST['form_diagnosis'], $_REQUEST['form_occur'], $_REQUEST['form_clasification'], $_REQUEST['form_referredby'], $_SESSION['authUser'],
750 $_SESSION['authProvider'], QuotedOrNull($_REQUEST['form_outcome']), $_REQUEST['form_destination'], $_REQUEST['form_reaction'], $subtype));
752 // For record/reporting purposes, place entry in lists_touch table.
753 setListTouch($pid, $form_type);
755 // If requested, link the issue to a specified encounter.
756 // we always link them, automatically.
757 if ($encounter) {
758 $query = "INSERT INTO issue_encounter ( " .
759 "pid, list_id, encounter " .
760 ") VALUES ( ?,?,? )";
761 sqlStatement($query, array($pid, $issue, $encounter));
765 $irow = '';
766 //if it is a medication do we need to do something with dosage fields?
767 //leave all in title field form now.
770 $PMSFH = build_PMSFH($pid);
771 send_json_values($PMSFH);
772 exit;
776 if ($_REQUEST['action'] == 'code_PMSFH') {
777 $query = "UPDATE lists SET diagnosis = ? WHERE id = ?";
778 sqlStatement($query, array($_POST['code'], $_POST['issue']));
779 exit;
782 if ($_REQUEST['action'] == 'code_visit') {
783 $CODING = json_decode($_REQUEST['parameter'], true);
784 $query = "delete from billing where encounter =?";
785 sqlStatement($query, array($encounter));
786 foreach ($CODING as $item) { //need toremove duplicate codes
787 if ($dups[$item["code"]] == '1') {
788 continue;
791 $dups[$item["code"]] = "1";
792 $sql = "SELECT codes.*, prices.pr_price FROM codes " .
793 "LEFT OUTER JOIN patient_data ON patient_data.pid = '$pid' " .
794 "LEFT OUTER JOIN prices ON prices.pr_id = codes.id AND " .
795 "prices.pr_selector = '' AND " .
796 "prices.pr_level = patient_data.pricelevel " .
797 "WHERE code =?" .
798 " LIMIT 1";
799 $result = sqlStatement($sql, array($item['code']));
800 while ($res = sqlFetchArray($result)) {
801 $item["codedesc"] = $res["code_text"];// eg. = "NP EYE intermediate exam"
802 if (!$item["modifier"]) {
803 $modifier = $res["modifier"];
805 $item["units"] = $res["units"];
806 $item["fee"] = $res["pr_price"];
808 $item["justify"] .= ":";
809 addBilling($encounter, $item["codetype"], $item["code"], $item["codedesc"], $pid, '1', $providerID, $item["modifier"], $item["units"], $item["fee"], $ndc_info, $item["justify"], $billed, '');
811 echo "OK";
812 exit;
815 /*** END CODE to DEAL WITH PMSFH/ISUUE_TYPES ****/
816 //Update the visit status for this appointment (from inside the Coding Engine)
817 //we also have to update the flow board... They are not linked automatically.
818 //Flow board counts items for each events so we need to insert new item and update total for the event, via pc_eid...
819 if ($_REQUEST['action'] == 'new_appt_status') {
820 if ($_POST['new_status']) {
821 //make sure visit_date is in YYYY-MM-DD format
822 $Vdated = new DateTime($_POST['visit_date']);
823 $Vdate = $Vdated->format('Y-m-d');
824 //get eid
825 $sql = "select * from patient_tracker where `pid` = ? and `apptdate`=?";
826 $tracker = sqlFetchArray(sqlStatement($sql, array($_POST['pid'], $Vdate)));
827 sqlStatement("UPDATE `patient_tracker` SET `lastseq` = ? WHERE `id` = ?", array(($tracker['lastseq'] + 1), $tracker['id']));
828 #Add a tracker item.
829 $sql = "INSERT INTO `patient_tracker_element` " .
830 "(`pt_tracker_id`, `start_datetime`, `user`, `status`, `room`, `seq`) " .
831 "VALUES (?,NOW(),?,?,?,?)";
832 sqlInsert($sql, array($tracker['id'], $userauthorized, $_POST['new_status'], ' ', ($tracker['lastseq'] + 1)));
833 sqlStatement("UPDATE `openemr_postcalendar_events` SET `pc_apptstatus` = ? WHERE `pc_eid` = ?", array($_POST['new_status'], $tracker['eid']));
834 exit;
836 echo "Failed to update Patient Tracker.";
837 exit;
839 /** Let's save the encounter specific values.
840 * Any field that exists in the database could be updated
841 * so we need to exclude the important ones...
842 * id date pid user groupname authorized activity. Any other just add them below.
843 * Doing it this way means you can add new fields on a web page and in the DB without touching this function.
844 * The update feature still works because it only updates columns that are in the table you are working on.
846 if (($_POST['IOPTIME'] == '00:00:00') || (!$_POST['IOPTIME'])) {
847 $_POST['IOPTIME'] = date('H:i:s');
850 $_POST['IOPTIME'] = date('H:i:s', strtotime($_POST['IOPTIME']));
851 // orders are checkboxes created from a user defined list in the PLAN area and stored as item1|item2|item3
852 // if there are any, create the $field['PLAN'] value.
853 // Remember -- If you uncheck a box, it won't be sent!
854 // So delete all made today by this provider and reload with any Orders sent in this $_POST
855 // in addition, we made a special table for orders, and when completed we can mark done?
856 $query = "select form_encounter.date as encounter_date from form_encounter where form_encounter.encounter =?";
857 $encounter_data = sqlQuery($query, array($encounter));
858 $dated = new DateTime($encounter_data['encounter_date']);
859 $visit_date = $dated->format('Y-m-d');
861 $N = count($_POST['PLAN']);
862 $sql_clear = "DELETE from form_eye_mag_orders where pid =? and ORDER_PLACED_BYWHOM=? and ORDER_DATE_PLACED=? and ORDER_STATUS ='pending'";
863 sqlQuery($sql_clear, array($pid, $providerID, $visit_date));
864 if ($N > '0') {
865 for ($i = 0; $i < $N; $i++) {
866 if ($_POST['PLAN'][$i] =='') continue;
867 $fields['PLAN'] .= $_POST['PLAN'][$i] . "|"; //this makes an entry for form_eyemag: PLAN
868 $ORDERS_sql = "INSERT INTO form_eye_mag_orders (form_id,pid,ORDER_DETAILS,ORDER_PRIORITY,ORDER_STATUS,ORDER_DATE_PLACED,ORDER_PLACED_BYWHOM) VALUES (?,?,?,?,?,?,?)";
869 $okthen = sqlQuery($ORDERS_sql, array($form_id, $pid, $_POST['PLAN'][$i], $i, 'pending', $visit_date, $providerID));
872 $_POST['PLAN'] = mb_substr($fields['PLAN'], 0, -1); //get rid of trailing "|"
875 $M = count($_POST['TEST']);
876 if ($M > '0') {
877 for ($i = 0; $i < $M; $i++) {
878 $_POST['Resource'] .= $_POST['TEST'][$i] . "|"; //this makes an entry for form_eyemag: Resource
881 $_POST['Resource'] = mb_substr($_POST['Resource'], 0, -1); //get rid of trailing "|"
884 /** Empty Checkboxes need to be entered manually as they are only submitted via POST when they are checked
885 * If NOT checked on the form, they are sent via POST and thus are NOT overridden in the DB,
886 * so DB won't change unless we define them into the $fields array as "0"...
888 if (!$_POST['alert']) {
889 $_POST['alert'] = '0';
892 if (!$_POST['oriented']) {
893 $_POST['oriented'] = '0';
896 if (!$_POST['confused']) {
897 $_POST['confused'] = '0';
900 if (!$_POST['PUPIL_NORMAL']) {
901 $_POST['PUPIL_NORMAL'] = '0';
904 if (!$_POST['MOTILITYNORMAL']) {
905 $_POST['MOTILITYNORMAL'] = '0';
908 if (!$_POST['ACT']) {
909 $_POST['ACT'] = 'off';
912 if (!$_POST['DIL_RISKS']) {
913 $_POST['DIL_RISKS'] = '0';
916 if (!$_POST['ATROPINE']) {
917 $_POST['ATROPINE'] = '0';
920 if (!$_POST['CYCLOGYL']) {
921 $_POST['CYCLOGYL'] = '0';
924 if (!$_POST['CYCLOMYDRIL']) {
925 $_POST['CYCLOMYDRIL'] = '0';
928 if (!$_POST['NEO25']) {
929 $_POST['NEO25'] = '0';
932 if (!$_POST['TROPICAMIDE']) {
933 $_POST['TROPICAMIDE'] = '0';
936 if (!$_POST['BALANCED']) {
937 $_POST['BALANCED'] = '0';
940 if (!$_POST['ODVF1']) {
941 $_POST['ODVF1'] = '0';
944 if (!$_POST['ODVF2']) {
945 $_POST['ODVF2'] = '0';
948 if (!$_POST['ODVF3']) {
949 $_POST['ODVF3'] = '0';
952 if (!$_POST['ODVF4']) {
953 $_POST['ODVF4'] = '0';
956 if (!$_POST['OSVF1']) {
957 $_POST['OSVF1'] = '0';
960 if (!$_POST['OSVF2']) {
961 $_POST['OSVF2'] = '0';
964 if (!$_POST['OSVF3']) {
965 $_POST['OSVF3'] = '0';
968 if (!$_POST['OSVF4']) {
969 $_POST['OSVF4'] = '0';
972 if (!$_POST['TEST']) {
973 $_POST['Resource'] = '';
976 if (!$_POST['PLAN']) {
977 $_POST['PLAN'] = ' ';
980 $tables = array('form_eye_hpi','form_eye_ros','form_eye_vitals',
981 'form_eye_acuity','form_eye_refraction','form_eye_biometrics',
982 'form_eye_external', 'form_eye_antseg','form_eye_postseg',
983 'form_eye_neuro','form_eye_locking');
985 foreach ($tables as $table_name) {
986 $query = "SHOW COLUMNS from " . $table_name . "";
987 $result = sqlStatement($query);
988 if (!$result) {
989 return 'Could not run query: No columns found in your table! ';// . mysql_error();
990 continue;
993 $fields = array();
994 $sql2 ='';
995 if (sqlNumRows($result) > 0) {
996 while ($row = sqlFetchArray($result)) {
998 //exclude critical columns/fields and those needing special processing from update
999 if ($row['Field'] == 'id' or
1000 $row['Field'] == 'date' or
1001 $row['Field'] == 'pid' or
1002 $row['Field'] == 'user' or
1003 $row['Field'] == 'groupname' or
1004 $row['Field'] == 'authorized' or
1005 $row['Field'] == 'LOCKED' or
1006 $row['Field'] == 'LOCKEDBY' or
1007 $row['Field'] == 'activity' or
1008 $row['Field'] == 'PLAN' or
1009 $row['Field'] == 'Resource') {
1010 continue;
1012 $fields[] = $_POST[$row['Field']]?:'';
1013 $sql2 .= " ". add_escape_custom($row['Field']) ." = ?,";
1015 $sql = "update " . escape_table_name($table_name) . " set pid ='".add_escape_custom($_SESSION['pid'])."',".$sql2;
1017 $sql = substr($sql, 0, -1);
1018 $sql .= " where id=?";
1019 $fields[] = $form_id;
1020 $success = sqlStatement($sql,$fields);
1023 //now save any Wear RXs (1-4) entered.
1025 $rx_number = '1';
1026 if ($_POST['W_1'] == '1') {
1027 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
1028 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
1029 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
1030 `RX_TYPE` ,`COMMENTS`,
1031 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
1032 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
1033 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
1034 `LENS_TREATMENTS`
1035 ) VALUES
1036 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
1037 $LENS_TREATMENTS_1 = implode("|", $_POST['LENS_TREATMENTS_1']);
1038 sqlQuery($query, array($encounter, $form_id, $pid, $rx_number, $_POST['ODSPH_1'], $_POST['ODCYL_1'], $_POST['ODAXIS_1'],
1039 $_POST['ODVA_1'], $_POST['ODADD_1'], $_POST['ODNEARVA_1'], $_POST['OSSPH_1'], $_POST['OSCYL_1'], $_POST['OSAXIS_1'],
1040 $_POST['OSVA_1'], $_POST['OSADD_1'], $_POST['OSNEARVA_1'], $_POST['ODMIDADD_1'], $_POST['OSMIDADD_1'],
1041 0 + $_POST['RX_TYPE_1'], $_POST['COMMENTS_1'],
1042 $_POST['ODHPD_1'], $_POST['ODHBASE_1'], $_POST['ODVPD_1'], $_POST['ODVBASE_1'], $_POST['ODSLABOFF_1'], $_POST['ODVERTEXDIST_1'],
1043 $_POST['OSHPD_1'], $_POST['OSHBASE_1'], $_POST['OSVPD_1'], $_POST['OSVBASE_1'], $_POST['OSSLABOFF_1'], $_POST['OSVERTEXDIST_1'],
1044 $_POST['ODMPDD_1'], $_POST['ODMPDN_1'], $_POST['OSMPDD_1'], $_POST['OSMPDN_1'], $_POST['BPDD_1'], $_POST['BPDN_1'], $_POST['LENS_MATERIAL_1'],
1045 $LENS_TREATMENTS_1));
1046 $rx_number++;
1047 } else {
1048 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
1049 sqlQuery($query, array($encounter, $pid, $form_id, '1'));
1051 if ($_POST['W_2'] == '1') {
1052 //store W_2
1053 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
1054 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
1055 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
1056 `RX_TYPE` ,`COMMENTS`,
1057 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
1058 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
1059 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
1060 `LENS_TREATMENTS`
1061 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
1062 $LENS_TREATMENTS_2 = implode("|", $_POST['LENS_TREATMENTS_2']);
1063 sqlQuery($query, array($encounter, $form_id, $pid, $rx_number, $_POST['ODSPH_2'], $_POST['ODCYL_2'], $_POST['ODAXIS_2'],
1064 $_POST['ODVA_2'], $_POST['ODADD_2'], $_POST['ODNEARVA_2'], $_POST['OSSPH_2'], $_POST['OSCYL_2'], $_POST['OSAXIS_2'],
1065 $_POST['OSVA_2'], $_POST['OSADD_2'], $_POST['OSNEARVA_2'], $_POST['ODMIDADD_2'], $_POST['OSMIDADD_2'],
1066 0 + $_POST['RX_TYPE_2'], $_POST['COMMENTS_2'],
1067 $_POST['ODHPD_2'], $_POST['ODHBASE_2'], $_POST['ODVPD_2'], $_POST['ODVBASE_2'], $_POST['ODSLABOFF_2'], $_POST['ODVERTEXDIST_2'],
1068 $_POST['OSHPD_2'], $_POST['OSHBASE_2'], $_POST['OSVPD_2'], $_POST['OSVBASE_2'], $_POST['OSSLABOFF_2'], $_POST['OSVERTEXDIST_2'],
1069 $_POST['ODMPDD_2'], $_POST['ODMPDN_2'], $_POST['OSMPDD_2'], $_POST['OSMPDN_2'], $_POST['BPDD_2'], $_POST['BPDN_2'], $_POST['LENS_MATERIAL_2'],
1070 $LENS_TREATMENTS_2));
1071 $rx_number++;
1072 } else {
1073 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
1074 sqlQuery($query, array($encounter, $pid, $form_id, '2'));
1076 if ($_POST['W_3'] == '1') {
1077 //store W_3
1078 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
1079 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
1080 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
1081 `RX_TYPE` ,`COMMENTS`,
1082 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
1083 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
1084 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
1085 `LENS_TREATMENTS`
1086 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
1087 $LENS_TREATMENTS_3 = implode("|", $_POST['LENS_TREATMENTS_3']);
1088 sqlQuery($query, array($encounter, $form_id, $pid, $rx_number, $_POST['ODSPH_3'], $_POST['ODCYL_3'], $_POST['ODAXIS_3'],
1089 $_POST['ODVA_3'], $_POST['ODADD_3'], $_POST['ODNEARVA_3'], $_POST['OSSPH_3'], $_POST['OSCYL_3'], $_POST['OSAXIS_3'],
1090 $_POST['OSVA_3'], $_POST['OSADD_3'], $_POST['OSNEARVA_3'], $_POST['ODMIDADD_3'], $_POST['OSMIDADD_3'],
1091 0 + $_POST['RX_TYPE_3'], $_POST['COMMENTS_3'],
1092 $_POST['ODHPD_3'], $_POST['ODHBASE_3'], $_POST['ODVPD_3'], $_POST['ODVBASE_3'], $_POST['ODSLABOFF_3'], $_POST['ODVERTEXDIST_3'],
1093 $_POST['OSHPD_3'], $_POST['OSHBASE_3'], $_POST['OSVPD_3'], $_POST['OSVBASE_3'], $_POST['OSSLABOFF_3'], $_POST['OSVERTEXDIST_3'],
1094 $_POST['ODMPDD_3'], $_POST['ODMPDN_3'], $_POST['OSMPDD_3'], $_POST['OSMPDN_3'], $_POST['BPDD_3'], $_POST['BPDN_3'], $_POST['LENS_MATERIAL_3'],
1095 $LENS_TREATMENTS_3));
1096 $rx_number++;
1097 } else {
1098 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
1099 sqlQuery($query, array($encounter, $pid, $form_id, '3'));
1101 if ($_POST['W_4'] == '1') {
1102 //store W_4
1103 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
1104 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
1105 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
1106 `RX_TYPE` ,`COMMENTS`,
1107 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
1108 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
1109 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
1110 `LENS_TREATMENTS`
1111 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
1112 $LENS_TREATMENTS_4 = implode("|", $_POST['LENS_TREATMENTS_4']);
1113 sqlQuery($query, array($encounter, $form_id, $pid, $rx_number, $_POST['ODSPH_4'], $_POST['ODCYL_4'], $_POST['ODAXIS_4'],
1114 $_POST['ODVA_4'], $_POST['ODADD_4'], $_POST['ODNEARVA_4'], $_POST['OSSPH_4'], $_POST['OSCYL_4'], $_POST['OSAXIS_4'],
1115 $_POST['OSVA_4'], $_POST['OSADD_4'], $_POST['OSNEARVA_4'], $_POST['ODMIDADD_4'], $_POST['OSMIDADD_4'],
1116 0 + $_POST['RX_TYPE_4'], $_POST['COMMENTS_4'],
1117 $_POST['ODHPD_4'], $_POST['ODHBASE_4'], $_POST['ODVPD_4'], $_POST['ODVBASE_4'], $_POST['ODSLABOFF_4'], $_POST['ODVERTEXDIST_4'],
1118 $_POST['OSHPD_4'], $_POST['OSHBASE_4'], $_POST['OSVPD_4'], $_POST['OSVBASE_4'], $_POST['OSSLABOFF_4'], $_POST['OSVERTEXDIST_4'],
1119 $_POST['ODMPDD_4'], $_POST['ODMPDN_4'], $_POST['OSMPDD_4'], $_POST['OSMPDN_4'], $_POST['BPDD_4'], $_POST['BPDN_4'], $_POST['LENS_MATERIAL_4'],
1120 $LENS_TREATMENTS_4));
1121 $rx_number++;
1122 } else {
1123 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
1124 sqlQuery($query, array($encounter, $pid, $form_id, '4'));
1127 for ($i = $rx_number; $i < 5; $i++) {
1128 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
1129 sqlQuery($query, array($encounter, $pid, $form_id, $i));
1131 //now return the obj
1132 $send['IMPPLAN_items'] = build_IMPPLAN_items($pid, $form_id);
1133 $send['Clinical'] = start_your_engines($_REQUEST);
1134 $send['PMH_panel'] = display_PMSFH('2');
1135 $send['right_panel'] = show_PMSFH_panel($PMSFH);
1136 $send['PMSFH'] = $PMSFH[0];
1137 $send['Coding'] = build_CODING_items($pid, $encounter);
1139 echo json_encode($send);
1140 exit;
1141 } elseif ($_REQUEST["mode"] == "retrieve") {
1142 if ($_REQUEST['PRIORS_query']) {
1143 if ($_REQUEST['zone'] == 'REFRACTIONS') {
1144 //TODO: Fix this so it works!
1145 //have to do query to join with _base pn pid since pid is not in sub files
1146 //get the last 3 encounters with refraction data, not Wear data, and display all that encounters Rx/W data.
1147 $sql = "SELECT id,date FROM form_eye_refraction WHERE
1148 pid=? AND id < ? AND
1149 (MRODVA <> '' OR
1150 MROSVA <> '' OR
1151 ARODVA <> '' OR
1152 AROSVA <> '' OR
1153 CRODVA <> '' OR
1154 CROSVA <> '' OR
1155 CTLODVA <> '' OR
1156 CTLOSVA <> ''
1158 ORDER BY id DESC LIMIT 3";
1160 //$result = sqlStatement($sql, array($pid, $_REQUEST['orig_id']));
1162 $sql = "SELECT id from form_eye_refraction where
1163 id in (SELECT id from form_eye_base where pid=? ORDER BY `date` DESC)
1164 ORDER by id DESC LIMIT 10;
1167 $result = sqlStatement($sql, array($pid));
1169 while ($visit = sqlFetchArray($result)) {
1170 echo display_PRIOR_section('REFRACTIONS', $visit['id'], $visit['id'], $pid);
1172 exit;
1173 } else {
1174 echo display_PRIOR_section($_REQUEST['zone'], $_REQUEST['orig_id'], $_REQUEST['id_to_show'], $pid);
1175 exit;
1181 * Save the canvas drawings
1184 if ($_REQUEST['canvas']) {
1185 if (!$pid || !$encounter || !$zone || !$_POST["imgBase64"]) {
1186 exit;
1189 $side = "OU";
1190 $base_name = $pid . "_" . $encounter . "_" . $side . "_" . $zone . "_VIEW";
1191 $filename = $base_name . ".jpg";
1193 $type = "image/jpeg"; // all our canvases are this type
1194 $data = $_POST["imgBase64"];
1195 $data = substr($data, strpos($data, ",") + 1);
1196 $data = base64_decode($data);
1197 $size = strlen($data);
1198 $query = "select id from categories where name = 'Drawings'";
1199 $result = sqlStatement($query);
1200 $ID = sqlFetchArray($result);
1201 $category_id = $ID['id'];
1203 // We want to overwrite so only one image is stored per zone per form/encounter
1204 // I do not believe this function exists in the current library, ie "UpdateDocument" function, so...
1205 // we need to delete the previous file from the documents and categories to documents tables and the actual file
1206 // There must be a delete_file function in documents class?
1207 // cannot find it.
1208 // this will work for harddisk people, not sure about couchDB people:
1209 $filepath = $GLOBALS['oer_config']['documents']['repository'] . $pid . "/";
1210 foreach (glob($filepath . '/' . $filename) as $file) {
1211 unlink($file);
1214 $sql = "DELETE from categories_to_documents where document_id IN (SELECT id from documents where documents.url like '%" . $filename . "')";
1215 sqlQuery($sql);
1216 $sql = "DELETE from documents where documents.url like '%" . $filename . "'";
1217 sqlQuery($sql);
1218 $return = addNewDocument($filename, $type, $_POST["imgBase64"], 0, $size, $_SESSION['authUserID'], $pid, $category_id);
1219 $doc_id = $return['doc_id'];
1220 $sql = "UPDATE documents set encounter_id=? where id=?"; //link it to this encounter
1221 sqlQuery($sql, array($encounter, $doc_id));
1222 exit;
1225 if ($_REQUEST['copy']) {
1226 copy_forward($_REQUEST['zone'], $_REQUEST['copy_from'], $_SESSION['ID'], $pid);
1227 return;
1230 function QuotedOrNull($fld)
1232 if ($fld) {
1233 return "'" . add_escape_custom($fld) . "'";
1236 return "NULL";
1239 function debug($local_var)
1241 echo "<pre><BR>We are in the debug function.<BR>";
1242 echo "Passed variable = " . $local_var . " <BR>";
1243 print_r($local_var);
1244 exit;
1247 /* From original issue.php */
1249 function row_delete($table, $where)
1251 $query = "SELECT * FROM $table WHERE $where";
1252 $tres = sqlStatement($query);
1253 $count = 0;
1254 while ($trow = sqlFetchArray($tres)) {
1255 $logstring = "";
1256 foreach ($trow as $key => $value) {
1257 if (!$value || $value == '0000-00-00 00:00:00') {
1258 continue;
1261 if ($logstring) {
1262 $logstring .= " ";
1265 $logstring .= $key . "='" . addslashes($value) . "'";
1268 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
1269 ++$count;
1272 if ($count) {
1273 $query = "DELETE FROM $table WHERE $where";
1274 sqlStatement($query);
1278 // Given an issue type as a string, compute its index.
1279 // Not sure of the value of this sub given transition to array $PMSFH
1280 // Can I use it to find out which PMSFH item we are looking for? YES
1281 function issueTypeIndex($tstr)
1283 global $ISSUE_TYPES;
1284 $i = 0;
1285 foreach ($ISSUE_TYPES as $key => $value) {
1286 if ($key == $tstr) {
1287 break;
1290 ++$i;
1293 return $i;
1296 exit;