<?php

/**
* permet de créer le pseudo-composant
* @param :
* - nom : le nom du composant
* - structure : les colonnes, les dimensions, les alignements (tableau)
* - contenu : les elements : valeur checkbox + autres colonnes (tableau)
* - selection : tableau de valeur a selectionner (correspond a la valeur de la checkbox)
*/
function checkedSelect( $nom, $structure, $contenu, $selection = null) {

//////////////// entete du composant \\\\\\\\\\\\\\\\\\\\\\\\\
if(sizeof($selection) == sizeof($contenu))
$checked = "checked";
else
$checked = "";


$str = '<div class="div_select">'
.'<div class="div_select_header">'
.'<table width="100%">'
.'<tr>'
.'<td width="20" align="center"><input type="checkbox" id="'.$nom.'" onClick="javascript:selectAll(this, '.sizeof($contenu).');" '.$checked.'/></td>';

//--- colonnes
for($i = 0, $max = sizeof($structure); $i < $max; $i++) {
$str .= '<td ';
if(isset($structure[$i]["align"])) $str .= 'align="'.$structure[$i]["align"].'" ';
if(isset($structure[$i]["width"])) $str .= 'width="'.$structure[$i]["width"].'" ';
$str .= '>'.$structure[$i]["name"].'</td>';
}

$str .= '<td width="1"></td>'
.'</tr>'
.'</table>'
.'</div>';

//////////////// contenu du composant \\\\\\\\\\\\\\\\\\\\\\\\\
$str .= '<div class="div_select_content" >'
.'<table width="100%">';

//--- pour chaque ligne
for($i = 0, $max = sizeof($contenu); $i < $max; $i++) {

//--- style css (pair / impair)
if($i%2)
$css_defaut = 'select_odd';
else
$css_defaut = 'select_even';

//--- element selectionne ?
if($selection != null && in_array($contenu[$i][0],$selection)) {
$checked = ' checked';
$css = 'select_checked';
}
else {
$checked = '';
$css = $css_defaut;
}

//--- la checkbox
$str .= '<tr id="tr_'.$nom.$i.'" class="'.$css.'" onMouseOver=\'javascript:setEvenement("'.$nom.'","'.$i.'","over","'.$css.'",'.sizeof($contenu).',"td");\' onMouseOut=\'javascript:setEvenement("'.$nom.'","'.$i.'","out","'.$css_defaut.'",'.sizeof($contenu).',"td");\' '
.' onClick=\'javascript:setEvenement("'.$nom.'","'.$i.'","click", "'.$css_defaut.'",'.sizeof($contenu).',"td");\' >'
.'<td width="20" ><input type="checkbox" id="'.$nom.$i.'" name="'.$nom.'[]" value="'.$contenu[$i][0].'" onClick=\'javascript:setEvenement("'.$nom.'","'.$i.'","click","'.$css_defaut.'",'.sizeof($contenu).',"ck");\' '.$checked.' /></td>';

//--- les colonnes
for($j = 1; $j < sizeof($contenu[$i]); $j++) {
$str .= '<td ';
if(isset($structure[$j-1]["align"])) $str .= 'align="'.$structure[$j-1]["align"].'" ';
if(isset($structure[$j-1]["width"])) $str .= 'width="'.$structure[$j-1]["width"].'" ';
$str .= '>'.$contenu[$i][$j].'</td>';
}

$str .= '</tr>';
}

//--- fermeture des balises
$str .= '</table>'
.'</div>'
.'</div>';

return $str;
}


//===============================================================
// VARIABLES D'EXEMPLES
//===============================================================
$nom = "test1";

$structure = array (
array("name" => "CODE", "align" => "center", "width" => "40px"),
array("name" => "NOM", "align" => "left", "width" => null)
);

$contenu = array (
array("AD", "AD", "TEST AD"),
array("CC", "CC", "TEST CC"),
array("DZ", "DZ", "TEST DZ"),
array("GV", "GV", "TEST GV"),
array("LS", "LS", "TEST LS"),
array("LC", "LC", "TEST LC"),
array("SG", "SG", "TEST SG")
);

$nom2 = "test2";

$structure2 = array (
array("name" => "CODE", "align" => "center", "width" => "40px"),
array("name" => "NOM", "align" => "left", "width" => null),
array("name" => "MONTANT", "align" => "right", "width" => "60px")
);

$contenu2 = array (
array("001", "001", "TEST 01", "150 €"),
array("002", "002", "TEST 02", "80 €"),
array("003", "003", "TEST 03", "120 €"),
array("004", "004", "TEST 04", "160 €"),
array("005", "005", "TEST 05", "20 €"),
array("006", "006", "TEST 06", "70 €"),
array("007", "007", "TEST 07", "150 €") ,
array("005", "005", "TEST 08", "2 654 €"),
array("009", "009", "TEST 09", "34 €"),
array("010", "010", "TEST 10", "27,50 €")
);

//======================================================
// VERFICATION DU POST
//======================================================
if($_POST["submit"]) {
echo '<b>Formulaire validé avec les éléments : </b><br>';

echo '<b>- '.$nom.' :</b> ';
for($i = 0; $i < sizeof($_POST[$nom]); $i++) {
if($i > 0) echo ', ';
echo $_POST[$nom][$i];
}

echo '<br /><b>- '.$nom2.' :</b> ';
for($i = 0; $i < sizeof($_POST[$nom2]); $i++) {
if($i > 0) echo ', ';
echo $_POST[$nom2][$i];
}

echo '<br /><br />';
}

?>