<?php
if($_POST['scheme_action']){
	switch($_POST['scheme_action']){
		case 'save_new':
			$title = xtc_db_input($_POST['scheme_title']);
			$insert_sql_array = array(
				'admin_access_scheme_title'=>$title,
				'date_created'=>'now()'
			);
			xtc_db_perform('admin_access_scheme',$insert_sql_array);
			$aasID = xtc_db_insert_id();
			for($i=0;$i<sizeOf($_POST['access']);$i++){
				xtc_db_query("INSERT INTO admin_access_scheme_values SET admin_access_scheme_id='".$aasID."',admin_access_value='".xtc_db_input($_POST['access'][$i])."'");
			}
			$messageStack->add_session('Die Berechtigungsgruppe '.xtc_db_prepare_input($title).' wurde gespeichert.','success');
			xtc_redirect(xtc_href_link('accounting.php',xtc_get_all_get_params()));
			break;
		case 'save':
			$title = xtc_db_input($_POST['scheme_title']);
			$update_sql_array = array(
				'admin_access_scheme_title'=>$title
			);
			xtc_db_perform('admin_access_scheme',$update_sql_array,'update',"admin_access_scheme_id='".xtc_db_input($_POST['scheme_id'])."'");
			$aasID = xtc_db_input($_POST['scheme_id']);
			xtc_db_query("DELETE FROM admin_access_scheme_values WHERE admin_access_scheme_id='".$aasID."'");
			for($i=0;$i<sizeOf($_POST['access']);$i++){
				xtc_db_query("INSERT INTO admin_access_scheme_values SET admin_access_scheme_id='".$aasID."',admin_access_value='".xtc_db_input($_POST['access'][$i])."'");
			}
			$messageStack->add_session('Die Berechtigungsgruppe '.xtc_db_prepare_input($title).' wurde aktualisiert.','success');
			xtc_redirect(xtc_href_link('accounting.php',xtc_get_all_get_params()));
			break;
		case 'delete':
			$title = $_POST['scheme_title'];
			$aasID = xtc_db_input($_POST['scheme_id']);
			xtc_db_query("DELETE FROM admin_access_scheme WHERE admin_access_scheme_id='".$aasID."'");
			xtc_db_query("DELETE FROM admin_access_scheme_values WHERE admin_access_scheme_id='".$aasID."'");
			$messageStack->add_session('Berechtigungsgruppe '.xtc_db_prepare_input($title).' wurde gel&ouml;scht.','success');
			xtc_redirect(xtc_href_link('accounting.php',xtc_get_all_get_params()));
			break;
	}
}
$scheme_array = array(0=>array('id'=>'new','text'=>'Neues Schema anlegen'));
$scheme_query = xtc_db_query("SELECT admin_access_scheme_id,admin_access_scheme_title FROM admin_access_scheme ORDER BY admin_access_scheme_title");
while($scheme = xtc_db_fetch_array($scheme_query)){
	$scheme_array[] = array('id'=>$scheme['admin_access_scheme_id'],'text'=>$scheme['admin_access_scheme_title']);
}
if(!isset($_POST['scheme_id'])) 
	$selected_scheme = $scheme_array[1]['id'];
else
	$selected_scheme = $_POST['scheme_id'];

$group_query=xtc_db_query("select * from " . TABLE_ADMIN_ACCESS . " where customers_id = 'groups'");
$group_access = xtc_db_fetch_array($group_query);

