<?php 
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
if(isset($_SESSION['user_droits'])) {
	if($_REQUEST['param']=='colorstatsdef') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '#FF9900#99FF00#CCFFFF#FF66FF#CC6600#FFFF33#99CC66#003399#9900FF#FF0033#CCCC00#66CC66#CCFFFF#333366#CC99CC#330033#FF6666#993300#33CC33#3399FF' WHERE param_nom = 'colors_stats_array'");
	}
	else if($_REQUEST['param']=='colorstatsmod') {
		$each_color='';
		for($i=0;$i<20;$i++) {
			$each_color .= $_REQUEST['c'.$i.''];
		}
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$each_color."' WHERE param_nom = 'colors_stats_array'");
	}
	else if($_REQUEST['param']=='infobulles') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['typc']."' WHERE param_nom = 'info_menu'");
	}
	else if($_REQUEST['param']=='language') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['vall']."' WHERE param_nom = 'language'");
		$_SESSION['lang']=	$_REQUEST['vall'];
	}
	else if($_REQUEST['param']=='mailing') {
		if(substr($_REQUEST['mailmethod'],0,3)=='php'||substr($_REQUEST['mailmethod'],0,4)=='smtp') {
			$mailmethod = mysqli_real_escape_string($bdd,trim($_REQUEST['mailmethod']));
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$mailmethod."' WHERE param_nom = 'mail_method'");
		}
	}
	else if($_REQUEST['param']=='niveauxv') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['typc']."' WHERE param_nom = 'freq_statsniv'");
	}
	else if($_REQUEST['param']=='persogroupes') {
		mysqli_query($bdd,"DELETE FROM ".$prefix."_edtnivcolor WHERE id > 9");
		$caracpush='';
		$nompush='';
		$virgule=0;
		for($j=0;$j<20;$j++) {
			if($_REQUEST['carac'.$j]!=""&&$_REQUEST['nom'.$j]!="") {
				if($virgule==1) { $caracpush .= ","; $nompush .= ","; }
				$caracpush .= mysqli_real_escape_string($bdd,$_REQUEST['carac'.$j]);
				if($caracpush=="ZZZ") {
					$caracpush="ZZZ1";
				}
				$nompush .= mysqli_real_escape_string($bdd,$_REQUEST['nom'.$j]);
				$virgule=1;
				$couleur = $_REQUEST['couleur'.$j];
				$colcheck = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtnivcolor WHERE niveau ='".$_REQUEST['carac'.$j]."'"));
				if($colcheck==1) {
					mysqli_query($bdd,"UPDATE ".$prefix."_edtnivcolor SET bgcolor = '".$couleur."' WHERE niveau ='".$_REQUEST['carac'.$j]."'");
				}
				else {
					mysqli_query($bdd,"INSERT INTO ".$prefix."_edtnivcolor (niveau, bgcolor) VALUES ('".$_REQUEST['carac'.$j]."', '".$couleur."')");
				}
			}
		}
		if($caracpush!=''&&$nompush!='') {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$caracpush."' WHERE param_nom = 'freq_statsniv_persoc'");
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$nompush."' WHERE param_nom = 'freq_statsniv_person'");
		}
	}
	else if($_REQUEST['param']=='espinit') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur='".$_REQUEST['nom_init']."' WHERE param_nom = 'espaces_first'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['resap']."' WHERE param_nom = 'espaces_first_resamulti'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['resad']."' WHERE param_nom = 'espaces_first_resadistmulti'");
	}
	else if($_REQUEST['param']=='defdefaut') {
		mysqli_query($bdd,"UPDATE ".$prefix."_espaces SET default_espace = 1 WHERE espace_id = '".$_REQUEST['val']."'");
		mysqli_query($bdd,"UPDATE ".$prefix."_espaces SET default_espace = 0 WHERE espace_id != '".$_REQUEST['val']."'");
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_REQUEST['verr']."' WHERE param_nom = 'freq_verrespace'");
	}
	else if($_REQUEST['param']=='effectifs') {
		mysqli_query($bdd,"UPDATE ".$prefix."_etab_effectifs SET effectif = '".$_REQUEST['nb']."' WHERE anneesco = '".$_REQUEST['an']."' AND division = '".$_REQUEST['div']."'");
	}
	else if($_REQUEST['param']=='aligndivs') {
		if($_REQUEST['newdiv']!='') {
			mysqli_query($bdd,"UPDATE ".$prefix."_edtbrik SET division = '".$_REQUEST['newdiv']."' WHERE division = '".$_REQUEST['div']."'");
			mysqli_query($bdd,"UPDATE ".$prefix."_etab_effectifs SET division = '".$_REQUEST['newdiv']."' WHERE division = '".$_REQUEST['div']."'");
			mysqli_query($bdd,"UPDATE ".$prefix."_freq SET division = '".$_REQUEST['newdiv']."' WHERE division = '".$_REQUEST['div']."'");
			mysqli_query($bdd,"UPDATE ".$prefix."_freqgrappes SET division = '".$_REQUEST['newdiv']."' WHERE division = '".$_REQUEST['div']."'");
			mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET division = '".$_REQUEST['newdiv']."' WHERE division = '".$_REQUEST['div']."'");
		}
	}
	else if($_REQUEST['param']=='suppra') {
		mysqli_query($bdd,"DELETE FROM ".$prefix."_etab_effectifs WHERE anneesco = '".$_REQUEST['an']."'");
	}
	else if($_REQUEST['param']=='degres') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur='".$_REQUEST['typc']."' WHERE param_nom = 'eval_degres'");
	}
	else if($_REQUEST['param']=='choixgrilles') {
		mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur='A".$_REQUEST['apd']."E".$_REQUEST['emi']."P".$_REQUEST['per']."' WHERE param_nom='savoirs'");
	}
	else if($_REQUEST['param']=='colorapddef') {
		mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET bgcolor='#CEF6CE' WHERE champs_id = 'CE01'");
		mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET bgcolor='#F5F6CE' WHERE champs_id = 'CP02'");
		mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET bgcolor='#CECEF6' WHERE champs_id = 'CR03'");
		mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET bgcolor='#F6CED8' WHERE champs_id = 'CT04'");
	}
	else if($_REQUEST['param']=='colorapdval') {
		$apd = mysqli_query($bdd,"SELECT * FROM ".$prefix."_savchamps WHERE champs_id = 'CE01' OR champs_id = 'CP02' OR champs_id = 'CR03' OR champs_id = 'CT04' ORDER BY champs_id");
		foreach($apd as $champsapd) {
			mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET bgcolor='".$_REQUEST['apdcouleur'.$champsapd['champs_id']]."' WHERE champs_id = '".$champsapd['champs_id']."'");
		}
	}
	else if($_REQUEST['param']=='coloremidef') {
		mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET bgcolor='#CEF6CE' WHERE champs_id = 'AU01'");
		mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET bgcolor='#F5F6CE' WHERE champs_id = 'EX02'");
		mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET bgcolor='#CECEF6' WHERE champs_id = 'ME03'");
		mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET bgcolor='#F6CED8' WHERE champs_id = 'PR04'");
	}
	else if($_REQUEST['param']=='coloremival') {
		$emi = mysqli_query($bdd,"SELECT * FROM ".$prefix."_savchamps WHERE champs_id = 'AU01' OR champs_id = 'EX02' OR champs_id = 'ME03' OR champs_id = 'PR04' ORDER BY champs_id");
		foreach($emi as $champsemi) {
			mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET bgcolor='".$_REQUEST['emicouleur'.$champsemi['champs_id']]."' WHERE champs_id = '".$champsemi['champs_id']."'");
		}
	}
	else if($_REQUEST['param']=='colorperval') {
		$tabDebniveau = array('6', '5', '4', '3', '2', '1', 't');
		$perchamps = array();
		foreach($tabDebniveau as $divdeb) {
			$testchs = mysqli_query($bdd,"SELECT champs_id FROM ".$prefix."_savoirs".$divdeb." WHERE s".$divdeb."_id LIKE 'PER%' GROUP BY champs_id");
			foreach($testchs as $testch) {
				if(!in_array($testch['champs_id'], $perchamps)) {
					array_push($perchamps, $testch['champs_id']);
				}
			}
		}
		foreach($perchamps as $perchamp) {
			$champsper = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_savchamps WHERE champs_id = '".$perchamp."'"));
			$newname = mysqli_real_escape_string($bdd,$_REQUEST['pername'.$champsper['champs_id']]);
			mysqli_query($bdd,"UPDATE ".$prefix."_savchamps SET champs_nom='".$newname."', bgcolor = '".$_REQUEST['percouleur'.$champsper['champs_id']]."' WHERE champs_id = '".$champsper['champs_id']."'");
		}
	}
	else if($_REQUEST['param']=='supprmes') {
		mysqli_query($bdd,"UPDATE ".$prefix."_calendrier SET info = '' WHERE date = '".$_REQUEST['date']."'");
	}	
}
mysqli_close($bdd);
?>