bug fix for apostrophe in facility name
[openemr.git] / interface / super / edit_globals.php
blob62aa5f2f19e84afc36991fe4c60c8904d4c80229
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("$srcdir/acl.inc");
11 require_once("$srcdir/formdata.inc.php");
12 require_once("$srcdir/globals.inc.php");
13 require_once("$srcdir/user.inc");
14 require_once("$srcdir/classes/CouchDB.class.php");
16 if ($_GET['mode'] != "user") {
17 // Check authorization.
18 $thisauth = acl_check('admin', 'super');
19 if (!$thisauth) die(xl('Not authorized'));
22 function checkCreateCDB(){
23 $globalsres = sqlStatement("SELECT gl_name, gl_index, gl_value FROM globals WHERE gl_name IN
24 ('couchdb_host','couchdb_user','couchdb_pass','couchdb_port','couchdb_dbase','document_storage_method')");
25 $options = array();
26 while($globalsrow = sqlFetchArray($globalsres)){
27 $GLOBALS[$globalsrow['gl_name']] = $globalsrow['gl_value'];
29 $directory_created = false;
30 if($GLOBALS['document_storage_method'] != 0){
31 // /documents/temp/ folder is required for CouchDB
32 if(!is_dir($GLOBALS['OE_SITE_DIR'] . '/documents/temp/')){
33 $directory_created = mkdir($GLOBALS['OE_SITE_DIR'] . '/documents/temp/',0777,true);
34 if(!$directory_created){
35 echo htmlspecialchars( xl("Failed to create temporary folder. CouchDB will not work."),ENT_NOQUOTES);
38 $couch = new CouchDB();
39 if(!$couch->check_connection()) {
40 echo "<script type='text/javascript'>alert('".addslashes(xl("CouchDB Connection Failed."))."');</script>";
41 return;
43 if($GLOBALS['couchdb_host'] || $GLOBALS['couchdb_port'] || $GLOBALS['couchdb_dbase']){
44 $couch->createDB($GLOBALS['couchdb_dbase']);
45 $couch->createView($GLOBALS['couchdb_dbase']);
48 return true;
52 <html>
54 <head>
55 <?php
57 html_header_show();
59 // If we are saving user_specific globals.
61 if ($_POST['form_save'] && $_GET['mode'] == "user") {
62 $i = 0;
63 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
64 if (in_array($grpname, $USER_SPECIFIC_TABS)) {
65 foreach ($grparr as $fldid => $fldarr) {
66 if (in_array($fldid, $USER_SPECIFIC_GLOBALS)) {
67 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
68 $label = "global:".$fldid;
69 $fldvalue = trim(strip_escape_custom($_POST["form_$i"]));
70 setUserSetting($label,$fldvalue,$_SESSION['authId'],FALSE);
71 if ( $_POST["toggle_$i"] == "YES" ) {
72 removeUserSetting($label);
74 ++$i;
79 echo "<script type='text/javascript'>";
80 echo "parent.left_nav.location.reload();";
81 echo "parent.Title.location.reload();";
82 echo "if(self.name=='RTop'){";
83 echo "parent.RBot.location.reload();";
84 echo "}else{";
85 echo "parent.RTop.location.reload();";
86 echo "}";
87 echo "self.location.href='edit_globals.php?mode=user&unique=yes';";
88 echo "</script>";
91 // If we are saving main globals.
93 if ($_POST['form_save'] && $_GET['mode'] != "user") {
95 $i = 0;
96 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
97 foreach ($grparr as $fldid => $fldarr) {
98 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
99 if($fldtype == 'pwd'){
100 $pass = sqlQuery("SELECT gl_value FROM globals WHERE gl_name = '$fldid'");
101 $fldvalueold = $pass['gl_value'];
103 sqlStatement("DELETE FROM globals WHERE gl_name = '$fldid'");
105 if (substr($fldtype, 0, 2) == 'm_') {
106 if (isset($_POST["form_$i"])) {
107 $fldindex = 0;
108 foreach ($_POST["form_$i"] as $fldvalue) {
109 $fldvalue = formDataCore($fldvalue, true);
110 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
111 "VALUES ( '$fldid', '$fldindex', '$fldvalue' )");
112 ++$fldindex;
116 else {
117 if (isset($_POST["form_$i"])) {
118 $fldvalue = formData("form_$i", "P", true);
120 else {
121 $fldvalue = "";
123 if($fldtype=='pwd')
124 $fldvalue = $fldvalue ? SHA1($fldvalue) : $fldvalueold;
125 if(fldvalue){
126 sqlStatement("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
127 "VALUES ( '$fldid', '0', '$fldvalue' )");
131 ++$i;
134 checkCreateCDB();
135 echo "<script type='text/javascript'>";
136 echo "parent.left_nav.location.reload();";
137 echo "parent.Title.location.reload();";
138 echo "if(self.name=='RTop'){";
139 echo "parent.RBot.location.reload();";
140 echo "}else{";
141 echo "parent.RTop.location.reload();";
142 echo "}";
143 echo "self.location.href='edit_globals.php?unique=yes';";
144 echo "</script>";
148 <!-- supporting javascript code -->
149 <script type="text/javascript" src="../../library/dialog.js"></script>
150 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
151 <script type="text/javascript" src="../../library/js/common.js"></script>
152 <script type="text/javascript" src="../../library/js/fancybox/jquery.fancybox-1.2.6.js"></script>
153 <link rel="stylesheet" type="text/css" href="../../library/js/fancybox/jquery.fancybox-1.2.6.css" media="screen" />
155 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
156 <?php if ($_GET['mode'] == "user") { ?>
157 <title><?php xl('User Settings','e'); ?></title>
158 <?php } else { ?>
159 <title><?php xl('Global Settings','e'); ?></title>
160 <?php } ?>
162 <style>
163 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
164 tr.detail { font-size:10pt; }
165 td { font-size:10pt; }
166 input { font-size:10pt; }
167 </style>
169 </head>
171 <body class="body_top">
173 <?php if ($_GET['mode'] == "user") { ?>
174 <form method='post' name='theform' id='theform' action='edit_globals.php?mode=user' onsubmit='return top.restoreSession()'>
175 <?php } else { ?>
176 <form method='post' name='theform' id='theform' action='edit_globals.php' onsubmit='return top.restoreSession()'>
177 <?php } ?>
179 <?php if ($_GET['mode'] == "user") { ?>
180 <p><b><?php xl('Edit User Settings','e'); ?></b>
181 <?php } else { ?>
182 <p><b><?php xl('Edit Global Settings','e'); ?></b>
183 <?php } ?>
185 <ul class="tabNav">
186 <?php
187 $i = 0;
188 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
189 if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($grpname, $USER_SPECIFIC_TABS)) ) {
190 echo " <li" . ($i ? "" : " class='current'") .
191 "><a href='/play/javascript-tabbed-navigation/'>" .
192 xl($grpname) . "</a></li>\n";
193 ++$i;
197 </ul>
199 <div class="tabContainer">
200 <?php
201 $i = 0;
202 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
203 if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($grpname, $USER_SPECIFIC_TABS)) ) {
204 echo " <div class='tab" . ($i ? "" : " current") .
205 "' style='height:auto;width:97%;'>\n";
207 echo " <table>";
209 if ($_GET['mode'] == "user") {
210 echo "<tr>";
211 echo "<th>&nbsp</th>";
212 echo "<th>" . htmlspecialchars( xl('User Specific Setting'), ENT_NOQUOTES) . "</th>";
213 echo "<th>" . htmlspecialchars( xl('Default Setting'), ENT_NOQUOTES) . "</th>";
214 echo "<th>&nbsp</th>";
215 echo "<th>" . htmlspecialchars( xl('Set to Default'), ENT_NOQUOTES) . "</th>";
216 echo "</tr>";
219 foreach ($grparr as $fldid => $fldarr) {
220 if ( $_GET['mode'] != "user" || ($_GET['mode'] == "user" && in_array($fldid, $USER_SPECIFIC_GLOBALS)) ) {
221 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
223 // Most parameters will have a single value, but some will be arrays.
224 // Here we cater to both possibilities.
225 $glres = sqlStatement("SELECT gl_index, gl_value FROM globals WHERE " .
226 "gl_name = '$fldid' ORDER BY gl_index");
227 $glarr = array();
228 while ($glrow = sqlFetchArray($glres)) $glarr[] = $glrow;
230 // $fldvalue is meaningful only for the single-value cases.
231 $fldvalue = count($glarr) ? $glarr[0]['gl_value'] : $flddef;
233 // Collect user specific setting if mode set to user
234 $userSetting = "";
235 $settingDefault = "checked='checked'";
236 if ($_GET['mode'] == "user") {
237 $userSettingArray = sqlQuery("SELECT * FROM user_settings WHERE setting_user=? AND setting_label=?",array($_SESSION['authId'],"global:".$fldid));
238 $userSetting = $userSettingArray['setting_value'];
239 $globalValue = $fldvalue;
240 if (!empty($userSettingArray)) {
241 $fldvalue = $userSetting;
242 $settingDefault = "";
246 echo " <tr title='$flddesc'><td valign='top'><b>$fldname </b></td><td valign='top'>\n";
248 if (is_array($fldtype)) {
249 echo " <select name='form_$i' id='form_$i'>\n";
250 foreach ($fldtype as $key => $value) {
251 if ($_GET['mode'] == "user") {
252 if ($globalValue == $key) $globalTitle = $value;
254 echo " <option value='$key'";
255 if ($key == $fldvalue) echo " selected";
256 echo ">";
257 echo $value;
258 echo "</option>\n";
260 echo " </select>\n";
263 else if ($fldtype == 'bool') {
264 if ($_GET['mode'] == "user") {
265 if ($globalValue == 1) {
266 $globalTitle = htmlspecialchars( xl('Checked'), ENT_NOQUOTES);
268 else {
269 $globalTitle = htmlspecialchars( xl('Not Checked'), ENT_NOQUOTES);
272 echo " <input type='checkbox' name='form_$i' id='form_$i' value='1'";
273 if ($fldvalue) echo " checked";
274 echo " />\n";
277 else if ($fldtype == 'num') {
278 if ($_GET['mode'] == "user") {
279 $globalTitle = $globalValue;
281 echo " <input type='text' name='form_$i' id='form_$i' " .
282 "size='6' maxlength='15' value='$fldvalue' />\n";
285 else if ($fldtype == 'text') {
286 if ($_GET['mode'] == "user") {
287 $globalTitle = $globalValue;
289 echo " <input type='text' name='form_$i' id='form_$i' " .
290 "size='50' maxlength='255' value='$fldvalue' />\n";
292 else if ($fldtype == 'pwd') {
293 if ($_GET['mode'] == "user") {
294 $globalTitle = $globalValue;
296 echo " <input type='password' name='form_$i' " .
297 "size='50' maxlength='255' value='' />\n";
300 else if ($fldtype == 'pass') {
301 if ($_GET['mode'] == "user") {
302 $globalTitle = $globalValue;
304 echo " <input type='password' name='form_$i' " .
305 "size='50' maxlength='255' value='$fldvalue' />\n";
308 else if ($fldtype == 'lang') {
309 $res = sqlStatement("SELECT * FROM lang_languages ORDER BY lang_description");
310 echo " <select name='form_$i' id='form_$i'>\n";
311 while ($row = sqlFetchArray($res)) {
312 echo " <option value='" . $row['lang_description'] . "'";
313 if ($row['lang_description'] == $fldvalue) echo " selected";
314 echo ">";
315 echo xl($row['lang_description']);
316 echo "</option>\n";
318 echo " </select>\n";
321 else if ($fldtype == 'm_lang') {
322 $res = sqlStatement("SELECT * FROM lang_languages ORDER BY lang_description");
323 echo " <select multiple name='form_{$i}[]' id='form_{$i}[]' size='3'>\n";
324 while ($row = sqlFetchArray($res)) {
325 echo " <option value='" . $row['lang_description'] . "'";
326 foreach ($glarr as $glrow) {
327 if ($glrow['gl_value'] == $row['lang_description']) {
328 echo " selected";
329 break;
332 echo ">";
333 echo xl($row['lang_description']);
334 echo "</option>\n";
336 echo " </select>\n";
339 else if ($fldtype == 'css') {
340 if ($_GET['mode'] == "user") {
341 $globalTitle = $globalValue;
343 $themedir = "$webserver_root/interface/themes";
344 $dh = opendir($themedir);
345 if ($dh) {
346 echo " <select name='form_$i' id='form_$i'>\n";
347 while (false !== ($tfname = readdir($dh))) {
348 // Only show files that contain style_ as options
349 // Skip style_blue.css since this is used for
350 // lone scripts such as setup.php
351 // Also skip style_pdf.css which is for PDFs and not screen output
352 if (!preg_match("/^style_.*\.css$/", $tfname) ||
353 $tfname == 'style_blue.css' || $tfname == 'style_pdf.css')
354 continue;
355 echo "<option value='$tfname'";
356 if ($tfname == $fldvalue) echo " selected";
357 echo ">";
358 echo $tfname;
359 echo "</option>\n";
361 closedir($dh);
362 echo " </select>\n";
366 else if ($fldtype == 'hour') {
367 if ($_GET['mode'] == "user") {
368 $globalTitle = $globalValue;
370 echo " <select name='form_$i' id='form_$i'>\n";
371 for ($h = 0; $h < 24; ++$h) {
372 echo "<option value='$h'";
373 if ($h == $fldvalue) echo " selected";
374 echo ">";
375 if ($h == 0) echo "12 AM";
376 else if ($h < 12) echo "$h AM";
377 else if ($h == 12) echo "12 PM";
378 else echo ($h - 12) . " PM";
379 echo "</option>\n";
381 echo " </select>\n";
383 if ($_GET['mode'] == "user") {
384 echo " </td>\n";
385 echo "<td align='center' style='color:red;'>" . $globalTitle . "</td>\n";
386 echo "<td>&nbsp</td>";
387 echo "<td align='center'><input type='checkbox' value='YES' name='toggle_" . $i . "' id='toggle_" . $i . "' " . $settingDefault . "/></td>\n";
388 echo "<input type='hidden' id='globaldefault_" . $i . "' value='" . $globalValue . "'>\n";
389 echo "</tr>\n";
391 else {
392 echo " </td></tr>\n";
394 ++$i;
397 echo " </table>\n";
398 echo " </div>\n";
402 </div>
405 <input type='submit' name='form_save' value='<?php xl('Save','e'); ?>' />
406 </p>
407 </center>
409 </form>
411 </body>
413 <script language="JavaScript">
415 $(document).ready(function(){
416 tabbify();
417 enable_modals();
419 // Use the counter ($i) to make the form user friendly for user-specific globals use
420 <?php if ($_GET['mode'] == "user") { ?>
421 <?php for ($j = 0; $j <= $i; $j++) { ?>
422 $("#form_<?php echo $j ?>").change(function() {
423 $("#toggle_<?php echo $j ?>").attr('checked',false);
425 $("#toggle_<?php echo $j ?>").change(function() {
426 if ($('#toggle_<?php echo $j ?>').attr('checked')) {
427 var defaultGlobal = $("#globaldefault_<?php echo $j ?>").val();
428 $("#form_<?php echo $j ?>").val(defaultGlobal);
431 <?php } ?>
432 <?php } ?>
436 </script>
438 </html>