<?php
#=
/****************************************************************************
Copyright 2002, Daniel Deycard, Jacques Messines, Alain Pomirol
(CATICE Académie de Bordeaux)

This file is part of GiBii/Melia.

GiBii/Melia is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

GiBii/Melia is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GiBii/Melia; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Modifié par Fabien NGUYEN
****************************************************************************/
include("../include/vars_adm.php");
include("../include/util.inc.php");
include("../include/get.inc.php");
include("../include/utiljs.php");
opensession("Sadmin",false);
if ($sess_login=='admstats') {
	aneantir_session();
	goto("./index.php");
	}
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
       <title>Activation des étabs</title>
<link rel="stylesheet" href="../include/general.css">
<script language="javascript">
 function radioglobal(formulaire, nomradio, ouinonzut)
     //ouinonzut vaut -1 ou 0 ou 1 pour les boutons radio resp. 0, 1 et 2
     {
     var frm=eval('document.'+formulaire);
     for (i=0;i<frm.elements.length; i++) {
         var nom=frm.elements[i].name.substr(0,3);
         var k=frm.elements[i];
          if (nom==nomradio) {
             if (k.value==ouinonzut) {
                 k.checked=true;
                }
             }
         }  //fin for

}
</script>
</head>
<body class="arial_sadmin" bgcolor="white" text="black" link="white" vlink="white" alink="blue">


<?php
$PHP_SELF=basename($_SERVER['PHP_SELF']);
connecte($host,$user,$mdp,$db);

print "<p class='titre'>Activer / Désactiver des établissements</p>";


function etabactif($id) {
 //retourne la valeur de 'actif' dans etablissements (-1 0 ou 1)
 $res=query("SELECT actif FROM etablissements WHERE etab_id=$id;");
 list($act)=mysql_fetch_array($res);
 return $act;
}

function activetab($id, $ouinonzut) {
 //change l'état d'un établissement

 switch ($ouinonzut) {
   case -1 : $m="<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Etablissement Désactivé.";
    break;
   case 0 : $m="<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Etablissement Activé (Uniquement pour les Admin).";
    break;
   case 1 : $m="<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Etablissement Activé.";
    break;
   default :
     $m="<br />";
    }


 $sql="UPDATE etablissements SET actif=".seci($ouinonzut)." 
      WHERE etab_id=$id;";
 $res=query($sql);
 if ($res)return $m;
    else return "Erreur activation de l'établissement $id";
}

