incremented database counter for prior commit and removed trailing whitespacing
[openemr.git] / interface / forms / eye_mag / save.php
blob275bedf557e934544322d66f36e24bf928b114d9
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/classes/Document.class.php");
61 require_once("$srcdir/classes/Note.class.php");
62 require_once("$srcdir/htmlspecialchars.inc.php");
63 require_once("$srcdir/html2pdf/html2pdf.class.php");
65 $returnurl = 'encounter_top.php';
67 if (isset($_REQUEST['id']))
69 $id = $_REQUEST['id'];
71 if (!$id) $id = $_REQUEST['pid'];
72 $encounter = $_REQUEST['encounter'];
74 $AJAX_PREFS = $_REQUEST['AJAX_PREFS'];
75 if ($encounter == "" && !$id && !$AJAX_PREFS && (($_REQUEST['mode'] != "retrieve") or ($_REQUEST['mode'] == "show_PDF"))) {
76 echo "Sorry Charlie..."; //should lead to a database of errors for explanation.
77 exit;
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']));
227 * ADD ANY NEW PREFERENCES above, and as a hidden field in the body.
230 /** <!-- End Preferences --> **/
233 * Create, update or retrieve a form and its values
235 if (!$pid) $pid = $_SESSION['pid'];
236 $userauthorized = $_SESSION['userauthorized'];
237 if ($encounter == "") $encounter = date("Ymd");
238 $form_id = $_REQUEST['form_id'];
239 $zone = $_REQUEST['zone'];
241 $providerID = findProvider($pid,$encounter);
242 if ($providerID =='0') $providerID = $userauthorized;//who is the default provider?
243 $providerNAME = getProviderName($providerID);
245 // The form is submitted to be updated or saved in some way.
246 // Give each instance of a form a uniqueID. If the form has no owner, update DB with this uniqueID.
247 // If the DB shows a uniqueID ie. an owner, and the save request uniqueID does not = the uniqueID in the DB,
248 // ask if the new user wishes to take ownership?
249 // If yes, any other's attempt to save fields/form are denied and the return code says you are not the owner...
250 if ($_REQUEST['unlock'] == '1') {
251 // we are releasing the form, by closing the page or clicking on ACTIVE FORM, so unlock it.
252 // if it's locked and they own it ($REQUEST[LOCKEDBY] == LOCKEDBY), they can unlock it
253 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from ".$table_name." WHERE ID=?";
254 $lock = sqlQuery($query,array($form_id));
255 if (($lock['LOCKED'] >'') && ($_REQUEST['LOCKEDBY'] == $lock['LOCKEDBY'])) {
256 $query = "update ".$table_name." set LOCKED='',LOCKEDBY='' where id=?";
257 sqlQuery($query,array($form_id));
259 exit;
260 } elseif ($_REQUEST['acquire_lock']=="1") {
261 //we are taking over the form's active state, others will go read-only
262 $query = "UPDATE ".$table_name." set LOCKED='1',LOCKEDBY=? where id=? and LOCKEDBY=?";
263 $result = sqlQuery($query,array($_REQUEST['uniqueID'],$form_id,$_REQUEST['locked_by']));
264 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from ".$table_name." WHERE ID=?";
265 $lock = sqlQuery($query,array($form_id));
266 exit;
267 } else {
268 $query = "SELECT LOCKED,LOCKEDBY,LOCKEDDATE from ".$table_name." WHERE ID=?";
269 $lock = sqlQuery($query,array($form_id));
270 if (($lock['LOCKED']) && ($_REQUEST['uniqueID'] != $lock['LOCKEDBY'])) {
271 // We are not the owner or it is not new so it is locked
272 // Did the user send a demand to take ownership?
273 if ($lock['LOCKEDBY'] != $_REQUEST['ownership']) {
274 //tell them they are locked out by another user now
275 echo "Code 400";
276 // or return a JSON encoded string with current LOCK ID?
277 // echo "Sorry Charlie, you get nothing since this is locked... No save for you!";
278 exit;
279 } elseif ($lock['LOCKEDBY'] == $_REQUEST['ownership']) {
280 // then they are taking ownership - all others get locked...
281 // new LOCKEDBY becomes our uniqueID LOCKEDBY
282 $_REQUEST['LOCKED'] = '1';
283 $_REQUEST['LOCKEDBY'] = $_REQUEST['uniqueID'];
284 //update table
285 $query = "update ".$table_name." set LOCKED=?,LOCKEDBY=? where id=?";
286 sqlQuery ($query,array('1',$_REQUEST['LOCKEDBY'],$form_id));
287 //go on to save what we want...
289 } elseif (!$lock['LOCKED']) { // it is not locked yet
290 $_REQUEST['LOCKED'] = '1';
291 $query = "update ".$table_name." set LOCKED=?,LOCKEDBY=? where id=?";
292 sqlQuery($query,array('1',$_REQUEST['LOCKEDBY'],$form_id));
293 //go on to save what we want...
295 if (!$_REQUEST['LOCKEDBY']) $_REQUEST['LOCKEDBY'] = rand();
298 if ($_REQUEST["mode"] == "new") {
299 $newid = formSubmit($table_name, $_POST, $id, $userauthorized);
300 addForm($encounter, $form_name, $newid, $form_folder, $pid, $userauthorized);
301 } elseif ($_REQUEST["mode"] == "update") {
302 // The user has write privileges to work with...
303 if ($_REQUEST['action']=="store_PDF") {
305 * We want to store/overwrite the current PDF version of this encounter's f
306 * Currently this is only called 'beforeunload', ie. when you finish the form
307 * In this current paradigm, anytime the form is opened, then closed, the PDF
308 * is overwritten. With esign implemented, the PDF should be locked. I suppose
309 * with esign the form can't even be opened so the only way to get to the PDF
310 * is through the Documents->Encounters links.
312 $query = "select id from categories where name = 'Encounters'";
313 $result = sqlStatement($query);
314 $ID = sqlFetchArray($result);
315 $category_id = $ID['id'];
316 $PDF_OUTPUT='1';
318 $filename = $pid."_".$encounter.".pdf";
319 $filepath = $GLOBALS['oer_config']['documents']['repository'] . $pid;
320 foreach (glob($filepath.'/'.$filename) as $file) {
321 unlink($file);
323 $sql = "DELETE from categories_to_documents where document_id IN (SELECT id from documents where documents.url like '%".$filename."')";
324 sqlQuery($sql);
325 $sql = "DELETE from documents where documents.url like '%".$filename."'";
326 sqlQuery($sql);
327 // We want to overwrite so only one PDF is stored per form/encounter
328 // $pdf = new HTML2PDF('P', 'Letter', 'en', array(5, 5, 5, 5) ); // add a little margin 5cm all around TODO: add to globals
330 /***********/
332 $pdf = new HTML2PDF ($GLOBALS['pdf_layout'],
333 $GLOBALS['pdf_size'],
334 $GLOBALS['pdf_language'],
335 true, // default unicode setting is true
336 'UTF-8', // default encoding setting is UTF-8
337 array($GLOBALS['pdf_left_margin'],$GLOBALS['pdf_top_margin'],$GLOBALS['pdf_right_margin'],$GLOBALS['pdf_bottom_margin']),
338 $_SESSION['language_direction'] == 'rtl' ? true : false
340 ob_start();
342 <link rel="stylesheet" href="<?php echo $webserver_root; ?>/interface/themes/style_pdf.css" type="text/css">
343 <div id="report_custom" style="width:100%;"> <!-- large outer DIV -->
344 <?php
345 echo report_header($pid);
346 include_once($GLOBALS['incdir'] . "/forms/eye_mag/report.php");
347 call_user_func($form_name . "_report", $pid, $form_encounter, $N, $form_id);
348 if ($printable)
349 echo "" . xl('Signature') . ": _______________________________<br />";
351 </div> <!-- end of report_custom DIV -->
353 <?php
355 global $web_root, $webserver_root;
356 $content = ob_get_clean();
357 // Fix a nasty html2pdf bug - it ignores document root!
358 $i = 0;
359 $wrlen = strlen($web_root);
360 $wsrlen = strlen($webserver_root);
361 while (true) {
362 $i = stripos($content, " src='/", $i + 1);
363 if ($i === false) break;
364 if (substr($content, $i+6, $wrlen) === $web_root &&
365 substr($content, $i+6, $wsrlen) !== $webserver_root)
367 $content = substr($content, 0, $i + 6) . $webserver_root . substr($content, $i + 6 + $wrlen);
370 $pdf->writeHTML($content, false);
371 $temp_filename = '/tmp/'.$filename;
372 $content_pdf = $pdf->Output($temp_filename, 'F');
373 $type = "application/pdf";
374 $size = filesize($temp_filename);
375 $return = addNewDocument($filename,$type,$temp_filename,0,$size,$_SESSION['authUserID'],$pid,$category_id);
376 $doc_id = $return['doc_id'];
377 $sql = "UPDATE documents set encounter_id=? where id=?"; //link it to this encounter
378 sqlQuery($sql,array($encounter,$doc_id));
379 exit;
381 // Store the IMPPLAN area. This is separate from the rest of the form
382 // It is in a separate table due to its one-to-many relationship with the form_id.
383 if ($_REQUEST['action']=="store_IMPPLAN") {
384 $IMPPLAN = json_decode($_REQUEST['parameter'],true);
385 //remove what is there and replace it with this data.
386 $query = "DELETE from form_".$form_folder."_impplan where form_id=? and pid=?";
387 sqlQuery($query,array($form_id,$pid));
389 for($i = 0; $i < count($IMPPLAN); $i++) {
390 $query ="INSERT IGNORE INTO form_".$form_folder."_impplan (form_id, pid, title, code, codetype, codedesc, codetext, plan, IMPPLAN_order, PMSFH_link) VALUES(?,?,?,?,?,?,?,?,?,?) ";
391 $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']));
392 //if it is a duplicate then delete this from the array and return the array via json.
393 //or rebuild it from mysql
395 //Since we are potentially ignoring duplicates, build json IMPPLAN_items and return it to the user to rebuild IMP/Plan area
396 $IMPPLAN_items = build_IMPPLAN_items($pid,$form_id);
397 echo json_encode($IMPPLAN_items);
398 exit;
401 //change PCP
402 if ($_REQUEST['action'] == 'docs') {
403 $query = "update patient_data set providerID=?,ref_providerID=? where pid =?";
404 sqlQuery($query,array($_REQUEST['pcp'],$_REQUEST['rDOC'],$pid));
405 exit;
408 /*** START CODE to DEAL WITH PMSFH/ISUUE_TYPES ****/
409 if ($_REQUEST['PMSFH_save'] =='1') {
410 if (!$PMSFH) $PMSFH = build_PMSFH($pid);
411 $issue = $_REQUEST['issue'];
412 $deletion = $_REQUEST['deletion'];
413 $form_save = $_REQUEST['form_save'];
414 $pid = $_SESSION['pid'];
415 $encounter = $_SESSION['encounter'];
416 $form_id = $_REQUEST['form_id'];
417 $form_type = $_REQUEST['form_type'];
418 $r_PMSFH = $_REQUEST['r_PMSFH'];
419 if ($deletion ==1) {
420 row_delete("issue_encounter", "list_id = '$issue'");
421 row_delete("lists", "id = '$issue'");
422 $PMSFH = build_PMSFH($pid);
423 send_json_values($PMSFH);
424 exit;
425 } else {
426 if ($form_type=='ROS') { //ROS
427 $query="UPDATE form_eye_mag set ROSGENERAL=?,ROSHEENT=?,ROSCV=?,ROSPULM=?,ROSGI=?,ROSGU=?,ROSDERM=?,ROSNEURO=?,ROSPSYCH=?,ROSMUSCULO=?,ROSIMMUNO=?,ROSENDOCRINE=? where id=? and pid=?";
428 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));
429 $PMSFH = build_PMSFH($pid);
430 send_json_values($PMSFH);
431 exit;
432 } elseif ($form_type=='SOCH') { //SocHx
433 $newdata = array();
434 $fres = sqlStatement("SELECT * FROM layout_options " .
435 "WHERE form_id = 'HIS' AND uor > 0 AND field_id != '' " .
436 "ORDER BY group_name, seq");
437 while ($frow = sqlFetchArray($fres)) {
438 $field_id = $frow['field_id'];
439 $newdata[$field_id] = get_layout_form_value($frow);
441 updateHistoryData($pid, $newdata);
442 if ($_REQUEST['marital_status'] >'') {
443 // have to match input with list_option for marital to not break openEMR
444 $query="select * from list_options where list_id='marital'";
445 $fres = sqlStatement($query);
446 while ($frow = sqlFetchArray($fres)) {
447 if (($_REQUEST['marital_status'] == $frow['option_id'])||($_REQUEST['marital_status'] == $frow['title'])) {
448 $status = $frow['option_id'];
449 $query = "UPDATE patient_data set status=? where pid=?";
450 sqlStatement($query,array($status,$pid));
454 if ($_REQUEST['occupation'] > '') {
455 $query = "UPDATE patient_data set occupation=? where pid=?";
456 sqlStatement($query,array($_REQUEST['occupation'],$pid));
458 $PMSFH = build_PMSFH($pid);
459 send_json_values($PMSFH);
460 exit;
461 } elseif ($form_type =='FH') {
462 $query = "UPDATE history_data set
463 relatives_cancer=?,
464 relatives_diabetes=?,
465 relatives_high_blood_pressure=?,
466 relatives_heart_problems=?,
467 relatives_stroke=?,
468 relatives_epilepsy=?,
469 relatives_mental_illness=?,
470 relatives_suicide=?,
471 usertext11=?,
472 usertext12=?,
473 usertext13=?,
474 usertext14=?,
475 usertext15=?,
476 usertext16=?,
477 usertext17=?,
478 usertext18=? where pid=?";
479 //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;
480 $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));
481 $PMSFH = build_PMSFH($pid);
482 send_json_values($PMSFH);
483 exit;
484 } else {
485 if ($_REQUEST['form_title'] =='') return;
486 $subtype ='';
487 if ($form_type =="POH") {
488 $form_type="medical_problem";
489 $subtype="eye";
490 } elseif ($form_type =="PMH") {
491 $form_type="medical_problem";
492 } elseif ($form_type =="Allergy") {
493 $form_type="allergy";
494 } elseif ($form_type =="Surgery") {
495 $form_type="surgery";
496 } elseif ($form_type =="POS") {
497 $form_type="surgery";
498 $subtype="eye";
499 } elseif ($form_type =="Medication") {
500 $form_type="medication";
501 if ($_REQUEST['form_eye_subtype']) {
502 $subtype="eye";
503 //we always want a default begin date
504 //if it is empty, fill it with today
505 if ($_REQUEST['form_begin'] =='')
506 { $_REQUEST['form_begin'] = date("Y-m-d"); }
508 if ($_REQUEST['form_begin'] =='') {
509 $_REQUEST['form_begin'] = $visit_date;
512 $i = 0;
513 $form_begin = fixDate($_REQUEST['form_begin'], '');
514 $form_end = fixDate($_REQUEST['form_end'], '');
517 * When adding an issue, see if the issue is already here.
518 * If so we need to update it. If not we are adding it.
519 * Check the PMSFH array first by title.
520 * If not present in PMSFH, check the DB to be sure.
522 foreach ($PMSFH[$form_type] as $item) {
523 if ($item['title'] == $_REQUEST['form_title']) {
524 $issue = $item['issue'];
527 if (!$issue) {
528 if ($subtype == '') {
529 $query = "SELECT id,pid from lists where title=? and type=? and pid=?";
530 $issue2 = sqlQuery($query,array($_REQUEST['form_title'],$form_type,$pid));
531 $issue = $issue2['id'];
532 } else {
533 $query = "SELECT id,pid from lists where title=? and type=? and pid=? and subtype=?";
534 $issue2 = sqlQuery($query,array($_REQUEST['form_title'],$form_type,$pid,$subtype));
535 $issue = $issue2['id'];
538 $issue = 0 + $issue;
539 if ($_REQUEST['form_reinjury_id'] =="") $form_reinjury_id="0";
540 if ($_REQUEST['form_injury_grade'] =="") $form_injury_grade="0";
541 if ($_REQUEST['form_outcome'] =='') $_REQUEST['form_outcome'] ='0';
543 if ($issue != '0') { //if this issue already exists we are updating it...
544 $query = "UPDATE lists SET " .
545 "type = '" . add_escape_custom($form_type) . "', " .
546 "title = '" . add_escape_custom($_REQUEST['form_title']) . "', " .
547 "comments = '" . add_escape_custom($_REQUEST['form_comments']) . "', " .
548 "begdate = " . QuotedOrNull($form_begin) . ", " .
549 "enddate = " . QuotedOrNull($form_end) . ", " .
550 "returndate = " . QuotedOrNull($form_return) . ", " .
551 "diagnosis = '" . add_escape_custom($_REQUEST['form_diagnosis']) . "', " .
552 "occurrence = '" . add_escape_custom($_REQUEST['form_occur']) . "', " .
553 "classification = '" . add_escape_custom($_REQUEST['form_classification']) . "', " .
554 "reinjury_id = '" . add_escape_custom($_REQUEST['form_reinjury_id']) . "', " .
555 "referredby = '" . add_escape_custom($_REQUEST['form_referredby']) . "', " .
556 "injury_grade = '" . add_escape_custom($_REQUEST['form_injury_grade']) . "', " .
557 "injury_part = '" . add_escape_custom($form_injury_part) . "', " .
558 "injury_type = '" . add_escape_custom($form_injury_type) . "', " .
559 "outcome = '" . add_escape_custom($_REQUEST['form_outcome']) . "', " .
560 "destination = '" . add_escape_custom($_REQUEST['form_destination']) . "', " .
561 "reaction ='" . add_escape_custom($_REQUEST['form_reaction']) . "', " .
562 "erx_uploaded = '0', " .
563 "modifydate = NOW(), " .
564 "subtype = '" . $subtype. "' " .
565 "WHERE id = '" . add_escape_custom($issue) . "'";
566 sqlStatement($query);
567 if ($text_type == "medication" && enddate != '') {
568 sqlStatement('UPDATE prescriptions SET '
569 . 'medication = 0 where patient_id = ? '
570 . " and upper(trim(drug)) = ? "
571 . ' and medication = 1', array($pid,strtoupper($_REQUEST['form_title'])) );
573 } else {
574 $query = "INSERT INTO lists ( " .
575 "date, pid, type, title, activity, comments, ".
576 "begdate, enddate, returndate, " .
577 "diagnosis, occurrence, classification, referredby, user, " .
578 "groupname, outcome, destination,reaction,subtype " .
579 ") VALUES ( " .
580 "NOW(), ?,?,?,1,?," .
581 QuotedOrNull($form_begin).", ".QuotedOrNull($form_end).", ".QuotedOrNull($form_return). ", " .
582 "?,?,?,?,?,".
583 "?,?,?,?,?)";
584 $issue = sqlInsert($query,array($pid,$form_type,$_REQUEST['form_title'],$_REQUEST['form_comments'],
585 $_REQUEST['form_diagnosis'],$_REQUEST['form_occur'],$_REQUEST['form_clasification'],$_REQUEST['form_referredby'],$_SESSION['authUser'],
586 $_SESSION['authProvider'],QuotedOrNull($_REQUEST['form_outcome']),$_REQUEST['form_destination'],$_REQUEST['form_reaction'],$subtype));
588 // For record/reporting purposes, place entry in lists_touch table.
589 setListTouch($pid,$form_type);
591 // If requested, link the issue to a specified encounter.
592 // we always link them, automatically.
593 if ($encounter) {
594 $query = "INSERT INTO issue_encounter ( " .
595 "pid, list_id, encounter " .
596 ") VALUES ( ?,?,? )";
597 sqlStatement($query, array($pid,$issue,$encounter));
601 $irow = '';
602 //if it is a medication do we need to do something with dosage fields?
603 //leave all in title field form now.
605 $PMSFH = build_PMSFH($pid);
606 send_json_values($PMSFH);
607 exit;
610 if ($_REQUEST['action'] =='code_PMSFH') {
611 $query = "UPDATE lists SET diagnosis = ? WHERE id = ?";
612 sqlStatement($query,array($_POST['code'],$_POST['issue']));
613 exit;
616 if ($_REQUEST['action'] == 'code_visit'){
617 $CODING = json_decode($_REQUEST['parameter'],true);
618 $query = "delete from billing where encounter =?";
619 sqlStatement($query,array($encounter));
620 foreach ($CODING as $item) //need toremove duplicate codes
622 if ($dups[$item["code"]]=='1') {
623 continue;
625 $dups[$item["code"]] = "1";
626 $sql = "SELECT codes.*, prices.pr_price FROM codes " .
627 "LEFT OUTER JOIN patient_data ON patient_data.pid = '$pid' " .
628 "LEFT OUTER JOIN prices ON prices.pr_id = codes.id AND " .
629 "prices.pr_selector = '' AND " .
630 "prices.pr_level = patient_data.pricelevel " .
631 "WHERE code =?" .
632 " LIMIT 1";
633 $result = sqlStatement($sql,array($item['code']));
634 while ($res = sqlFetchArray($result)) {
635 $item["codedesc"] = $res["code_text"];// = "NP EYE intermediate exam"
636 $item["modifier"] = $res["modifier"];
637 $item["units"] = $res["units"];
638 $item["fee"] = $res["pr_price"];
640 addBilling($encounter,$item["codetype"],$item["code"],$item["codedesc"],$pid,'1',$providerID,$item["modifier"], $item["units"], $item["fee"],$ndc_info, $justify, $billed, '');
642 echo 'ok';
643 exit;
645 /*** END CODE to DEAL WITH PMSFH/ISUUE_TYPES ****/
647 /* Let's save the encounter specific values.
648 // Any field that exists in the database could be updated
649 // so we need to exclude the important ones...
650 // id date pid user groupname authorized activity. Any other just add them below.
651 // Doing it this way means you can add new fields on a web page and in the DB without touching this function.
652 // The update feature still works because it only updates columns that are in the table you are working on.
654 $query = "SHOW COLUMNS from ".$table_name."";
655 $result = sqlStatement($query);
656 if (!$result) {
657 return 'Could not run query: No columns found in your table! ' . mysql_error();
658 exit;
660 $fields = array();
661 if (($_POST['IOPTIME'] == '00:00:00')||(!$_POST['IOPTIME'])) {
662 $_POST['IOPTIME'] = date('H:i:s');
664 $_POST['IOPTIME'] = date('H:i:s',strtotime($_POST['IOPTIME']));
666 if (sqlNumRows($result) > 0) {
667 while ($row = sqlFetchArray($result)) {
668 //exclude critical columns/fields and those needing special processing from update
669 if ($row['Field'] == 'id' or
670 $row['Field'] == 'date' or
671 $row['Field'] == 'pid' or
672 $row['Field'] == 'user' or
673 $row['Field'] == 'groupname' or
674 $row['Field'] == 'authorized' or
675 $row['Field'] == 'LOCKED' or
676 $row['Field'] == 'LOCKEDBY' or
677 $row['Field'] == 'activity' or
678 $row['Field'] == 'PLAN' or
679 $row['Field'] == 'Resource')
680 continue;
681 if (isset($_POST[$row['Field']])) $fields[$row['Field']] = $_POST[$row['Field']];
683 // orders are checkboxes created from a user defined list in the PLAN area and stored as item1|item2|item3
684 // if there are any, create the $field['PLAN'] value.
685 // Remember -- If you uncheck a box, it won't be sent!
686 // So delete all made today by this provider and reload with any Orders sent in this $_POST
687 // in addition, we made a special table for orders, and when completed we can mark done?
688 $query="select form_encounter.date as encounter_date from form_encounter where form_encounter.encounter =?";
689 $encounter_data =sqlQuery($query,array($encounter));
690 $dated = new DateTime($encounter_data['encounter_date']);
691 $dated = $dated->format('Y-m-d');
692 $visit_date = oeFormatShortDate($dated);
694 $N = count($_POST['PLAN']);
695 $sql_clear = "DELETE from form_eye_mag_orders where ORDER_PID =? and ORDER_PLACED_BYWHOM=? and ORDER_DATE_PLACED=? and ORDER_STATUS ='pending'";
696 sqlQuery($sql_clear,array($pid,$providerID,$visit_date));
697 if ($N > '0') {
698 for($i=0; $i < $N; $i++)
700 $fields['PLAN'] .= $_POST['PLAN'][$i] . "|"; //this makes an entry for form_eyemag: PLAN
701 $ORDERS_sql = "REPLACE INTO form_eye_mag_orders (ORDER_PID,ORDER_DETAILS,ORDER_STATUS,ORDER_DATE_PLACED,ORDER_PLACED_BYWHOM) VALUES (?,?,?,?,?)";
702 $okthen = sqlQuery($ORDERS_sql,array($pid,$_POST['PLAN'][$i],'pending',$visit_date,$providerID));
704 $fields['PLAN'] = mb_substr($fields['PLAN'], 0, -1); //get rid of trailing "|"
706 if ($_REQUEST['PLAN2']) {
707 $fields['PLAN'] .= $_REQUEST['PLAN2'];
708 //there is something in the "freeform" plan textarea...
709 $ORDERS_sql = "REPLACE INTO form_eye_mag_orders (ORDER_PID,ORDER_DETAILS,ORDER_STATUS,ORDER_PRIORITY,ORDER_DATE_PLACED,ORDER_PLACED_BYWHOM) VALUES (?,?,?,?,?,?)";
710 $okthen = sqlQuery($ORDERS_sql,array($pid,$_POST['PLAN'][$i],'pending',"PLAN2:$PLAN2",$visit_date,$providerID));
713 $M = count($_POST['TEST']);
714 if ($M > '0') {
715 for($i=0; $i < $M; $i++)
717 $fields['Resource'] .= $_POST['TEST'][$i] . "|"; //this makes an entry for form_eyemag: Resource
719 $fields['Resource'] = mb_substr($fields['Resource'], 0, -1); //get rid of trailing "|"
722 /** Empty Checkboxes need to be entered manually as they are only submitted via POST when they are checked
723 * If NOT checked on the form, they are sent via POST and thus are NOT overridden in the DB,
724 * so DB won't change unless we define them into the $fields array as "0"...
726 if (!$_POST['alert']) $fields['alert'] = '0';
727 if (!$_POST['oriented']) $fields['oriented'] = '0';
728 if (!$_POST['confused']) $fields['confused'] = '0';
729 if (!$_POST['PUPIL_NORMAL']) $fields['PUPIL_NORMAL'] = '0';
730 if (!$_POST['MOTILITYNORMAL']) $fields['MOTILITYNORMAL'] = '0';
731 if (!$_POST['ACT']) $fields['ACT'] = '0';
732 if (!$_POST['DIL_RISKS']) $fields['DIL_RISKS'] = '0';
733 if (!$_POST['ATROPINE']) $fields['ATROPINE'] = '0';
734 if (!$_POST['CYCLOGYL']) $fields['CYCLOGYL'] = '0';
735 if (!$_POST['CYCLOMYDRIL']) $fields['CYCLOMYDRIL'] = '0';
736 if (!$_POST['NEO25']) $fields['NEO25'] = '0';
737 if (!$_POST['TROPICAMIDE']) $fields['TROPICAMIDE'] = '0';
738 if (!$_POST['BALANCED']) $fields['BALANCED'] = '0';
739 if (!$_POST['ODVF1']) $fields['ODVF1'] = '0';
740 if (!$_POST['ODVF2']) $fields['ODVF2'] = '0';
741 if (!$_POST['ODVF3']) $fields['ODVF3'] = '0';
742 if (!$_POST['ODVF4']) $fields['ODVF4'] = '0';
743 if (!$_POST['OSVF1']) $fields['OSVF1'] = '0';
744 if (!$_POST['OSVF2']) $fields['OSVF2'] = '0';
745 if (!$_POST['OSVF3']) $fields['OSVF3'] = '0';
746 if (!$_POST['OSVF4']) $fields['OSVF4'] = '0';
747 if (!$_POST['TEST']) $fields['Resource'] = '';
748 if (!$fields['PLAN']) $fields['PLAN'] = '0';
750 $success = formUpdate($table_name, $fields, $form_id, $_SESSION['userauthorized']);
752 //now save any Wear RXs (1-4) entered.
753 $rx_number='1';
754 if ($_POST['W_1']=='1') {
755 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
756 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
757 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
758 `RX_TYPE` ,`COMMENTS`,
759 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
760 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
761 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
762 `LENS_TREATMENTS`
763 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
764 $LENS_TREATMENTS_1 = implode("|",$_POST['LENS_TREATMENTS_1']);
765 sqlQuery($query,array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_1'],$_POST['ODCYL_1'],$_POST['ODAXIS_1'],
766 $_POST['ODVA_1'],$_POST['ODADD_1'],$_POST['ODNEARVA_1'],$_POST['OSSPH_1'],$_POST['OSCYL_1'],$_POST['OSAXIS_1'],
767 $_POST['OSVA_1'],$_POST['OSADD_1'],$_POST['OSNEARVA_1'],$_POST['ODMIDADD_1'],$_POST['OSMIDADD_1'],
768 0+$_POST['RX_TYPE_1'],$_POST['COMMENTS_1'],
769 $_POST['ODHPD_1'],$_POST['ODHBASE_1'],$_POST['ODVPD_1'],$_POST['ODVBASE_1'],$_POST['ODSLABOFF_1'],$_POST['ODVERTEXDIST_1'],
770 $_POST['OSHPD_1'],$_POST['OSHBASE_1'],$_POST['OSVPD_1'],$_POST['OSVBASE_1'],$_POST['OSSLABOFF_1'],$_POST['OSVERTEXDIST_1'],
771 $_POST['ODMPDD_1'],$_POST['ODMPDN_1'],$_POST['OSMPDD_1'],$_POST['OSMPDN_1'],$_POST['BPDD_1'],$_POST['BPDN_1'],$_POST['LENS_MATERIAL_1'],
772 $LENS_TREATMENTS_1 ));
773 $rx_number++;
774 } else {
775 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
776 sqlQuery($query,array($encounter,$pid,$form_id,'1'));
778 if ($_POST['W_2']=='1') {
779 //store W_2
780 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
781 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
782 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
783 `RX_TYPE` ,`COMMENTS`,
784 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
785 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
786 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
787 `LENS_TREATMENTS`
788 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
789 $LENS_TREATMENTS_2 = implode("|",$_POST['LENS_TREATMENTS_2']);
790 sqlQuery($query,array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_2'],$_POST['ODCYL_2'],$_POST['ODAXIS_2'],
791 $_POST['ODVA_2'],$_POST['ODADD_2'],$_POST['ODNEARVA_2'],$_POST['OSSPH_2'],$_POST['OSCYL_2'],$_POST['OSAXIS_2'],
792 $_POST['OSVA_2'],$_POST['OSADD_2'],$_POST['OSNEARVA_2'],$_POST['ODMIDADD_2'],$_POST['OSMIDADD_2'],
793 0+$_POST['RX_TYPE_2'],$_POST['COMMENTS_2'],
794 $_POST['ODHPD_2'],$_POST['ODHBASE_2'],$_POST['ODVPD_2'],$_POST['ODVBASE_2'],$_POST['ODSLABOFF_2'],$_POST['ODVERTEXDIST_2'],
795 $_POST['OSHPD_2'],$_POST['OSHBASE_2'],$_POST['OSVPD_2'],$_POST['OSVBASE_2'],$_POST['OSSLABOFF_2'],$_POST['OSVERTEXDIST_2'],
796 $_POST['ODMPDD_2'],$_POST['ODMPDN_2'],$_POST['OSMPDD_2'],$_POST['OSMPDN_2'],$_POST['BPDD_2'],$_POST['BPDN_2'],$_POST['LENS_MATERIAL_2'],
797 $LENS_TREATMENTS_2 ));
798 $rx_number++;
799 } else {
800 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
801 sqlQuery($query,array($encounter,$pid,$form_id,'2'));
803 if ($_POST['W_3']=='1') {
804 //store W_3
805 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
806 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
807 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
808 `RX_TYPE` ,`COMMENTS`,
809 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
810 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
811 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
812 `LENS_TREATMENTS`
813 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
814 $LENS_TREATMENTS_3 = implode("|",$_POST['LENS_TREATMENTS_3']);
815 sqlQuery($query,array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_3'],$_POST['ODCYL_3'],$_POST['ODAXIS_3'],
816 $_POST['ODVA_3'],$_POST['ODADD_3'],$_POST['ODNEARVA_3'],$_POST['OSSPH_3'],$_POST['OSCYL_3'],$_POST['OSAXIS_3'],
817 $_POST['OSVA_3'],$_POST['OSADD_3'],$_POST['OSNEARVA_3'],$_POST['ODMIDADD_3'],$_POST['OSMIDADD_3'],
818 0+$_POST['RX_TYPE_3'],$_POST['COMMENTS_3'],
819 $_POST['ODHPD_3'],$_POST['ODHBASE_3'],$_POST['ODVPD_3'],$_POST['ODVBASE_3'],$_POST['ODSLABOFF_3'],$_POST['ODVERTEXDIST_3'],
820 $_POST['OSHPD_3'],$_POST['OSHBASE_3'],$_POST['OSVPD_3'],$_POST['OSVBASE_3'],$_POST['OSSLABOFF_3'],$_POST['OSVERTEXDIST_3'],
821 $_POST['ODMPDD_3'],$_POST['ODMPDN_3'],$_POST['OSMPDD_3'],$_POST['OSMPDN_3'],$_POST['BPDD_3'],$_POST['BPDN_3'],$_POST['LENS_MATERIAL_3'],
822 $LENS_TREATMENTS_3 ));
823 $rx_number++;
824 } else {
825 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
826 sqlQuery($query,array($encounter,$pid,$form_id,'3'));
828 if ($_POST['W_4']=='1') {
829 //store W_4
830 $query = "REPLACE INTO `form_eye_mag_wearing` (`ENCOUNTER` ,`FORM_ID` ,`PID` ,`RX_NUMBER` ,`ODSPH` ,`ODCYL` ,`ODAXIS` ,
831 `ODVA` ,`ODADD` ,`ODNEARVA` ,`OSSPH` ,`OSCYL` ,`OSAXIS` ,
832 `OSVA` ,`OSADD` ,`OSNEARVA` ,`ODMIDADD` ,`OSMIDADD` ,
833 `RX_TYPE` ,`COMMENTS`,
834 `ODHPD`,`ODHBASE`,`ODVPD`,`ODVBASE`,`ODSLABOFF`,`ODVERTEXDIST`,
835 `OSHPD`,`OSHBASE`,`OSVPD`,`OSVBASE`,`OSSLABOFF`,`OSVERTEXDIST`,
836 `ODMPDD`,`ODMPDN`,`OSMPDD`,`OSMPDN`,`BPDD`,`BPDN`,`LENS_MATERIAL`,
837 `LENS_TREATMENTS`
838 ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
839 $LENS_TREATMENTS_4 = implode("|",$_POST['LENS_TREATMENTS_4']);
840 sqlQuery($query,array($encounter,$form_id,$pid,$rx_number,$_POST['ODSPH_4'],$_POST['ODCYL_4'],$_POST['ODAXIS_4'],
841 $_POST['ODVA_4'],$_POST['ODADD_4'],$_POST['ODNEARVA_4'],$_POST['OSSPH_4'],$_POST['OSCYL_4'],$_POST['OSAXIS_4'],
842 $_POST['OSVA_4'],$_POST['OSADD_4'],$_POST['OSNEARVA_4'],$_POST['ODMIDADD_4'],$_POST['OSMIDADD_4'],
843 0+$_POST['RX_TYPE_4'],$_POST['COMMENTS_4'],
844 $_POST['ODHPD_4'],$_POST['ODHBASE_4'],$_POST['ODVPD_4'],$_POST['ODVBASE_4'],$_POST['ODSLABOFF_4'],$_POST['ODVERTEXDIST_4'],
845 $_POST['OSHPD_4'],$_POST['OSHBASE_4'],$_POST['OSVPD_4'],$_POST['OSVBASE_4'],$_POST['OSSLABOFF_4'],$_POST['OSVERTEXDIST_4'],
846 $_POST['ODMPDD_4'],$_POST['ODMPDN_4'],$_POST['OSMPDD_4'],$_POST['OSMPDN_4'],$_POST['BPDD_4'],$_POST['BPDN_4'],$_POST['LENS_MATERIAL_4'],
847 $LENS_TREATMENTS_4 ));
848 $rx_number++;
849 } else {
850 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
851 sqlQuery($query,array($encounter,$pid,$form_id,'4'));
853 for ($i=$rx_number; $i < 5; $i++) {
854 $query = "DELETE FROM form_eye_mag_wearing where ENCOUNTER=? and PID=? and FORM_ID=? and RX_NUMBER=?";
855 sqlQuery($query,array($encounter,$pid,$form_id,$i));
857 //now return the obj
858 $send['IMPPLAN_items'] = build_IMPPLAN_items($pid,$form_id);
859 $send['Clinical'] = start_your_engines($_REQUEST);
860 $send['PMH_panel'] = display_PMSFH('2');
861 $send['right_panel'] = show_PMSFH_panel($PMSFH);
862 $send['PMSFH'] = $PMSFH[0];
863 echo json_encode($send);
864 exit;
866 } elseif ($_REQUEST["mode"] == "retrieve") {
868 if ($_REQUEST['PRIORS_query']) {
869 echo display_PRIOR_section($_REQUEST['zone'],$_REQUEST['orig_id'],$_REQUEST['id_to_show'],$pid);
870 exit;
875 * Save the canvas drawings
878 if ($_REQUEST['canvas']) {
879 if (!$pid||!$encounter||!$zone||!$_POST["imgBase64"]) exit;
881 $side = "OU";
882 $base_name = $pid."_".$encounter."_".$side."_".$zone."_VIEW";
883 $filename = $base_name.".jpg";
885 $type = "image/jpeg"; // all our canvases are this type
886 $data = $_POST["imgBase64"];
887 $data = substr($data, strpos($data, ",")+1);
888 $data = base64_decode($data);
889 $size = strlen($data);
890 $query = "select id from categories where name = 'Drawings'";
891 $result = sqlStatement($query);
892 $ID = sqlFetchArray($result);
893 $category_id = $ID['id'];
895 // We want to overwrite so only one image is stored per zone per form/encounter
896 // I do not believe this function exists in the current library, ie "UpdateDocument" function, so...
897 // we need to delete the previous file from the documents and categories to documents tables and the actual file
898 // There must be a delete_file function in documents class?
899 // cannot find it.
900 // this will work for harddisk people, not sure about couchDB people:
901 $filepath = $GLOBALS['oer_config']['documents']['repository'] . $pid ."/";
902 foreach (glob($filepath.'/'.$filename) as $file) {
903 unlink($file);
906 $sql = "DELETE from categories_to_documents where document_id IN (SELECT id from documents where documents.url like '%".$filename."')";
907 sqlQuery($sql);
908 $sql ="DELETE from documents where documents.url like '%".$filename."'";
909 sqlQuery($sql);
910 $return = addNewDocument($filename,$type,$_POST["imgBase64"],0,$size,$_SESSION['authUserID'],$pid,$category_id);
911 $doc_id = $return['doc_id'];
912 $sql = "UPDATE documents set encounter_id=? where id=?"; //link it to this encounter
913 sqlQuery($sql,array($encounter,$doc_id));
914 exit;
917 if ($_REQUEST['copy']) {
918 copy_forward($_REQUEST['zone'],$_REQUEST['copy_from'],$_SESSION['ID'],$pid);
919 return;
921 function QuotedOrNull($fld) {
922 if ($fld) return "'".add_escape_custom($fld)."'";
923 return "NULL";
925 function debug($local_var) {
926 echo "<pre><BR>We are in the debug function.<BR>";
927 echo "Passed variable = ". $local_var . " <BR>";
928 print_r($local_var);
929 exit;
932 /* From original issue.php */
934 function row_delete($table, $where) {
935 $query = "SELECT * FROM $table WHERE $where";
936 $tres = sqlStatement($query);
937 $count = 0;
938 while ($trow = sqlFetchArray($tres)) {
939 $logstring = "";
940 foreach ($trow as $key => $value) {
941 if (! $value || $value == '0000-00-00 00:00:00') continue;
942 if ($logstring) $logstring .= " ";
943 $logstring .= $key . "='" . addslashes($value) . "'";
945 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
946 ++$count;
948 if ($count) {
949 $query = "DELETE FROM $table WHERE $where";
950 sqlStatement($query);
953 // Given an issue type as a string, compute its index.
954 // Not sure of the value of this sub given transition to array $PMSFH
955 // Can I use it to find out which PMSFH item we are looking for? YES
956 function issueTypeIndex($tstr) {
957 global $ISSUE_TYPES;
958 $i = 0;
959 foreach ($ISSUE_TYPES as $key => $value) {
960 if ($key == $tstr) break;
961 ++$i;
963 return $i;
967 * The following 2 functions can be removed from the production environment
970 function merge($filename_x, $filename_y, $filename_result) {
972 * Three png files (OU,OD,OS) per LOCATION (EXT,ANTSEG,RETINA,NEURO)
973 * BASE, found in forms/$form_folder/images eg. OU_EXT_BASE.png
974 * BASE is the blank image to start from and can be customized. Currently 432x150px
975 * VIEW, found in /sites/$_SESSION['site_id']."/".$form_folder."/".$pid."/".$encounter
976 * TEMP, intermediate png merge file of new drawings with BASE or previous VIEW
977 * These are saved to be used in an undo feature...
978 * NO LONGER USING but I kept it here because it is cool and I will use it later
981 This section
982 if (file_exists($storage."/OU_".$zone."_VIEW.png")) { //add new drawings to previous for this encounter
983 $file_base = $storage."/OU_".$zone."_VIEW.png";
984 } else { //start from the base image
985 $file_base = $GLOBALS['webserver_root']."/interface/forms/".$form_folder."/images/OU_".$zone."_BASE.png";
987 //merge needs to store to a separate file first, then rename to new VIEW
988 $file_temp = $storage."/OU_".$zone."_TEMP.png";
989 $file_here = $storage."/OU_".$zone."_VIEW.png";
990 merge( $file_draw, $file_base, $file_temp);
991 rename( $file_temp , $file_here );
993 // Get dimensions for specified images
994 list($width_x, $height_x) = getimagesize($filename_x);
995 list($width_y, $height_y) = getimagesize($filename_y);
997 // Create new image with desired dimensions
998 $image = imagecreatetruecolor($width_y, $height_y);
1000 // Load images and then copy to destination image
1001 $image_x = imagecreatefrompng($filename_x);
1002 $image_y = imagecreatefrompng($filename_y);
1004 imagecopy($image, $image_y, 0, 0, 0, 0, $width_x, $height_x);
1005 imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
1007 // Save the resulting image to disk (as png)
1008 imagepng($image, $filename_result);
1010 // Clean up
1011 imagedestroy($image);
1012 imagedestroy($image_x);
1013 imagedestroy($image_y);
1016 // this function is here to understand the core openEMR function addBilling, so we can improve the Billing Engine in Eye Form
1017 // We still need to add modifiers and justify capabilities to the Coding Engine...
1018 function addBilling2($encounter_id, $code_type, $code, $code_text, $pid, $authorized="0", $provider, $modifier="", $units="", $fee="0.00",
1019 $ndc_info='', $justify='', $billed=0, $notecodes='')
1021 $sql = "insert into billing (date, encounter, code_type, code, code_text, " .
1022 "pid, authorized, user, groupname, activity, billed, provider_id, " .
1023 "modifier, units, fee, ndc_info, justify, notecodes) values (" .
1024 "NOW(), ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?)";
1025 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));
1027 exit;