<?PHP
/************************************************************************/
/* NPDS V : Net Portal Dynamic System .                                 */
/* ===========================                                          */
/*                                                                      */
/* Original Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)   */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* 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.       */
/************************************************************************/
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/surveys.html";
$result = mysql_query("select radminsurvey, radminsuper from authors where aid='$aid'");
list($radminsurvey, $radminsuper) = mysql_fetch_row($result);
if (($radminsurvey!=1) AND ($radminsuper!=1)) {
   Access_Error();
}

function poll_createPoll() {
   global $hlpfile, $maxOptions;
   include ('header.php');
   GraphicAdmin($hlpfile);
   OpenTable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
   echo adm_translate("Créer un nouveau Sondage");
   echo "</td></tr></table>\n";
   echo "<br />";
   echo "<p align=\"center\"><a href=\"admin.php?op=remove\" class=\"ROUGE\">".adm_translate("Effacer les Sondages")."</a> -
         <a href=\"admin.php?op=editpoll\" class=\"NOIR\">".adm_translate("Editer les Sondages")."</a></p>
         <form action=\"admin.php\" method=\"post\">
         <input type=\"hidden\" name=\"op\" value=\"createPosted\">
         <p><b>".adm_translate("Intitulé du Sondage")." : </b><input class=\"TEXTBOX_STANDARD\" type=\"text\" name=\"pollTitle\" size=\"50\" maxlength=\"100\"></p>
         <br />
         <p align=\"center\">".adm_translate("S.V.P. entrez chaque option disponible dans un seul champ")."</p>";
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">\n";
   for ($i = 1; $i <= $maxOptions; $i++) {
      $rowcolor = tablos();
      echo "<tr $rowcolor>";
      echo "<td width=\"20%\">".adm_translate("Option")." $i :</td><td><input class=\"textbox\" type=\"text\" name=\"optionText[$i]\" size=\"51\" maxlength=\"255\"></td>";
      echo "</tr>";
   }
   echo "<tr>";
   echo "<td>".adm_translate("Seulement aux membres")."</td><td><input type=\"checkbox\" name=\"poll_type\" value=\"1\"></td></tr>";
   echo "</table>";
   echo "<input class=\"BOUTON_STANDARD\" type=\"submit\" value=\"".adm_translate("Créer")."\">";
   echo "</form>";
   closetable();
   include ('footer.php');
}

function poll_createPosted() {
   global $maxOptions, $pollTitle, $optionText, $poll_type;
   $timeStamp = time();
   $pollTitle = FixQuotes($pollTitle);
   $result = mysql_query("INSERT INTO poll_desc VALUES (NULL, '$pollTitle', '$timeStamp', 0)");
   $object = mysql_fetch_object(mysql_query("SELECT pollID FROM poll_desc WHERE pollTitle='$pollTitle'"));
   $id = $object->pollID;
   for ($i = 1; $i <= sizeof($optionText); $i++) {
      if ($optionText[$i] != "")
         $optionText[$i] = FixQuotes($optionText[$i]);
      $result = mysql_query("INSERT INTO poll_data (pollID, optionText, optionCount, voteID, pollType) VALUES ('$id', '$optionText[$i]', 0, '$i', '$poll_type')");
   }
   Header("Location: admin.php?op=adminMain");
}

function poll_removePoll() {
   global $hlpfile;
   include ('header.php');
   GraphicAdmin($hlpfile);
   OpenTable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
   echo adm_translate("Retirer un Sondage existant");
   echo "</td></tr></table>\n";
   echo "<br />";
   echo "<p align=\"center\"><span class=\"ROUGE\">".adm_translate("ATTENTION : Le Sondage choisi va être supprimé IMMEDIATEMENT de la base de données !")."</span></p>
         <br /><table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
   echo "<tr $rowcolor><td class=\"ONGL\">".adm_translate("S.V.P. Choisissez un sondage dans la liste suivante.")."</td></tr>";
   echo "<form action=\"admin.php\" method=\"post\">
         <input type=\"hidden\" name=\"op\" value=\"removePosted\"><table>";
   $result = mysql_query("SELECT pollID, pollTitle, timeStamp FROM poll_desc ORDER BY timeStamp");
   while ($object = mysql_fetch_object($result)) {
      $pollID = $object->pollID;
      echo "<tr><td><input type=\"radio\" name=\"id\" value=\"".$object->pollID."\">".$object->pollTitle."</td></tr>";
   }
   echo "</table><br />";
   echo "<input class=\"BOUTON_STANDARD\" type=\"submit\" value=\"".adm_translate("Retirer")."\">";
   echo "</form></td></tr></table></td></tr></table>";
   include ('footer.php');
}

function poll_removePosted() {
   global $id, $setCookies, $NPDS_Prefix;
   // ----------------------------------------------------------------------------
   // Specified the index and the name off the application for the table appli_log
   $al_id = 1;
   $al_nom = "Poll";
   // ----------------------------------------------------------------------------
   if ($setCookies=="1"){
      $sql="DELETE FROM ".$NPDS_Prefix."appli_log where al_id='$al_id' and al_subid='$id'";
      mysql_query($sql);
   }
   mysql_query("DELETE FROM poll_desc WHERE pollID='$id'");
   mysql_query("DELETE FROM poll_data WHERE pollID='$id'");
   Header("Location: admin.php?op=adminMain");
}

