<?PHP
/************************************************************************/
/* NPDS V : Net Portal Dynamic System .                                 */
/* ===========================                                          */
/*                                                                      */
/* This version name NPDS Copyright (c) 2001-2005 by Philippe Brunier   */
/*                                                                      */
/* 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.       */
/************************************************************************/
/*                                                                      */
/* Mods by jules for NPDS SABLE                                         */
/*                                                                      */
/************************************************************************/
function Access_Error () {
  if (file_exists("meta/meta.php"))
     include ("meta/meta.php");
  echo "<title>NPDS</title></head><body bgcolor=\"white\"><br /><p align=\"center\"><span style=\"font-size: 14px; font-family: Arial; font-weight: bold; color: red;\">";
  echo "Access Denied / Acc&egrave;s Refus&eacute;";
  if (cur_charset=="utf-8") echo" / &#x901A;&#x5165;&#x88AB;&#x5426;&#x8BA4;";
  echo "</span>";
  echo "<br /><br /><span style=\"font-size: 12px; font-family: Arial; font-weight: bold; color: black;\">NPDS - Portal System";
  echo "</span></p></body></html>";
  die();
}
if (!eregi("admin.php", $PHP_SELF)) { Access_Error(); }
global $pdst; $pdst="0";

global $language;
$hlpfile = "manuels/$language/groupes.html";
$result = mysql_query("select radminsdv, radminsuper from authors where aid='$aid'");
list($radminsdv, $radminsuper) = mysql_fetch_row($result);
if (($radminsdv!=1) AND ($radminsuper!=1)) {
   Access_Error();
}

function group_liste() {
   global $hlpfile, $NPDS_Prefix;
   include ('header.php');
   GraphicAdmin($hlpfile);
   opentable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
   echo adm_translate("Liste des groupes");
   echo "</td></tr></table>\n";
   echo "<br />";
   $result = mysql_query("select uid, groupe from users_status where groupe!='' order by uid ASC");
   echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
   $one_gp=false;
   while(list($uid, $groupe) = mysql_fetch_row($result)) {
      $one_gp=true;
      $tab_groupe=explode(",",$groupe);
      if ($tab_groupe) {
         foreach($tab_groupe as $groupevalue) {
            if ($groupevalue!="") {
               $tab_groupeII[$groupevalue].=$uid." ";
               $tab_groupeIII[$groupevalue]=$groupevalue;
            }
         }
      }
   }
   include_once ("lib/togglediv.class.php");
   $toggle = new ToggleDiv(count($tab_groupeIII));
   echo "<tr><td align=\"left\" width=\"5%\" class=\"ONGL\">".adm_translate("ID")."</td><td width=\"10%\" class=\"ONGL\">".adm_translate("Nom")."</td><td width=\"75%\" class=\"ONGL\">".adm_translate("Liste des membres")."</td><td align=\"right\" width=\"10%\" class=\"ONGL\">&nbsp;</td></tr>";
   if ($one_gp) {
      sort($tab_groupeIII);
      while (list($bidon,$gp)=each($tab_groupeIII)) {
         $rowcolor = tablos();
         echo "<tr $rowcolor><td align=\"left\" width=\"5%\">$gp</td><td align=\"left\" width=\"20%\">";
         // chargement de la table groupe avec les groupes déjà présent
         $result=mysql_fetch_array(mysql_query("select groupe_id, groupe_name from ".$NPDS_Prefix."groupes where groupe_id='$gp'"));
         if (!$result) {
            mysql_query("insert into ".$NPDS_Prefix."groupes values ('$gp', 'groupe $gp','')");
         }
         echo "<b>".$result[groupe_name]."</b>";
         echo "<br /><a href=\"admin.php?op=groupe_edit&amp;groupe_id=$gp\" class=\"NOIR\" style=\"font-size: 9px;\">".adm_translate("Editer")."</a> | <a href=\"admin.php?op=groupe_maj&amp;groupe_id=$gp&amp;sub_op=".adm_translate("Supprimer")."\" class=\"ROUGE\" style=\"font-size: 9px;\">".adm_translate("Effacer")."</a>";
         echo "</td><td>";
         echo $toggle->Img();
         echo $toggle->Begin();
         $tab_groupe=explode(" ",ltrim($tab_groupeII[$gp]));
         $cptX=0;
         echo "<table width=\"100%\" cellpadding=\"2\" cellspacing=\"1\" border=\"0\">";
         while (list($bidon,$uidX)=each($tab_groupe)) {
            if ($uidX) {
               if ($cptX==0) {echo "<tr>";}
               list($uname)=mysql_fetch_row(mysql_query("select uname from users where uid='$uidX'"));
               echo "<td width=\"20%\" align=\"left\">&nbsp;<a href=\"admin.php?chng_uid=$uidX&amp;op=modifyUser\" class=\"NOIR\">$uname</a>&nbsp;";
               echo "<a href=\"admin.php?op=retiredugroupe&amp;uid=$uidX&amp;groupe_id=$gp\" class=\"ROUGE\" style=\"font-size: 10px;\">X</a>&nbsp;</td>";
               $cptX++;
               if ($cptX>4) {echo "</tr>\n";$cptX=0;}
            }
         }
         if ($cptX<=4) {echo "</tr>";}
         echo "</table>";
         echo $toggle->End();
         echo "</td>
         <td align=\"right\" nowrap=\"nowrap\">
         <a href=\"admin.php?op=membre_add&amp;groupe_id=$gp\" CLASS=\"NOIR\" style=\"font-size: 10px;\">".adm_translate("Ajouter des membres")."</a>";
         echo "</td></tr>";
      }
   }
   // liste des groupes sans membre
   $result=mysql_query("select groupe_id, groupe_name, groupe_description from ".$NPDS_Prefix."groupes order by groupe_id ASC");
   while (list($gp, $gp_name, $gp_description)=mysql_fetch_array($result)) {
      $gpA=true;
      if ($tab_groupeIII) {
         reset ($tab_groupeIII);
         while (list($bidon,$gpU)=each($tab_groupeIII)) {
            if ($gp==$gpU) {$gpA=false;}
         }
      }
      if ($gpA) {
         $rowcolor = tablos();
         echo "<tr $rowcolor><td align=\"left\" width=\"5%\"><span class=\"ROUGE\">$gp</span></td><td align=\"left\" width=\"20%\">";
         echo $gp_name;
         echo "<br /><a href=\"admin.php?op=groupe_edit&amp;groupe_id=$gp\" class=\"NOIR\" style=\"font-size: 9px;\">".adm_translate("Editer")."</a> | <a href=\"admin.php?op=groupe_maj&amp;groupe_id=$gp&amp;sub_op=".adm_translate("Supprimer")."\" class=\"ROUGE\" style=\"font-size: 9px;\">".adm_translate("Effacer")."</a>";
         echo "</td><td>&nbsp;</td>
         <td align=\"right\" nowrap=\"nowrap\">
         <a href=\"admin.php?op=membre_add&amp;groupe_id=$gp\" CLASS=\"NOIR\" style=\"font-size: 10px;\">".adm_translate("Ajouter des membres")."</a>";
         echo "</td></tr>";
      }
   }
   echo "</table>";
   echo "<br />";
   echo "<form action=\"admin.php\" method=\"post\"><input type=\"hidden\" name=\"op\" value=\"groupe_add\">";
   echo "<input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"submit\" value=\"".adm_translate("Ajouter un groupe")."\">";
   echo "</form>";
   closetable();
   include ('footer.php');
}

