Interim autoloaded library/classes via composer classmap, take 4. (#422)
[openemr.git] / interface / forms / eye_mag / save.php
blob8fbc16a57457d237d51b8474f6f3e33c90e70bcd
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
39 $fake_register_globals=false;
40 $sanitize_all_escapes=true;
42 $table_name = "form_eye_mag";
43 $form_name = "eye_mag";
44 $form_folder = "eye_mag";
46 require_once("../../globals.php");
48 require_once("$srcdir/html2pdf/vendor/autoload.php");
49 require_once("$srcdir/api.inc");
50 require_once("$srcdir/forms.inc");
51 require_once("php/".$form_name."_functions.php");
52 require_once("$srcdir/formatting.inc.php");
53 require_once($srcdir . "/../controllers/C_Document.class.php");
54 require_once($srcdir . "/documents.php");
55 require_once("$srcdir/patient.inc");
56 require_once("$srcdir/options.inc.php");
57 require_once("$srcdir/acl.inc");
58 require_once("$srcdir/lists.inc");
59 require_once("$srcdir/report.inc");
60 require_once("$srcdir/html2pdf/html2pdf.class.php");
62 $returnurl = 'encounter_top.php';
64 if (isset($_REQUEST['id']))
66 $id = $_REQUEST['id'];
68 if (!$id) $id = $_REQUEST['pid'];
69 $encounter = $_REQUEST['encounter'];
71 $AJAX_PREFS = $_REQUEST['AJAX_PREFS'];
72 if ($encounter == "" && !$id && !$AJAX_PREFS && (($_REQUEST['mode'] != "retrieve") or ($_REQUEST['mode'] == "show_PDF"))) {
73 echo "Sorry Charlie..."; //should lead to a database of errors for explanation.
74 exit;
76 /**
77 * Save/update the preferences
79 if ($_REQUEST['AJAX_PREFS']) {
80 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
81 VALUES
82 ('PREFS','VA','Vision',?,'RS','51',?,'1')";
83 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_VA']));
85 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
86 VALUES
87 ('PREFS','W','Current Rx',?,'W','52',?,'2')";
88 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_W']));
90 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
91 VALUES
92 ('PREFS','W_width','Detailed Rx',?,'W_width','80',?,'100')";
93 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_W_width']));
95 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
96 VALUES
97 ('PREFS','MR','Manifest Refraction',?,'MR','53',?,'3')";
98 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_MR']));
100 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
101 VALUES
102 ('PREFS','MR_width','Detailed MR',?,'MR_width','81',?,'110')";
103 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_W_width']));
105 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
106 VALUES
107 ('PREFS','CR','Cycloplegic Refraction',?,'CR','54',?,'4')";
108 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_CR']));
110 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
111 VALUES
112 ('PREFS','CTL','Contact Lens',?,'CTL','55',?,'5')";
113 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_CTL']));
115 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
116 VALUES
117 ('PREFS', 'VAX', 'Visual Acuities', ?, 'VAX','65', ?,'15')";
118 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_VAX']));
120 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
121 VALUES
122 ('PREFS','ADDITIONAL','Additional Data Points',?,'ADDITIONAL','56',?,'6')";
123 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_ADDITIONAL']));
125 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
126 VALUES
127 ('PREFS','CLINICAL','CLINICAL',?,'CLINICAL','57',?,'7')";
128 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_CLINICAL']));
130 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
131 VALUES
132 ('PREFS','IOP','Intraocular Pressure',?,'IOP','67',?,'17')";
133 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_IOP']));
135 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
136 VALUES
137 ('PREFS','EXAM','EXAM',?,'EXAM','58',?,'8')";
138 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_EXAM']));
140 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
141 VALUES
142 ('PREFS','CYLINDER','CYL',?,'CYL','59',?,'9')";
143 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_CYL']));
145 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
146 VALUES
147 ('PREFS','HPI_VIEW','HPI View',?,'HPI_VIEW','60',?,'10')";
148 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_HPI_VIEW']));
150 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
151 VALUES
152 ('PREFS','EXT_VIEW','External View',?,'EXT_VIEW','66',?,'16')";
153 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_EXT_VIEW']));
155 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
156 VALUES
157 ('PREFS','ANTSEG_VIEW','Anterior Segment View',?,'ANTSEG_VIEW','61',?,'11')";
158 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_ANTSEG_VIEW']));
160 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
161 VALUES
162 ('PREFS','RETINA_VIEW','Retina View',?,'RETINA_VIEW','62',?,'12')";
163 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_RETINA_VIEW']));
165 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
166 VALUES
167 ('PREFS','NEURO_VIEW','Neuro View',?,'NEURO_VIEW','63',?,'13')";
168 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_NEURO_VIEW']));
170 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
171 VALUES
172 ('PREFS','ACT_VIEW','ACT View',?,'ACT_VIEW','64',?,'14')";
173 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_ACT_VIEW']));
175 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
176 VALUES
177 ('PREFS','ACT_SHOW','ACT Show',?,'ACT_SHOW','65',?,'15')";
178 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_ACT_SHOW']));
180 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
181 VALUES
182 ('PREFS','HPI_RIGHT','HPI DRAW',?,'HPI_RIGHT','70',?,'16')";
183 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_HPI_RIGHT']));
185 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
186 VALUES
187 ('PREFS','PMH_RIGHT','PMH DRAW',?,'PMH_RIGHT','71',?,'17')";
188 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_PMH_RIGHT']));
189 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
190 VALUES
191 ('PREFS','EXT_RIGHT','EXT DRAW',?,'EXT_RIGHT','72',?,'18')";
192 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_EXT_RIGHT']));
193 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
194 VALUES
195 ('PREFS','ANTSEG_RIGHT','ANTSEG DRAW',?,'ANTSEG_RIGHT','73',?,'19')";
196 $result = sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_ANTSEG_RIGHT']));
198 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
199 VALUES
200 ('PREFS','RETINA_RIGHT','RETINA DRAW',?,'RETINA_RIGHT','74',?,'20')";
201 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_RETINA_RIGHT']));
202 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
203 VALUES
204 ('PREFS','NEURO_RIGHT','NEURO DRAW',?,'NEURO_RIGHT','75',?,'21')";
205 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_NEURO_RIGHT']));
206 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
207 VALUES
208 ('PREFS','IMPPLAN_RIGHT','IMPPLAN DRAW',?,'IMPPLAN_RIGHT','76',?,'22')";
209 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_IMPPLAN_RIGHT']));
210 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
211 VALUES
212 ('PREFS','PANEL_RIGHT','PMSFH Panel',?,'PANEL_RIGHT','77',?,'23')";
213 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_PANEL_RIGHT']));
214 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
215 VALUES
216 ('PREFS','KB_VIEW','KeyBoard View',?,'KB_VIEW','78',?,'24')";
217 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_KB']));
218 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
219 VALUES
220 ('PREFS','TOOLTIPS','Toggle Tooltips',?,'TOOLTIPS','79',?,'25')";
221 sqlQuery($query,array($_SESSION['authId'],$_REQUEST['PREFS_TOOLTIPS']));
224 * ADD ANY NEW PREFERENCES above, and as a hidden field in the body.
227 /** <!-- End Preferences --> **/
230 * Create, update or retrieve a form and its values
232 if (!$pid) $pid = $_SESSION['pid'];
233 $userauthorized = $_SESSION['userauthorized'];
234 if ($encounter == "") $encounter = date("Ymd");
235 $form_id = $_REQUEST['form_id'];
236 $zone = $_REQUEST['zone'];
238 $providerID = findProvider($pid,$encounter);
239 if ($providerID =='0') $providerID = $userauthorized;//who is the default provider?
240 $providerNAME = getProviderName($providerID);
242 // The form is submitted to be updated or saved in some way.
243 // Give each instance of a form a uniqueID. If the form has no owner, update DB with this uniqueID.
244 // If the DB shows a uniqueID ie. an owner, and the save request uniqueID does not = the uniqueID in the DB,
245 // ask if the new user wishes to take ownership?
246 // If yes, any other's attempt to save fields/form are denied and the return code says you are not the owner...
247 if ($_REQUEST['unlock'] == '1') {
248 // we are releasing the form, by closing the page or clicking on ACTIVE FORM, so unlock it.
249 // if it's locked and they own it ($REQUEST[LOCKEDBY] == LOCKEDBY), they can unlock it
250 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from ".$table_name." WHERE ID=?";
251 $lock = sqlQuery($query,array($form_id));
252 if (($lock['LOCKED'] >'') && ($_REQUEST['LOCKEDBY'] == $lock['LOCKEDBY'])) {
253 $query = "update ".$table_name." set LOCKED='',LOCKEDBY='' where id=?";
254 sqlQuery($query,array($form_id));
256 exit;
257 } elseif ($_REQUEST['acquire_lock']=="1") {
258 //we are taking over the form's active state, others will go read-only
259 $query = "UPDATE ".$table_name." set LOCKED='1',LOCKEDBY=? where id=? and LOCKEDBY=?";
260 $result = sqlQuery($query,array($_REQUEST['uniqueID'],$form_id,$_REQUEST['locked_by']));
261 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from ".$table_name." WHERE ID=?";
262 $lock = sqlQuery($query,array($form_id));
263 exit;
264 } else {
265 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from ".$table_name." WHERE ID=?";
266 $lock = sqlQuery($query,array($form_id));
267 if (($lock['LOCKED']) && ($_REQUEST['uniqueID'] != $lock['LOCKEDBY'])) {
268 // We are not the owner or it is not new so it is locked
269 // Did the user send a demand to take ownership?
270 if ($lock['LOCKEDBY'] != $_REQUEST['ownership']) {
271 //tell them they are locked out by another user now
272 echo "Code 400";
273 // or return a JSON encoded string with current LOCK ID?
274 // echo "Sorry Charlie, you get nothing since this is locked... No save for you!";
275 exit;
276 } elseif ($lock['LOCKEDBY'] == $_REQUEST['ownership']) {
277 // then they are taking ownership - all others get locked...
278 // new LOCKEDBY becomes our uniqueID LOCKEDBY
279 $_REQUEST['LOCKED'] = '1';
280 $_REQUEST['LOCKEDBY'] = $_REQUEST['uniqueID'];
281 //update table
282 $query = "update ".$table_name." set LOCKED=?,LOCKEDBY=? where id=?";
283 sqlQuery ($query,array('1',$_REQUEST['LOCKEDBY'],$form_id));
284 //go on to save what we want...
286 } elseif (!$lock['LOCKED']) { // it is not locked yet
287 $_REQUEST['LOCKED'] = '1';
288 $query = "update ".$table_name." set LOCKED=?,LOCKEDBY=? where id=?";
289 sqlQuery($query,array('1',$_REQUEST['LOCKEDBY'],$form_id));
290 //go on to save what we want...
292 if (!$_REQUEST['LOCKEDBY']) $_REQUEST['LOCKEDBY'] = rand();
295 if ($_REQUEST["mode"] == "new") {
296 $newid = formSubmit($table_name, $_POST, $id, $userauthorized);
297 addForm($encounter, $form_name, $newid, $form_folder, $pid, $userauthorized);
298 } elseif ($_REQUEST["mode"] == "update") {
299 // The user has write privileges to work with...
300 if ($_REQUEST['action']=="store_PDF") {
302 * We want to store/overwrite the current PDF version of this encounter's f
303 * Currently this is only called 'beforeunload', ie. when you finish the form
304 * In this current paradigm, anytime the form is opened, then closed, the PDF
305 * is overwritten. With esign implemented, the PDF should be locked. I suppose
306 * with esign the form can't even be opened so the only way to get to the PDF
307 * is through the Documents->Encounters links.
309 $query = "select id from categories where name = 'Encounters'";
310 $result = sqlStatement($query);
311 $ID = sqlFetchArray($result);
312 $category_id = $ID['id'];
313 $PDF_OUTPUT='1';
315 $filename = $pid."_".$encounter.".pdf";
316 $filepath = $GLOBALS['oer_config']['documents']['repository'] . $pid;
317 foreach (glob($filepath.'/'.$filename) as $file) {
318 unlink($file);
320 $sql = "DELETE from categories_to_documents where document_id IN (SELECT id from documents where documents.url like '%".$filename."')";
321 sqlQuery($sql);
322 $sql = "DELETE from documents where documents.url like '%".$filename."'";
323 sqlQuery($sql);
324 // We want to overwrite so only one PDF is stored per form/encounter
325 // $pdf = new HTML2PDF('P', 'Letter', 'en', array(5, 5, 5, 5) ); // add a little margin 5cm all around TODO: add to globals
327 /***********/
329 $pdf = new HTML2PDF ($GLOBALS['pdf_layout'],
330 $GLOBALS['pdf_size'],
331 $GLOBALS['pdf_language'],
332 true, // default unicode setting is true
333 'UTF-8', // default encoding setting is UTF-8
334 array($GLOBALS['pdf_left_margin'],$GLOBALS['pdf_top_margin'],$GLOBALS['pdf_right_margin'],$GLOBALS['pdf_bottom_margin']),
335 $_SESSION['language_direction'] == 'rtl' ? true : false
337 ob_start();
339 <link rel="stylesheet" href="<?php echo $webserver_root; ?>/interface/themes/style_pdf.css" type="text/css">
340 <div id="report_custom" style="width:100%;"> <!-- large outer DIV -->
341 <?php
342 echo report_header($pid);
343 include_once($GLOBALS['incdir'] . "/forms/eye_mag/report.php");
344 call_user_func($form_name . "_report", $pid, $form_encounter, $N, $form_id);
345 if ($printable)
346 echo "" . xl('Signature') . ": _______________________________<br />";
348 </div> <!-- end of report_custom DIV -->
350 <?php
352 global $web_root, $webserver_root;
353 $content = ob_get_clean();
354 // Fix a nasty html2pdf bug - it ignores document root!
355 $i = 0;
356 $wrlen = strlen($web_root);
357 $wsrlen = strlen($webserver_root);
358 while (true) {
359 $i = stripos($content, " src='/", $i + 1);
360 if ($i === false) break;
361 if (substr($content, $i+6, $wrlen) === $web_root &&
362 substr($content, $i+6, $wsrlen) !== $webserver_root)
364 $content = substr($content, 0, $i + 6) . $webserver_root . substr($content, $i + 6 + $wrlen);
367 $pdf->writeHTML($content, false);
368 $temp_filename = '/tmp/'.$filename;
369 $content_pdf = $pdf->Output($temp_filename, 'F');
370 $type = "application/pdf";
371 $size = filesize($temp_filename);
372 $return = addNewDocument($filename,$type,$temp_filename,0,$size,$_SESSION['authUserID'],$pid,$category_id);
373 $doc_id = $return['doc_id'];
374 $sql = "UPDATE documents set encounter_id=? where id=?"; //link it to this encounter
375 sqlQuery($sql,array($encounter,$doc_id));
376 exit;
378 // Store the IMPPLAN area. This is separate from the rest of the form
379 // It is in a separate table due to its one-to-many relationship with the form_id.
380 if ($_REQUEST['action']=="store_IMPPLAN") {
381 $IMPPLAN = json_decode($_REQUEST['parameter'],true);
382 //remove what is there and replace it with this data.
383 $query = "DELETE from form_".$form_folder."_impplan where form_id=? and pid=?";
384 sqlQuery($query,array($form_id,$pid));
386 for($i = 0; $i < count($IMPPLAN); $i++) {
387 $query ="INSERT IGNORE INTO form_".$form_folder."_impplan (form_id, pid, title, code, codetype, codedesc, codetext, plan, IMPPLAN_order, PMSFH_link) VALUES(?,?,?,?,?,?,?,?,?,?) ";
388 $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']));
389 //if it is a duplicate then delete this from the array and return the array via json.
390 //or rebuild it from mysql
392 //Since we are potentially ignoring duplicates, build json IMPPLAN_items and return it to the user to rebuild IMP/Plan area
393 $IMPPLAN_items = build_IMPPLAN_items($pid,$form_id);
394 echo json_encode($IMPPLAN_items);
395 exit;
398 //change PCP
399 if ($_REQUEST['action'] == 'docs') {
400 $query = "update patient_data set providerID=?,ref_providerID=? where pid =?";
401 sqlQuery($query,array($_REQUEST['pcp'],$_REQUEST['rDOC'],$pid));
402 exit;
405 /*** START CODE to DEAL WITH PMSFH/ISUUE_TYPES ****/
406 if ($_REQUEST['PMSFH_save'] =='1') {
407 if (!$PMSFH) $PMSFH = build_PMSFH($pid);
408 $issue = $_REQUEST['issue'];
409 $deletion = $_REQUEST['deletion'];
410 $form_save = $_REQUEST['form_save'];
411 $pid = $_SESSION['pid'];
412 $encounter = $_SESSION['encounter'];
413 $form_id = $_REQUEST['form_id'];
414 $form_type = $_REQUEST['form_type'];
415 $r_PMSFH = $_REQUEST['r_PMSFH'];
416 if ($deletion ==1) {
417 row_delete("issue_encounter", "list_id = '$issue'");
418 row_delete("lists", "id = '$issue'");
419 $PMSFH = build_PMSFH($pid);
420 send_json_values($PMSFH);
421 exit;
422 } else {
423 if ($form_type=='ROS') { //ROS
424 $query="UPDATE form_eye_mag set ROSGENERAL=?,ROSHEENT=?,ROSCV=?,ROSPULM=?,ROSGI=?,ROSGU=?,ROSDERM=?,ROSNEURO=?,ROSPSYCH=?,ROSMUSCULO=?,ROSIMMUNO=?,ROSENDOCRINE=? where id=? and pid=?";
425 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'],$form_id,$pid));
426 $PMSFH = build_PMSFH($pid);
427 send_json_values($PMSFH);
428 exit;
429 } elseif ($form_type=='SOCH') { //SocHx
430 $newdata = array();
431 $fres = sqlStatement("SELECT * FROM layout_options " .
432 "WHERE form_id = 'HIS' AND uor > 0 AND field_id != '' " .
433 "ORDER BY group_name, seq");
434 while ($frow = sqlFetchArray($fres)) {
435 $field_id = $frow['field_id'];
436 $newdata[$field_id] = get_layout_form_value($frow);
438 updateHistoryData($pid, $newdata);
439 if ($_REQUEST['marital_status'] >'') {
440 // have to match input with list_option for marital to not break openEMR
441 $query="select * from list_options where list_id='marital'";
442 $fres = sqlStatement($query);
443 while ($frow = sqlFetchArray($fres)) {
444 if (($_REQUEST['marital_status'] == $frow['option_id'])||($_REQUEST['marital_status'] == $frow['title'])) {
445 $status = $frow['option_id'];
446 $query = "UPDATE patient_data set status=? where pid=?";
447 sqlStatement($query,array($status,$pid));
451 if ($_REQUEST['occupation'] > '') {
452 $query = "UPDATE patient_data set occupation=? where pid=?";
453 sqlStatement($query,array($_REQUEST['occupation'],$pid));
455 $PMSFH = build_PMSFH($pid);
456 send_json_values($PMSFH);
457 exit;
458 } elseif ($form_type =='FH') {
459 $query = "UPDATE history_data set
460 relatives_cancer=?,
461 relatives_diabetes=?,
462 relatives_high_blood_pressure=?,
463 relatives_heart_problems=?,
464 relatives_stroke=?,
465 relatives_epilepsy=?,
466 relatives_mental_illness=?,
467 relatives_suicide=?,
468 usertext11=?,
469 usertext12=?,
470 usertext13=?,
471 usertext14=?,
472 usertext15=?,
473 usertext16=?,
474 usertext17=?,
475 usertext18=? where pid=?";
476 //echo $_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;
477 $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));
478 $PMSFH = build_PMSFH($pid);
479 send_json_values($PMSFH);
480 exit;
481 } else {
482 if ($_REQUEST['form_title'] =='') return;
483 $subtype ='';
484 if ($form_type =="POH") {
485 $form_type="medical_problem";
486 $subtype="eye";
487 } elseif ($form_type =="PMH") {
488 $form_type="medical_problem";
489 } elseif ($form_type =="Allergy") {
490 $form_type="allergy";
491 } elseif ($form_type =="Surgery") {
492 $form_type="surgery";
493 } elseif ($form_type =="POS") {
494 $form_type="surgery";
495 $subtype="eye";
496 } elseif ($form_type =="Medication") {
497 $form_type="medication";
498 if ($_REQUEST['form_eye_subtype']) {
499 $subtype="eye";
500 //we always want a default begin date
501 //if it is empty, fill it with today
502 if ($_REQUEST['form_begin'] =='')
503 { $_REQUEST['form_begin'] = date("Y-m-d"); }
505 if ($_REQUEST['form_begin'] =='') {
506 $_REQUEST['form_begin'] = $visit_date;
509 $i = 0;
510 $form_begin = fixDate($_REQUEST['form_begin'], '');
511 $form_end = fixDate($_REQUEST['form_end'], '');
514 * When adding an issue, see if the issue is already here.
515 * If so we need to update it. If not we are adding it.
516 * Check the PMSFH array first by title.
517 * If not present in PMSFH, check the DB to be sure.
519 foreach ($PMSFH[$form_type] as $item) {
520 if ($item['title'] == $_REQUEST['form_title']) {
521 $issue = $item['issue'];
524 if (!$issue) {
525 if ($subtype == '') {
526 $query = "SELECT id,pid from lists where title=? and type=? and pid=?";
527 $issue2 = sqlQuery($query,array($_REQUEST['form_title'],$form_type,$pid));
528 $issue = $issue2['id'];
529 } else {
530 $query = "SELECT id,pid from lists where title=? and type=? and pid=? and subtype=?";
531 $issue2 = sqlQuery($query,array($_REQUEST['form_title'],$form_type,$pid,$subtype));
532 $issue = $issue2['id'];
535 $issue = 0 + $issue;
536 if ($_REQUEST['form_reinjury_id'] =="") $form_reinjury_id="0";
537 if ($_REQUEST['form_injury_grade'] =="") $form_injury_grade="0";
538 if ($_REQUEST['form_outcome'] =='') $_REQUEST['form_outcome'] ='0';
540 if ($issue != '0') { //if this issue already exists we are updating it...
541 $query = "UPDATE lists SET " .
542 "type = '" . add_escape_custom($form_type) . "', " .
543 "title = '" . add_escape_custom($_REQUEST['form_title']) . "', " .
544 "comments = '" . add_escape_custom($_REQUEST['form_comments']) . "', " .
545 "begdate = " . QuotedOrNull($form_begin) . ", " .
546 "enddate = " . QuotedOrNull($form_end) . ", " .
547 "returndate = " . QuotedOrNull($form_return) . ", " .
548 "diagnosis = '" . add_escape_custom($_REQUEST['form_diagnosis']) . "', " .
549 "occurrence = '" . add_escape_custom($_REQUEST['form_occur']) . "', " .
550 "classification = '" . add_escape_custom($_REQUEST['form_classification']) . "', " .
551 "reinjury_id = '" . add_escape_custom($_REQUEST['form_reinjury_id']) . "', " .
552 "referredby = '" . add_escape_custom($_REQUEST['form_referredby']) . "', " .
553 "injury_grade = '" . add_escape_custom($_REQUEST['form_injury_grade']) . "', " .
554 "injury_part = '" . add_escape_custom($form_injury_part) . "', " .
555 "injury_type = '" . add_escape_custom($form_injury_type) . "', " .
556 "outcome = '" . add_escape_custom($_REQUEST['form_outcome']) . "', " .
557 "destination = '" . add_escape_custom($_REQUEST['form_destination']) . "', " .
558 "reaction ='" . add_escape_custom($_REQUEST['form_reaction']) . "', " .
559 "erx_uploaded = '0', " .
560 "modifydate = NOW(), " .
561 "subtype = '" . $subtype. "' " .
562 "WHERE id = '" . add_escape_custom($issue) . "'";
563 sqlStatement($query);
564 if ($text_type == "medication" && enddate != '') {
565 sqlStatement('UPDATE prescriptions SET '
566 . 'medication = 0 where patient_id = ? '
567 . " and upper(trim(drug)) = ? "
568 . ' and medication = 1', array($pid,strtoupper($_REQUEST['form_title'])) );
570 } else {
571 $query = "INSERT INTO lists ( " .
572 "date, pid, type, title, activity, comments, ".
573 "begdate, enddate, returndate, " .
574 "diagnosis, occurrence, classification, referredby, user, " .
575 "groupname, outcome, destination,reaction,subtype " .
576 ") VALUES ( " .
577 "NOW(), ?,?,?,1,?," .
578 QuotedOrNull($form_begin).", ".QuotedOrNull($form_end).", ".QuotedOrNull($form_return). ", " .
579 "?,?,?,?,?,".
580 "?,?,?,?,?)";
581 $issue = sqlInsert($query,array($pid,$form_type,$_REQUEST['form_title'],$_REQUEST['form_comments'],
582 $_REQUEST['form_diagnosis'],$_REQUEST['form_occur'],$_REQUEST['form_clasification'],$_REQUEST['form_referredby'],$_SESSION['authUser'],
583 $_SESSION['authProvider'],QuotedOrNull($_REQUEST['form_outcome']),$_REQUEST['form_destination'],$_REQUEST['form_reaction'],$subtype));
585 // For record/reporting purposes, place entry in lists_touch table.
586 setListTouch($pid,$form_type);
588 // If requested, link the issue to a specified encounter.
589 // we always link them, automatically.
590 if ($encounter) {
591 $query = "INSERT INTO issue_encounter ( " .
592 "pid, list_id, encounter " .
593 ") VALUES ( ?,?,? )";
594 sqlStatement($query, array($pid,$issue,$encounter));
598 $irow = '';
599 //if it is a medication do we need to do something with dosage fields?
600 //leave all in title field form now.
602 $PMSFH = build_PMSFH($pid);
603 send_json_values($PMSFH);
604 exit;
607 if ($_REQUEST['action'] =='code_PMSFH') {
608 $query = "UPDATE lists SET diagnosis = ? WHERE id = ?";
609 sqlStatement($query,array($_POST['code'],$_POST['issue']));
610 exit;
613 if ($_REQUEST['action'] == 'code_visit'){
614 $CODING = json_decode($_REQUEST['parameter'],true);
615 $query = "delete from billing where encounter =?";
616 sqlStatement($query,array($encounter));
617 foreach ($CODING as $item) //need toremove duplicate codes
619 if ($dups[$item["code"]]=='1') {
620 continue;
622 $dups[$item["code"]] = "1";
623 $sql = "SELECT codes.*, prices.pr_price FROM codes " .
624 "LEFT OUTER JOIN patient_data ON patient_data.pid = '$pid' " .
625 "LEFT OUTER JOIN prices ON prices.pr_id = codes.id AND " .
626 "prices.pr_selector = '' AND " .
627 "prices.pr_level = patient_data.pricelevel " .
628 "WHERE code =?" .
629 " LIMIT 1";
630 $result = sqlStatement($sql,array($item['code']));
631 while ($res = sqlFetchArray($result)) {
632 $item["codedesc"] = $res["code_text"];// eg. = "NP EYE intermediate exam"
633 if (!$item["modifier"]) $modifier = $res["modifier"];
634 $item["units"] = $res["units"];
635 $item["fee"] = $res["pr_price"];
637 addBilling($encounter,$item["codetype"],$item["code"],$item["codedesc"],$pid,'1',$providerID,$item["modifier"], $item["units"], $item["fee"],$ndc_info, $justify, $billed, '');
639 echo 'ok';
640 exit;
642 /*** END CODE to DEAL WITH PMSFH/ISUUE_TYPES ****/
644 /* Let's save the encounter specific values.
645 // Any field that exists in the database could be updated
646 // so we need to exclude the important ones...
647 // id date pid user groupname authorized activity. Any other just add them below.
648 // Doing it this way means you can add new fields on a web page and in the DB without touching this function.
649 // The update feature still works because it only updates columns that are in the table you are working on.
651 $query = "SHOW COLUMNS from ".$table_name."";
652 $result = sqlStatement($query);
653 if (!$result) {
654 return 'Could not run query: No columns found in your table! ' . mysql_error();
655 exit;
657 $fields = array();
658 if (($_POST['IOPTIME'] == '00:00:00')||(!$_POST['IOPTIME'])) {
659 $_POST['IOPTIME'] = date('H:i:s');
661 $_POST['IOPTIME'] = date('H:i:s',strtotime($_POST['IOPTIME']));
663 if (sqlNumRows($result) > 0) {
664 while ($row = sqlFetchArray($result)) {
665 //exclude critical columns/fields and those needing special processing from update
666 if ($row['Field'] == 'id' or
667 $row['Field'] == 'date' or
668 $row['Field'] == 'pid' or
669 $row['Field'] == 'user' or
670 $row['Field'] == 'groupname' or
671 $row['Field'] == 'authorized' or
672 $row['Field'] == 'LOCKED' or
673 $row['Field'] == 'LOCKEDBY' or
674 $row['Field'] == 'activity' or
675 $row['Field'] == 'PLAN' or
676 $row['Field'] == 'Resource')
677 continue;
678 if (isset($_POST[$row['Field']])) $fields[$row['Field']] = $_POST[$row['Field']];
680 // orders are checkboxes created from a user defined list in the PLAN area and stored as item1|item2|item3
681 // if there are any, create the $field['PLAN'] value.
682 // Remember -- If you uncheck a box, it won't be sent!
683 // So delete all made today by this provider and reload with any Orders sent in this $_POST
684 // in addition, we made a special table for orders, and when completed we can mark done?
685 $query="select form_encounter.date as encounter_date from form_encounter where form_encounter.encounter =?";
686 $encounter_data =sqlQuery($query,array($encounter));
687 $dated = new DateTime($encounter_data['encounter_date']);
688 $dated = $dated->format('Y-m-d');
689 $visit_date = oeFormatShortDate($dated);
691 $N = count($_POST['PLAN']);
692 $sql_clear = "DELETE from form_eye_mag_orders where ORDER_PID =? and ORDER_PLACED_BYWHOM=? and ORDER_DATE_PLACED=? and ORDER_STATUS ='pending'";
693 sqlQuery($sql_clear,array($pid,$providerID,$visit_date));
694 if ($N > '0') {
695 for($i=0; $i < $N; $i++)
697 $fields['PLAN'] .= $_POST['PLAN'][$i] . "|"; //this makes an entry for form_eyemag: PLAN
698 $ORDERS_sql = "REPLACE INTO form_eye_mag_orders (ORDER_PID,ORDER_DETAILS,ORDER_STATUS,ORDER_DATE_PLACED,ORDER_PLACED_BYWHOM) VALUES (?,?,?,?,?)";
699 $okthen = sqlQuery($ORDERS_sql,array($pid,$_POST['PLAN'][$i],'pending',$visit_date,$providerID));
701 $fields['PLAN'] = mb_substr($fields['PLAN'], 0, -1); //get rid of trailing "|"
703 if ($_REQUEST['PLAN2']) {
704 $fields['PLAN'] .= $_REQUEST['PLAN2'];
705 //there is something in the "freeform" plan textarea...
706 $ORDERS_sql = "REPLACE INTO form_eye_mag_orders (ORDER_PID,ORDER_DETAILS,ORDER_STATUS,ORDER_PRIORITY,ORDER_DATE_PLACED,ORDER_PLACED_BYWHOM) VALUES (?,?,?,?,?,?)";
707 $okthen = sqlQuery($ORDERS_sql,array($pid,$_POST['PLAN'][$i],'pending',"PLAN2:$PLAN2",$visit_date,$providerID));
710 $M = count($_POST['TEST']);
711 if ($M > '0') {
712 for($i=0; $i < $M; $i++)
714 $fields['Resource'] .= $_POST['TEST'][$i] . "|"; //this makes an entry for form_eyemag: Resource
716 $fields['Resource'] = mb_substr($fields['Resource'], 0, -1); //get rid of trailing "|"
719 /** Empty Checkboxes need to be entered manually as they are only submitted via POST when they are checked
720 * If NOT checked on the form, they are sent via POST and thus are NOT overridden in the DB,
721 * so DB won't change unless we define them into the $fields array as "0"...
723 if (!$_POST['alert']) $fields['alert'] = '0';
724 if (!$_POST['oriented']) $fields['oriented'] = '0';
725 if (!$_POST['confused']) $fields['confused'] = '0';
726 if (!$_POST['PUPIL_NORMAL']) $fields['PUPIL_NORMAL'] = '0';
727 if (!$_POST['MOTILITYNORMAL']) $fields['MOTILITYNORMAL'] = '0';
728 if (!$_POST['ACT']) $fields['ACT'] = 'off';
729 if (!$_POST['DIL_RISKS']) $fields['DIL_RISKS'] = '0';
730 if (!$_POST['ATROPINE']) $fields['ATROPINE'] = '0';
731 if (!$_POST['CYCLOGYL']) $fields['CYCLOGYL'] = '0';
732 if (!$_POST['CYCLOMYDRIL']) $fields['CYCLOMYDRIL'] = '0';
733 if (!$_POST['NEO25']) $fields['NEO25'] = '0';
734 if (!$_POST['TROPICAMIDE']) $fields['TROPICAMIDE'] = '0';
735 if (!$_POST['BALANCED']) $fields['BALANCED'] = '0';
736 if (!$_POST['ODVF1']) $fields['ODVF1'] = '0';
737 if (!$_POST['ODVF2']) $fields['ODVF2'] = '0';
738 if (!$_POST['ODVF3']) $fields['ODVF3'] = '0';
739 if (!$_POST['ODVF4']) $fields['ODVF4'] = '0';
740 if (!$_POST['OSVF1']) $fields['OSVF1'] = '0';
741 if (!$_POST['OSVF2']) $fields['OSVF2'] = '0';
742 if (!$_POST['OSVF3']) $fields['OSVF3'] = '0';
743 if (!$_POST['OSVF4']) $fields['OSVF4'] = '0';
744 if (!$_POST['TEST']) $fields['Resource'] = '';
745 if (!$fields['PLAN']) $fields['PLAN'] = '0';
747 $success = formUpdate($table_name, $fields, $form_id, $_SESSION['userauthorized']);
749 //now save any Wear RXs (1-4) entered.
750 $rx_number='1';
751 if ($_POST['W_1']=='1') {
752 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
753 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
754 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
755 `RX_TYPE` ,`COMMENTS`,
756 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
757 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
758 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
759 `LENS_TREATMENTS`
760 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
761 $LENS_TREATMENTS_1 = implode("|",$_POST['LENS_TREATMENTS_1']);
762 sqlQuery($query,array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_1'],$_POST['ODCYL_1'],$_POST['ODAXIS_1'],
763 $_POST['ODVA_1'],$_POST['ODADD_1'],$_POST['ODNEARVA_1'],$_POST['OSSPH_1'],$_POST['OSCYL_1'],$_POST['OSAXIS_1'],
764 $_POST['OSVA_1'],$_POST['OSADD_1'],$_POST['OSNEARVA_1'],$_POST['ODMIDADD_1'],$_POST['OSMIDADD_1'],
765 0+$_POST['RX_TYPE_1'],$_POST['COMMENTS_1'],
766 $_POST['ODHPD_1'],$_POST['ODHBASE_1'],$_POST['ODVPD_1'],$_POST['ODVBASE_1'],$_POST['ODSLABOFF_1'],$_POST['ODVERTEXDIST_1'],
767 $_POST['OSHPD_1'],$_POST['OSHBASE_1'],$_POST['OSVPD_1'],$_POST['OSVBASE_1'],$_POST['OSSLABOFF_1'],$_POST['OSVERTEXDIST_1'],
768 $_POST['ODMPDD_1'],$_POST['ODMPDN_1'],$_POST['OSMPDD_1'],$_POST['OSMPDN_1'],$_POST['BPDD_1'],$_POST['BPDN_1'],$_POST['LENS_MATERIAL_1'],
769 $LENS_TREATMENTS_1 ));
770 $rx_number++;
771 } else {
772 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
773 sqlQuery($query,array($encounter,$pid,$form_id,'1'));
775 if ($_POST['W_2']=='1') {
776 //store W_2
777 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
778 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
779 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
780 `RX_TYPE` ,`COMMENTS`,
781 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
782 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
783 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
784 `LENS_TREATMENTS`
785 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
786 $LENS_TREATMENTS_2 = implode("|",$_POST['LENS_TREATMENTS_2']);
787 sqlQuery($query,array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_2'],$_POST['ODCYL_2'],$_POST['ODAXIS_2'],
788 $_POST['ODVA_2'],$_POST['ODADD_2'],$_POST['ODNEARVA_2'],$_POST['OSSPH_2'],$_POST['OSCYL_2'],$_POST['OSAXIS_2'],
789 $_POST['OSVA_2'],$_POST['OSADD_2'],$_POST['OSNEARVA_2'],$_POST['ODMIDADD_2'],$_POST['OSMIDADD_2'],
790 0+$_POST['RX_TYPE_2'],$_POST['COMMENTS_2'],
791 $_POST['ODHPD_2'],$_POST['ODHBASE_2'],$_POST['ODVPD_2'],$_POST['ODVBASE_2'],$_POST['ODSLABOFF_2'],$_POST['ODVERTEXDIST_2'],
792 $_POST['OSHPD_2'],$_POST['OSHBASE_2'],$_POST['OSVPD_2'],$_POST['OSVBASE_2'],$_POST['OSSLABOFF_2'],$_POST['OSVERTEXDIST_2'],
793 $_POST['ODMPDD_2'],$_POST['ODMPDN_2'],$_POST['OSMPDD_2'],$_POST['OSMPDN_2'],$_POST['BPDD_2'],$_POST['BPDN_2'],$_POST['LENS_MATERIAL_2'],
794 $LENS_TREATMENTS_2 ));
795 $rx_number++;
796 } else {
797 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
798 sqlQuery($query,array($encounter,$pid,$form_id,'2'));
800 if ($_POST['W_3']=='1') {
801 //store W_3
802 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
803 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
804 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
805 `RX_TYPE` ,`COMMENTS`,
806 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
807 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
808 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
809 `LENS_TREATMENTS`
810 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
811 $LENS_TREATMENTS_3 = implode("|",$_POST['LENS_TREATMENTS_3']);
812 sqlQuery($query,array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_3'],$_POST['ODCYL_3'],$_POST['ODAXIS_3'],
813 $_POST['ODVA_3'],$_POST['ODADD_3'],$_POST['ODNEARVA_3'],$_POST['OSSPH_3'],$_POST['OSCYL_3'],$_POST['OSAXIS_3'],
814 $_POST['OSVA_3'],$_POST['OSADD_3'],$_POST['OSNEARVA_3'],$_POST['ODMIDADD_3'],$_POST['OSMIDADD_3'],
815 0+$_POST['RX_TYPE_3'],$_POST['COMMENTS_3'],
816 $_POST['ODHPD_3'],$_POST['ODHBASE_3'],$_POST['ODVPD_3'],$_POST['ODVBASE_3'],$_POST['ODSLABOFF_3'],$_POST['ODVERTEXDIST_3'],
817 $_POST['OSHPD_3'],$_POST['OSHBASE_3'],$_POST['OSVPD_3'],$_POST['OSVBASE_3'],$_POST['OSSLABOFF_3'],$_POST['OSVERTEXDIST_3'],
818 $_POST['ODMPDD_3'],$_POST['ODMPDN_3'],$_POST['OSMPDD_3'],$_POST['OSMPDN_3'],$_POST['BPDD_3'],$_POST['BPDN_3'],$_POST['LENS_MATERIAL_3'],
819 $LENS_TREATMENTS_3 ));
820 $rx_number++;
821 } else {
822 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
823 sqlQuery($query,array($encounter,$pid,$form_id,'3'));
825 if ($_POST['W_4']=='1') {
826 //store W_4
827 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
828 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
829 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
830 `RX_TYPE` ,`COMMENTS`,
831 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
832 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
833 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
834 `LENS_TREATMENTS`
835 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
836 $LENS_TREATMENTS_4 = implode("|",$_POST['LENS_TREATMENTS_4']);
837 sqlQuery($query,array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_4'],$_POST['ODCYL_4'],$_POST['ODAXIS_4'],
838 $_POST['ODVA_4'],$_POST['ODADD_4'],$_POST['ODNEARVA_4'],$_POST['OSSPH_4'],$_POST['OSCYL_4'],$_POST['OSAXIS_4'],
839 $_POST['OSVA_4'],$_POST['OSADD_4'],$_POST['OSNEARVA_4'],$_POST['ODMIDADD_4'],$_POST['OSMIDADD_4'],
840 0+$_POST['RX_TYPE_4'],$_POST['COMMENTS_4'],
841 $_POST['ODHPD_4'],$_POST['ODHBASE_4'],$_POST['ODVPD_4'],$_POST['ODVBASE_4'],$_POST['ODSLABOFF_4'],$_POST['ODVERTEXDIST_4'],
842 $_POST['OSHPD_4'],$_POST['OSHBASE_4'],$_POST['OSVPD_4'],$_POST['OSVBASE_4'],$_POST['OSSLABOFF_4'],$_POST['OSVERTEXDIST_4'],
843 $_POST['ODMPDD_4'],$_POST['ODMPDN_4'],$_POST['OSMPDD_4'],$_POST['OSMPDN_4'],$_POST['BPDD_4'],$_POST['BPDN_4'],$_POST['LENS_MATERIAL_4'],
844 $LENS_TREATMENTS_4 ));
845 $rx_number++;
846 } else {
847 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
848 sqlQuery($query,array($encounter,$pid,$form_id,'4'));
850 for ($i=$rx_number; $i < 5; $i++) {
851 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
852 sqlQuery($query,array($encounter,$pid,$form_id,$i));
854 //now return the obj
855 $send['IMPPLAN_items'] = build_IMPPLAN_items($pid,$form_id);
856 $send['Clinical'] = start_your_engines($_REQUEST);
857 $send['PMH_panel'] = display_PMSFH('2');
858 $send['right_panel'] = show_PMSFH_panel($PMSFH);
859 $send['PMSFH'] = $PMSFH[0];
860 echo json_encode($send);
861 exit;
863 } elseif ($_REQUEST["mode"] == "retrieve") {
865 if ($_REQUEST['PRIORS_query']) {
866 echo display_PRIOR_section($_REQUEST['zone'],$_REQUEST['orig_id'],$_REQUEST['id_to_show'],$pid);
867 exit;
872 * Save the canvas drawings
875 if ($_REQUEST['canvas']) {
876 if (!$pid||!$encounter||!$zone||!$_POST["imgBase64"]) exit;
878 $side = "OU";
879 $base_name = $pid."_".$encounter."_".$side."_".$zone."_VIEW";
880 $filename = $base_name.".jpg";
882 $type = "image/jpeg"; // all our canvases are this type
883 $data = $_POST["imgBase64"];
884 $data = substr($data, strpos($data, ",")+1);
885 $data = base64_decode($data);
886 $size = strlen($data);
887 $query = "select id from categories where name = 'Drawings'";
888 $result = sqlStatement($query);
889 $ID = sqlFetchArray($result);
890 $category_id = $ID['id'];
892 // We want to overwrite so only one image is stored per zone per form/encounter
893 // I do not believe this function exists in the current library, ie "UpdateDocument" function, so...
894 // we need to delete the previous file from the documents and categories to documents tables and the actual file
895 // There must be a delete_file function in documents class?
896 // cannot find it.
897 // this will work for harddisk people, not sure about couchDB people:
898 $filepath = $GLOBALS['oer_config']['documents']['repository'] . $pid ."/";
899 foreach (glob($filepath.'/'.$filename) as $file) {
900 unlink($file);
903 $sql = "DELETE from categories_to_documents where document_id IN (SELECT id from documents where documents.url like '%".$filename."')";
904 sqlQuery($sql);
905 $sql ="DELETE from documents where documents.url like '%".$filename."'";
906 sqlQuery($sql);
907 $return = addNewDocument($filename,$type,$_POST["imgBase64"],0,$size,$_SESSION['authUserID'],$pid,$category_id);
908 $doc_id = $return['doc_id'];
909 $sql = "UPDATE documents set encounter_id=? where id=?"; //link it to this encounter
910 sqlQuery($sql,array($encounter,$doc_id));
911 exit;
914 if ($_REQUEST['copy']) {
915 copy_forward($_REQUEST['zone'],$_REQUEST['copy_from'],$_SESSION['ID'],$pid);
916 return;
918 function QuotedOrNull($fld) {
919 if ($fld) return "'".add_escape_custom($fld)."'";
920 return "NULL";
922 function debug($local_var) {
923 echo "<pre><BR>We are in the debug function.<BR>";
924 echo "Passed variable = ". $local_var . " <BR>";
925 print_r($local_var);
926 exit;
929 /* From original issue.php */
931 function row_delete($table, $where) {
932 $query = "SELECT * FROM $table WHERE $where";
933 $tres = sqlStatement($query);
934 $count = 0;
935 while ($trow = sqlFetchArray($tres)) {
936 $logstring = "";
937 foreach ($trow as $key => $value) {
938 if (! $value || $value == '0000-00-00 00:00:00') continue;
939 if ($logstring) $logstring .= " ";
940 $logstring .= $key . "='" . addslashes($value) . "'";
942 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
943 ++$count;
945 if ($count) {
946 $query = "DELETE FROM $table WHERE $where";
947 sqlStatement($query);
950 // Given an issue type as a string, compute its index.
951 // Not sure of the value of this sub given transition to array $PMSFH
952 // Can I use it to find out which PMSFH item we are looking for? YES
953 function issueTypeIndex($tstr) {
954 global $ISSUE_TYPES;
955 $i = 0;
956 foreach ($ISSUE_TYPES as $key => $value) {
957 if ($key == $tstr) break;
958 ++$i;
960 return $i;
964 * The following 2 functions can be removed from the production environment
967 function merge($filename_x, $filename_y, $filename_result) {
969 * Three png files (OU,OD,OS) per LOCATION (EXT,ANTSEG,RETINA,NEURO)
970 * BASE, found in forms/$form_folder/images eg. OU_EXT_BASE.png
971 * BASE is the blank image to start from and can be customized. Currently 432x150px
972 * VIEW, found in /sites/$_SESSION['site_id']."/".$form_folder."/".$pid."/".$encounter
973 * TEMP, intermediate png merge file of new drawings with BASE or previous VIEW
974 * These are saved to be used in an undo feature...
975 * NO LONGER USING but I kept it here because it is cool and I will use it later
978 This section
979 if (file_exists($storage."/OU_".$zone."_VIEW.png")) { //add new drawings to previous for this encounter
980 $file_base = $storage."/OU_".$zone."_VIEW.png";
981 } else { //start from the base image
982 $file_base = $GLOBALS['webserver_root']."/interface/forms/".$form_folder."/images/OU_".$zone."_BASE.png";
984 //merge needs to store to a separate file first, then rename to new VIEW
985 $file_temp = $storage."/OU_".$zone."_TEMP.png";
986 $file_here = $storage."/OU_".$zone."_VIEW.png";
987 merge( $file_draw, $file_base, $file_temp);
988 rename( $file_temp , $file_here );
990 // Get dimensions for specified images
991 list($width_x, $height_x) = getimagesize($filename_x);
992 list($width_y, $height_y) = getimagesize($filename_y);
994 // Create new image with desired dimensions
995 $image = imagecreatetruecolor($width_y, $height_y);
997 // Load images and then copy to destination image
998 $image_x = imagecreatefrompng($filename_x);
999 $image_y = imagecreatefrompng($filename_y);
1001 imagecopy($image, $image_y, 0, 0, 0, 0, $width_x, $height_x);
1002 imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
1004 // Save the resulting image to disk (as png)
1005 imagepng($image, $filename_result);
1007 // Clean up
1008 imagedestroy($image);
1009 imagedestroy($image_x);
1010 imagedestroy($image_y);
1013 // this function is here to understand the core openEMR function addBilling, so we can improve the Billing Engine in Eye Form
1014 // We still need to add modifiers and justify capabilities to the Coding Engine...
1015 function addBilling2($encounter_id, $code_type, $code, $code_text, $pid, $authorized="0", $provider, $modifier="", $units="", $fee="0.00",
1016 $ndc_info='', $justify='', $billed=0, $notecodes='')
1018 $sql = "insert into billing (date, encounter, code_type, code, code_text, " .
1019 "pid, authorized, user, groupname, activity, billed, provider_id, " .
1020 "modifier, units, fee, ndc_info, justify, notecodes) values (" .
1021 "NOW(), ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?)";
1022 return sqlInsert($sql,array( $encounter_id,$code_type,$code,$code_text,$pid,$authorized,$_SESSION['authId'],$_SESSION['authProvider'], $billed,$provider,$modifier,$units,$fee,$ndc_info,$justify,$notecodes));
1024 exit;