?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $_SESSION['language_charset']; ?>"> 
<title>Berechtigungsgruppen verwalten</title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript">
<!--
function reallydelete(){
	yes=confirm('Diese Berechtigungsgruppe wirklich loeschen?');
	if(yes){
		document.forms['scheme_edit'].scheme_action.value='delete';
		document.forms['scheme_edit'].submit();
	}
}
//-->
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr>
	<td class="boxCenter" width="100%" valign="top">
		<table border="0" width="100%" cellspacing="0" cellpadding="0">
		<tr>
			<td>
				<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td class="pageHeading">Berechtigungsgruppen verwalten</td>
				</tr>
				</table>
			</td>
		</tr>
      		<tr>
			<td colspan="2" class="main">&nbsp;</td>
      		</tr>
      		<tr>
			<td colspan="2" class="main">
			<?php echo xtc_draw_form('scheme_select','accounting.php',xtc_get_all_get_params() ); ?>
			<?php echo xtc_draw_hidden_field('scheme_action','select'); ?>
			<?php echo xtc_draw_pull_down_menu('scheme_id',$scheme_array,$selected_scheme);?>
			<input type="submit" value="Auswahl" class="iButton">
			</form>

			</td>
		</tr>
		<?php if($selected_scheme != ''){?>
      		<tr>
			<td colspan="2" class="main"><br /><?php echo xtc_draw_separator('pixel_black.gif'); ?><br /><br />

			<?php echo xtc_draw_form('scheme_edit','accounting.php',xtc_get_all_get_params() ); ?>
			<?php if($selected_scheme != 'new'){
				$scheme_title_query = xtc_db_query("SELECT admin_access_scheme_title FROM admin_access_scheme WHERE admin_access_scheme_id='".$selected_scheme."'");
				$scheme_title = xtc_db_fetch_array($scheme_title_query);
				$scheme_query = xtc_db_query("SELECT admin_access_value FROM admin_access_scheme_values WHERE admin_access_scheme_id='".$selected_scheme."'");
				while($scheme_value = xtc_db_fetch_array($scheme_query)){
					$scheme[$scheme_value['admin_access_value']] = true;
				}
			?>
			<?php echo xtc_draw_hidden_field('scheme_action','save'); ?>
			<?php echo xtc_draw_hidden_field('scheme_id',$selected_scheme); ?>
			<?php $scheme_title = $scheme_title['admin_access_scheme_title']; ?>
			<?php }else{ ?>
			<?php echo xtc_draw_hidden_field('scheme_action','save_new'); ?>
			<?php $scheme_title = 'Neue Berechtigungsgruppe'; ?>
			<?php } ?>
				<b>Name der Berechtigungsgruppe:</b>&nbsp;<?php echo xtc_draw_input_field('scheme_title',$scheme_title,'size="30" maxlength="64"'); ?>
			<?php if($selected_scheme != 'new'){ ?>
				&nbsp;<a href="javascript:reallydelete('<?php echo $selected_scheme; ?>');" title="Berechtigungsgruppe l&ouml;schen" style="color:#FF0000;"><u>Berechtigungsgruppe l&ouml;schen</u></font></a>
			<?php } ?>

				<table border="0" cellpadding="0" cellspacing="0">
			<?php
			$fields = mysql_list_fields(DB_DATABASE, TABLE_ADMIN_ACCESS);
			$columns = mysql_num_fields($fields);

			for ($i = 0; $i < $columns; $i++) {
			    $field=mysql_field_name($fields, $i);
			    if ($field!='customers_id') {
				$checked='';
				if ($scheme[$field]) 
					$checked='checked';

				// colors
				switch ($group_access[$field]) {
				            case '1':
				            $color='#FF6969';
				            break;
				            case '2':
				            $color='#69CDFF';
				            break;
				            case '3':
				            $color='#6BFF7F';
				            break;
				            case '4':
				            $color='#BFA8FF';
				            break;
				            case '5':
				            $color='#FFE6A8';

			    	}
				if($c_admin_access['f_accounting_system_groups'] != 1){
					switch( $group_access[$field] ){
						case 0:
							$show = false;
							break;
						default:
							$show = true;
							break;
					}
				}else{
					$show = true;
				}
				if($show){
					echo '<tr class="dataTable">
			    <td style="border: 1px solid; border-color: #000000;" width="10" bgcolor="'.$color.'" >'.xtc_draw_separator('pixel_trans.gif',15, 15).'</td>
			        <td class="dataTableContentRow">
			        <input type="checkbox" name="access[]" value="'.$field.'"'.$checked.'>
			        '.$field.'</td>
			        <td></td></tr>';
				}
			  }
			}
			?>

				</table>
				<input type="submit" value="Speichern" class="iButton">
				</form>
			</td>
		</tr>
		<?php } ?>
		</table>
	</td>
</tr>
</table>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