function retiredugroupe($groupe_id,$uid) {
   $resultat = mysql_query("select groupe from users_status where uid='$uid'");
   $valeurs=mysql_fetch_array($resultat);
   $lesgroupes=split(",",$valeurs[groupe]);
   $nbregroupes=count($lesgroupes); 
   $groupesmodif="";
   for ($i=0; $i<$nbregroupes;$i++) {
      if ($lesgroupes[$i]!=$groupe_id) {
         if ($groupesmodif=="") $groupesmodif.=$lesgroupes[$i];
         else $groupesmodif.=",".$lesgroupes[$i];
      }
   }
   $resultat = mysql_query("update users_status set groupe='$groupesmodif' where uid='$uid'");
   Header("Location: admin.php?op=groupes");
}

function groupe_edit($groupe_id) {
   global $hlpfile, $NPDS_Prefix;
   include ('header.php');
   GraphicAdmin($hlpfile);
   opentable();
   $result=mysql_fetch_array(mysql_query("select groupe_name, groupe_description from ".$NPDS_Prefix."groupes where groupe_id='$groupe_id'"));
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
   if ($groupe_id!="groupe_add")
      echo adm_translate("Groupe")." : $groupe_id";
   echo "</td></tr></table>\n";
   echo "<br />";
   echo "<form action=\"admin.php\" method=\"post\">";
   if ($groupe_id!="groupe_add")
      echo "<input type=\"hidden\" name=\"groupe_id\" value=\"$groupe_id\">";
   else
      echo "ID : <input type=\"text\" class=\"TEXTBOX_STANDARD\" name=\"groupe_id\" value=\"\" size=\"4\" maxlength=\"3\">&nbsp;&nbsp;";

   echo adm_translate("Nom")." : <input class=\"TEXTBOX_STANDARD\" type=\"text\" name=\"groupe_name\" size=\"31\" maxlengtht=\"30\" value=\"".$result[groupe_name]."\"><br /><br />\n
   ".adm_translate("Description :  (255 caractères max)")." :<br /><textarea class=\"TEXTBOX_STANDARD\" name=\"groupe_description\" cols=\"70\" rows=\"15\">".$result[groupe_description]."</textarea><br /><br />\n";
   if ($groupe_id!="groupe_add")
      echo "<input type=\"hidden\" name=\"op\" value=\"groupe_maj\">";
   else
      echo "<input type=\"hidden\" name=\"op\" value=\"groupe_add_finish\">";
   echo "<input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"sub_op\" value=\"".adm_translate("Sauver les modifications")."\">";
   echo "</form>";
   closetable();
   include ('footer.php');
}

