Multiple improvements from IPPF related to layouts. (#1081)
[openemr.git] / interface / forms / eye_mag / save.php
blobbf219e16578e602cd2c3d652260a55c7f046e181
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
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 . "/../controllers/C_Document.class.php");
53 require_once($srcdir . "/documents.php");
54 require_once("$srcdir/patient.inc");
55 require_once("$srcdir/options.inc.php");
56 require_once("$srcdir/acl.inc");
57 require_once("$srcdir/lists.inc");
58 require_once("$srcdir/report.inc");
59 require_once("$srcdir/html2pdf/html2pdf.class.php");
61 $returnurl = 'encounter_top.php';
63 if (isset($_REQUEST['id'])) {
64 $id = $_REQUEST['id'];
67 if (!$id) {
68 $id = $_REQUEST['pid'];
71 $encounter = $_REQUEST['encounter'];
73 $AJAX_PREFS = $_REQUEST['AJAX_PREFS'];
74 if ($encounter == "" && !$id && !$AJAX_PREFS && (($_REQUEST['mode'] != "retrieve") or ($_REQUEST['mode'] == "show_PDF"))) {
75 echo "Sorry Charlie..."; //should lead to a database of errors for explanation.
76 exit;
79 /**
80 * Save/update the preferences
82 if ($_REQUEST['AJAX_PREFS']) {
83 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
84 VALUES
85 ('PREFS','VA','Vision',?,'RS','51',?,'1')";
86 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_VA']));
88 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
89 VALUES
90 ('PREFS','W','Current Rx',?,'W','52',?,'2')";
91 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_W']));
93 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
94 VALUES
95 ('PREFS','W_width','Detailed Rx',?,'W_width','80',?,'100')";
96 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_W_width']));
98 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
99 VALUES
100 ('PREFS','MR','Manifest Refraction',?,'MR','53',?,'3')";
101 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_MR']));
103 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
104 VALUES
105 ('PREFS','MR_width','Detailed MR',?,'MR_width','81',?,'110')";
106 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_W_width']));
108 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
109 VALUES
110 ('PREFS','CR','Cycloplegic Refraction',?,'CR','54',?,'4')";
111 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_CR']));
113 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
114 VALUES
115 ('PREFS','CTL','Contact Lens',?,'CTL','55',?,'5')";
116 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_CTL']));
118 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
119 VALUES
120 ('PREFS', 'VAX', 'Visual Acuities', ?, 'VAX','65', ?,'15')";
121 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_VAX']));
123 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
124 VALUES
125 ('PREFS','ADDITIONAL','Additional Data Points',?,'ADDITIONAL','56',?,'6')";
126 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_ADDITIONAL']));
128 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
129 VALUES
130 ('PREFS','CLINICAL','CLINICAL',?,'CLINICAL','57',?,'7')";
131 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_CLINICAL']));
133 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
134 VALUES
135 ('PREFS','IOP','Intraocular Pressure',?,'IOP','67',?,'17')";
136 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_IOP']));
138 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
139 VALUES
140 ('PREFS','EXAM','EXAM',?,'EXAM','58',?,'8')";
141 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_EXAM']));
143 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
144 VALUES
145 ('PREFS','CYLINDER','CYL',?,'CYL','59',?,'9')";
146 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_CYL']));
148 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
149 VALUES
150 ('PREFS','HPI_VIEW','HPI View',?,'HPI_VIEW','60',?,'10')";
151 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_HPI_VIEW']));
153 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
154 VALUES
155 ('PREFS','EXT_VIEW','External View',?,'EXT_VIEW','66',?,'16')";
156 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_EXT_VIEW']));
158 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
159 VALUES
160 ('PREFS','ANTSEG_VIEW','Anterior Segment View',?,'ANTSEG_VIEW','61',?,'11')";
161 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_ANTSEG_VIEW']));
163 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
164 VALUES
165 ('PREFS','RETINA_VIEW','Retina View',?,'RETINA_VIEW','62',?,'12')";
166 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_RETINA_VIEW']));
168 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
169 VALUES
170 ('PREFS','NEURO_VIEW','Neuro View',?,'NEURO_VIEW','63',?,'13')";
171 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_NEURO_VIEW']));
173 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
174 VALUES
175 ('PREFS','ACT_VIEW','ACT View',?,'ACT_VIEW','64',?,'14')";
176 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_ACT_VIEW']));
178 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
179 VALUES
180 ('PREFS','ACT_SHOW','ACT Show',?,'ACT_SHOW','65',?,'15')";
181 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_ACT_SHOW']));
183 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
184 VALUES
185 ('PREFS','HPI_RIGHT','HPI DRAW',?,'HPI_RIGHT','70',?,'16')";
186 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_HPI_RIGHT']));
188 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
189 VALUES
190 ('PREFS','PMH_RIGHT','PMH DRAW',?,'PMH_RIGHT','71',?,'17')";
191 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_PMH_RIGHT']));
192 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
193 VALUES
194 ('PREFS','EXT_RIGHT','EXT DRAW',?,'EXT_RIGHT','72',?,'18')";
195 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_EXT_RIGHT']));
196 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
197 VALUES
198 ('PREFS','ANTSEG_RIGHT','ANTSEG DRAW',?,'ANTSEG_RIGHT','73',?,'19')";
199 $result = sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_ANTSEG_RIGHT']));
201 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
202 VALUES
203 ('PREFS','RETINA_RIGHT','RETINA DRAW',?,'RETINA_RIGHT','74',?,'20')";
204 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_RETINA_RIGHT']));
205 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
206 VALUES
207 ('PREFS','NEURO_RIGHT','NEURO DRAW',?,'NEURO_RIGHT','75',?,'21')";
208 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_NEURO_RIGHT']));
209 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
210 VALUES
211 ('PREFS','IMPPLAN_RIGHT','IMPPLAN DRAW',?,'IMPPLAN_RIGHT','76',?,'22')";
212 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_IMPPLAN_RIGHT']));
213 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
214 VALUES
215 ('PREFS','PANEL_RIGHT','PMSFH Panel',?,'PANEL_RIGHT','77',?,'23')";
216 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_PANEL_RIGHT']));
217 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
218 VALUES
219 ('PREFS','KB_VIEW','KeyBoard View',?,'KB_VIEW','78',?,'24')";
220 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_KB']));
221 $query = "REPLACE INTO ".$table_name."_prefs (PEZONE,LOCATION,LOCATION_text,id,selection,ZONE_ORDER,GOVALUE,ordering)
222 VALUES
223 ('PREFS','TOOLTIPS','Toggle Tooltips',?,'TOOLTIPS','79',?,'25')";
224 sqlQuery($query, array($_SESSION['authId'],$_REQUEST['PREFS_TOOLTIPS']));
228 * ADD ANY NEW PREFERENCES above, and as a hidden field in the body.
231 /** <!-- End Preferences --> **/
234 * Create, update or retrieve a form and its values
236 if (!$pid) {
237 $pid = $_SESSION['pid'];
240 $userauthorized = $_SESSION['userauthorized'];
241 if ($encounter == "") {
242 $encounter = date("Ymd");
245 $form_id = $_REQUEST['form_id'];
246 $zone = $_REQUEST['zone'];
248 $providerID = findProvider($pid, $encounter);
249 if ($providerID =='0') {
250 $providerID = $userauthorized;//who is the default provider?
253 $providerNAME = getProviderName($providerID);
255 // The form is submitted to be updated or saved in some way.
256 // Give each instance of a form a uniqueID. If the form has no owner, update DB with this uniqueID.
257 // If the DB shows a uniqueID ie. an owner, and the save request uniqueID does not = the uniqueID in the DB,
258 // ask if the new user wishes to take ownership?
259 // If yes, any other's attempt to save fields/form are denied and the return code says you are not the owner...
260 if ($_REQUEST['unlock'] == '1') {
261 // we are releasing the form, by closing the page or clicking on ACTIVE FORM, so unlock it.
262 // if it's locked and they own it ($REQUEST[LOCKEDBY] == LOCKEDBY), they can unlock it
263 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from ".$table_name." WHERE ID=?";
264 $lock = sqlQuery($query, array($form_id));
265 if (($lock['LOCKED'] >'') && ($_REQUEST['LOCKEDBY'] == $lock['LOCKEDBY'])) {
266 $query = "update ".$table_name." set LOCKED='',LOCKEDBY='' where id=?";
267 sqlQuery($query, array($form_id));
270 exit;
271 } elseif ($_REQUEST['acquire_lock']=="1") {
272 //we are taking over the form's active state, others will go read-only
273 $query = "UPDATE ".$table_name." set LOCKED='1',LOCKEDBY=? where id=? and LOCKEDBY=?";
274 $result = sqlQuery($query, array($_REQUEST['uniqueID'],$form_id,$_REQUEST['locked_by']));
275 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from ".$table_name." WHERE ID=?";
276 $lock = sqlQuery($query, array($form_id));
277 exit;
278 } else {
279 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from ".$table_name." WHERE ID=?";
280 $lock = sqlQuery($query, array($form_id));
281 if (($lock['LOCKED']) && ($_REQUEST['uniqueID'] != $lock['LOCKEDBY'])) {
282 // We are not the owner or it is not new so it is locked
283 // Did the user send a demand to take ownership?
284 if ($lock['LOCKEDBY'] != $_REQUEST['ownership']) {
285 //tell them they are locked out by another user now
286 echo "Code 400";
287 // or return a JSON encoded string with current LOCK ID?
288 // echo "Sorry Charlie, you get nothing since this is locked... No save for you!";
289 exit;
290 } elseif ($lock['LOCKEDBY'] == $_REQUEST['ownership']) {
291 // then they are taking ownership - all others get locked...
292 // new LOCKEDBY becomes our uniqueID LOCKEDBY
293 $_REQUEST['LOCKED'] = '1';
294 $_REQUEST['LOCKEDBY'] = $_REQUEST['uniqueID'];
295 //update table
296 $query = "update ".$table_name." set LOCKED=?,LOCKEDBY=? where id=?";
297 sqlQuery($query, array('1',$_REQUEST['LOCKEDBY'],$form_id));
298 //go on to save what we want...
300 } elseif (!$lock['LOCKED']) { // it is not locked yet
301 $_REQUEST['LOCKED'] = '1';
302 $query = "update ".$table_name." set LOCKED=?,LOCKEDBY=? where id=?";
303 sqlQuery($query, array('1',$_REQUEST['LOCKEDBY'],$form_id));
304 //go on to save what we want...
307 if (!$_REQUEST['LOCKEDBY']) {
308 $_REQUEST['LOCKEDBY'] = rand();
312 if ($_REQUEST["mode"] == "new") {
313 $newid = formSubmit($table_name, $_POST, $id, $userauthorized);
314 addForm($encounter, $form_name, $newid, $form_folder, $pid, $userauthorized);
315 } elseif ($_REQUEST["mode"] == "update") {
316 // The user has write privileges to work with...
317 if ($_REQUEST['action']=="store_PDF") {
319 * We want to store/overwrite the current PDF version of this encounter's f
320 * Currently this is only called 'beforeunload', ie. when you finish the form
321 * In this current paradigm, anytime the form is opened, then closed, the PDF
322 * is overwritten. With esign implemented, the PDF should be locked. I suppose
323 * with esign the form can't even be opened so the only way to get to the PDF
324 * is through the Documents->Encounters links.
326 $query = "select id from categories where name = 'Encounters'";
327 $result = sqlStatement($query);
328 $ID = sqlFetchArray($result);
329 $category_id = $ID['id'];
330 $PDF_OUTPUT='1';
332 $filename = $pid."_".$encounter.".pdf";
333 $filepath = $GLOBALS['oer_config']['documents']['repository'] . $pid;
334 foreach (glob($filepath.'/'.$filename) as $file) {
335 unlink($file);
338 $sql = "DELETE from categories_to_documents where document_id IN (SELECT id from documents where documents.url like '%".$filename."')";
339 sqlQuery($sql);
340 $sql = "DELETE from documents where documents.url like '%".$filename."'";
341 sqlQuery($sql);
342 // We want to overwrite so only one PDF is stored per form/encounter
343 // $pdf = new HTML2PDF('P', 'Letter', 'en', array(5, 5, 5, 5) ); // add a little margin 5cm all around TODO: add to globals
345 /***********/
347 $pdf = new HTML2PDF(
348 $GLOBALS['pdf_layout'],
349 $GLOBALS['pdf_size'],
350 $GLOBALS['pdf_language'],
351 true, // default unicode setting is true
352 'UTF-8', // default encoding setting is UTF-8
353 array($GLOBALS['pdf_left_margin'],$GLOBALS['pdf_top_margin'],$GLOBALS['pdf_right_margin'],$GLOBALS['pdf_bottom_margin']),
354 $_SESSION['language_direction'] == 'rtl' ? true : false
356 ob_start();
358 <link rel="stylesheet" href="<?php echo $webserver_root; ?>/interface/themes/style_pdf.css" type="text/css">
359 <div id="report_custom" style="width:100%;"> <!-- large outer DIV -->
360 <?php
361 echo report_header($pid);
362 include_once($GLOBALS['incdir'] . "/forms/eye_mag/report.php");
363 call_user_func($form_name . "_report", $pid, $form_encounter, $N, $form_id);
364 if ($printable) {
365 echo "" . xl('Signature') . ": _______________________________<br />";
368 </div> <!-- end of report_custom DIV -->
370 <?php
372 global $web_root, $webserver_root;
373 $content = ob_get_clean();
374 // Fix a nasty html2pdf bug - it ignores document root!
375 $i = 0;
376 $wrlen = strlen($web_root);
377 $wsrlen = strlen($webserver_root);
378 while (true) {
379 $i = stripos($content, " src='/", $i + 1);
380 if ($i === false) {
381 break;
384 if (substr($content, $i+6, $wrlen) === $web_root &&
385 substr($content, $i+6, $wsrlen) !== $webserver_root) {
386 $content = substr($content, 0, $i + 6) . $webserver_root . substr($content, $i + 6 + $wrlen);
390 $pdf->writeHTML($content, false);
391 $temp_filename = '/tmp/'.$filename;
392 $content_pdf = $pdf->Output($temp_filename, 'F');
393 $type = "application/pdf";
394 $size = filesize($temp_filename);
395 $return = addNewDocument($filename, $type, $temp_filename, 0, $size, $_SESSION['authUserID'], $pid, $category_id);
396 $doc_id = $return['doc_id'];
397 $sql = "UPDATE documents set encounter_id=? where id=?"; //link it to this encounter
398 sqlQuery($sql, array($encounter,$doc_id));
399 exit;
402 // Store the IMPPLAN area. This is separate from the rest of the form
403 // It is in a separate table due to its one-to-many relationship with the form_id.
404 if ($_REQUEST['action']=="store_IMPPLAN") {
405 $IMPPLAN = json_decode($_REQUEST['parameter'], true);
406 //remove what is there and replace it with this data.
407 $query = "DELETE from form_".$form_folder."_impplan where form_id=? and pid=?";
408 sqlQuery($query, array($form_id,$pid));
410 for ($i = 0; $i < count($IMPPLAN); $i++) {
411 $query ="INSERT IGNORE INTO form_".$form_folder."_impplan (form_id, pid, title, code, codetype, codedesc, codetext, plan, IMPPLAN_order, PMSFH_link) VALUES(?,?,?,?,?,?,?,?,?,?) ";
412 $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']));
413 //if it is a duplicate then delete this from the array and return the array via json.
414 //or rebuild it from mysql
417 //Since we are potentially ignoring duplicates, build json IMPPLAN_items and return it to the user to rebuild IMP/Plan area
418 $IMPPLAN_items = build_IMPPLAN_items($pid, $form_id);
419 echo json_encode($IMPPLAN_items);
420 exit;
423 //change PCP
424 if ($_REQUEST['action'] == 'docs') {
425 $query = "update patient_data set providerID=?,ref_providerID=? where pid =?";
426 sqlQuery($query, array($_REQUEST['pcp'],$_REQUEST['rDOC'],$pid));
427 exit;
430 /*** START CODE to DEAL WITH PMSFH/ISUUE_TYPES ****/
431 if ($_REQUEST['PMSFH_save'] =='1') {
432 if (!$PMSFH) {
433 $PMSFH = build_PMSFH($pid);
436 $issue = $_REQUEST['issue'];
437 $deletion = $_REQUEST['deletion'];
438 $form_save = $_REQUEST['form_save'];
439 $pid = $_SESSION['pid'];
440 $encounter = $_SESSION['encounter'];
441 $form_id = $_REQUEST['form_id'];
442 $form_type = $_REQUEST['form_type'];
443 $r_PMSFH = $_REQUEST['r_PMSFH'];
444 if ($deletion ==1) {
445 row_delete("issue_encounter", "list_id = '$issue'");
446 row_delete("lists", "id = '$issue'");
447 $PMSFH = build_PMSFH($pid);
448 send_json_values($PMSFH);
449 exit;
450 } else {
451 if ($form_type=='ROS') { //ROS
452 $query="UPDATE form_eye_mag set ROSGENERAL=?,ROSHEENT=?,ROSCV=?,ROSPULM=?,ROSGI=?,ROSGU=?,ROSDERM=?,ROSNEURO=?,ROSPSYCH=?,ROSMUSCULO=?,ROSIMMUNO=?,ROSENDOCRINE=? where id=? and pid=?";
453 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));
454 $PMSFH = build_PMSFH($pid);
455 send_json_values($PMSFH);
456 exit;
457 } elseif ($form_type=='SOCH') { //SocHx
458 $newdata = array();
459 $fres = sqlStatement("SELECT * FROM layout_options " .
460 "WHERE form_id = 'HIS' AND uor > 0 AND field_id != '' " .
461 "ORDER BY group_id, seq");
462 while ($frow = sqlFetchArray($fres)) {
463 $field_id = $frow['field_id'];
464 //get value only if field exist in $_POST (prevent deleting of field with disabled attribute)
465 if (isset($_POST["form_$field_id"])) {
466 $newdata[$field_id] = get_layout_form_value($frow);
470 updateHistoryData($pid, $newdata);
471 if ($_REQUEST['marital_status'] >'') {
472 // have to match input with list_option for marital to not break openEMR
473 $query="select * from list_options where list_id='marital'";
474 $fres = sqlStatement($query);
475 while ($frow = sqlFetchArray($fres)) {
476 if (($_REQUEST['marital_status'] == $frow['option_id'])||($_REQUEST['marital_status'] == $frow['title'])) {
477 $status = $frow['option_id'];
478 $query = "UPDATE patient_data set status=? where pid=?";
479 sqlStatement($query, array($status,$pid));
484 if ($_REQUEST['occupation'] > '') {
485 $query = "UPDATE patient_data set occupation=? where pid=?";
486 sqlStatement($query, array($_REQUEST['occupation'],$pid));
489 $PMSFH = build_PMSFH($pid);
490 send_json_values($PMSFH);
491 exit;
492 } elseif ($form_type =='FH') {
493 $query = "UPDATE history_data set
494 relatives_cancer=?,
495 relatives_diabetes=?,
496 relatives_high_blood_pressure=?,
497 relatives_heart_problems=?,
498 relatives_stroke=?,
499 relatives_epilepsy=?,
500 relatives_mental_illness=?,
501 relatives_suicide=?,
502 usertext11=?,
503 usertext12=?,
504 usertext13=?,
505 usertext14=?,
506 usertext15=?,
507 usertext16=?,
508 usertext17=?,
509 usertext18=? where pid=?";
510 //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;
511 $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));
512 $PMSFH = build_PMSFH($pid);
513 send_json_values($PMSFH);
514 exit;
515 } else {
516 if ($_REQUEST['form_title'] =='') {
517 return;
520 $subtype ='';
521 if ($form_type =="POH") {
522 $form_type="medical_problem";
523 $subtype="eye";
524 } elseif ($form_type =="PMH") {
525 $form_type="medical_problem";
526 } elseif ($form_type =="Allergy") {
527 $form_type="allergy";
528 } elseif ($form_type =="Surgery") {
529 $form_type="surgery";
530 } elseif ($form_type =="POS") {
531 $form_type="surgery";
532 $subtype="eye";
533 } elseif ($form_type =="Medication") {
534 $form_type="medication";
535 if ($_REQUEST['form_eye_subtype']) {
536 $subtype="eye";
537 //we always want a default begin date
538 //if it is empty, fill it with today
539 if ($_REQUEST['form_begin'] =='') {
540 $_REQUEST['form_begin'] = date("Y-m-d");
544 if ($_REQUEST['form_begin'] =='') {
545 $_REQUEST['form_begin'] = $visit_date;
549 $i = 0;
550 $form_begin = fixDate($_REQUEST['form_begin'], '');
551 $form_end = fixDate($_REQUEST['form_end'], '');
554 * When adding an issue, see if the issue is already here.
555 * If so we need to update it. If not we are adding it.
556 * Check the PMSFH array first by title.
557 * If not present in PMSFH, check the DB to be sure.
559 foreach ($PMSFH[$form_type] as $item) {
560 if ($item['title'] == $_REQUEST['form_title']) {
561 $issue = $item['issue'];
565 if (!$issue) {
566 if ($subtype == '') {
567 $query = "SELECT id,pid from lists where title=? and type=? and pid=?";
568 $issue2 = sqlQuery($query, array($_REQUEST['form_title'],$form_type,$pid));
569 $issue = $issue2['id'];
570 } else {
571 $query = "SELECT id,pid from lists where title=? and type=? and pid=? and subtype=?";
572 $issue2 = sqlQuery($query, array($_REQUEST['form_title'],$form_type,$pid,$subtype));
573 $issue = $issue2['id'];
577 $issue = 0 + $issue;
578 if ($_REQUEST['form_reinjury_id'] =="") {
579 $form_reinjury_id="0";
582 if ($_REQUEST['form_injury_grade'] =="") {
583 $form_injury_grade="0";
586 if ($_REQUEST['form_outcome'] =='') {
587 $_REQUEST['form_outcome'] ='0';
590 if ($issue != '0') { //if this issue already exists we are updating it...
591 $query = "UPDATE lists SET " .
592 "type = '" . add_escape_custom($form_type) . "', " .
593 "title = '" . add_escape_custom($_REQUEST['form_title']) . "', " .
594 "comments = '" . add_escape_custom($_REQUEST['form_comments']) . "', " .
595 "begdate = " . QuotedOrNull($form_begin) . ", " .
596 "enddate = " . QuotedOrNull($form_end) . ", " .
597 "returndate = " . QuotedOrNull($form_return) . ", " .
598 "diagnosis = '" . add_escape_custom($_REQUEST['form_diagnosis']) . "', " .
599 "occurrence = '" . add_escape_custom($_REQUEST['form_occur']) . "', " .
600 "classification = '" . add_escape_custom($_REQUEST['form_classification']) . "', " .
601 "reinjury_id = '" . add_escape_custom($_REQUEST['form_reinjury_id']) . "', " .
602 "referredby = '" . add_escape_custom($_REQUEST['form_referredby']) . "', " .
603 "injury_grade = '" . add_escape_custom($_REQUEST['form_injury_grade']) . "', " .
604 "injury_part = '" . add_escape_custom($form_injury_part) . "', " .
605 "injury_type = '" . add_escape_custom($form_injury_type) . "', " .
606 "outcome = '" . add_escape_custom($_REQUEST['form_outcome']) . "', " .
607 "destination = '" . add_escape_custom($_REQUEST['form_destination']) . "', " .
608 "reaction ='" . add_escape_custom($_REQUEST['form_reaction']) . "', " .
609 "erx_uploaded = '0', " .
610 "modifydate = NOW(), " .
611 "subtype = '" . $subtype. "' " .
612 "WHERE id = '" . add_escape_custom($issue) . "'";
613 sqlStatement($query);
614 if ($text_type == "medication" && enddate != '') {
615 sqlStatement('UPDATE prescriptions SET '
616 . 'medication = 0 where patient_id = ? '
617 . " and upper(trim(drug)) = ? "
618 . ' and medication = 1', array($pid,strtoupper($_REQUEST['form_title'])));
620 } else {
621 $query = "INSERT INTO lists ( " .
622 "date, pid, type, title, activity, comments, ".
623 "begdate, enddate, returndate, " .
624 "diagnosis, occurrence, classification, referredby, user, " .
625 "groupname, outcome, destination,reaction,subtype " .
626 ") VALUES ( " .
627 "NOW(), ?,?,?,1,?," .
628 QuotedOrNull($form_begin).", ".QuotedOrNull($form_end).", ".QuotedOrNull($form_return). ", " .
629 "?,?,?,?,?,".
630 "?,?,?,?,?)";
631 $issue = sqlInsert($query, array($pid,$form_type,$_REQUEST['form_title'],$_REQUEST['form_comments'],
632 $_REQUEST['form_diagnosis'],$_REQUEST['form_occur'],$_REQUEST['form_clasification'],$_REQUEST['form_referredby'],$_SESSION['authUser'],
633 $_SESSION['authProvider'],QuotedOrNull($_REQUEST['form_outcome']),$_REQUEST['form_destination'],$_REQUEST['form_reaction'],$subtype));
635 // For record/reporting purposes, place entry in lists_touch table.
636 setListTouch($pid, $form_type);
638 // If requested, link the issue to a specified encounter.
639 // we always link them, automatically.
640 if ($encounter) {
641 $query = "INSERT INTO issue_encounter ( " .
642 "pid, list_id, encounter " .
643 ") VALUES ( ?,?,? )";
644 sqlStatement($query, array($pid,$issue,$encounter));
648 $irow = '';
649 //if it is a medication do we need to do something with dosage fields?
650 //leave all in title field form now.
653 $PMSFH = build_PMSFH($pid);
654 send_json_values($PMSFH);
655 exit;
659 if ($_REQUEST['action'] =='code_PMSFH') {
660 $query = "UPDATE lists SET diagnosis = ? WHERE id = ?";
661 sqlStatement($query, array($_POST['code'],$_POST['issue']));
662 exit;
665 if ($_REQUEST['action'] == 'code_visit') {
666 $CODING = json_decode($_REQUEST['parameter'], true);
667 $query = "delete from billing where encounter =?";
668 sqlStatement($query, array($encounter));
669 foreach ($CODING as $item) { //need toremove duplicate codes
670 if ($dups[$item["code"]]=='1') {
671 continue;
674 $dups[$item["code"]] = "1";
675 $sql = "SELECT codes.*, prices.pr_price FROM codes " .
676 "LEFT OUTER JOIN patient_data ON patient_data.pid = '$pid' " .
677 "LEFT OUTER JOIN prices ON prices.pr_id = codes.id AND " .
678 "prices.pr_selector = '' AND " .
679 "prices.pr_level = patient_data.pricelevel " .
680 "WHERE code =?" .
681 " LIMIT 1";
682 $result = sqlStatement($sql, array($item['code']));
683 while ($res = sqlFetchArray($result)) {
684 $item["codedesc"] = $res["code_text"];// eg. = "NP EYE intermediate exam"
685 if (!$item["modifier"]) {
686 $modifier = $res["modifier"];
689 $item["units"] = $res["units"];
690 $item["fee"] = $res["pr_price"];
693 addBilling($encounter, $item["codetype"], $item["code"], $item["codedesc"], $pid, '1', $providerID, $item["modifier"], $item["units"], $item["fee"], $ndc_info, $justify, $billed, '');
696 echo 'ok';
697 exit;
700 /*** END CODE to DEAL WITH PMSFH/ISUUE_TYPES ****/
702 /* Let's save the encounter specific values.
703 // Any field that exists in the database could be updated
704 // so we need to exclude the important ones...
705 // id date pid user groupname authorized activity. Any other just add them below.
706 // Doing it this way means you can add new fields on a web page and in the DB without touching this function.
707 // The update feature still works because it only updates columns that are in the table you are working on.
709 $query = "SHOW COLUMNS from ".$table_name."";
710 $result = sqlStatement($query);
711 if (!$result) {
712 return 'Could not run query: No columns found in your table! ' . mysql_error();
713 exit;
716 $fields = array();
717 if (($_POST['IOPTIME'] == '00:00:00')||(!$_POST['IOPTIME'])) {
718 $_POST['IOPTIME'] = date('H:i:s');
721 $_POST['IOPTIME'] = date('H:i:s', strtotime($_POST['IOPTIME']));
723 if (sqlNumRows($result) > 0) {
724 while ($row = sqlFetchArray($result)) {
725 //exclude critical columns/fields and those needing special processing from update
726 if ($row['Field'] == 'id' or
727 $row['Field'] == 'date' or
728 $row['Field'] == 'pid' or
729 $row['Field'] == 'user' or
730 $row['Field'] == 'groupname' or
731 $row['Field'] == 'authorized' or
732 $row['Field'] == 'LOCKED' or
733 $row['Field'] == 'LOCKEDBY' or
734 $row['Field'] == 'activity' or
735 $row['Field'] == 'PLAN' or
736 $row['Field'] == 'Resource') {
737 continue;
740 if (isset($_POST[$row['Field']])) {
741 $fields[$row['Field']] = $_POST[$row['Field']];
745 // orders are checkboxes created from a user defined list in the PLAN area and stored as item1|item2|item3
746 // if there are any, create the $field['PLAN'] value.
747 // Remember -- If you uncheck a box, it won't be sent!
748 // So delete all made today by this provider and reload with any Orders sent in this $_POST
749 // in addition, we made a special table for orders, and when completed we can mark done?
750 $query="select form_encounter.date as encounter_date from form_encounter where form_encounter.encounter =?";
751 $encounter_data =sqlQuery($query, array($encounter));
752 $dated = new DateTime($encounter_data['encounter_date']);
753 $dated = $dated->format('Y-m-d');
754 $visit_date = oeFormatShortDate($dated);
756 $N = count($_POST['PLAN']);
757 $sql_clear = "DELETE from form_eye_mag_orders where ORDER_PID =? and ORDER_PLACED_BYWHOM=? and ORDER_DATE_PLACED=? and ORDER_STATUS ='pending'";
758 sqlQuery($sql_clear, array($pid,$providerID,$visit_date));
759 if ($N > '0') {
760 for ($i=0; $i < $N; $i++) {
761 $fields['PLAN'] .= $_POST['PLAN'][$i] . "|"; //this makes an entry for form_eyemag: PLAN
762 $ORDERS_sql = "REPLACE INTO form_eye_mag_orders (ORDER_PID,ORDER_DETAILS,ORDER_STATUS,ORDER_DATE_PLACED,ORDER_PLACED_BYWHOM) VALUES (?,?,?,?,?)";
763 $okthen = sqlQuery($ORDERS_sql, array($pid,$_POST['PLAN'][$i],'pending',$visit_date,$providerID));
766 $fields['PLAN'] = mb_substr($fields['PLAN'], 0, -1); //get rid of trailing "|"
769 if ($_REQUEST['PLAN2']) {
770 $fields['PLAN'] .= $_REQUEST['PLAN2'];
771 //there is something in the "freeform" plan textarea...
772 $ORDERS_sql = "REPLACE INTO form_eye_mag_orders (ORDER_PID,ORDER_DETAILS,ORDER_STATUS,ORDER_PRIORITY,ORDER_DATE_PLACED,ORDER_PLACED_BYWHOM) VALUES (?,?,?,?,?,?)";
773 $okthen = sqlQuery($ORDERS_sql, array($pid,$_POST['PLAN'][$i],'pending',"PLAN2:$PLAN2",$visit_date,$providerID));
776 $M = count($_POST['TEST']);
777 if ($M > '0') {
778 for ($i=0; $i < $M; $i++) {
779 $fields['Resource'] .= $_POST['TEST'][$i] . "|"; //this makes an entry for form_eyemag: Resource
782 $fields['Resource'] = mb_substr($fields['Resource'], 0, -1); //get rid of trailing "|"
785 /** Empty Checkboxes need to be entered manually as they are only submitted via POST when they are checked
786 * If NOT checked on the form, they are sent via POST and thus are NOT overridden in the DB,
787 * so DB won't change unless we define them into the $fields array as "0"...
789 if (!$_POST['alert']) {
790 $fields['alert'] = '0';
793 if (!$_POST['oriented']) {
794 $fields['oriented'] = '0';
797 if (!$_POST['confused']) {
798 $fields['confused'] = '0';
801 if (!$_POST['PUPIL_NORMAL']) {
802 $fields['PUPIL_NORMAL'] = '0';
805 if (!$_POST['MOTILITYNORMAL']) {
806 $fields['MOTILITYNORMAL'] = '0';
809 if (!$_POST['ACT']) {
810 $fields['ACT'] = 'off';
813 if (!$_POST['DIL_RISKS']) {
814 $fields['DIL_RISKS'] = '0';
817 if (!$_POST['ATROPINE']) {
818 $fields['ATROPINE'] = '0';
821 if (!$_POST['CYCLOGYL']) {
822 $fields['CYCLOGYL'] = '0';
825 if (!$_POST['CYCLOMYDRIL']) {
826 $fields['CYCLOMYDRIL'] = '0';
829 if (!$_POST['NEO25']) {
830 $fields['NEO25'] = '0';
833 if (!$_POST['TROPICAMIDE']) {
834 $fields['TROPICAMIDE'] = '0';
837 if (!$_POST['BALANCED']) {
838 $fields['BALANCED'] = '0';
841 if (!$_POST['ODVF1']) {
842 $fields['ODVF1'] = '0';
845 if (!$_POST['ODVF2']) {
846 $fields['ODVF2'] = '0';
849 if (!$_POST['ODVF3']) {
850 $fields['ODVF3'] = '0';
853 if (!$_POST['ODVF4']) {
854 $fields['ODVF4'] = '0';
857 if (!$_POST['OSVF1']) {
858 $fields['OSVF1'] = '0';
861 if (!$_POST['OSVF2']) {
862 $fields['OSVF2'] = '0';
865 if (!$_POST['OSVF3']) {
866 $fields['OSVF3'] = '0';
869 if (!$_POST['OSVF4']) {
870 $fields['OSVF4'] = '0';
873 if (!$_POST['TEST']) {
874 $fields['Resource'] = '';
877 if (!$fields['PLAN']) {
878 $fields['PLAN'] = '0';
881 $success = formUpdate($table_name, $fields, $form_id, $_SESSION['userauthorized']);
883 //now save any Wear RXs (1-4) entered.
884 $rx_number='1';
885 if ($_POST['W_1']=='1') {
886 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
887 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
888 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
889 `RX_TYPE` ,`COMMENTS`,
890 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
891 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
892 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
893 `LENS_TREATMENTS`
894 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
895 $LENS_TREATMENTS_1 = implode("|", $_POST['LENS_TREATMENTS_1']);
896 sqlQuery($query, array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_1'],$_POST['ODCYL_1'],$_POST['ODAXIS_1'],
897 $_POST['ODVA_1'],$_POST['ODADD_1'],$_POST['ODNEARVA_1'],$_POST['OSSPH_1'],$_POST['OSCYL_1'],$_POST['OSAXIS_1'],
898 $_POST['OSVA_1'],$_POST['OSADD_1'],$_POST['OSNEARVA_1'],$_POST['ODMIDADD_1'],$_POST['OSMIDADD_1'],
899 0+$_POST['RX_TYPE_1'],$_POST['COMMENTS_1'],
900 $_POST['ODHPD_1'],$_POST['ODHBASE_1'],$_POST['ODVPD_1'],$_POST['ODVBASE_1'],$_POST['ODSLABOFF_1'],$_POST['ODVERTEXDIST_1'],
901 $_POST['OSHPD_1'],$_POST['OSHBASE_1'],$_POST['OSVPD_1'],$_POST['OSVBASE_1'],$_POST['OSSLABOFF_1'],$_POST['OSVERTEXDIST_1'],
902 $_POST['ODMPDD_1'],$_POST['ODMPDN_1'],$_POST['OSMPDD_1'],$_POST['OSMPDN_1'],$_POST['BPDD_1'],$_POST['BPDN_1'],$_POST['LENS_MATERIAL_1'],
903 $LENS_TREATMENTS_1 ));
904 $rx_number++;
905 } else {
906 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
907 sqlQuery($query, array($encounter,$pid,$form_id,'1'));
910 if ($_POST['W_2']=='1') {
911 //store W_2
912 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
913 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
914 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
915 `RX_TYPE` ,`COMMENTS`,
916 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
917 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
918 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
919 `LENS_TREATMENTS`
920 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
921 $LENS_TREATMENTS_2 = implode("|", $_POST['LENS_TREATMENTS_2']);
922 sqlQuery($query, array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_2'],$_POST['ODCYL_2'],$_POST['ODAXIS_2'],
923 $_POST['ODVA_2'],$_POST['ODADD_2'],$_POST['ODNEARVA_2'],$_POST['OSSPH_2'],$_POST['OSCYL_2'],$_POST['OSAXIS_2'],
924 $_POST['OSVA_2'],$_POST['OSADD_2'],$_POST['OSNEARVA_2'],$_POST['ODMIDADD_2'],$_POST['OSMIDADD_2'],
925 0+$_POST['RX_TYPE_2'],$_POST['COMMENTS_2'],
926 $_POST['ODHPD_2'],$_POST['ODHBASE_2'],$_POST['ODVPD_2'],$_POST['ODVBASE_2'],$_POST['ODSLABOFF_2'],$_POST['ODVERTEXDIST_2'],
927 $_POST['OSHPD_2'],$_POST['OSHBASE_2'],$_POST['OSVPD_2'],$_POST['OSVBASE_2'],$_POST['OSSLABOFF_2'],$_POST['OSVERTEXDIST_2'],
928 $_POST['ODMPDD_2'],$_POST['ODMPDN_2'],$_POST['OSMPDD_2'],$_POST['OSMPDN_2'],$_POST['BPDD_2'],$_POST['BPDN_2'],$_POST['LENS_MATERIAL_2'],
929 $LENS_TREATMENTS_2 ));
930 $rx_number++;
931 } else {
932 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
933 sqlQuery($query, array($encounter,$pid,$form_id,'2'));
936 if ($_POST['W_3']=='1') {
937 //store W_3
938 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
939 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
940 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
941 `RX_TYPE` ,`COMMENTS`,
942 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
943 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
944 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
945 `LENS_TREATMENTS`
946 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
947 $LENS_TREATMENTS_3 = implode("|", $_POST['LENS_TREATMENTS_3']);
948 sqlQuery($query, array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_3'],$_POST['ODCYL_3'],$_POST['ODAXIS_3'],
949 $_POST['ODVA_3'],$_POST['ODADD_3'],$_POST['ODNEARVA_3'],$_POST['OSSPH_3'],$_POST['OSCYL_3'],$_POST['OSAXIS_3'],
950 $_POST['OSVA_3'],$_POST['OSADD_3'],$_POST['OSNEARVA_3'],$_POST['ODMIDADD_3'],$_POST['OSMIDADD_3'],
951 0+$_POST['RX_TYPE_3'],$_POST['COMMENTS_3'],
952 $_POST['ODHPD_3'],$_POST['ODHBASE_3'],$_POST['ODVPD_3'],$_POST['ODVBASE_3'],$_POST['ODSLABOFF_3'],$_POST['ODVERTEXDIST_3'],
953 $_POST['OSHPD_3'],$_POST['OSHBASE_3'],$_POST['OSVPD_3'],$_POST['OSVBASE_3'],$_POST['OSSLABOFF_3'],$_POST['OSVERTEXDIST_3'],
954 $_POST['ODMPDD_3'],$_POST['ODMPDN_3'],$_POST['OSMPDD_3'],$_POST['OSMPDN_3'],$_POST['BPDD_3'],$_POST['BPDN_3'],$_POST['LENS_MATERIAL_3'],
955 $LENS_TREATMENTS_3 ));
956 $rx_number++;
957 } else {
958 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
959 sqlQuery($query, array($encounter,$pid,$form_id,'3'));
962 if ($_POST['W_4']=='1') {
963 //store W_4
964 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
965 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
966 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
967 `RX_TYPE` ,`COMMENTS`,
968 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
969 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
970 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
971 `LENS_TREATMENTS`
972 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
973 $LENS_TREATMENTS_4 = implode("|", $_POST['LENS_TREATMENTS_4']);
974 sqlQuery($query, array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_4'],$_POST['ODCYL_4'],$_POST['ODAXIS_4'],
975 $_POST['ODVA_4'],$_POST['ODADD_4'],$_POST['ODNEARVA_4'],$_POST['OSSPH_4'],$_POST['OSCYL_4'],$_POST['OSAXIS_4'],
976 $_POST['OSVA_4'],$_POST['OSADD_4'],$_POST['OSNEARVA_4'],$_POST['ODMIDADD_4'],$_POST['OSMIDADD_4'],
977 0+$_POST['RX_TYPE_4'],$_POST['COMMENTS_4'],
978 $_POST['ODHPD_4'],$_POST['ODHBASE_4'],$_POST['ODVPD_4'],$_POST['ODVBASE_4'],$_POST['ODSLABOFF_4'],$_POST['ODVERTEXDIST_4'],
979 $_POST['OSHPD_4'],$_POST['OSHBASE_4'],$_POST['OSVPD_4'],$_POST['OSVBASE_4'],$_POST['OSSLABOFF_4'],$_POST['OSVERTEXDIST_4'],
980 $_POST['ODMPDD_4'],$_POST['ODMPDN_4'],$_POST['OSMPDD_4'],$_POST['OSMPDN_4'],$_POST['BPDD_4'],$_POST['BPDN_4'],$_POST['LENS_MATERIAL_4'],
981 $LENS_TREATMENTS_4 ));
982 $rx_number++;
983 } else {
984 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
985 sqlQuery($query, array($encounter,$pid,$form_id,'4'));
988 for ($i=$rx_number; $i < 5; $i++) {
989 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
990 sqlQuery($query, array($encounter,$pid,$form_id,$i));
993 //now return the obj
994 $send['IMPPLAN_items'] = build_IMPPLAN_items($pid, $form_id);
995 $send['Clinical'] = start_your_engines($_REQUEST);
996 $send['PMH_panel'] = display_PMSFH('2');
997 $send['right_panel'] = show_PMSFH_panel($PMSFH);
998 $send['PMSFH'] = $PMSFH[0];
999 echo json_encode($send);
1000 exit;
1002 } elseif ($_REQUEST["mode"] == "retrieve") {
1003 if ($_REQUEST['PRIORS_query']) {
1004 echo display_PRIOR_section($_REQUEST['zone'], $_REQUEST['orig_id'], $_REQUEST['id_to_show'], $pid);
1005 exit;
1010 * Save the canvas drawings
1013 if ($_REQUEST['canvas']) {
1014 if (!$pid||!$encounter||!$zone||!$_POST["imgBase64"]) {
1015 exit;
1018 $side = "OU";
1019 $base_name = $pid."_".$encounter."_".$side."_".$zone."_VIEW";
1020 $filename = $base_name.".jpg";
1022 $type = "image/jpeg"; // all our canvases are this type
1023 $data = $_POST["imgBase64"];
1024 $data = substr($data, strpos($data, ",")+1);
1025 $data = base64_decode($data);
1026 $size = strlen($data);
1027 $query = "select id from categories where name = 'Drawings'";
1028 $result = sqlStatement($query);
1029 $ID = sqlFetchArray($result);
1030 $category_id = $ID['id'];
1032 // We want to overwrite so only one image is stored per zone per form/encounter
1033 // I do not believe this function exists in the current library, ie "UpdateDocument" function, so...
1034 // we need to delete the previous file from the documents and categories to documents tables and the actual file
1035 // There must be a delete_file function in documents class?
1036 // cannot find it.
1037 // this will work for harddisk people, not sure about couchDB people:
1038 $filepath = $GLOBALS['oer_config']['documents']['repository'] . $pid ."/";
1039 foreach (glob($filepath.'/'.$filename) as $file) {
1040 unlink($file);
1043 $sql = "DELETE from categories_to_documents where document_id IN (SELECT id from documents where documents.url like '%".$filename."')";
1044 sqlQuery($sql);
1045 $sql ="DELETE from documents where documents.url like '%".$filename."'";
1046 sqlQuery($sql);
1047 $return = addNewDocument($filename, $type, $_POST["imgBase64"], 0, $size, $_SESSION['authUserID'], $pid, $category_id);
1048 $doc_id = $return['doc_id'];
1049 $sql = "UPDATE documents set encounter_id=? where id=?"; //link it to this encounter
1050 sqlQuery($sql, array($encounter,$doc_id));
1051 exit;
1054 if ($_REQUEST['copy']) {
1055 copy_forward($_REQUEST['zone'], $_REQUEST['copy_from'], $_SESSION['ID'], $pid);
1056 return;
1059 function QuotedOrNull($fld)
1061 if ($fld) {
1062 return "'".add_escape_custom($fld)."'";
1065 return "NULL";
1067 function debug($local_var)
1069 echo "<pre><BR>We are in the debug function.<BR>";
1070 echo "Passed variable = ". $local_var . " <BR>";
1071 print_r($local_var);
1072 exit;
1075 /* From original issue.php */
1077 function row_delete($table, $where)
1079 $query = "SELECT * FROM $table WHERE $where";
1080 $tres = sqlStatement($query);
1081 $count = 0;
1082 while ($trow = sqlFetchArray($tres)) {
1083 $logstring = "";
1084 foreach ($trow as $key => $value) {
1085 if (! $value || $value == '0000-00-00 00:00:00') {
1086 continue;
1089 if ($logstring) {
1090 $logstring .= " ";
1093 $logstring .= $key . "='" . addslashes($value) . "'";
1096 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
1097 ++$count;
1100 if ($count) {
1101 $query = "DELETE FROM $table WHERE $where";
1102 sqlStatement($query);
1105 // Given an issue type as a string, compute its index.
1106 // Not sure of the value of this sub given transition to array $PMSFH
1107 // Can I use it to find out which PMSFH item we are looking for? YES
1108 function issueTypeIndex($tstr)
1110 global $ISSUE_TYPES;
1111 $i = 0;
1112 foreach ($ISSUE_TYPES as $key => $value) {
1113 if ($key == $tstr) {
1114 break;
1117 ++$i;
1120 return $i;
1124 * The following 2 functions can be removed from the production environment
1127 function merge($filename_x, $filename_y, $filename_result)
1130 * Three png files (OU,OD,OS) per LOCATION (EXT,ANTSEG,RETINA,NEURO)
1131 * BASE, found in forms/$form_folder/images eg. OU_EXT_BASE.png
1132 * BASE is the blank image to start from and can be customized. Currently 432x150px
1133 * VIEW, found in /sites/$_SESSION['site_id']."/".$form_folder."/".$pid."/".$encounter
1134 * TEMP, intermediate png merge file of new drawings with BASE or previous VIEW
1135 * These are saved to be used in an undo feature...
1136 * NO LONGER USING but I kept it here because it is cool and I will use it later
1139 This section
1140 if (file_exists($storage."/OU_".$zone."_VIEW.png")) { //add new drawings to previous for this encounter
1141 $file_base = $storage."/OU_".$zone."_VIEW.png";
1142 } else { //start from the base image
1143 $file_base = $GLOBALS['webserver_root']."/interface/forms/".$form_folder."/images/OU_".$zone."_BASE.png";
1145 //merge needs to store to a separate file first, then rename to new VIEW
1146 $file_temp = $storage."/OU_".$zone."_TEMP.png";
1147 $file_here = $storage."/OU_".$zone."_VIEW.png";
1148 merge( $file_draw, $file_base, $file_temp);
1149 rename( $file_temp , $file_here );
1151 // Get dimensions for specified images
1152 list($width_x, $height_x) = getimagesize($filename_x);
1153 list($width_y, $height_y) = getimagesize($filename_y);
1155 // Create new image with desired dimensions
1156 $image = imagecreatetruecolor($width_y, $height_y);
1158 // Load images and then copy to destination image
1159 $image_x = imagecreatefrompng($filename_x);
1160 $image_y = imagecreatefrompng($filename_y);
1162 imagecopy($image, $image_y, 0, 0, 0, 0, $width_x, $height_x);
1163 imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
1165 // Save the resulting image to disk (as png)
1166 imagepng($image, $filename_result);
1168 // Clean up
1169 imagedestroy($image);
1170 imagedestroy($image_x);
1171 imagedestroy($image_y);
1174 // this function is here to understand the core openEMR function addBilling, so we can improve the Billing Engine in Eye Form
1175 // We still need to add modifiers and justify capabilities to the Coding Engine...
1176 function addBilling2(
1177 $encounter_id,
1178 $code_type,
1179 $code,
1180 $code_text,
1181 $pid,
1182 $authorized = "0",
1183 $provider,
1184 $modifier = "",
1185 $units = "",
1186 $fee = "0.00",
1187 $ndc_info = '',
1188 $justify = '',
1189 $billed = 0,
1190 $notecodes = ''
1193 $sql = "insert into billing (date, encounter, code_type, code, code_text, " .
1194 "pid, authorized, user, groupname, activity, billed, provider_id, " .
1195 "modifier, units, fee, ndc_info, justify, notecodes) values (" .
1196 "NOW(), ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?)";
1197 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));
1199 exit;