<?php
$briks1 = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre = '' AND activ_brik = '2'");
$briks1_nb = mysqli_num_rows($briks1);

$briks2 = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre!='' AND activ_brik = '2'");
$briks2_nb = mysqli_num_rows($briks2);

if($briks1_nb>0||$briks2_nb>0) {
	echo '<h2 id="listecla">'.EDT_BRIKA1.'</h2>';
}

if($briks1_nb!=0) {
	//Listage des briques existantes avec modification ou suppression (classes) :
	foreach($briksniv as $niv) {
		$arch_sequences[$niv]=array();
		$arch_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_nom 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_nom ORDER BY seq.progression ASC, edtbrik.code ASC, seq.seq_nom ASC");
		}
		foreach($briks as $seq) {
			if($seq['seq_nom']!='') { 
				array_push($arch_sequences[$niv], $seq['seq_nom']);
			}
			else { array_push($arch_nosequences[$niv], $seq['brik_id']); }
			$nbseq[$niv] = $j;
			$j++;
		}
	}
	echo '<table>';

	$briksdiv = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre = '' AND activ_brik = '2' AND ".$groupes_in." GROUP BY division ORDER BY division ASC");
	foreach($briksdiv as $div) {
		$k=1;
		echo '<tr>';
		foreach($arch_sequences[substr($div['division'],0,1)] 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 = '".$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_archives.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 = '".$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($arch_nosequences[substr($div['division'],0,1)] as &$brik) {
			$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 = '".$div['division']."' AND edtbrik.brik_id = '".$brik."'");
			$noseq=0;
			if(mysqli_num_rows($briks)>0) {
				include('./db/proc/edt_briques_td_archives.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($arch_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 = '".$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_archives.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 = '".$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($arch_nosequences['ZZZ'] as &$brik) {
			$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 = '".$groupe['division']."' AND edtbrik.brik_id = '".$brik."'");
			$noseq=0;
			if(mysqli_num_rows($briks)>0) {
				include('./db/proc/edt_briques_td_archives.php');
			}
			else { echo '<td></td>'; }
		}
		echo '</tr>';
	}
	echo '</table>';
}
		
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 archivées :
	$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"'; }
		else if($briksup['activ_brik']==2) { echo ' class="brik_archiv archiv"'; }
		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'])>20) { echo '<span class="txtlibre_2">'; }
		else { echo '<span class="txtlibre_1">'; }
		echo $briksup['textelibre'].'</span></div>';
		$event_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT uid FROM ".$prefix."_edtevent WHERE brik_id = '".$briksup['brik_id']."'"));
		$verif_brik = mysqli_num_rows(mysqli_query($bdd,"SELECT brik_id FROM ".$prefix."_edtbrik WHERE textelibre='".$briksup['textelibre']."'"));
		echo ' <img style="height:24px;" src="./img/nav/help.png" title="';
		if($event_nb==0) { echo EDT_BRIKA2; }
		else { echo $event_nb.' '.EDIT_INFO3; if($event_nb>1) { echo PLURIEL; } echo EDT_BRIKA3; }
		echo '"/><div class="radio-item-emb"><form action="" method="POST" name="react">';
		if($verif_brik==1) { echo '<input type="submit" class="boutontab" name="reactivb'.$briksup['brik_id'].'" value="'.REACT_TINY.'" />'; }
		if($event_nb==0) { echo ' <input type="submit" class="boutontab" name="supprb'.$briksup['brik_id'].'" value="'.SUPPR_TINY.'" />'; }
		echo '</form>';
		if(isset($_POST['reactivb'.$briksup['brik_id'].''])) {
			$brik_id = $briksup['brik_id'];
			mysqli_query($bdd,"UPDATE ".$prefix."_edtbrik SET activ_brik = '1' WHERE brik_id = '".$brik_id."'");
			echo '<script language="Javascript">
			<!--
			document.location.replace("edt.php?page=briques");
			// -->
			</script>';	
		}
		if(isset($_POST['supprb'.$briksup['brik_id'].''])) {
			$brik_id = $briksup['brik_id'];
			mysqli_query($bdd,"DELETE FROM ".$prefix."_edtbrik WHERE brik_id = '".$brik_id."'");
			echo '<script language="Javascript">
			<!--
			document.location.replace("edt.php?page=briques");
			// -->
			</script>';	
		}
		echo '</div></td>'; 
		if(($line%5)==0) { echo '</tr>'; }
		$line++;
	}
	echo '</tr></table>';
}
?>