<?
/***************************************************************************
                          AnnuUsers.php -  Annuaire des utilisateurs.
                             -------------------
    begin                : 13 Juillet UTC 2002
    copyright            : (C) 2002 by NADAL Jean-Baptiste
    email                : jbnadal@ifrance.com
 ***************************************************************************
 *
 *  Cette classe permet de gerer la liste des utilisateur ainsi que la 
 *  gestion de leur niveau.
 *
 ***************************************************************************
 *                                                                         *
 *   This program 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.                                   *
 *                                                                         *
 ***************************************************************************
 * $Id: AnnuUsers.php,v 1.1 2002/12/15 20:51:51 jb Exp $
 ***************************************************************************/

if (file_exists ("conf.php"))
   require ("conf.php");
else
   require ("./Scripts/conf.php");


class AnnuUsers
{
   /**************************************************************************
    * Les Variables
    **************************************************************************/
   var $szNomBase;             /* Nom de la base de travail. */
   var $szPage;                /* Nom de la page courante dans le site. */
   
   var $nID;
   var $szPseudo;
   var $szPassword;
   var $szemail;
   var $nState;

   /**************************************************************************
    *  Constructeur
    **************************************************************************/
   function AnnuUsers ($nomB, $page)
   {
      $this->szNomBase = $nomB;
      $this->szPage = $page;
      $this->nState = 0;
   }

   /**************************************************************************
    *  List all the users.
    **************************************************************************/
   function ListerUsers ()
   {
      global $sql_serveur, $sql_bdd, $sql_user, $sql_passwd;
      global $USER_ADMIN, $MODERATOR;
      global $racine, $page_index, $state, $pseudo;

      /* on affiche l"entete du tableau. */
      echo "<center><table border=0 width=\"95%\">
            <tr>
              <th bgcolor=\"#d5dee6\"> Pseudo </th>
              <th bgcolor=\"#d5dee6\" colspan=2>Status </th>
              <th bgcolor=\"#d5dee6\" colspan=2> Action</th>
            </tr>\n";

      /* On se connecte sur la base de Users. */
      $connect_db = jb_sql_connect (false);
      $requete = "select * from $this->szNomBase  order by pseudo";
      $resultat = mysql_db_query ($sql_bdd, $requete, $connect_db);
      $i = 0;
      while ($row = mysql_fetch_row ($resultat))
      {
         if (($i % 2) == 0)
            echo"<tr bgcolor=\"#DDDDDD\" align=\"center\">\n";
         else
            echo "<tr bgcolor=\"#CCCCCC\" align=\"center\">\n";
         echo "<td>$row[2] </td>\n";
         echo "<td>Admin: ".(($row[5] & $USER_ADMIN)?"OUI":"NON")."</td>\n";
         echo "<td>Modérateur: ".(($row[5]&$MODERATOR)?"OUI":"NON")."</td>\n";
         echo "<td>";
         if (($state & $USER_ADMIN) || 
             (($state & $MODERATOR) && ($row[5] == 0)) ||
            ($pseudo == $row[2])) {
            echo "
               <a href=$racine/$page_index?page=$this->szPage&".
               "action=modifier&elem=$row[0]>Modifier</a>
              </td>
              <td><a href=$racine/$page_index?page=$this->szPage&".
               "action=supprimer&elem=$row[0]>Supprimer</a>";
         }
         else {
            echo "--
                  </td><td>--";
         }
         echo "</td> </tr>\n";
         $i++;
      }
      mysql_free_result ($resultat);
      MYSQL_CLOSE;
      echo "</table> </center><br><br>";
   }

   /**************************************************************************
    *  Add a new user.
    **************************************************************************/
   function AjouterUser ()
   {
      echo " <center><h1> Ajoute d'un nouvel utilisateur</h1></center><br/>\n";
      $this->debutFormulaire ("ValideAjouter");
      $this->FinFormulaire ();
   }

   /**************************************************************************
    *  Insert a new user in the database.
    **************************************************************************/
   function InsertUser ($elem, $pseudo, $password, $email, $state)
   {
       global $racine, $page_index, $sql_bdd;
       
       if (($pseudo == '') || ($password == "") ||
           ($state == ""))
          echo "<br/><br/><center> <font size=6 color=red>
                <b>BUG !!! L'utilisateur $pseudo  n'a pas été ajouté</b>
                </font><br/>
                certains champs sont vides</center><br><br>\n";
       else {
          // On se connecte sur la base de Download
          $connect_db = jb_sql_connect (false);
          $requete = "insert into $this->szNomBase (pseudo,password,".
             "email,state) values ('$pseudo','$password','$email','$state')";

          $resultat = mysql_db_query ($sql_bdd, $requete, $connect_db);

          if ($resultat == 1)
             echo "<br><br><center><font size=4 color=red>
                   <b>L'utilisateur $pseudo  a été Ajouté</b></font>
                   </center><br/><br/>\n";
          else
             echo "<br><br><center><font size=6 color=red>
                   <b>BUG !!! L'utilisateur $pseudo  n'a pas été ajouté</b>
                   </font></center><br/><br/>\n";

          MYSQL_CLOSE;
       }
       echo "<br/><br/>
             <center><a href=$racine/$page_index?page=$this->szPage>
             Retour à la page d'administration des utilisateurs</a>
             </center>\n";
   }

