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