Fee sheet improvements. Can set default code type. take 3.
[openemr.git] / interface / super / edit_globals.php
blob121161da888e94842c1c23bf0a794c7010a75c81
1 <?php
2 // Copyright (C) 2010 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 require_once("../globals.php");
10 require_once("../../custom/code_types.inc.php");
11 require_once("$srcdir/acl.inc");
12 require_once("$srcdir/formdata.inc.php");
13 require_once("$srcdir/globals.inc.php");
14 require_once("$srcdir/user.inc");
15 require_once("$srcdir/classes/CouchDB.class.php");
16 require_once(dirname(__FILE__)."/../../myportal/soap_service/portal_connectivity.php");
18 if ($_GET['mode'] != "user") {
19 // Check authorization.
20 $thisauth = acl_check('admin', 'super');
21 if (!$thisauth) die(xl('Not authorized'));
24 function checkCreateCDB(){
25 $globalsres = sqlStatement("SELECT gl_name, gl_index, gl_value FROM globals WHERE gl_name IN
26 ('couchdb_host','couchdb_user','couchdb_pass','couchdb_port','couchdb_dbase','document_storage_method')");
27 $options = array();
28 while($globalsrow = sqlFetchArray($globalsres)){
29 $GLOBALS[$globalsrow['gl_name']] = $globalsrow['gl_value'];
31 $directory_created = false;
32 if($GLOBALS['document_storage_method'] != 0){
33 // /documents/temp/ folder is required for CouchDB
34 if(!is_dir($GLOBALS['OE_SITE_DIR'] . '/documents/temp/')){
35 $directory_created = mkdir($GLOBALS['OE_SITE_DIR'] . '/documents/temp/',0777,true);
36 if(!$directory_created){
37 echo htmlspecialchars( xl("Failed to create temporary folder. CouchDB will not work."),ENT_NOQUOTES);
40 $couch = new CouchDB();
41 if(!$couch->check_connection()) {
42 echo "<script type='text/javascript'>alert('".addslashes(xl("CouchDB Connection Failed."))."');</script>";
43 return;
45 if($GLOBALS['couchdb_host'] || $GLOBALS['couchdb_port'] || $GLOBALS['couchdb_dbase']){
46 $couch->createDB($GLOBALS['couchdb_dbase']);
47 $couch->createView($GLOBALS['couchdb_dbase']);
50 return true;
53 /**
54 * Update background_services table for a specific service following globals save.
55 * @author EMR Direct
57 function updateBackgroundService($name,$active,$interval) {
58 //order important here: next_run change dependent on _old_ value of execute_interval so it comes first
59 $sql = 'UPDATE background_services SET active=?, '
60 . 'next_run = next_run + INTERVAL (? - execute_interval) MINUTE, execute_interval=? WHERE name=?';
61 return sqlStatement($sql,array($active,$interval,$interval,$name));
64 /**
65 * Make any necessary changes to background_services table when globals are saved.
66 * To prevent an unexpected service call during startup or shutdown, follow these rules:
67 * 1. Any "startup" operations should occur _before_ the updateBackgroundService() call.
68 * 2. Any "shutdown" operations should occur _after_ the updateBackgroundService() call. If these operations
69 * would cause errors in a running service call, it would be best to make the shutdown function itself
70 * a background service that is activated here, does nothing if active=1 or running=1 for the
71 * parent service, then deactivates itself by setting active=0 when it is done shutting the parent service
72 * down. This will prevent nonresponsiveness to the user by waiting for a service to finish.
73 * 3. If any "previous" values for globals are required for startup/shutdown logic, they need to be
74 * copied to a temp variable before the while($globalsrow...) loop.
75 * @author EMR Direct
77 function checkBackgroundServices(){
78 //load up any necessary globals
79 $bgservices = sqlStatement("SELECT gl_name, gl_index, gl_value FROM globals WHERE gl_name IN
80 ('phimail_enable','phimail_interval')");
81 while($globalsrow = sqlFetchArray($bgservices)){
82 $GLOBALS[$globalsrow['gl_name']] = $globalsrow['gl_value'];
85 //Set up phimail service
86 $phimail_active = $GLOBALS['phimail_enable'] ? '1' : '0';
87 $phimail_interval = max(0,(int)$GLOBALS['phimail_interval']);
88 updateBackgroundService('phimail',$phimail_active,$phimail_interval);
92 <html>
94 <head>
95 <?php
97 html_header_show();
99 // If we are saving user_specific globals.
101 if ($_POST['form_save'] && $_GET['mode'] == "user") {
102 $i = 0;
103 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
104 if (in_array($grpname, $USER_SPECIFIC_TABS)) {
105 foreach ($grparr as $fldid => $fldarr) {
106 if (in_array($fldid, $USER_SPECIFIC_GLOBALS)) {
107 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
108 $label = "global:".$fldid;
109 $fldvalue = trim(strip_escape_custom($_POST["form_$i"]));
110 setUserSetting($label,$fldvalue,$_SESSION['authId'],FALSE);
111 if ( $_POST["toggle_$i"] == "YES" ) {
112 removeUserSetting($label);
114 ++$i;
119 echo "<script type='text/javascript'>";
120 echo "parent.left_nav.location.reload();";
121 echo "parent.Title.location.reload();";
122 echo "if(self.name=='RTop'){";
123 echo "parent.RBot.location.reload();";
124 echo "}else{";
125 echo "parent.RTop.location.reload();";
126 echo "}";
127 echo "self.location.href='edit_globals.php?mode=user&unique=yes';";
128 echo "</script>";
131 if ($_POST['form_download']) {
132 $client = portal_connection();
133 try {
134 $response = $client->getPortalConnectionFiles($credentials);
136 catch(SoapFault $e){
137 error_log('SoapFault Error');
138 error_log(var_dump(get_object_vars($e)));
140 catch(Exception $e){
141 error_log('Exception Error');
142 error_log(var_dump(get_object_vars($e)));
144 if($response['status'] == "1") {//WEBSERVICE RETURNED VALUE SUCCESSFULLY
145 $tmpfilename = realpath(sys_get_temp_dir())."/".date('YmdHis').".zip";
146 $fp = fopen($tmpfilename,"wb");
147 fwrite($fp,base64_decode($response['value']));
148 fclose($fp);
149 $practice_filename = $response['file_name'];//practicename.zip
150 ob_clean();
151 // Set headers
152 header("Cache-Control: public");
153 header("Content-Description: File Transfer");
154 header("Content-Disposition: attachment; filename=".$practice_filename);
155 header("Content-Type: application/zip");
156 header("Content-Transfer-Encoding: binary");
157 // Read the file from disk
158 readfile($tmpfilename);
159 unlink($tmpfilename);
160 exit;
162 else{//WEBSERVICE CALL FAILED AND RETURNED AN ERROR MESSAGE
163 ob_end_clean();
165 <script type="text/javascript">
166 alert('<?php echo xlt('Offsite Portal web Service Failed').":\\n".text($response['value']);?>');
167 </script>
168 <?php
172 <html>
173 <head>
174 <?php
176 // If we are saving main globals.
178 if ($_POST['form_save'] && $_GET['mode'] != "user") {
179 $force_off_enable_auditlog_encryption = true;
180 // Need to force enable_auditlog_encryption off if the php mycrypt module
181 // is not installed.
182 if (extension_loaded('mcrypt')) {
183 $force_off_enable_auditlog_encryption = false;
186 // Aug 22, 2014: Ensoftek: For Auditable events and tamper-resistance (MU2)
187 // Check the current status of Audit Logging
188 $auditLogStatusFieldOld = $GLOBALS['enable_auditlog'];
190 $i = 0;
191 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
192 foreach ($grparr as $fldid => $fldarr) {
193 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
194 if($fldtype == 'pwd'){
195 $pass = sqlQuery("SELECT gl_value FROM globals WHERE gl_name = '$fldid'");
196 $fldvalueold = $pass['gl_value'];
198 sqlStatement("DELETE FROM globals WHERE gl_name = '$fldid'");
200 if (substr($fldtype, 0, 2) == 'm_') {
201 if (isset($_POST["form_$i"])) {
202 $fldindex = 0;
203 foreach ($_POST["form_$i"] as $fldvalue) {
204 $fldvalue = formDataCore($fldvalue, true);
205 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
206 "VALUES ( '$fldid', '$fldindex', '$fldvalue' )");
207 ++$fldindex;
211 else {
212 if (isset($_POST["form_$i"])) {
213 $fldvalue = formData("form_$i", "P", true);
215 else {
216 $fldvalue = "";
218 if($fldtype=='pwd') $fldvalue = $fldvalue ? SHA1($fldvalue) : $fldvalueold;
219 if(fldvalue){
220 // Need to force enable_auditlog_encryption off if the php mycrypt module
221 // is not installed.
222 if ( $force_off_enable_auditlog_encryption && ($fldid == "enable_auditlog_encryption") ) {
223 error_log("OPENEMR ERROR: UNABLE to support auditlog encryption since the php mycrypt module is not installed",0);
224 $fldvalue=0;
226 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
227 "VALUES ( '$fldid', '0', '$fldvalue' )");
231 ++$i;
234 checkCreateCDB();
235 checkBackgroundServices();
237 // July 1, 2014: Ensoftek: For Auditable events and tamper-resistance (MU2)
238 // If Audit Logging status has changed, log it.
239 $auditLogStatusNew = sqlQuery("SELECT gl_value FROM globals WHERE gl_name = 'enable_auditlog'");
240 $auditLogStatusFieldNew = $auditLogStatusNew['gl_value'];
241 if ( $auditLogStatusFieldOld != $auditLogStatusFieldNew )
243 auditSQLAuditTamper($auditLogStatusFieldNew);
245 echo "<script type='text/javascript'>";
246 echo "parent.left_nav.location.reload();";
247 echo "parent.Title.location.reload();";
248 echo "if(self.name=='RTop'){";
249 echo "parent.RBot.location.reload();";
250 echo "}else{";
251 echo "parent.RTop.location.reload();";
252 echo "}";
253 echo "self.location.href='edit_globals.php?unique=yes';";
254 echo "</script>";
258 <!-- supporting javascript code -->
259 <script type="text/javascript" src="../../library/dialog.js"></script>
260 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
261 <script type="text/javascript" src="../../library/js/common.js"></script>
262 <script type="text/javascript" src="../../library/js/fancybox/jquery.fancybox-1.2.6.js"></script>
263 <link rel="stylesheet" type="text/css" href="../../library/js/fancybox/jquery.fancybox-1.2.6.css" media="screen" />
265 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
266 <?php if ($_GET['mode'] == "user") { ?>
267 <title><?php xl('User Settings','e'); ?></title>
268 <?php } else { ?>
269 <title><?php xl('Global Settings','e'); ?></title>
270 <?php } ?>
272 <style>
273 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
274 tr.detail { font-size:10pt; }
275 td { font-size:10pt; }
276 input { font-size:10pt; }
277 </style>
278 <script type="text/javascript">
279 function validate_file(){
280 $.ajax({
281 type: "POST",
282 url: "<?php echo $GLOBALS['webroot']?>/library/ajax/offsite_portal_ajax.php",
283 data: {
284 action: 'check_file',
286 cache: false,
287 success: function( message )
289 if(message == 'OK'){
290 document.getElementById('form_download').value = 1;
291 document.getElementById('file_error_message').innerHTML = '';
292 document.forms[0].submit();
294 else{
295 document.getElementById('form_download').value = 0;
296 document.getElementById('file_error_message').innerHTML = message;
297 return false;
302 </script>
303 </head>
305 <body class="body_top">
307 <?php if ($_GET['mode'] == "user") { ?>
308 <form method='post' name='theform' id='theform' action='edit_globals.php?mode=user' onsubmit='return top.restoreSession()'>
309 <?php } else { ?>
310 <form method='post' name='theform' id='theform' action='edit_globals.php' onsubmit='return top.restoreSession()'>
311 <?php } ?>
313 <?php if ($_GET['mode'] == "user") { ?>
314 <p><b><?php xl('Edit User Settings','e'); ?></b>
315 <?php } else { ?>
316 <p><b><?php xl('Edit Global Settings','e'); ?></b>
317 <?php } ?>
319 <ul class="tabNav">
320 <?php
321 $i = 0;
322 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
323 if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($grpname, $USER_SPECIFIC_TABS)) ) {
324 echo " <li" . ($i ? "" : " class='current'") .
325 "><a href='/play/javascript-tabbed-navigation/'>" .
326 xl($grpname) . "</a></li>\n";
327 ++$i;
331 </ul>
333 <div class="tabContainer">
334 <?php
335 $i = 0;
336 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
337 if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($grpname, $USER_SPECIFIC_TABS)) ) {
338 echo " <div class='tab" . ($i ? "" : " current") .
339 "' style='height:auto;width:97%;'>\n";
341 echo " <table>";
343 if ($_GET['mode'] == "user") {
344 echo "<tr>";
345 echo "<th>&nbsp</th>";
346 echo "<th>" . htmlspecialchars( xl('User Specific Setting'), ENT_NOQUOTES) . "</th>";
347 echo "<th>" . htmlspecialchars( xl('Default Setting'), ENT_NOQUOTES) . "</th>";
348 echo "<th>&nbsp</th>";
349 echo "<th>" . htmlspecialchars( xl('Set to Default'), ENT_NOQUOTES) . "</th>";
350 echo "</tr>";
353 foreach ($grparr as $fldid => $fldarr) {
354 if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($fldid, $USER_SPECIFIC_GLOBALS)) ) {
355 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
357 // Most parameters will have a single value, but some will be arrays.
358 // Here we cater to both possibilities.
359 $glres = sqlStatement("SELECT gl_index, gl_value FROM globals WHERE " .
360 "gl_name = '$fldid' ORDER BY gl_index");
361 $glarr = array();
362 while ($glrow = sqlFetchArray($glres)) $glarr[] = $glrow;
364 // $fldvalue is meaningful only for the single-value cases.
365 $fldvalue = count($glarr) ? $glarr[0]['gl_value'] : $flddef;
367 // Collect user specific setting if mode set to user
368 $userSetting = "";
369 $settingDefault = "checked='checked'";
370 if ($_GET['mode'] == "user") {
371 $userSettingArray = sqlQuery("SELECT * FROM user_settings WHERE setting_user=? AND setting_label=?",array($_SESSION['authId'],"global:".$fldid));
372 $userSetting = $userSettingArray['setting_value'];
373 $globalValue = $fldvalue;
374 if (!empty($userSettingArray)) {
375 $fldvalue = $userSetting;
376 $settingDefault = "";
380 echo " <tr title='$flddesc'><td valign='top'><b>$fldname </b></td><td valign='top'>\n";
382 if (is_array($fldtype)) {
383 echo " <select name='form_$i' id='form_$i'>\n";
384 foreach ($fldtype as $key => $value) {
385 if ($_GET['mode'] == "user") {
386 if ($globalValue == $key) $globalTitle = $value;
388 echo " <option value='$key'";
389 if ($key == $fldvalue) echo " selected";
390 echo ">";
391 echo $value;
392 echo "</option>\n";
394 echo " </select>\n";
397 else if ($fldtype == 'bool') {
398 if ($_GET['mode'] == "user") {
399 if ($globalValue == 1) {
400 $globalTitle = htmlspecialchars( xl('Checked'), ENT_NOQUOTES);
402 else {
403 $globalTitle = htmlspecialchars( xl('Not Checked'), ENT_NOQUOTES);
406 echo " <input type='checkbox' name='form_$i' id='form_$i' value='1'";
407 if ($fldvalue) echo " checked";
408 echo " />\n";
411 else if ($fldtype == 'num') {
412 if ($_GET['mode'] == "user") {
413 $globalTitle = $globalValue;
415 echo " <input type='text' name='form_$i' id='form_$i' " .
416 "size='6' maxlength='15' value='$fldvalue' />\n";
419 else if ($fldtype == 'text') {
420 if ($_GET['mode'] == "user") {
421 $globalTitle = $globalValue;
423 echo " <input type='text' name='form_$i' id='form_$i' " .
424 "size='50' maxlength='255' value='$fldvalue' />\n";
426 else if ($fldtype == 'pwd') {
427 if ($_GET['mode'] == "user") {
428 $globalTitle = $globalValue;
430 echo " <input type='password' name='form_$i' " .
431 "size='50' maxlength='255' value='' />\n";
434 else if ($fldtype == 'pass') {
435 if ($_GET['mode'] == "user") {
436 $globalTitle = $globalValue;
438 echo " <input type='password' name='form_$i' " .
439 "size='50' maxlength='255' value='$fldvalue' />\n";
442 else if ($fldtype == 'lang') {
443 $res = sqlStatement("SELECT * FROM lang_languages ORDER BY lang_description");
444 echo " <select name='form_$i' id='form_$i'>\n";
445 while ($row = sqlFetchArray($res)) {
446 echo " <option value='" . $row['lang_description'] . "'";
447 if ($row['lang_description'] == $fldvalue) echo " selected";
448 echo ">";
449 echo xl($row['lang_description']);
450 echo "</option>\n";
452 echo " </select>\n";
455 else if ($fldtype == 'all_code_types') {
456 global $code_types;
457 echo " <select name='form_$i' id='form_$i'>\n";
458 foreach (array_keys($code_types) as $code_key ) {
459 echo " <option value='" . attr($code_key) . "'";
460 if ($code_key == $fldvalue) echo " selected";
461 echo ">";
462 echo xlt($code_types[$code_key]['label']);
463 echo "</option>\n";
465 echo " </select>\n";
468 else if ($fldtype == 'm_lang') {
469 $res = sqlStatement("SELECT * FROM lang_languages ORDER BY lang_description");
470 echo " <select multiple name='form_{$i}[]' id='form_{$i}[]' size='3'>\n";
471 while ($row = sqlFetchArray($res)) {
472 echo " <option value='" . $row['lang_description'] . "'";
473 foreach ($glarr as $glrow) {
474 if ($glrow['gl_value'] == $row['lang_description']) {
475 echo " selected";
476 break;
479 echo ">";
480 echo xl($row['lang_description']);
481 echo "</option>\n";
483 echo " </select>\n";
486 else if ($fldtype == 'css') {
487 if ($_GET['mode'] == "user") {
488 $globalTitle = $globalValue;
490 $themedir = "$webserver_root/interface/themes";
491 $dh = opendir($themedir);
492 if ($dh) {
493 echo " <select name='form_$i' id='form_$i'>\n";
494 while (false !== ($tfname = readdir($dh))) {
495 // Only show files that contain style_ as options
496 // Skip style_blue.css since this is used for
497 // lone scripts such as setup.php
498 // Also skip style_pdf.css which is for PDFs and not screen output
499 if (!preg_match("/^style_.*\.css$/", $tfname) ||
500 $tfname == 'style_blue.css' || $tfname == 'style_pdf.css')
501 continue;
502 echo "<option value='$tfname'";
503 if ($tfname == $fldvalue) echo " selected";
504 echo ">";
505 echo $tfname;
506 echo "</option>\n";
508 closedir($dh);
509 echo " </select>\n";
513 else if ($fldtype == 'hour') {
514 if ($_GET['mode'] == "user") {
515 $globalTitle = $globalValue;
517 echo " <select name='form_$i' id='form_$i'>\n";
518 for ($h = 0; $h < 24; ++$h) {
519 echo "<option value='$h'";
520 if ($h == $fldvalue) echo " selected";
521 echo ">";
522 if ($h == 0) echo "12 AM";
523 else if ($h < 12) echo "$h AM";
524 else if ($h == 12) echo "12 PM";
525 else echo ($h - 12) . " PM";
526 echo "</option>\n";
528 echo " </select>\n";
530 if ($_GET['mode'] == "user") {
531 echo " </td>\n";
532 echo "<td align='center' style='color:red;'>" . $globalTitle . "</td>\n";
533 echo "<td>&nbsp</td>";
534 echo "<td align='center'><input type='checkbox' value='YES' name='toggle_" . $i . "' id='toggle_" . $i . "' " . $settingDefault . "/></td>\n";
535 echo "<input type='hidden' id='globaldefault_" . $i . "' value='" . $globalValue . "'>\n";
536 echo "</tr>\n";
538 else {
539 echo " </td></tr>\n";
541 ++$i;
543 if(trim(strtolower($fldid)) == 'portal_offsite_address_patient_link' && $GLOBALS['portal_offsite_enable'] && $GLOBALS['portal_offsite_providerid']){
544 echo "<input type='hidden' name='form_download' id='form_download'>";
545 echo "<tr><td><input onclick=\"return validate_file()\" type='button' value='".xla('Download Offsite Portal Connection Files')."' /></td><td id='file_error_message' style='color:red'></td></tr>";
548 echo " </table>\n";
549 echo " </div>\n";
553 </div>
556 <input type='submit' name='form_save' value='<?php echo xla('Save'); ?>' />
557 </p>
558 </center>
560 </form>
562 </body>
564 <script language="JavaScript">
566 $(document).ready(function(){
567 tabbify();
568 enable_modals();
570 // Use the counter ($i) to make the form user friendly for user-specific globals use
571 <?php if ($_GET['mode'] == "user") { ?>
572 <?php for ($j = 0; $j <= $i; $j++) { ?>
573 $("#form_<?php echo $j ?>").change(function() {
574 $("#toggle_<?php echo $j ?>").attr('checked',false);
576 $("#toggle_<?php echo $j ?>").change(function() {
577 if ($('#toggle_<?php echo $j ?>').attr('checked')) {
578 var defaultGlobal = $("#globaldefault_<?php echo $j ?>").val();
579 $("#form_<?php echo $j ?>").val(defaultGlobal);
582 <?php } ?>
583 <?php } ?>
587 </script>
589 </html>