   /**************************************************************************
    *  Modify a user in the database.
    **************************************************************************/
    function ModifierUser ($elem)
    {
       global $sql_bdd;

       /* On se connecte sur la base des Utilisaterus. */
       $connect_db = jb_sql_connect (false);
       $requete = "select * from $this->szNomBase where id_membre='$elem'";
       $resultat = mysql_db_query ($sql_bdd, $requete, $connect_db);

       $this->nID = mysql_result ($resultat, 0, "id_membre");

       $this->szPseudo = mysql_result ($resultat, 0, "pseudo");
       $this->szPassword = mysql_result ($resultat, 0, "password");
       $this->szemail = mysql_result ($resultat, 0, "email");
       $this->nState = mysql_result ($resultat, 0, "state");


       echo "<center><h1> Modification de l'utilisateur : $this->szNom </h1>
             </center><br/>\n";
       $this->debutFormulaire ("ValideModifier");
       $this->FinFormulaire ();
       mysql_free_result ($resultat);
       MYSQL_CLOSE;
    }

   /**************************************************************************
    *  Valid the Modification for a user in the database.
    **************************************************************************/
    function ValideModifierUser ($elem, $pseudo, $password,
                                 $email, $state)
    {
       global $racine, $page_index, $sql_bdd;
       
       /* On se connecte sur la base de REpertoire. */
       $connect_db = jb_sql_connect (false);
       $requete = "update $this->szNomBase  set pseudo='$pseudo' ,password=".
          "'$password',email='$email', state='$state' where id_membre='$elem'";

       $resultat = mysql_db_query($sql_bdd, $requete, $connect_db);
       if ($resultat == 1)
          echo "<br/><br/><center><font size=4 color=red>
                <b>L'utilisateur $pseudo a été Modifié</b></font>
                </center><br/><br/>\n";
       else
          echo "<br/><br/><center><font size=6 color=red>
                <b>BUG !!! L'utilisateur $pseudo n'a pas été Modifié</b></font>
                </center><br/><br/>\n";


       MYSQL_CLOSE;
       echo "<br/><br/>
             <center><a href=$racine/$page_index?page=$this->szPage>
             Retour à la page d'administration des Utilisateurs</a>
             </center>\n";
    }

   /**************************************************************************
    *  Erase an user in the database.
    **************************************************************************/
    function Supprimer ($num_elem)
    {
       global $racine, $page_index, $sql_bdd;
       
       /* On se connecte sur la base. */
       $connect_db = jb_sql_connect (false);
       $resultat = mysql_db_query ($sql_bdd,
                                   "delete from $this->szNomBase where ".
                                   "id_membre=$num_elem",$connect_db);
       if ($resultat == 1)
          echo "<br><br><center><font size=4 color=red>
                <b>L' utilisateur $num_elem a été effacé</b>
                </font></center><br/><br/>\n";
       else
          echo "<br><br><center><font size=6 color=red>
                <b>BUG !!! suppression Impossible</b></font></center>
                <br/><br/>\n";

       MYSQL_CLOSE;
       echo "<br/><br/>
             <center><a href=$racine/$page_index?page=$this->szPage>
             Retour à la page d'administration des Utilisateurs</a>
             </center>\n";
    }

   /**************************************************************************
    *  This function create an addition form.
    **************************************************************************/
    function debutFormulaire ($valide)
    {
       global $racine, $page_index, $state;
       global $USER_ADMIN, $MODERATOR;

       $this->InputJavaScript();
       echo "<form method=post action=$racine/$page_index?page=$this->szPage".
          "&action=$valide>
           <input type=hidden name=elem size=32 value=\"$this->nID\" />
           <input type=hidden name=userstate  value=\"$this->nState\" />
           <center>
           <table width=\"60%\">
             <tr>
               <td width=\"50%\">  
                   pseudo:
               </td>
               <td>
                  <input type=text name=userpseudo style='width:100px;' 
                         maxlength=19  value=\"$this->szPseudo\" />
               </td>
             <tr>
               <td>  
                   password: 
               </td>
               <td>  
                  <input type=text name=userpassword style='width:100px;' 
                         maxlength=19  value=\"$this->szPassword\" />
               </td>           
             </tr>
             <tr>
               <td>
                   email:
               </td>
               <td>  
                  <input type=text name=useremail style='width:200px;' 
                          maxlength=49  value=\"$this->szemail\" />
               </td>
             </tr>
         ";
       if ($state & $USER_ADMIN) {
          echo "
          <tr>
            <td>
              Niveau:
            </td>
            <td>
              <table><tr><td witdth=\"50%\">
              Administrateur: </td><td>
              <input type=\"checkbox\" name=\"admin\" 
                     value=\"$USER_ADMIN\" onClick='changeState(this.form)'";
          if ($this->nState & $USER_ADMIN)
             echo " checked ";
          echo " /> 
              </td></tr><tr><td>
              Mod&eacute;rateur: </td><td>
              <input type=\"checkbox\" name=\"moderateur\" 
                     value=\"$MODERATOR\" onClick='changeState(this.form)'";
          if ($this->nState & $MODERATOR)
             echo " checked ";

          echo " />
              </td></tr></table>
            </td>
          </tr> 
          ";
       }
       echo "
         </table>
           </center>
           <br/>
         ";
    }

   /**************************************************************************
    *  This function close the form.
    **************************************************************************/
    function FinFormulaire ()
    {
       echo "<center><input type=submit value=valider /></center><br></form>";
    }

   /**************************************************************************
    *  JavaScript function used to set the state variable.
    **************************************************************************/
   function InputJavaScript ()
   {
      echo "<script language=\"JavaScript1.1\">
        <!--
        function changeState(user_form)
        {
           var state = 0;
           var mod = 0, adm = 0;
           if (user_form.moderateur.checked)
              mod = Number (user_form.moderateur.value);

           if (user_form.admin.checked)
              adm = Number (user_form.admin.value);

           state = adm + mod;
           user_form.userstate.value = state;
           //alert(user_form.state.value);
        }
        // -->
        </script>\n";
   }
}


?>