<?php
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][2]!="2") {
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="../index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?><style type="text/css">
		#mdt2 { background-color: #fff; }
		#mdt2 td { border: 3px #E77015 solid; }
		#mdt1 td { border: 0; }
		</style>
		<script type="text/javascript">
			function StatusBrik(brik,act){
			  $.ajax({
			    type: "POST",
			    url: "js/idl/edt_brikup.php",
			    data: { brik: brik, act: act},
			    
			   });
			}
			function ChgtColor(td,act){
				var couleur;
				if(act == "0"){
					couleur = "#FACC2E";
				}
				else if(act == "1"){
					couleur = "#9AFE2E";
				}
				else if(act == "2"){
					couleur = "red";
				}
				document.getElementById(td).style.backgroundColor = couleur;
			}
		</script><?php
		echo '<script language="javascript">
		function toggleDiv(divid){
			if(document.getElementById(divid).style.display == \'none\'){
				document.getElementById(divid).style.display = \'block\';
			}else{
				document.getElementById(divid).style.display = \'none\';
			}
		}
		</script>';

		//Début de l'affichage de la gestion des briques:
		echo '<div class="edt_padding" style="overflow: auto;  white-space: nowrap;"><h1>'.EDTNAV2.'</h1>';
		include('db/proc/etab_config_check.php');
		$hor_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires"));
		$nivs_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv'"));
		// S'il n'y a pas d'horaires configurés :
		if($hor_nb==0) {
			echo '<p><a href="param.php?page=horaires">'.FREQ_HORNEED.'</a>.</p>';
		}
		else if($nivs_info['valeur']=='') {
			echo '<p><a href="param.php?page=etab">'.EDTC_BRIK1.'</a>.</p>';
		}
		else {
			// enregistrement pour une classe
			if(isset($_POST['briknew'])) {
				if($_POST['niveau']==""&&@$_POST['division']=="") {
					echo '<span class="signal">'.EDTC_BRIK2.'</span>';
				}
				else {
					// Récupération des données
					if(@$_POST['division']!="") {
						$div = 1;
					}
					else {
						$niveau = mysqli_real_escape_string($bdd,$_POST['niveau']);
						$div = 0;
					}					
					$code = mysqli_real_escape_string($bdd,$_POST['code']);
					$summary = mysqli_real_escape_string($bdd,trim($_POST['summary']));
					$sequence = mysqli_real_escape_string($bdd,$_POST['sequence']);
					
					// Enregistrement
					// Si tout un niveau
					if($div==0) {
						$liste_div = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE categ = '1' WHERE division LIKE '".$niveau."%' GROUP BY division");
						$liste_nb = mysqli_num_rows($liste_div);
						while($listediv = mysqli_fetch_assoc($liste_div)) {
							$division = mysqli_real_escape_string($bdd,$listediv['division']);
							//vérification :
							$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE code = '".$code."' AND seq_id = '".$sequence."' AND division = '".$division."' AND activ_brik < 2");
							$briks_nb = mysqli_num_rows($briks);
							if($briks_nb==0) {
								//insertion :
								mysqli_query($bdd,"INSERT INTO ".$prefix."_edtbrik (brik_id, code, summary, seq_id, division, activ_brik, close, seance) VALUES ('', '".$code."', '".$summary."', '".$sequence."', '".$division."', 1, 0, 1)");
							}
						}
					}
					// Si choix spécifique de divisions et/ou groupes
					else if($div==1) {
						$division = $_POST['division'];
						foreach($division as $regdiv) {
							//vérification :
							$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE code = '".$code."' AND seq_id = '".$sequence."' AND division = '".mysqli_real_escape_string($bdd,$regdiv)."' AND activ_brik < 2");
							$briks_nb = mysqli_num_rows($briks);
							if($briks_nb==0) {
								mysqli_query($bdd,"INSERT INTO ".$prefix."_edtbrik (brik_id, code, summary, seq_id, division, activ_brik, close, seance) VALUES ('', '".$code."', '".$summary."', '".$sequence."', '".mysqli_real_escape_string($bdd,$regdiv)."', 1, 0, 1)");
							}
						}
					}
				}
			}
			
			// Enregistrement pour du texte libre :
			if(isset($_POST['briktxt'])) {
				// Récupération des données					
				$textelibre = mysqli_real_escape_string($bdd,trim($_POST['textelibre']));
				//vérification :
				$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre = '".$textelibre."' AND activ_brik < 2");
				$briks_nb = mysqli_num_rows($briks);
				if($briks_nb==0) {
					// Gestion de la fermeture ou non sur cette brique :
					if(isset($_POST['close'])) {
						$ifcode = '1'; }
					else { $ifcode = '0'; }
					// Gestion de séance ou non sur cette brique :
					if(isset($_POST['seance'])) {
						$ifseance = '1'; 
						if(isset($_POST['close'])) {
							$ifcode = '0'; }
					}
					else { $ifseance = '0'; }
					// Enregistrement :
					mysqli_query($bdd,"INSERT INTO ".$prefix."_edtbrik (brik_id, textelibre, activ_brik, close, seance) VALUES ('', '".$textelibre."', 1, '".$ifcode."', '".$ifseance."')");
				}
			}

			if(isset($_POST['brikmodif'])) {
				$brik_id = $_POST['brikid'];
				$division = mysqli_real_escape_string($bdd,$_POST['division']);
				$code = mysqli_real_escape_string($bdd,$_POST['code']);
				$summary = mysqli_real_escape_string($bdd,trim($_POST['summary']));
				$sequence = mysqli_real_escape_string($bdd,$_POST['sequence']);
				//vérification :
				$briks_nb = mysqli_num_rows($briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE code = '".$code."' AND seq_id = '".$sequence."' AND division = '".$division."' AND summary = '".$summary."'"));
				if($briks_nb==0) {
					//insertion :
					mysqli_query($bdd,"UPDATE ".$prefix."_edtbrik SET code = '".$code."', summary = '".$summary."', seq_id = '".$sequence."', division = '".$division."', close = '0', seance = '1' WHERE brik_id = '".$brik_id."'");
				}
			}
			if(isset($_POST['btxtmodif'])) {
				$brik_id = $_POST['brikid'];
				$textelibre = mysqli_real_escape_string($bdd,trim($_POST['textelibre']));
				//vérification :
				$briks_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE code = '".$textelibre."'"));
				if($briks_nb==0) {
					if(isset($_POST['close'])) {
						$ifcode = '1'; }
					else { $ifcode = '0'; }
					if(isset($_POST['seance'])) {
						$ifseance = '1'; 
						if(isset($_POST['close'])) {
							$ifcode = '0'; }
					}
					else { $ifseance = '0'; }
					//insertion :
					mysqli_query($bdd,"UPDATE ".$prefix."_edtbrik SET close = '".$ifcode."', textelibre = '".$textelibre."', seance = '".$ifseance."' WHERE brik_id = '".$brik_id."'");
				}
			}
						
			//Formulaire de création d'une brique :
			echo EDTC_BRIK3;
			echo '<form method="POST" name="briknew" action="" class="padd2">';
			echo '<table class="freqform">';
			echo '<tr><td>'.LEVEL.DOTS.'<br />'.EDTC_BRIK4.'</td><td><div class="box"><select name="niveau"><option></option>';
			include('db/proc/freq_stats_nivcolors.php');
			for($i=0;$i<count($tabDebniveau);$i++) {
				echo '<option value="'.$tabDebniveau[$i].'">'.$tabNiveau[$i].'</option>';
			}

			echo '</select></div></td></tr>
			<tr><td>'.EDTC_BRIK5.'</td>';
			$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE categ = '1' GROUP BY division ORDER BY division ASC");
			$liste_division_nb = mysqli_num_rows($liste_division);
			$liste_groupe = mysqli_query($bdd,"SELECT groupe_nom FROM ".$prefix."_groupes WHERE groupe_actif = 1 GROUP BY groupe_nom ORDER BY groupe_nom ASC");
			$liste_groupe_nb = mysqli_num_rows($liste_groupe);
			echo '<td><select name="division[]" class="inptxt marg25" multiple size="'.($liste_division_nb+$liste_groupe_nb+1).'"><option></option>';
			while($listed = mysqli_fetch_assoc($liste_division)) {
				echo '<option>'.$listed['division'].'</option>';
			}
			while($listeg = mysqli_fetch_assoc($liste_groupe)) {
				echo '<option>'.$listeg['groupe_nom'].'</option>';
			}
			echo '</select></td></tr>
			<tr><td>'.CODE.DOTS.'</td><td><div class="box"><select name="code" value=""><option></option>';
			$liste_code = mysqli_query($bdd,"SELECT code, nom, SUBSTR(nom,1,25) FROM ".$prefix."_edtcode ORDER BY code ASC");
			while($listec = mysqli_fetch_assoc($liste_code)) {
				$code_nomaffopt = mb_ereg_replace("[?]","",mb_convert_encoding($listec['SUBSTR(nom,1,25)'],"utf-8"));
				echo '<option value="'.$listec['code'].'">'.$listec['code'].' : '.$code_nomaffopt.'';
				if($listec['SUBSTR(nom,1,25)']!=$listec['nom']) { echo '...'; }
				echo '</option>';
			}
			echo '</select></div></td></tr>
			<tr><td>'.RESUME.'</td><td><textarea rows="3" cols="40" name="summary" value=""></textarea></td></tr>
			<tr><td>'.EDTC_BRIK6.'<br /></td><td><div class="box"><select name="sequence" value=""><option></option>';
			$liste_sequence = mysqli_query($bdd,"SELECT seq_id, seq_nom, niveau, progression, SUBSTR(seq_nom,1,27) FROM ".$prefix."_sequences ORDER BY niveau DESC, progression ASC, seq_nom ASC");
			while($listes = mysqli_fetch_assoc($liste_sequence)) {
				echo '<option value="'.$listes['seq_id'].'">';
				if($listes['niveau']=='6') { echo '6ème'; }
				else if($listes['niveau']=='5') { echo '5ème'; }
				else if($listes['niveau']=='4') { echo '4ème'; }
				else if($listes['niveau']=='3') { echo '3ème'; }
				else if($listes['niveau']=='2') { echo '2nde'; }
				else if($listes['niveau']=='1') { echo '1re'; }
				else if($listes['niveau']=='t') { echo 'Tale'; }
				else if($listes['niveau']=='a') { echo 'Autre'; }
				$seq_nomaffopt = mb_ereg_replace("[?]","",mb_convert_encoding($listes['SUBSTR(seq_nom,1,27)'],"utf-8"));
				echo ' : ';
				if($listes['progression']!='1000') { echo $listes['progression'].'. '; }
				echo $seq_nomaffopt.'';
				if($listes['SUBSTR(seq_nom,1,27)']!=$listes['seq_nom']) { echo '...'; }
				echo '</option>';
			}
			echo '</select></div></td></tr>
			</table>';
			echo '<span class="ecart"><input class="bouton boutedt" type="submit" value="'.EDTC_BRIK7.'" name="briknew" /></span>';
			echo '</form>';
			// Ajout pour texte libre
			echo '<h2>'.EDTC_BRIK8.'</h2>';
			echo '<form method="POST" name="briktxt" action="" class="padd2">';
			echo '<table class="freqform">';
				echo '<tr><td>'.EDTC_BRIK9.'</td><td><input type="text" class="inptxt" name="textelibre" maxlength="30" /></td><td><input type="checkbox" id="bclose" name="close" /><label for="bclose">'.EDTC_BRIK10.'</label><br><input type="checkbox" id="bseance" name="seance" /><label for="bseance">'.EDTC_BRIK11.'</label></td></tr>
			</table>';
			echo '<span class="ecart"><input class="bouton boutedt" type="submit" value="'.EDTC_BRIK7.'" name="briktxt" /></span>';
			echo '</form>';

			echo "<script language='javascript'>
				window.onmousedown = function (e) {
			    var el = e.target;
			    if (el.tagName.toLowerCase() == 'option' && el.parentNode.hasAttribute('multiple')) {
			        e.preventDefault();

			        if (el.hasAttribute('selected')) el.removeAttribute('selected');
			        else el.setAttribute('selected', '');

			        var select = el.parentNode.cloneNode(true);
			        el.parentNode.parentNode.replaceChild(select, el.parentNode);
			    }
			}
			</script>";
			
			if($nivs_info['valeur']=="COL") { 
				$briksniv = array("6","5","4","3","ZZZ"); 
				$groupes_out = "AND edtbrik.division NOT LIKE '6%' AND edtbrik.division NOT LIKE '5%' AND edtbrik.division NOT LIKE '4%' AND edtbrik.division NOT LIKE '3%' ";
				$groupes_in = " (division LIKE '6%' OR division LIKE '5%' OR division LIKE '4%' OR division LIKE '3%') ";
			}
			else if($nivs_info['valeur']=="LYC") { 
				$briksniv = array("2","1","t","T","ZZZ"); 
				$groupes_out = "AND edtbrik.division NOT LIKE '2%' AND edtbrik.division NOT LIKE '1%' AND edtbrik.division NOT LIKE 't%' AND edtbrik.division NOT LIKE 'T%' ";
				$groupes_in = " (division LIKE '2%' OR division LIKE '1%' OR division LIKE 't%' OR division LIKE 'T%') ";
			}
			else if($nivs_info['valeur']=="LYA") { 
				$briksniv = array("2","1","t","T","a","A","ZZZ"); 
				$groupes_out = "AND edtbrik.division NOT LIKE '2%' AND edtbrik.division NOT LIKE '1%' AND edtbrik.division NOT LIKE 't%' AND edtbrik.division NOT LIKE 'T%' AND edtbrik.division NOT LIKE 'a%' AND edtbrik.division NOT LIKE 'A%'";
				$groupes_in = " (division LIKE '2%' OR division LIKE '1%' OR division LIKE 't%' OR division LIKE 'T%' OR division NOT LIKE 'a%' OR division LIKE 'A%') ";
			}
			else if($nivs_info['valeur']=="CIT") { 
				$briksniv = array("6","5","4","3", "2","1","t","T","ZZZ"); 
				$groupes_out = "AND edtbrik.division NOT LIKE '6%' AND edtbrik.division NOT LIKE '5%' AND edtbrik.division NOT LIKE '4%' AND edtbrik.division NOT LIKE '3%' AND edtbrik.division NOT LIKE '2%' AND edtbrik.division NOT LIKE '1%' AND edtbrik.division NOT LIKE 't%' AND edtbrik.division NOT LIKE 'T%' ";
				$groupes_in = " (division LIKE '6%' OR division LIKE '5%' OR division LIKE '4%' OR division LIKE '3%' OR division LIKE '2%' OR division LIKE '1%' OR division LIKE 't%' OR division LIKE 'T%') ";
			}
			else if($nivs_info['valeur']=="CIA") { 
				$briksniv = array("6","5","4","3", "2","1","t", "a","A","ZZZ"); 
				$groupes_out = "AND edtbrik.division NOT LIKE '6%' AND edtbrik.division NOT LIKE '5%' AND edtbrik.division NOT LIKE '4%' AND edtbrik.division NOT LIKE '3%' AND edtbrik.division NOT LIKE '2%' AND edtbrik.division NOT LIKE '1%' AND edtbrik.division NOT LIKE 't%' AND edtbrik.division NOT LIKE 'T%' AND edtbrik.division NOT LIKE 'a%' AND edtbrik.division NOT LIKE 'A%'";
				$groupes_in = " (division LIKE '6%' OR division LIKE '5%' OR division LIKE '4%' OR division LIKE '3%' OR division LIKE '2%' OR division LIKE '1%' OR division LIKE 't%' OR division LIKE 'T%' OR division NOT LIKE 'a%' OR division LIKE 'A%') ";
			}
			else if($nivs_info['valeur']=="PER") { 
				$pernb = count($caracs);
				$briksniv = array();
				$groupes_out = '';
				$groupes_in = ' (';
				$per=0;
				foreach($caracs as $checked_caracs) {
					array_push($briksniv, $checked_caracs);
					$groupes_out .= " AND edtbrik.division NOT LIKE '".$checked_caracs."%' ";
					$groupes_in .= " division LIKE '".$checked_caracs."%' ";
					if($per<($pernb-1)) { $groupes_in .= " OR "; }
					$per++;
				}
				$groupes_in .= ") ";
				array_push($briksniv, "ZZZ");					
			}
			
			// Préparation du listage des briques :
			$briks1 = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre = '' AND activ_brik < 2");
			$briks1_nb = mysqli_num_rows($briks1);
			echo '</article><article id="artleft" class="artedtc">';
			echo '<div class="briques_modif" style="overflow: auto;  white-space: nowrap;">';
			if($briks1_nb!=0) {
							
				//Listage des briques existantes avec modification ou suppression (classes) :
				echo '<h2 id="listecla">'.EDTC_BRIK12.EDTC_BRIK13.'</h2>';				
				
				foreach($briksniv as $niv) {
					$sequences[$niv]=array();
					$nosequences[$niv]=array();
					$j=1;
					if($niv!="ZZZ") {
						$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik LEFT JOIN ".$prefix."_sequences seq ON edtbrik.seq_id = seq.seq_id WHERE edtbrik.textelibre = '' AND edtbrik.activ_brik < 2 AND edtbrik.division LIKE '".$niv."%' GROUP BY seq.seq_id ORDER BY seq.progression ASC, edtbrik.code ASC, seq.seq_nom ASC");
					}
					else if($niv=="ZZZ") {
						$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik LEFT JOIN ".$prefix."_sequences seq ON edtbrik.seq_id = seq.seq_id WHERE edtbrik.textelibre = '' AND edtbrik.activ_brik < 2 ".$groupes_out." GROUP BY seq.seq_id ORDER BY seq.progression ASC, edtbrik.code ASC, seq.seq_nom ASC");
					}
					foreach($briks as $seq) {
						if($seq['seq_nom']!='') { 
							array_push($sequences[$niv], $seq['seq_nom']);
						}
						$nbseq[$niv] = $j;
						$j++;
					}
					if($niv!="ZZZ") {
						$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik WHERE edtbrik.textelibre = '' AND edtbrik.activ_brik < 2 AND edtbrik.division LIKE '".$niv."%' AND seq_id = '0' GROUP BY edtbrik.code ORDER BY edtbrik.code ASC");
					}
					else if($niv=="ZZZ") {
						$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik WHERE edtbrik.textelibre = '' AND edtbrik.activ_brik < 2 ".$groupes_out." AND seq_id = '0' GROUP BY edtbrik.code ORDER BY edtbrik.code ASC");
					}
					foreach($briks as $seq) {
						array_push($nosequences[$niv], $seq['code']);
						$nbseq[$niv] = $j;
						$j++;
					}
				}
				echo '<table>';
				$briksdiv = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre = '' AND ".$groupes_in." AND activ_brik < 2 GROUP BY division ORDER BY division ASC");
				foreach($briksdiv as $div) {
					$k=1;
					echo '<tr>';
					if($nivs_info['valeur']=="PER") { 
						foreach ($sorted_caracs as $prefixe => $niveau) {
						    // Si la division commence par ce préfixe, on a trouvé le niveau
						    if (strpos($div['division'], $prefixe) === 0) {
						        $nivseq = $niveau;
						        break; // On arrête la boucle une fois le niveau trouvé
						    }
						}
					}
					else {
						$nivseq = substr($div['division'],0,1);
					} 
					if (isset($sequences[$nivseq]) && is_array($sequences[$nivseq])) {
						foreach($sequences[$nivseq] as &$seq_nom) {
							$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik LEFT JOIN ".$prefix."_sequences seq ON edtbrik.seq_id = seq.seq_id WHERE edtbrik.textelibre = '' AND edtbrik.activ_brik < 2 AND edtbrik.division = '".mysqli_real_escape_string($bdd,$div['division'])."' AND edtbrik.seq_id != 0 AND seq.seq_nom = '".mysqli_real_escape_string($bdd,$seq_nom)."'");
							$noseq=1;
							if(mysqli_num_rows($briks)>0) {
								include('./db/proc/edt_briques_td.php');
							}
							else { echo '<td></td>'; }
							$checknb=0;
							$checkdiv = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre = '' AND activ_brik < 2 GROUP BY division ORDER BY division ASC");
							foreach($checkdiv as $divc) {
								$briksnb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik LEFT JOIN ".$prefix."_sequences seq ON edtbrik.seq_id = seq.seq_id WHERE edtbrik.textelibre = '' AND edtbrik.activ_brik < 2 AND edtbrik.division = '".mysqli_real_escape_string($bdd,$divc['division'])."' AND edtbrik.seq_id != 0 AND seq.seq_nom = '".mysqli_real_escape_string($bdd,$seq_nom)."'"));
								if($briksnb>$checknb) { $checknb = $briksnb; }
							}
							if($checknb>1&&$checknb>mysqli_num_rows($briks)) {
								$tds = ($checknb-mysqli_num_rows($briks));
								for($i=0;$i<$tds;$i++) {
									echo '<td></td>';
								}
							}
						}
					}
					if (isset($nosequences[$nivseq]) && is_array($nosequences[$nivseq])) {
						foreach($nosequences[$nivseq] as &$code) {
							$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik WHERE edtbrik.textelibre = '' AND edtbrik.activ_brik < 2 AND edtbrik.division = '".mysqli_real_escape_string($bdd,$div['division'])."' AND edtbrik.seq_id = '0' AND edtbrik.code = '".$code."'");
							$noseq=0;
							if(mysqli_num_rows($briks)>0) {
								include('./db/proc/edt_briques_td.php');
							}
							else { echo '<td></td>'; }
						}
					}
					echo '</tr>';
				}
				$briksgroups = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik WHERE edtbrik.textelibre = '' ".$groupes_out." AND edtbrik.activ_brik < 2 GROUP BY edtbrik.division ORDER BY edtbrik.division ASC");
				foreach($briksgroups as $groupe) {
					$k=1;
					echo '<tr>';
					foreach($sequences['ZZZ'] as &$seq_nom) {
						$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik LEFT JOIN ".$prefix."_sequences seq ON edtbrik.seq_id = seq.seq_id WHERE edtbrik.textelibre = '' AND edtbrik.activ_brik < 2 AND edtbrik.division = '".mysqli_real_escape_string($bdd,$groupe['division'])."' AND edtbrik.seq_id != 0 AND seq.seq_nom = '".mysqli_real_escape_string($bdd,$seq_nom)."'");
						$noseq=1;
						if(mysqli_num_rows($briks)>0) {
							include('./db/proc/edt_briques_td.php');
						}
						else { echo '<td></td>'; }
						$checknb=0;
						$checkdiv = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre = '' AND activ_brik < 2 GROUP BY division ORDER BY division ASC");
						foreach($checkdiv as $divc) {
							$briksnb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik LEFT JOIN ".$prefix."_sequences seq ON edtbrik.seq_id = seq.seq_id WHERE edtbrik.textelibre = '' AND edtbrik.activ_brik < 2 AND edtbrik.division = '".mysqli_real_escape_string($bdd,$divc['division'])."' AND edtbrik.seq_id != 0 AND seq.seq_nom = '".mysqli_real_escape_string($bdd,$seq_nom)."'"));
							if($briksnb>$checknb) { $checknb = $briksnb; }
						}
						if($checknb>1&&$checknb>mysqli_num_rows($briks)) {
							$tds = ($checknb-mysqli_num_rows($briks));
							for($i=0;$i<$tds;$i++) {
								echo '<td></td>';
							}
						}
					}
					foreach($nosequences['ZZZ'] as &$code) {
						$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik edtbrik WHERE edtbrik.textelibre = '' AND edtbrik.activ_brik < 2 AND edtbrik.division = '".mysqli_real_escape_string($bdd,$groupe['division'])."' AND edtbrik.seq_id = '0' AND edtbrik.code = '".$code."'");
						$noseq=0;
						if(mysqli_num_rows($briks)>0) {
							include('./db/proc/edt_briques_td.php');
						}
						else { echo '<td></td>'; }
					}
					echo '</tr>';
				}
				echo '</table>';
			}
						
			$briks2 = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre != '' AND activ_brik < 2");
			$briks2_nb = mysqli_num_rows($briks2);
			
			if($briks2_nb!=0) {
				$cren_colors = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_crencolors'"));
				//Listage des briques existantes avec modification ou suppression (texte libre) :
				echo '<h2 id="listetxt">'.EDTC_BRIK12.EDTC_BRIK14.'</h2>';
				$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre != '' AND activ_brik < 2 ORDER BY textelibre ASC");
				echo '<table><tr>';
				$line=1;
				foreach($briks as $briksup) {
					if(($line%5)==0+1) { echo '<tr>'; }
					echo '<td'; 
					if($briksup['activ_brik']==0) { echo ' class="brik_desactiv"'; }
					else if($briksup['activ_brik']==1) { echo ' class="brik_activ"'; }
					echo ' id="td'.$briksup['brik_id'].'">'; 
					if($briksup['close']=='1') {
						$bgcolor = substr($cren_colors['valeur'],7,7);
					}
					if($colorsys_info['valeur']=='niv'&&$colortex_info['valeur']=='1') {
						$colorniv = null;
						if($nivs_info['valeur']=="PER") { 
							foreach ($sorted_caracs as $prefixe => $niveau) {
							    // Si la division commence par ce préfixe, on a trouvé le niveau
							    if (strpos($briksup['textelibre'], $prefixe) === 0) {
							        $colorniv = $niveau;
							        break; // On arrête la boucle une fois le niveau trouvé
							    }
							}
							if($colorniv!==null&&@$colorniv!='r') {
								$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
								$color_info = mysqli_fetch_assoc($color);
								$bgcolor = $color_info['bgcolor'];
								$nocolniv = '0';
							}
							else { $nocolniv = '1'; }
						}
						else {
							$colorniv = substr($briksup['textelibre'],0,1);
							if($colorniv!='6'&&$colorniv!='5'&&$colorniv!='4'&&$colorniv!='3'&&$colorniv!='2'&&$colorniv!='1'&&$colorniv!='t'&&$colorniv!='T'&&$colorniv!='a') { $nocolniv = '1'; }
							else {
								$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
								$color_info = mysqli_fetch_assoc($color);
								$bgcolor = $color_info['bgcolor'];
								$nocolniv = '0';
							}
						} 			
					}
					else { $nocolniv = '1'; }
					if($nocolniv=='1') {
						$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = 'r'");
						$color_info = mysqli_fetch_assoc($color);
						$bgcolor = $color_info['bgcolor'];
					}
					echo '<div class="cartouche" style="background-color:'.$bgcolor.';" name="'.$briksup['brik_id'].'">'; 
					if(strlen($briksup['textelibre'])>16) { echo '<span class="txtlibre_2">'; }
					else { echo '<span class="txtlibre_1">'; }
					echo $briksup['textelibre'].'</span></div>';
					echo ' <a onmousedown="toggleDiv(\'param'.$briksup['brik_id'].'\');"><img style="height:24px;" src="./img/div/param.png" /></a><div class="radio-item-emb"><div class="radio-item activ"><input type="radio" id="chkActiv'.$briksup['brik_id'].'" name="chkBrik'.$briksup['brik_id'].'" onclick="StatusBrik('.$briksup['brik_id'].',1);ChgtColor(\'td'.$briksup['brik_id'].'\',1);"'; if($briksup['activ_brik']==1) { echo ' checked'; } echo ' /></div> <div class="radio-item desactiv"><input type="radio" id="chkDesact'.$briksup['brik_id'].'" name="chkBrik'.$briksup['brik_id'].'" onclick="StatusBrik('.$briksup['brik_id'].',0);ChgtColor(\'td'.$briksup['brik_id'].'\',0);"'; if($briksup['activ_brik']==0) { echo ' checked'; } echo ' /></div> <div class="radio-item archiv"><input type="radio" id="chkArchiv'.$briksup['brik_id'].'" name="chkBrik'.$briksup['brik_id'].'" onclick="StatusBrik('.$briksup['brik_id'].',2);ChgtColor(\'td'.$briksup['brik_id'].'\',2);"" /></div></div><div id="param'.$briksup['brik_id'].'" class="parambrik" style="display:none;">';
					echo '<form method="POST" name="btxtup" action="edt.php?page=briques#listetxt">';
					echo TEXT.DOTS.' <input type="text" class="inptxt" name="textelibre" value="'.$briksup['textelibre'].'" />';
					if($briksup['close']=='1') { $check = "checked"; } else { $check = "0"; }
					echo '<br><input type="checkbox" id="bclose'.$briksup['brik_id'].'" name="close" '.$check.' /><label for="bclose'.$briksup['brik_id'].'">'.EDTC_BRIK10.'</label>';
					if($briksup['seance']=='1') { $check = "checked"; } else { $check = "0"; }
					echo ' <input type="checkbox" id="bseance'.$briksup['brik_id'].'" name="seance" '.$check.' /><label for="bseance'.$briksup['brik_id'].'">'.EDTC_BRIK111.'</label>';
					echo '<input type="hidden" value="'.$briksup['brik_id'].'" name="brikid" />';
					echo '&nbsp; <input type="submit" class="bouton boutedt" value="'.VALID.'" name="btxtmodif" /></form>';
					echo '</div></td>'; 
					if(($line%5)==0) { echo '</tr>'; }
					$line++;
				}
				echo '</tr></table>';
			}
			// Briques archivées :
			include('./db/proc/edt_briques_archives.php');
			echo '</div>';
		}
		echo '</div>';
	}
}
else {
	echo '<br><a href="../index.php">'.ACCUEIL.'</a></p>';
}
?>