<?php

// $Id: sa_settings.inc.php,v 1.11 2006/11/02 22:06:15 perlstalker Exp $

if (!defined('SM_PATH')) { define('SM_PATH' , '../../'); }

include_once(SM_PATH.'include/validate.php');
include_once(SM_PATH.'functions/page_header.php');
include_once(SM_PATH.'include/load_prefs.php');
include_once(SM_PATH.'plugins/sasql/sasql_conf.php');
include_once(SM_PATH.'plugins/sasql/sasql_db.php');

// From sasql_conf.php
global $SqlDSN;
global $SqlTable;
global $useSA2_43;
global $globalUsername;
global $allowed_params;
global $allowed_wl_params;
global $sa_params;
global $wl_params;
global $default_domain;

// from SM
global $username;
global $color;

// init
// Post vars

// Read global preferences
$globalPrefs = sasql_ReadPrefs ($DBHandle, $SqlTable, $globalUsername );

foreach ($allowed_params as $param) {
  $escaped_param = ereg_replace("[ \.]", "_", $param);
  sqgetGlobalVar($escaped_param, ${$param}, SQ_FORM);
  if (! isset(${$param})) {
    // Checkboxes are special because unset values are valid. We need to
    // make this exception for them.
    if ($sa_params[$param]['type'] == 'checkbox') {
      ${$param} = 0;
    } else {
      if (isset ($global_prefs[$param]['default'])) {
	${$param} = $global_prefs[$param]['default'];
      } elseif (isset ($sa_params[$param]['default'])) {
	${$param} = $sa_params[$param]['default'];
      }
    }
  }
}

sqgetGlobalVar('update', $update, SQ_FORM);

// Read user prefs
$prefs = sasql_ReadPrefs ($DBHandle, $SqlTable, $username);

/*
foreach ($allowed_params as $param) {
  if (isset($prefs[$param])) {
    ${$param} = $prefs[$param]['value'];
  } elseif (isset($globalPrefs[$param])) {
    ${$param} = $globalPrefs[$param]['value'];
  } 
}
*/

if ($update) {
  foreach ($allowed_params as $param) {
    // Note: $param is the parameter name and ${$param} is the parameter value
    if (isset ($prefs[$param])) {
      if (isset($sa_params[$param]['multiple'])
          and $sa_params[$param]['multiple']) {
	sasql_UpdatePref_multiple ($DBHandle, $SqlTable, $username,
				   $param, ${$param});
      } elseif (isset($sa_params[$param]['delete'])
                and $sa_params[$param]['delete']
                and ${$param} == '') {
	sasql_DelPref_name ($DBHandle, $SqlTable, $username, $param);
      } else {
	sasql_UpdatePref ($DBHandle, $SqlTable, $username,
			  $prefs[$param]['prefid'], $param, ${$param});
      }
    } else {
      if (isset($sa_params[$param]['multiple'])
          and $sa_params[$param]['multiple']) {
	sasql_AddPref_multiple ($DBHandle, $SqlTable, $username,
				$param, ${$param});
      } else {
	sasql_AddPref ($DBHandle, $SqlTable, $username, $param, ${$param});
      }
    }
  }
  // Get the prefs again to see the "possible" changes.
  $prefs = sasql_ReadPrefs ($DBHandle, $SqlTable, $username);
}

$prefs = sasql_ReadPrefs ($DBHandle, $SqlTable, $username);

foreach ($allowed_params as $param) {
  if (isset($prefs[$param])) { ${$param} = $prefs[$param]['value'];
  } elseif (isset($globalPrefs[$param])) {
    ${$param} = $globalPrefs[$param]['value'];
  } elseif (isset($sa_params[$param]['default'])) {
    ${$param} = $sa_params[$param]['default'];
  }
}

?>
<form action="sasql_options.php" method="post">
 <input type="hidden" name="update" value="1">
 <table width="100%" border="0" cellpadding="1" cellspacing="1">
  <tr>
   <td align="center" bgcolor="<?php echo $color[4]; ?>">
    <b><?php echo _("General Settings"); ?></b>
   </td>
  </tr>
  <tr>
   <td align="center" bgcolor="<?php echo $color[4] ?>">
    <table border='0'>
<?php
   foreach ($allowed_params as $param) {
     echo '<tr>';
     echo "<td align='right'>";
     if ($sa_params[$param]['type'] == 'select'){
       echo '<select ';
       if (isset ($sa_params[$param]['size'])
	   and $sa_params[$param]['size']) {
	 echo " size='".$sa_params[$param]['size']."' ";
       }
       echo "name='$param";
       if (isset ($sa_params[$param]['multiple'])
           and $sa_params[$param]['multiple']) {
	 echo "[]' multiple>";
       } else {
	 echo "'>";
       }
       foreach ($sa_params[$param]['values'] as $key => $val) {
	 echo "<option value='$key'";
	 if (is_array(${$param})) {
	   if (in_array ($key, ${$param})) { echo " SELECTED"; }
	 } else {
	   if (${$param} == $key) { echo " SELECTED"; }
	 }
	 echo ">$val</option>";
       }
       echo "</select>";
     } elseif ($sa_params[$param]['type'] == 'checkbox') {
       echo "<input type='checkbox' name='$param' ";
       echo "value='".$sa_params[$param]['values']."'";
       if (${$param} == $sa_params[$param]['values']) {
	 echo " CHECKED";
       }
       echo ">";
     } elseif ($sa_params[$param]['type'] == 'text') {
       echo "<input type='text' name='$param' ";
       if (isset($sa_params[$param]['size'])) {
	 echo " size='".$sa_params[$param]['size']."' ";
       }
       echo "value='${$param}' size='10'>";
     }
     echo "</td>\n";
     echo "<td bgcolor='$color[4]'><b>".$sa_params[$param]['title']."</b></td>\n";
     echo "<td bgcolor='$color[4]'>".$sa_params[$param]['descr']."</td>\n";
     //echo "</tr></table>\n";
     echo '</tr>';
   }
?>
    </table>
   </td>
  </tr>
  <tr>
   <td bgcolor="<?php echo $color[4] ?>">
    <input name="submit" type="submit" value="<?php echo _("Save Settings"); ?>">
   </td>
  </tr>
 </table>
</form>