<?php
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x source code                                     */
/*                                                                      */
/* NPDS Copyright (c) 2002-2011 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 () {
  include("admin/die.php");
}
if (!stristr($_SERVER['PHP_SELF'],"admin.php")) { Access_Error(); }

global $language;
$hlpfile = "manuels/$language/surveys.html";
$result = sql_query("select radminsurvey, radminsuper from ".$NPDS_Prefix."authors where aid='$aid'");
list($radminsurvey, $radminsuper) = sql_fetch_row($result);
if (($radminsurvey!=1) AND ($radminsuper!=1)) {
   Access_Error();
}

function poll_createPoll() {
   global $hlpfile, $maxOptions;
   include ('header.php');
   GraphicAdmin($hlpfile);
   opentable();
   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>";

   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 "<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>
         <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;
   global $NPDS_Prefix;

   $timeStamp = time();
   $pollTitle = FixQuotes($pollTitle);
   $result = sql_query("INSERT INTO ".$NPDS_Prefix."poll_desc VALUES (NULL, '$pollTitle', '$timeStamp', 0)");
   $object = sql_fetch_assoc(sql_query("SELECT pollID FROM ".$NPDS_Prefix."poll_desc WHERE pollTitle='$pollTitle'"));
   $id = $object['pollID'];
   for ($i = 1; $i <= sizeof($optionText); $i++) {
      if ($optionText[$i] != "")
         $optionText[$i] = FixQuotes($optionText[$i]);
      $result = sql_query("INSERT INTO ".$NPDS_Prefix."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;
   global $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("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><td class=\"ongl\">&nbsp;</td></tr>";
   echo "<form action=\"admin.php\" method=\"post\">
         <input type=\"hidden\" name=\"op\" value=\"removePosted\" />";
   $result = sql_query("SELECT pollID, pollTitle FROM ".$NPDS_Prefix."poll_desc ORDER BY timeStamp");
   while ($object = sql_fetch_assoc($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("Retirer")."\" />";
   echo "</form></td></tr></table></td></tr></table>";
   include ('footer.php');
}

function poll_removePosted() {
   global $id, $setCookies;
   global $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'";
      sql_query($sql);
   }
   sql_query("DELETE FROM ".$NPDS_Prefix."poll_desc WHERE pollID='$id'");
   sql_query("DELETE FROM ".$NPDS_Prefix."poll_data WHERE pollID='$id'");
   Header("Location: admin.php?op=adminMain");
}

function poll_editPoll() {
   global $hlpfile;
   global $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("Edition des sondages");
   echo "</td></tr></table>\n";
   echo "<br />";
   $result = sql_query("SELECT pollID, pollTitle, timeStamp FROM ".$NPDS_Prefix."poll_desc ORDER BY timeStamp");
   echo "<form action=\"admin.php\" method=\"post\">";
   echo "<input type=\"hidden\" name=\"op\" value=\"editpollPosted\" />";
   echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
   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 = sql_fetch_assoc($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;
   global $NPDS_Prefix;

   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 = sql_query("SELECT pollID, pollTitle, timeStamp FROM ".$NPDS_Prefix."poll_desc WHERE pollID='$id'");
      $holdtitle = sql_fetch_row($result);
      $result = sql_query("SELECT optionText, voteID, pollType FROM ".$NPDS_Prefix."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) = sql_fetch_row($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=\"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=\"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;
   global $NPDS_Prefix;

   $result = sql_query("UPDATE ".$NPDS_Prefix."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 = sql_query("UPDATE ".$NPDS_Prefix."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;
}
?>