<?php

	//--------------------------------------------------------------------------------------------------------
	$code_erreur 					= 0;
	$msg_erreur					= "<font color='Salmon'>" . "- aucune ... (#0)" . "</font>" ;
	$MySQL_connected 			= false;
	$p_USERS_TO_MODIFY 			= [];
	$p_USERS_TO_MODIFY_LISTE 	= "" ;
	
	//--------------------------------------------------------------------------------------------------------
	$p_USERS_TO_MODIFY	= isset($_POST["select_users_name"]) 	? $_POST["select_users_name"] : [];

	$p_aqcm_checkbox		= [];
	$p_aqcm_checkbox[0]	= isset($_POST["aqcm_STATUT_checkbox"])		? true : false;
	$p_aqcm_checkbox[1]	= isset($_POST["aqcm_USER_LEVEL_checkbox"])	? true : false;
	$p_aqcm_checkbox[2]	= isset($_POST["aqcm_T_LIMIT_checkbox"])		? true : false;
	$p_aqcm_checkbox[3]	= isset($_POST["aqcm_WEEK_MAIL_checkbox"])	? true : false;
	$p_aqcm_checkbox[4]	= isset($_POST["aqcm_RX_WARNING_checkbox"])	? true : false;

	$p_aqcm_STATUT 		= isset($_POST["aqcm_STATUT"])			? $_POST["aqcm_STATUT"] 		: "OFF";
	$p_aqcm_USER_LEVEL 	= isset($_POST["aqcm_USER_LEVEL"])		? $_POST["aqcm_USER_LEVEL"] 	: "1";
	
	$p_aqcm_T_MAX			= isset($_POST["aqcm_USER_T_MAX"])		? $_POST["aqcm_USER_T_MAX"] 			: "";
	$p_T_MAX_value		= strtotime($p_aqcm_T_MAX);

	$p_aqcm_WEEK_MAIL	= isset($_POST["aqcm_WEEK_MAIL"])			? $_POST["aqcm_WEEK_MAIL"] 		: "OFF";
	$p_aqcm_RX_WARNING	= isset($_POST["aqcm_RX_WARNING"])		? $_POST["aqcm_RX_WARNING"] 	: "OFF";
	
	/*
	print_r($p_USERS_TO_MODIFY); echo("</br>");
	echo($p_aqcm_STATUT); echo("</br>");
	echo($p_aqcm_USER_LEVEL); echo("</br>");
	echo($p_aqcm_JOUR); echo("</br>");
	echo($p_aqcm_MOIS); echo("</br>");
	echo($p_aqcm_ANNEE); echo("</br>");
	echo($p_aqcm_WEEK_MAIL); echo("</br>");
	echo($p_aqcm_RX_WARNING); echo("</br>");
	*/


	//--------------------------------------------------------------------------------------------------------
	if ( sizeof($p_USERS_TO_MODIFY) == 0 ) {
		
		$code_erreur	= 1 ;
		$msg_erreur	= "<font color='Salmon'>" . "choisir des utilisateurs à modifier !!! (#1)" . "</font>" ;
		
	}
	else {
		
		if ( sizeof($p_USERS_TO_MODIFY) == 1 ) {
			
			$p_USERS_TO_MODIFY_LISTE = "'" . $p_USERS_TO_MODIFY[0] . "'" ;
			
		}
		else {
			
			for ( $i = 0; $i < sizeof($p_USERS_TO_MODIFY) - 1 ; $i++ ) {
				
				$p_USERS_TO_MODIFY_LISTE = $p_USERS_TO_MODIFY_LISTE . "'" . $p_USERS_TO_MODIFY[$i] . "'," ;

			}
			$p_USERS_TO_MODIFY_LISTE = $p_USERS_TO_MODIFY_LISTE . "'" . $p_USERS_TO_MODIFY[$i] . "'" ;
			
		}
		
	}
	
	//print_r($p_USERS_TO_MODIFY); echo("</br>");
	//echo($p_USERS_TO_MODIFY_LISTE); echo("</br>");

	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données
	//--------------------------------------------------------------------------------------------------------
	if ( $code_erreur == 0 ) {
		
		include("../_identifiants/sql_identification_sans_base.php");
		$mysql_base = "AQCM05_SITE_" . $_SESSION['SITE_ID'] ;

		$link_AQCM05 = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
		
		if ( ! $link_AQCM05 ) {
			
			$code_erreur = 2 ;
			$msg_erreur	= "<font color='Salmon'>" . "- connexion à la base de donnée MySQL : échec !!! (#2)" . "</font>" ;
			
		}
		else {
			
			$MySQL_connected = true;
			
		}

	}

	//--------------------------------------------------------------------------------------------------------
	// modification des utilisateurs
	//--------------------------------------------------------------------------------------------------------
	if ( $code_erreur == 0 ) {
		
		$chaine_query		= [];
		$chaine_query_2	= [];
		
		$chaine_query[0] = "USER_STATUS='" 			. $p_aqcm_STATUT 		. "'," ;
		$chaine_query[1] = "USER_LEVEL='" 			. $p_aqcm_USER_LEVEL 	. "'," ;
		$chaine_query[2] = "USER_T_MAX='" 			. $p_T_MAX_value			. "'," ;
		$chaine_query[3] = "USER_WEEK_MAIL='" 		. $p_aqcm_WEEK_MAIL 	. "'," ;
		$chaine_query[4] = "USER_RX_WARNING_EN='"	. $p_aqcm_RX_WARNING 	. "'," ;
		
		$chaine_query_2[0]	= "statut = "			. $p_aqcm_STATUT  ;
		$chaine_query_2[1]	= "niveau = " 		. $p_aqcm_USER_LEVEL ;
		$chaine_query_2[2]	= "limite = " 		. date("d/m/Y",$p_T_MAX_value) ;
		$chaine_query_2[3]	= "bilan hebdo = " 	. $p_aqcm_WEEK_MAIL ;
		$chaine_query_2[4]	= "alerte = "			. $p_aqcm_RX_WARNING  ;


		$chaine_query_SET = "";
		for ( $k = 0; $k < 5; $k++ ) {
			if ( $p_aqcm_checkbox[$k] ) {
				$chaine_query_SET = $chaine_query_SET . $chaine_query[$k] ;
			}
		}
		
		if ( $chaine_query_SET == "" ) {
			
			$code_erreur = 3;
			$msg_erreur	= "<font color='Salmon'>" . "- requête MySQL : échec !!! (#3)" . "</font>" ;
			
		}
		else {
			
			$chaine_query_SET = substr($chaine_query_SET, 0, -1);

			$query = "
				UPDATE AQCM05_SITE_USERS 
				SET " . $chaine_query_SET . " 
				WHERE USER_ID IN (" . $p_USERS_TO_MODIFY_LISTE . ")" ;

			//echo( $query ); echo("<br>");
			
			$result = mysqli_query($link_AQCM05, $query); 
			//$result = true; 
			
			if ( ! $result ) {
				
				$code_erreur = 4;
				$msg_erreur	= "<font color='Salmon'>" . "- requête MySQL : échec !!! (#4)" . "</font>" ;
				
			} 
			
		}
		
	}
		
	//--------------------------------------------------------------------------------------------------------
	// 
	//--------------------------------------------------------------------------------------------------------
	if ( $MySQL_connected ) {
		
		mysqli_close($link_AQCM05);
		
	}

	//--------------------------------------------------------------------------------------------------------
	if ( $code_erreur == 0 ) {
		
		echo("<font color='lime'>&nbsp;&nbsp;&nbsp;&nbsp;→&nbsp;&nbsp;succès...</font></br>");

		echo("</br>&nbsp;&nbsp;&nbsp;&nbsp;modifications :");
		for ( $k = 0; $k < 5; $k++ ) {
			if ( $p_aqcm_checkbox[$k] ) {
				echo( "</br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;" . $chaine_query_2[$k] ) ;
			}
		}
		
		echo("</br></br>");

		echo("&nbsp;&nbsp;&nbsp;&nbsp;utilisateurs modifiés :</br>");
		for ( $j = 0; $j < 	sizeof($p_USERS_TO_MODIFY); $j++ ) {
			echo("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;" . $p_USERS_TO_MODIFY[$j] . "</br>" ) ;
		}
				
		log_string( "AQCM05", "NOTICE ---> " . "modification d'utilisateurs dans [AQCM05_SITE_" . $_SESSION['SITE_ID'] . "] par : " . $_SESSION['login'] . " : succès..." ) ;
		
	} else {
		
		echo("<font color='salmon'>&nbsp;→&nbsp;&nbsp;modification des utilisateurs sélectionnés : échec !!! (#".$code_erreur.")</font>");
		echo("</br></br>");
		echo( "&nbsp;&nbsp;&nbsp;&nbsp;#" . $code_erreur . " : " . $msg_erreur ) ;
		
		
		log_string( "AQCM05", "NOTICE ---> " . "modification d'utilisateurs dans [AQCM05_SITE_" . $_SESSION['SITE_ID'] . "] par : : " . $_SESSION['login'] . " : échec !!!" ) ;
		
	}

	//--------------------------------------------------------------------------------------------------------
/*
*/

?>