function poll_editPoll() {
   global $hlpfile;
   include ('header.php');
   GraphicAdmin($hlpfile);
   OpenTable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
   echo adm_translate("Edition des sondages");
   echo "</td></tr></table>\n";
   echo "<br />";
   $result = mysql_query("SELECT pollID, pollTitle, timeStamp FROM poll_desc ORDER BY timeStamp");
   echo "<form action=\"".basename($GLOBALS[PHP_SELF])."\" method=\"post\">";
   echo "<input type=\"hidden\" name=\"op\" value=\"editpollPosted\">";
   echo "<table width=\"100%\">";
   echo "<tr><td class=\"ONGL\">".adm_translate("S.V.P. Choisissez un sondage dans la liste suivante.")."</td><td class=\"ONGL\">&nbsp;</td></tr>";
   while ($object = mysql_fetch_object($result)) {
      $rowcolor=tablos();
      echo "<tr $rowcolor><td><input type=\"radio\" name=\"id\" value=\"".$object->pollID."\">".$object->pollTitle."</td><td>ID : $object->pollID</td></tr>";
   }
   echo "</table><br />";
   echo "<input class=\"BOUTON_STANDARD\" type=\"submit\" value=\"".adm_translate("Editer")."\">";
   echo "</form>";
   CloseTable();
   include ('footer.php');
}

function poll_editPollPosted() {
   global $id, $maxOptions;
   if ($id) {
      global $hlpfile;
      include ('header.php');
      GraphicAdmin($hlpfile);
      OpenTable();
      echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
      echo adm_translate("Edition des sondages");
      echo "</td></tr></table>\n";
      echo "<br />";
      $result = mysql_query("SELECT pollID, pollTitle, timeStamp FROM poll_desc WHERE pollID='$id'");
      $holdtitle = mysql_fetch_row($result);
      $result = mysql_query("SELECT optionText, voteID, pollType FROM poll_data WHERE pollID='$id' order by voteID asc");
      echo "<form method=\"post\" action=\"admin.php\"><input type=\"hidden\" name=\"op\" value=\"SendEditPoll\"><input type=\"hidden\" name=\"pollID\" value=\"$id\">";
      echo "<p><b>".adm_translate("Intitulé du Sondage")." : </b><input class=\"TEXTBOX_STANDARD\" type=\"text\" name=\"pollTitle\" value=\"$holdtitle[1]\" size=\"50\" maxlength=\"100\"></p>";
      echo "<br />
      <p align=\"center\">".adm_translate("S.V.P. entrez chaque option disponible dans un seul champ")."</p>";
      echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
      for ($i = 1; $i <= $maxOptions; $i++) {
         $rowcolor = tablos();
         echo "<tr $rowcolor>";
         list($optionText, $voteID, $pollType) = mysql_fetch_array($result);
         echo "<td width=\"20%\">".adm_translate("Option")." $i :</td><td><input class=\"textbox\" type=\"text\" name=\"optionText[$voteID]\" size=\"51\" maxlength=\"255\" value=\"$optionText\"></td>";
         echo "</tr>";
      }
      $pollClose = (($pollType / 128) >= 1 ? 1 : 0);
      $pollType = $pollType%128;
      echo "<tr><td colspan=\"1\">".adm_translate("Seulement aux membres")." :</td><td><input type=\"checkbox\" name=\"poll_type\"";
      if ($pollType == "1") echo "checked";
      echo " value=\"1\"></td></tr>";
      echo "<tr><td colspan=\"1\">".adm_translate("Vote fermé")." :</td><td><input type=\"checkbox\" name=\"poll_close\" ";
      if ($pollClose == 1) echo "checked";
      echo " value=\"1\"></td></tr></table>";
      echo "<input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"valide\" value=\"Ok\">";
      echo "</form>";
      CloseTable();
      include ('footer.php');
   } else {
      header("location: admin.php?op=editpoll");
   }
}

function poll_SendEditPoll() {
   global $maxOptions, $pollTitle, $optionText, $poll_type, $pollID, $poll_close;
   $result = mysql_query("UPDATE poll_desc SET pollTitle='$pollTitle' where pollID='$pollID'");
   $poll_type = $poll_type + 128 * $poll_close;
   for ($i = 1; $i <= sizeof($optionText); $i++) {
      if ($optionText[$i] != "")
         $optionText[$i] = FixQuotes($optionText[$i]);
      $result = mysql_query("UPDATE poll_data set optionText='$optionText[$i]', pollType='$poll_type' where pollID='$pollID' and voteID='$i'");
   }
   Header("Location: admin.php?op=create");
}

switch($op) {
   case "create":
        poll_createPoll();
        break;

   case "createPosted":
        poll_createPosted();
        break;

   case "remove":
        poll_removePoll();
        break;

   case "removePosted":
        poll_removePosted();
        break;

   case "editpoll":
        poll_editPoll();
        break;

   case "editpollPosted":
        poll_editPollPosted();
        break;

   case "SendEditPoll":
        poll_SendEditPoll();
        break;
}
?>