if (!isset($Valider)) {  //Formulaire

if (!isset($tri)) $tri="";
switch($tri) {
   case "id":
        $ordre="etab_id";
        break;
   case "rne":
        $ordre="rne";
        break;
   case "codepostal":
        $ordre="codepostal";
        break;
   case "ville":
        $ordre="ville";
        break;
   case "nom":
        $ordre="nom";
        break;
   default :
        $ordre="rne";
}
if (!isset($order)) $order="";
if ($order=="d") $ordre=$ordre." DESC";


$sql="SELECT * ".
             " FROM etablissements ".
             " ORDER BY $ordre" ;
$resetab=query ($sql);
$nbetabs=0;
//alert($sql);


print "<form name='fetabs' method='POST' action=\"$PHP_SELF\" >";

//début du tableau
print "<table border='1' align='center' cellspacing='0' >\n";
print "<tr class='rubrique'>\n";
print "\t<td align='center' width='25%'>Etat des établissements<br />";
print "<table border='0' width=98%><tr ><td align='center' width='30%'>";
print "<a href=\"#\" title=\"Désactiver tous les établissements\"onclick=\"radioglobal('fetabs','BR_', '-1');\"><img src=\"./images/coche.gif\" border=\"0\"></a>";
print "</td><td align='center' width='40%'>";
print "<a href=\"#\" title=\"Activer tous (uniquement pour les Admin)\" onclick=\"radioglobal('fetabs','BR_', '0');\"><img src=\"./images/coche.gif\" border=\"0\"></a>";
print "</td><td align='center'>";
print "<a href=\"#\" title=\"Activer tous les établissements\"  onclick=\"radioglobal('fetabs','BR_', '1');\"><img src=\"./images/coche.gif\" border=\"0\"></a>";
print "</td></tr></table>";
print "</td><td>Initialiser mdp admin & gestion";
print "</td><td><a href=\"$PHP_SELF?tri=id\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;Ident.&nbsp;<a href=\"$PHP_SELF?tri=id&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td>";
print "<td><a href=\"$PHP_SELF?tri=rne\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;RNE&nbsp;<a href=\"$PHP_SELF?tri=rne&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td>";
print "<td><a href=\"$PHP_SELF?tri=nom\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;Nom&nbsp;<a href=\"$PHP_SELF?tri=nom&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td>";
print "<td><a href=\"$PHP_SELF?tri=adresse\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;Adresse&nbsp;<a href=\"$PHP_SELF?tri=adresse&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td>";
print "<td><a href=\"$PHP_SELF?tri=codepostal\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;Code postal&nbsp;<a href=\"$PHP_SELF?tri=codepostal&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td>";
print "<td><a href=\"$PHP_SELF?tri=ville\"><img src=\"./images/haut.gif\" border=\"0\" align=\"middle\"></a>&nbsp;Ville&nbsp;<a href=\"$PHP_SELF?tri=ville&order=d\"><img src=\"./images/bas.gif\" border=\"0\" align=\"middle\"></a></td></tr>\n";

$num=0;
while ($rowetab=mysql_fetch_array($resetab)) {
      $etab_id=$rowetab["etab_id"];
      $nbetabs++;
      $rne=$rowetab["rne"];
      $nom=$rowetab["nom"];
      $adresse=$rowetab["adresse"];
      $codepost=$rowetab["codepostal"];
      $ville=$rowetab["ville"];

     //couleur de la ligne suivant sa parité
     $cl=($nbetabs%2==0) ? "sadmin_paire" : "sadmin_impaire";

     //Préparation des boutons radio
     $old_check=etabactif($etab_id);
     $check0=($old_check==-1)? "checked":"";
     $check1=($old_check==0)? "checked":"";
     $check2=($old_check==1)? "checked":"";
     $dis="";
     //Affichage d'une ligne
     print "\t<tr class=$cl>\n";
     print "\t<td align='center'>";
     print "<table border='0' width=98%><tr ><td align='left' width='30%'>";
     print "<font size='-1'><SPAN title=\"Personne ne peut ouvrir de session\">Inactif<input type='radio' name=\"BR_".$num."\" value=\"-1\" $check0  $dis></SPAN></font>\n";
     print "</td><td align='center' width='40%'>";
     print "<font size='-1'><span title=\"Seul un Admin peut ouvrir une session\">Actif (Admin)<input type='radio' name=\"BR_".$num."\" value=\"0\" $check1 $dis></span></font>\n";
     print "</td><td align='right'>";
     print "<font size='-1'><span title=\"Tout le monde peut ouvrir une session\">Actif<input type='radio' name=\"BR_".$num."\" value=\"1\" $check2 $dis></span></font>\n";
     print "</td></tr></table>";
     print "<input type='hidden' name=\"etab_$num\" value='$etab_id' >\n";
     print "<input type='hidden' name=\"etat_$num\" value='$old_check' >\n";

     print "</td>\n";
     print "\t<td align=\"center\"><SPAN title=\"Réinitialise admin et gestion \n et envoie la fiche à ce.$rne\"><input type='checkbox' name=\"CB_$num\" value=$etab_id></span><input type='hidden' name=\"RNE_$num\" value=\"$rne\"></td>\n";
     print "\t<td align=\"center\">$etab_id</td>\n";
     print "\t<td >$rne</td>\n";
     print "\t<td >$nom</td>\n";
     print "\t<td >$adresse</td>\n";
     print "\t<td >$codepost</td>\n";
     print "\t<td >$ville</td>\n";

     print "\t</tr>\n";
     $num++;
}

 print "</table><br>\n";
 if ($nbetabs<2) $pluriel="";
    else $pluriel="s";
 print $nbetabs." établissement".$pluriel;
 print "<input type='hidden' name='nbetabs' value='$nbetabs'>";
 print "<p align='center'><input type='checkbox' name='mail_init' value=1  checked>&nbsp;Pour la réinitialisation, envoyer à nouveau par mail la fiche d'inscription (mêmes paramètres que pour la création)</p>";
 print "<p align='center'><input type='submit' class='coul' name='Valider' value='Valider'>";
 print "&nbsp;&nbsp;&nbsp;<input type='reset' class='coul' name='Initialiser' value='Rétablir'></p>";
 print "</form></body></html>\n";
 //=============================================
 } else {  //Traitement
 //=============================================
//print_r($_POST);
function decode($ch,$r,$i) {  //décodage chaînes pour le mail
$buffer=str_replace('[rne]',$r,$ch);
$buffer=str_replace('[id]',$i,$buffer);
return $buffer;
        }
        
$mess="";
 for ($i=0;$i<$nbetabs;$i++) {
   $br="BR_".$i;
   $etat="etat_".$i;
   $etab="etab_".$i;
   $etid=$$etab;
   //Pour RNE
   $rne_i="RNE_".$i;
   $rnei=$$rne_i;

   $mess.="<br># ";
   $tabetab=getetab($etid);
   $mess.=$tabetab['nom']."  ".$tabetab['ville']."  ".$tabetab['codepostal'];
   $mess.=" : <i>";

   // $$br contient directement la valeur pour 'actif'
   if ($$br!=$$etat) $mess.=activetab($$etab,$$br);

   //On réinitialise le mdp admin si la CB est cochée
   $chb="CB_".$i;
   if ((isset($$chb)) && ($$chb==$$etab)) {
     //on y va

     $mdp=md5("admin".$rnei);
     $ok=@query("UPDATE comptes SET mdp='$mdp' WHERE etab_id=$etid and login='admin' ;");
     $mdp=md5("gestion".$rnei);
     $ok=@query("UPDATE comptes SET mdp='$mdp' WHERE etab_id=$etid and login='gestion' ;");
     $mdp=md5("prof".$rnei);
     $ok=@query("UPDATE comptes SET mdp='$mdp' WHERE etab_id=$etid and login='proftest' ;");
     
     if (!$ok) $mess.="<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Comptes non réinitialisé.";
        else $mess.="<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mot de passe réinitialisé pour les comptes admin (admin$rnei), gestion (gestion$rnei) et proftest (prof$rnei).";

     //Envoi du mail automatique suivant le modèle choisi
     if (isset($mail_init)) {
     	$from=decode($MAIL_FROM,$rnei,$etid);
     	$to=decode($MAIL_TO,$rnei,$etid);     //ou plusieurs destinataires séparés par ','
     	$cc=decode($MAIL_CC,$rnei,$etid);     //idem
    
     	//$subject=decode($MAIL_SUBJECT,$rnei,$etid);
     	$subject="Votre GiSCCo est réinitialisé, voici la fiche des comptes";
     	$err_mail=!envoi_mail($from,$to,$cc,$subject,"../data/modele_inscript.txt",$etid,$rnei);
 	 	if (!$err_mail) $mess.="<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Courrier de<b> $from </b>envoyé à<b> $to </b>, copie à<b> $cc </b><br />";
 	 	}
	 }
   $mess.="</i>";
   }
   //Affichage bilan
   print "<br />".$mess;
}
?>


</body>

</html>