<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */
  define("_VALID_PHP", true);
  
  require_once("../../init.php");
  if (!$user->is_Admin())
      redirect_to("../../login.php");
  
$filename = $_SERVER['DOCUMENT_ROOT'] . "/admin/modules/poll/lang/" . $core->language . ".lang.php";
 
 if (file_exists($filename)) {
 require_once("lang/" . $core->language . ".lang.php");
 } else {
 require_once("lang/en.lang.php");
 }
 
  require("admin_class.php");
  
  $poll = new poll();
?>
<?php
  /* Add Poll*/
  if (isset($_POST['addPoll'])):
  $poll->addPoll();
  endif;
?>
<?php
  /* Update Poll*/
  if (isset($_POST['updatePoll'])):
  $poll->pollid = (isset($_POST['pollid'])) ? $_POST['pollid'] : 0; 
  $poll->updatePoll();
  endif;
?>
<?php
  /* Sort poll*/
  if (isset($_GET['sortpoll']) && $_GET['sortpoll'] == 1) :
      foreach ($_GET['input'] as $k => $v) :
          $p = $k + 1;
          
          $data['position'] = $p;
          
          $db->update("mod_poll_options", $data, "id='" . intval($v) . "'");
      endforeach;
 endif;
?>
<?php
  /* Delete poll*/
  if (isset($_POST['deletePoll'])):
  
  $id = sanitize($_POST['deletePoll']);
  
  $action = $db->delete("mod_poll_questions", "id='" . (int)$id . "'");
  $db->delete("mod_poll_votes", "option_id IN(SELECT id FROM mod_poll_options WHERE question_id='" . (int)$id . "')");
  $db->delete("mod_poll_options", "question_id='" . (int)$id . "'");

  $title = sanitize($_POST['polltitle']);
  
  print ($action) ? $zazavisec->writeLog(MOD_PL_POLL .' <strong>'.$title.'</strong> '._DELETED, "", "no", "module") . $core->msgOk(MOD_PL_POLL .' <strong>'.$title.'</strong> '._DELETED) : $core->msgAlert(_SYSTEM_PROCCESS); 
  endif;
?>