function groupe_maj() {
   global $hlpfile, $NPDS_Prefix;
   global $groupe_id, $groupe_name, $groupe_description, $sub_op;

   if ($sub_op==adm_translate("Sauver les modifications")) {
      mysql_query("update ".$NPDS_Prefix."groupes set groupe_name='$groupe_name', groupe_description='$groupe_description' where groupe_id='$groupe_id'");
   }
   if ($sub_op==adm_translate("Supprimer")) {
      $result = mysql_query("select uid, groupe from users_status where groupe!='' order by uid ASC");
      $maj_ok=true;
      while (list($to_userid, $groupeX) = mysql_fetch_row($result)) {
         $tab_groupe=explode(",",$groupeX);
         if ($tab_groupe) {
            foreach($tab_groupe as $groupevalue) {
               if ($groupevalue==$groupe_id) {
                  $maj_ok=false;
                  break;
               }
            }
         }
      }
      if ($maj_ok) mysql_query("delete from ".$NPDS_Prefix."groupes where groupe_id='$groupe_id'");
   }
   Header("Location: admin.php?op=groupes");
}

function membre_add($gp) {
   global $hlpfile, $NPDS_Prefix;
   include ('header.php');
   GraphicAdmin($hlpfile);
   opentable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
   echo adm_translate("Ajouter des membres")." / ".adm_translate("Groupe")." : $gp";
   echo "</td></tr></table>\n";
   echo "<br />";
   echo "<form action=\"admin.php\" method=\"post\">";
   echo adm_translate("Liste des membres")." : <input type=\"text\" class=\"TEXTBOX_STANDARD\" name=\"luname\" value=\"\" size=\"80\" maxlength=\"255\">&nbsp;&nbsp;";
   echo "<input type=\"hidden\" name=\"op\" value=\"membre_add_finish\">";
   echo "<input type=\"hidden\" name=\"groupe_id\" value=\"$gp\">";
   echo "<input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"sub_op\" value=\"".adm_translate("Sauver les modifications")."\">";
   echo "</form>";
   closetable();
   include ('footer.php');
}

function membre_add_finish($groupe_id,$luname) {
   $list_membres=split(",",$luname);
   $nbremembres=count($list_membres);

   for ($j=0;$j<$nbremembres;$j++) {
      $uname=$list_membres[$j];
      $result1 = mysql_query("select uid from users where uname='$uname'");
      $ibid=mysql_fetch_array($result1);
      if ($ibid[uid]) {
         $result2 = mysql_query("select groupe from users_status where uid='$ibid[uid]'");
         $ibid2=mysql_fetch_array($result2);
         $lesgroupes=split(",",$ibid2[groupe]);
         $nbregroupes=count($lesgroupes); 
         $groupeexistedeja=false;
         for ($i=0; $i<$nbregroupes;$i++) {
            if ($lesgroupes[$i]==$groupe_id) { $groupeexistedeja=true; break; }
         }
         if (!$groupeexistedeja) {
            if ($nbregroupes) $groupesmodif=$ibid2[groupe].",".$groupe_id;
            else $groupesmodif=$groupe_id;
            $resultat = mysql_query("update users_status set groupe='$groupesmodif' where uid='$ibid[uid]'");
         }
      }
   }
   Header("Location: admin.php?op=groupes");
}

switch($op) {
   case "membre_add":
        membre_add($groupe_id);
        break;

   case "membre_add_finish":
        membre_add_finish($groupe_id,$luname);
        break;

   case "retiredugroupe":
        retiredugroupe($groupe_id,$uid);
        break;

   case "groupe_edit":
        groupe_edit($groupe_id);
        break;

   case "groupe_maj":
        groupe_maj();
        break;

   case "groupe_add":
        groupe_edit("groupe_add");
        break;

   case "groupe_add_finish":
        if (($groupe_id!="") and ($groupe_id>"1") and ($groupe_id<"127"))
           mysql_query("insert into ".$NPDS_Prefix."groupes values ('$groupe_id', '$groupe_name','$groupe_description')");

   default:
        group_liste();
        break;
}
?>
