plugins/Surveyor.php
author Dan
Sat, 29 Nov 2008 01:24:30 -0500
changeset 1 97507d2ca929
parent 0 e96ded22104a
child 2 f1ff9ce00132
permissions -rw-r--r--
Updated header and metadata blocks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
     1
<?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
     2
/*
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
     3
Plugin Name: Survey/Poll plugin
1
97507d2ca929 Updated header and metadata blocks
Dan
parents: 0
diff changeset
     4
Plugin URI: http://enanocms.org/Survey_plugin
0
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
     5
Description: Adds a customizable poll to your sidebar. You can have any number of options, and the poll is randomly selected from a list of enabled polls. <b>Important:</b> When first loaded, this plugin creates the following tables in your Enano database: enano_polls, enano_poll_options, enano_poll_results
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
     6
Author: Dan Fuhry
1
97507d2ca929 Updated header and metadata blocks
Dan
parents: 0
diff changeset
     7
Version: 0.3
97507d2ca929 Updated header and metadata blocks
Dan
parents: 0
diff changeset
     8
Author URI: http://enanocms.org/
0
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
     9
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    10
Changelog:
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    11
  9/27/06:
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    12
  Updated to be valid XHTML 1.1
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    13
  11/2/07:
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    14
  Made compatible with Loch Ness and later (oops!)
1
97507d2ca929 Updated header and metadata blocks
Dan
parents: 0
diff changeset
    15
  11/29/08:
97507d2ca929 Updated header and metadata blocks
Dan
parents: 0
diff changeset
    16
  One change a year! Moved to Mercurial and brought up to date with naming conventions.
0
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    17
*/
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    18
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    19
/*
1
97507d2ca929 Updated header and metadata blocks
Dan
parents: 0
diff changeset
    20
 * Surveyor
97507d2ca929 Updated header and metadata blocks
Dan
parents: 0
diff changeset
    21
 * Version 0.3
0
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    22
 * Copyright (C) 2006-2007 Dan Fuhry
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    23
 *
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    24
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    25
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    26
 *
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    27
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    28
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    29
 */
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    30
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    31
global $db, $session, $paths, $template, $plugins; // Common objects
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    32
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    33
// Uncomment this line once the plugin has been enabled for the first time and at least one page has been requested
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    34
define('ENANO_SURVEYOR_TABLES_CREATED', 'true');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    35
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    36
  if(!defined('ENANO_SURVEYOR_TABLES_CREATED')) {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    37
  $e = $db->sql_query('CREATE TABLE IF NOT EXISTS '.table_prefix.'polls(
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    38
                         poll_id mediumint(5) NOT NULL auto_increment,
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    39
                         poll_question text,
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    40
                         end_time datetime,
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    41
                         enabled tinyint(1),
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    42
                         PRIMARY KEY (poll_id)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    43
                       );');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    44
  if(!$e) $db->_die('Surveyor plugin: error creating table '.table_prefix.'polls.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    45
  
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    46
  $e = $db->sql_query('CREATE TABLE IF NOT EXISTS '.table_prefix.'poll_options(
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    47
                         item_id mediumint(5) NOT NULL auto_increment,
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    48
                         poll_id mediumint(5) NOT NULL,
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    49
                         option_value text,
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    50
                         PRIMARY KEY (item_id)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    51
                       );');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    52
  if(!$e) $db->_die('Surveyor plugin: error creating table '.table_prefix.'poll_options.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    53
  
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    54
  $e = $db->sql_query('CREATE TABLE IF NOT EXISTS '.table_prefix.'poll_results(
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    55
                         poll_id mediumint(5),
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    56
                         item_id mediumint(5),
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    57
                         user_id mediumint(8),
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    58
                         ip_addr varchar(10)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    59
                       );');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    60
  if(!$e) $db->_die('Surveyor plugin: error creating table '.table_prefix.'poll_results.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    61
  
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    62
}
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    63
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    64
class Surveyor_Plugin {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    65
  var $header_added;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    66
  function html($pid = false)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    67
  {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    68
    global $db, $session, $paths, $template, $plugins; // Common objects
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    69
    $s = '';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    70
    if(is_int($pid)) $s = ' AND p.poll_id='.$pid;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    71
    $ret = '';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    72
    if(!is_int($pid)) $ret .= '<div id="mdgVotePlugin" style="padding: 5px;">';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    73
    $ret .= '<form id="survey" action="'.makeUrlNS('Special', 'SubmitVote').'" method="post"><div>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    74
    $q = $db->sql_query('SELECT p.poll_id AS pid,o.item_id AS oid,p.poll_question AS q,o.option_value AS v FROM '.table_prefix.'polls p, '.table_prefix.'poll_options o WHERE p.poll_id=o.poll_id AND p.enabled=1'.$s.';');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    75
    if(!$q) $db->_die('An error occurred whilst selecting the poll data.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    76
    $l = Array();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    77
    while($row = $db->fetchrow())
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    78
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    79
      if(!isset($l[$row['q']]))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    80
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    81
        $l[$row['q']] = Array();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    82
        $l[$row['q']]['pid'] = $row['pid'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    83
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    84
      $l[$row['q']][] = $row;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    85
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    86
    if(sizeof($l) < 1) return 'No polls created yet';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    87
    $ques = array_rand($l);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    88
    $poll_id = $l[$ques]['pid'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    89
    unset($l[$ques]['pid']);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    90
    if(!$poll_id) die_semicritical('Surveyor plugin error', 'Invalid poll ID: '.$poll_id);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    91
    $q = $db->sql_query('SELECT * FROM '.table_prefix.'poll_results WHERE poll_id='.$poll_id.' AND ( ip_addr=\''.mysql_real_escape_string(ip2hex($_SERVER['REMOTE_ADDR'])).'\' OR user_id='.$session->user_id.' );');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    92
    if(!$q) $db->_die('Error obtaining vote result information');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    93
    if($db->numrows() > 0)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    94
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    95
      if(!isset($_GET['results'])) $_GET['results'] = '';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    96
      $_REQUEST['poll_id'] = $poll_id.'';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    97
      $_GET['poll_id'] = $poll_id.'';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    98
      return __enanoVoteAjaxhandler(false);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
    99
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   100
    $ret .= '<input type="hidden" name="poll_id" value="'.$poll_id.'" />';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   101
    $ret .= '<span style="font-weight: bold;">'.$ques.'</span><br />';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   102
    foreach($l[$ques] as $o)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   103
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   104
      $ret .= '<label><input type="radio" name="item_id" value="'.addslashes($o['oid']).'" /> '.$o['v'].'</label><br />';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   105
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   106
    $ret .= '<br /><div style="text-align: center"><input type="button" value="Vote!" onclick="ajaxSubmitVote(); return false;" /> <input type="button" onclick="ajaxVoteResults(); return false;" value="View results" /></div>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   107
    $ret .= '</div></form>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   108
    if(!is_int($pid)) $ret .= '</div>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   109
    
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   110
    $template->add_header('
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   111
      <script type="text/javascript">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   112
      //<![CDATA[
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   113
        function ajaxSubmitVote()
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   114
        {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   115
          frm = document.forms.survey;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   116
          radios = document.getElementsByTagName(\'input\');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   117
          optlist = new Array();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   118
          j = 0;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   119
          for(i=0;i<radios.length;i++)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   120
          {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   121
            if(radios[i].name == \'item_id\')
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   122
            {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   123
              optlist[j] = radios[i];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   124
              j++;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   125
            }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   126
          }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   127
          val = \'enanoNuLl\';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   128
          for(i=0;i<optlist.length;i++)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   129
          {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   130
            if(optlist[i].checked) val = optlist[i].value;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   131
          }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   132
          if(val==\'enanoNuLl\') { alert(\'Please select an option.\'); return; }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   133
          ajaxPost(\''.makeUrlNS('Special', 'SubmitVote', 'redirect=no').'\', \'poll_id=\'+frm.poll_id.value+unescape(\'%26\')+\'item_id=\'+val, function() {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   134
              if(ajax.readyState==4)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   135
              {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   136
                ajaxVoteResults();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   137
              }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   138
            });
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   139
        }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   140
        function ajaxVoteForm()
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   141
        {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   142
          ajaxGet(\''.makeUrlNS('Special', 'SubmitVote', 'voteform\'+unescape(\'%26\')+\'poll_id='.$poll_id).'\', function() {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   143
              if(ajax.readyState==4)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   144
              {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   145
                document.getElementById("mdgVotePlugin").innerHTML = ajax.responseText;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   146
              }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   147
            });
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   148
        }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   149
        function ajaxVoteResults()
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   150
        {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   151
          ajaxGet(\''.makeUrlNS('Special', 'SubmitVote', 'results\'+unescape(\'%26\')+\'poll_id='.$poll_id).'\', function() {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   152
              if(ajax.readyState==4)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   153
              {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   154
                document.getElementById("mdgVotePlugin").innerHTML = ajax.responseText;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   155
              }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   156
            });
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   157
        }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   158
        // ]]>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   159
      </script>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   160
      ');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   161
    
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   162
    return $ret;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   163
  }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   164
}
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   165
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   166
$plugins->attachHook('base_classes_initted', '
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   167
  $paths->add_page(Array(
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   168
      \'name\'=>\'Submit a poll vote\',
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   169
      \'urlname\'=>\'SubmitVote\',
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   170
      \'namespace\'=>\'Special\',
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   171
      \'special\'=>0,\'visible\'=>0,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   172
      ));
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   173
  $paths->addAdminNode(\'Plugin configuration\', \'Manage polls\', \'PollEditor\');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   174
  ');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   175
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   176
function __mdgPluginDoSurvey() {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   177
  global $db, $session, $paths, $template, $plugins; // Common objects
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   178
  $s = new Surveyor_Plugin();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   179
  $template->sidebar_widget('Poll', $s->html());
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   180
}
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   181
$plugins->attachHook('compile_template', '__mdgPluginDoSurvey();');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   182
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   183
function page_Special_SubmitVote()
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   184
{
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   185
  echo __enanoVoteAjaxhandler();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   186
}
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   187
function __enanoVoteAjaxhandler($allow_vote = true)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   188
{
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   189
  global $db, $session, $paths, $template, $plugins; // Common objects
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   190
  $ret = '';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   191
  if(!isset($_REQUEST['poll_id'])) { die_semicritical('Critical error in plugin', '$_REQUEST[\'poll_id\'] is not set'); $paths->main_page(); exit; }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   192
  if(!preg_match('/^([0-9]+)$/', $_REQUEST['poll_id'])) die('Hacking attempt'); // Prevents SQL injection from the URL
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   193
  if(isset($_GET['results']))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   194
  {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   195
    $q = $db->sql_query('SELECT p.poll_id AS pid,o.item_id AS oid,p.poll_question AS q,o.option_value AS v FROM '.table_prefix.'polls p, '.table_prefix.'poll_options o WHERE p.poll_id=o.poll_id AND p.poll_id=\''.$_GET['poll_id'].'\';');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   196
    $l = Array();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   197
    while($row = $db->fetchrow())
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   198
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   199
      if(!isset($l[$row['q']]))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   200
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   201
        $l[$row['q']] = Array();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   202
        $l[$row['q']]['pid'] = $row['pid'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   203
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   204
      $l[$row['q']][] = $row;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   205
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   206
    // The reason we use array_rand() here? Simple - we used a WHERE clause to select only one poll, and since poll_id is
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   207
    // a primary key, there is only one match in the polls table. Therefore, array_rand() effectively returns the first key in the array
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   208
    $ques = array_rand($l);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   209
    $poll_id = $l[$ques]['pid'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   210
    unset($l[$ques]['pid']);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   211
    $results = Array();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   212
    foreach($l[$ques] as $o)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   213
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   214
      $q = $db->sql_query('SELECT * FROM '.table_prefix.'poll_results WHERE poll_id='.$_GET['poll_id'].' AND item_id='.$o['oid'].';');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   215
      if(!$q) $db->_die('The poll result data could not be selected.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   216
      $results[$o['v']] = $db->numrows();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   217
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   218
    $k = array_keys($results);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   219
    $total = 0;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   220
    foreach($k as $key)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   221
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   222
      $total = $total + $results[$key];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   223
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   224
    if($total==0) $total = 1;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   225
    // Figure out the percentage, round it, and send the images
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   226
    $ret .= '<table border="0" style="margin: 0; padding: 0; width: 100%;" cellspacing="0" cellpadding="0">';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   227
    $ret .= '<tr><td colspan="2"><b>'.$ques.'</b></td></tr>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   228
    foreach($k as $key)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   229
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   230
      $this_width = round(100*($results[$key] / $total));
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   231
      if ( $this_width == 0 )
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   232
        $this_width = 4;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   233
      $ret .= '<tr>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   234
                 <td colspan="2">'.$key.'</td>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   235
               </tr>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   236
               <tr>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   237
                 <td style="padding: 0px 4px 0px 4px;">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   238
                   <img alt="Poll bar" src="'.scriptPath.'/plugins/surveyor/poll-bar-left.png"
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   239
                    width="2" height="12" style="margin: 2px 0px 2px 0px; padding: 0;" hspace="0" 
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   240
                    
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   241
                  /><img alt="Poll bar" src="'.scriptPath.'/plugins/surveyor/poll-bar-middle.png"
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   242
                    width="'.$this_width.'" height="12" style="margin: 2px 0px 2px 0px; padding: 0;" hspace="0"
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   243
                    
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   244
                  /><img alt="Poll bar" src="'.scriptPath.'/plugins/surveyor/poll-bar-right.png"
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   245
                    width="2" height="12" style="margin: 2px 0px 2px 0px; padding: 0;" hspace="0" />
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   246
                    
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   247
                  </td>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   248
                  
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   249
                  <td>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   250
                    ['.$results[$key].']
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   251
                  </td>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   252
                </tr>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   253
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   254
    if($allow_vote) $ret .= '<tr><td colspan="2" style="text-align: center"><input type="button" value="Cast your vote" onclick="ajaxVoteForm(); return false;" /></td></tr>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   255
    $ret .= '</table>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   256
  } elseif(isset($_GET['voteform'])) {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   257
    $s = new Surveyor_Plugin();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   258
    $pid = (int)$_GET['poll_id'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   259
    $ret .= $s->html($pid);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   260
  } else {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   261
    if(!isset($_POST['item_id']) || (isset($_POST['item_id']) && !preg_match('/^([0-9]+)$/', $_POST['item_id']))) die('Hacking attempt'); // Once again, ensure that only numbers are passed on the URL
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   262
    if(isset($_GET['redirect']) && $_GET['redirect'] == 'no')
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   263
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   264
      header('Content-type: text/plain');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   265
      $q = $db->sql_query('SELECT * FROM '.table_prefix.'poll_results WHERE poll_id='.$_POST['poll_id'].' AND ( ip_addr=\''.mysql_real_escape_string(ip2hex($_SERVER['REMOTE_ADDR'])).'\' OR user_id='.$session->user_id.' );');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   266
      if(!$q) $db->_die('Error obtaining vote result information');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   267
      if($db->numrows() > 0)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   268
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   269
        die('Looks like you already voted in this poll.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   270
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   271
      $q = $db->sql_query('INSERT INTO '.table_prefix.'poll_results(poll_id,item_id,ip_addr,user_id) VALUES('.$_POST['poll_id'].', '.$_POST['item_id'].', \''.ip2hex($_SERVER['REMOTE_ADDR']).'\', '.$session->user_id.');');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   272
      if(!$q) $db->_die('Your vote could not be inserted into the results table.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   273
      $ret .= 'Your vote has been cast.';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   274
    } else {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   275
      $paths->main_page();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   276
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   277
  }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   278
  return $ret;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   279
}
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   280
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   281
function page_Admin_PollEditor()
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   282
{
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   283
  global $db, $session, $paths, $template, $plugins; if(!$session->sid_super || $session->user_level < 2) { header('Location: '.makeUrl($paths->nslist['Special'].'Administration'.urlSeparator.'noheaders')); die('Hacking attempt'); }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   284
  if(isset($_POST['newpoll_create']))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   285
  {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   286
    $date_string = $_POST['newpoll_year'].'-'.$_POST['newpoll_month'].'-'.$_POST['newpoll_day'].' '.$_POST['newpoll_hour'].':'.$_POST['newpoll_minute'].':00';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   287
    if(isset($_POST['newpoll_never']))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   288
      $date_string = '9999-01-01 00:00:00';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   289
    if(!$db->sql_query('INSERT INTO '.table_prefix.'polls(poll_question,enabled,end_time) VALUES(\''.mysql_real_escape_string($_POST['newpoll_name']).'\', 1, \''.$date_string.'\');')) $db->_die('The poll information could not be inserted.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   290
    $q = $db->sql_query('SELECT poll_id FROM '.table_prefix.'polls WHERE poll_question=\''.mysql_real_escape_string($_POST['newpoll_name']).'\' AND end_time=\''.$date_string.'\';');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   291
    if(!$q) $db->_die('The new poll ID could not be fetched.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   292
    $r = $db->fetchrow();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   293
    if(!$db->sql_query('INSERT INTO '.table_prefix.'poll_options(poll_id,option_value) VALUES('.$r['poll_id'].', \'First option\')')) $db->_die('The default option data could not be inserted.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   294
  }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   295
  
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   296
  echo '<form action="'.makeUrl($paths->nslist['Special'].'Administration', 'module='.$paths->cpage['module'], true).'" method="post">';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   297
    ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   298
    <h3>Create a new poll</h3>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   299
    <p>Question: <input name="newpoll_name" type="text" /></p>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   300
    <p>Ending time:
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   301
    <select name="newpoll_month">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   302
      <option value="01">January</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   303
      <option value="02">February</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   304
      <option value="03">March</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   305
      <option value="04">April</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   306
      <option value="05">May</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   307
      <option value="06">June</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   308
      <option value="07">July</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   309
      <option value="08">August</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   310
      <option value="09">September</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   311
      <option value="10">October</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   312
      <option value="11">November</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   313
      <option value="12">December</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   314
    </select>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   315
    <select name="newpoll_day">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   316
    <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   317
      // This would be too hard to write by hand, so let's use a simple for-loop to take care of it for us
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   318
      for($i=1;$i<=31;$i++)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   319
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   320
        if($i < 10) $t = '0'.$i;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   321
        else $t = $i.'';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   322
        echo '<option value="'.$t.'">'.$t.'</option>'."\n      "; 
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   323
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   324
    ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   325
    </select>,
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   326
    <select name="newpoll_year">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   327
    <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   328
      // What the heck? Let's do it again :-D
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   329
      for($i=2006;$i<=2026;$i++)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   330
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   331
        echo '<option value="'.$i.'">'.$i.'</option>'."\n      "; 
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   332
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   333
    ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   334
    </select>&nbsp;&nbsp;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   335
    <select name="newpoll_hour">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   336
    <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   337
      for($i=0;$i<=23;$i++)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   338
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   339
        if($i < 10) $t = '0'.$i;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   340
        else $t = $i.'';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   341
        echo '<option value="'.$t.'">'.$t.'</option>'."\n      "; 
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   342
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   343
    ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   344
    </select>:<select name="newpoll_minute">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   345
    <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   346
      for($i=0;$i<=59;$i++)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   347
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   348
        if($i < 10) $t = '0'.$i;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   349
        else $t = $i.'';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   350
        echo '<option value="'.$t.'">'.$t.'</option>'."\n      "; 
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   351
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   352
    ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   353
    </select><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input type="checkbox" name="newpoll_never" />Never ends</label></p>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   354
    
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   355
    <p><input type="submit" name="newpoll_create" value="Create poll" /></p>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   356
    <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   357
  echo '</form>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   358
  
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   359
  $q = $db->sql_query('SELECT p.poll_id AS pid,o.item_id AS oid,p.poll_question AS q,o.option_value AS v,p.end_time,p.enabled FROM '.table_prefix.'polls p, '.table_prefix.'poll_options o WHERE p.poll_id=o.poll_id;');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   360
  if(!$q) $db->_die('The poll information could not be selected.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   361
  $l = Array();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   362
  while($row = $db->fetchrow())
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   363
  {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   364
    if(!isset($l[$row['q']]))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   365
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   366
      $l[$row['q']] = Array();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   367
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   368
    $l[$row['q']][] = $row;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   369
  }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   370
  $k = array_keys($l);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   371
  foreach ( $k as $key )
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   372
  {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   373
    $c = $l[$key][0];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   374
    $poll_id = $c['pid'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   375
    $enabled = $c['enabled'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   376
    $ending_time = $c['end_time'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   377
    $year = substr($ending_time, 0, 4);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   378
    $month = substr($ending_time, 5, 2);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   379
    $day = substr($ending_time, 8, 2);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   380
    $hour = substr($ending_time, 11, 2);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   381
    $minute = substr($ending_time, 14, 2);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   382
    if(isset($_POST['poll_'.$c['pid'].'_update']))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   383
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   384
      $date_string = $_POST['poll_'.$c['pid'].'_year'].'-'.$_POST['poll_'.$c['pid'].'_month'].'-'.$_POST['poll_'.$c['pid'].'_day'].' '.$_POST['poll_'.$c['pid'].'_hour'].':'.$_POST['poll_'.$c['pid'].'_minute'].':00';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   385
      if(isset($_POST['poll_'.$c['pid'].'_never']))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   386
        $date_string = '9999-01-01 00:00:00';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   387
      $en = isset($_POST['poll_'.$c['pid'].'_enabled']) ? '1' : '0';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   388
      $q = $db->sql_query('UPDATE '.table_prefix.'polls SET enabled='.$en.',end_time=\''.$date_string.'\' WHERE poll_id='.$c['pid'].';');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   389
      if(!$q) $db->_die('The poll data could not be updated.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   390
      
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   391
      $q = $db->sql_query('SELECT p.poll_id AS pid,o.item_id AS oid,p.poll_question AS q,o.option_value AS v,p.end_time,p.enabled FROM '.table_prefix.'polls p, '.table_prefix.'poll_options o WHERE p.poll_id=o.poll_id;');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   392
      if(!$q) $db->_die('The poll information could not be selected.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   393
      $l = Array();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   394
      while($row = $db->fetchrow())
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   395
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   396
        if(!isset($l[$row['q']]))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   397
        {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   398
          $l[$row['q']] = Array();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   399
        }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   400
        $l[$row['q']][] = $row;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   401
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   402
      $k = array_keys($l);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   403
      
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   404
      echo '<h3>Information</h3><p>Poll updated successfully.</p>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   405
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   406
    if(isset($_POST['poll_'.$c['pid'].'_delete']))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   407
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   408
      // Safe to use the poll ID here because it's the primary key
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   409
      if(!$db->sql_query('DELETE FROM '.table_prefix.'poll_results WHERE poll_id='.$c['pid'].';') ) $db->_die('The poll results could not be deleted.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   410
      if(!$db->sql_query('DELETE FROM '.table_prefix.'poll_options WHERE poll_id='.$c['pid'].';') ) $db->_die('The poll options could not be deleted.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   411
      if(!$db->sql_query('DELETE FROM '.table_prefix.'polls WHERE poll_id='.$c['pid'].';')        ) $db->_die('The poll could not be deleted.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   412
      unset($l[$key]);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   413
      echo '<h3>Information</h3><p>Poll deleted.</p>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   414
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   415
  }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   416
  $k = array_keys($l); // Refresh the key list after any deletions that may have been done
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   417
  foreach ( $k as $key )
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   418
  {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   419
    if(isset($_POST['create_'.$l[$key][0]['pid']]))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   420
    {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   421
      $str = mysql_real_escape_string($_POST['value_'.$l[$key][0]['pid']]);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   422
      $q = $db->sql_query('INSERT INTO '.table_prefix.'poll_options(poll_id,option_value) VALUES('.$l[$key][0]['pid'].', \''.$str.'\');');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   423
      if(!$q) $db->_die('The poll data could not be inserted.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   424
      $q = $db->sql_query('SELECT o.item_id AS oid,option_value AS v, p.poll_id AS pid FROM '.table_prefix.'polls p, '.table_prefix.'poll_options o WHERE p.poll_id=o.poll_id AND option_value=\''.$str.'\';');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   425
      if(!$q) $db->_die('The poll data could not be selected.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   426
      $nr = $db->fetchrow();
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   427
      $l[$key][] = $nr; // Fetches the option ID, which is needed for updating and deleting the poll option
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   428
    }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   429
    echo '<hr /><h3>Poll: '.$key.'</h3>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   430
    echo '<form action="'.makeUrl($paths->nslist['Special'].'Administration', 'module='.$paths->cpage['module'], true).'" method="post">';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   431
    $poll_id = $l[$key][0]['pid'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   432
    $enabled = $l[$key][0]['enabled'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   433
    $ending_time = $l[$key][0]['end_time'];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   434
    $year = substr($ending_time, 0, 4);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   435
    $month = substr($ending_time, 5, 2);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   436
    $day = substr($ending_time, 8, 2);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   437
    $hour = substr($ending_time, 11, 2);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   438
    $minute = substr($ending_time, 14, 2);
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   439
    ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   440
    <p>Ending time:
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   441
    <select name="poll_<?php echo $poll_id; ?>_month">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   442
      <option<?php if($month=='01') echo ' selected="selected"'; ?> value="01">January</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   443
      <option<?php if($month=='02') echo ' selected="selected"'; ?> value="02">February</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   444
      <option<?php if($month=='03') echo ' selected="selected"'; ?> value="03">March</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   445
      <option<?php if($month=='04') echo ' selected="selected"'; ?> value="04">April</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   446
      <option<?php if($month=='05') echo ' selected="selected"'; ?> value="05">May</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   447
      <option<?php if($month=='06') echo ' selected="selected"'; ?> value="06">June</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   448
      <option<?php if($month=='07') echo ' selected="selected"'; ?> value="07">July</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   449
      <option<?php if($month=='08') echo ' selected="selected"'; ?> value="08">August</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   450
      <option<?php if($month=='09') echo ' selected="selected"'; ?> value="09">September</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   451
      <option<?php if($month=='10') echo ' selected="selected"'; ?> value="10">October</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   452
      <option<?php if($month=='11') echo ' selected="selected"'; ?> value="11">November</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   453
      <option<?php if($month=='12') echo ' selected="selected"'; ?> value="12">December</option>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   454
    </select>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   455
    <select name="poll_<?php echo $poll_id; ?>_day">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   456
    <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   457
      // This would be too hard to write by hand, so let's use a simple for-loop to take care of it for us
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   458
      for($i=1;$i<=31;$i++)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   459
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   460
        if($i < 10) $t = '0'.$i;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   461
        else $t = $i.'';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   462
        echo '<option';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   463
        if($t == $day) echo ' selected="selected"';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   464
        echo ' value="'.$t.'">'.$t.'</option>'."\n      "; 
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   465
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   466
    ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   467
    </select>,
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   468
    <select name="poll_<?php echo $poll_id; ?>_year">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   469
    <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   470
      // What the heck? Let's do it again :-D
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   471
      for($i=2006;$i<=2026;$i++)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   472
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   473
        echo '<option';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   474
        if($i.'' == $year) echo ' selected="selected"';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   475
        echo ' value="'.$i.'">'.$i.'</option>'."\n      "; 
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   476
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   477
    ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   478
    </select>&nbsp;&nbsp;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   479
    <select name="poll_<?php echo $poll_id; ?>_hour">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   480
    <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   481
      for($i=0;$i<=23;$i++)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   482
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   483
        if($i < 10) $t = '0'.$i;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   484
        else $t = $i.'';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   485
        echo '<option';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   486
        if($t == $hour) echo ' selected="selected"';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   487
        echo ' value="'.$t.'">'.$t.'</option>'."\n      "; 
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   488
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   489
    ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   490
    </select>:<select name="poll_<?php echo $poll_id; ?>_minute">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   491
    <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   492
      for($i=0;$i<=59;$i++)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   493
      {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   494
        if($i < 10) $t = '0'.$i;
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   495
        else $t = $i.'';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   496
        echo '<option';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   497
        if($t == $minute) echo ' selected="selected"';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   498
        echo ' value="'.$t.'">'.$t.'</option>'."\n      "; 
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   499
      }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   500
    ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   501
    </select><br />
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   502
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input<?php if($year=='9999' && $month=='01' && $day=='01' && $hour=='00' && $minute=='00') echo ' checked="checked"'; ?> type="checkbox" name="poll_<?php echo $poll_id; ?>_never" />Never ends</label></p>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   503
    <p><label><input<?php if($enabled) echo ' checked="checked"'; ?> type="checkbox" name="poll_<?php echo $poll_id; ?>_enabled" /> Poll is enabled</label></p>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   504
    <p><input type="submit" name="poll_<?php echo $poll_id; ?>_update" value="Update this poll" />  <input type="submit" name="poll_<?php echo $poll_id; ?>_delete" value="Delete this poll" /></p></p>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   505
    <table border="0" width="100%" cellspacing="1" cellpadding="4">
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   506
      <tr><th>Option value</th><th>Votes</th><th>Actions</th></tr>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   507
      <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   508
        foreach($l[$key] as $row)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   509
        {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   510
          if(isset($_POST['delete_'.$row['pid'].'_'.$row['oid']]) && sizeof($l[$key]) > 1)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   511
          {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   512
            $q = $db->sql_query('DELETE FROM '.table_prefix.'poll_options WHERE poll_id='.$row['pid'].' AND item_id='.$row['oid'].';');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   513
            if(!$q) $db->_die('The poll data could not be deleted.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   514
            $q = $db->sql_query('DELETE FROM '.table_prefix.'poll_results WHERE poll_id='.$row['pid'].' AND item_id='.$row['oid'].';');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   515
            if(!$q) $db->_die('The poll result data could not be deleted.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   516
            echo '<tr><td colspan="3" style="text-align: center"><b>Item deleted.</b></tr>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   517
          } else {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   518
            if(isset($_POST['delete_'.$row['pid'].'_'.$row['oid']]) && sizeof($l[$key]) < 2)
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   519
              echo '<tr><td colspan="3" style="text-align: center"><b>You cannot delete the last option in a poll.<br />Instead, please use the "Update" button.</b></tr>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   520
            if(isset($_POST['update_'.$row['pid'].'_'.$row['oid']]))
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   521
            {
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   522
              $q = $db->sql_query('UPDATE '.table_prefix.'poll_options SET option_value=\''.mysql_real_escape_string($_POST['value_'.$row['pid'].'_'.$row['oid']]).'\' WHERE poll_id='.$row['pid'].' AND item_id='.$row['oid'].';');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   523
              if(!$q) $db->_die('The poll data could not be updated.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   524
              $row['v'] = $_POST['value_'.$row['pid'].'_'.$row['oid']];
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   525
            }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   526
            // Sorry guys, really, I hate to make a ton of queries here but there's really no other way to do this :'(
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   527
            $q = $db->sql_query('SELECT * FROM '.table_prefix.'poll_results WHERE poll_id='.$row['pid'].' AND item_id='.$row['oid'].';');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   528
            if(!$q) $db->_die('The poll result data could not be selected.');
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   529
            echo '<tr><td><input name="value_'.$row['pid'].'_'.$row['oid'].'" value="'.htmlspecialchars($row['v']).'" /></td><td>'.$db->numrows().'</td><td style="text-align: center"><input name="update_'.$row['pid'].'_'.$row['oid'].'" type="submit" value="Update" />  <input name="delete_'.$row['pid'].'_'.$row['oid'].'" type="submit" value="Delete" /></td></tr>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   530
          }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   531
          //$last_pid
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   532
        }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   533
      ?>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   534
      <tr><td colspan="2"><input name="value_<?php echo $l[$key][0]['pid']; ?>" type="text" /></td><td style="text-align: center;"><input type="submit" name="create_<?php echo $l[$key][0]['pid']; ?>" value="Create option" /></td>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   535
    </table>
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   536
    <?php
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   537
    echo '</form>';
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   538
  }
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   539
}
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   540
e96ded22104a First revision, moved around a bit but other than that mostly unchanged from the old version from stable.
Dan
parents:
diff changeset
   541
?>