* @copyright Copyright 2009/2011 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License V2.0 */ error_reporting(-1); require('includes/application_top.php'); // Initialise $action = PMCheckInput("action", ""); $poll_id = PMCheckInput("poll_id", ""); $ip = PMCheckInput("ip", ""); $poll_descr = PMCheckInput("poll_descr", ""); $poll_type = PMCheckInput("poll_type", "R"); $poll_multivote = PMCheckInput("poll_multivote", "N"); $poll_prim = PMCheckInput("poll_prim", "N"); $poll_status = PMCheckInput("poll_status", "I"); $poll_question = PMCheckInput("poll_question", ""); $poll_show_cust = PMCheckInput("poll_show_cust", "N"); $headertext = PM_LIST_HEADER; $options = array(); /* The following actions are defined: * - no action : just display the list page * - new_poll : display the empty page to enter a new poll * - edit_poll : display the fields for the specified poll on the edit/add page * - add_poll : adds the just entered poll to the DB from the incoming variables; returns to the list page * - add_option : adds an empty option to the poll that is being edited; returns to the edit/add page * - delete_poll : deletes the poll and all related options and votes; returns to the list page * - delete_votes : deletes all votes for the specified poll; returns to the list page * - delete_IP_votes : delete votes for the specified poll, with the IP on which the admin is active now */ // Delete the entire poll and go on to the list page if ($action == "delete_poll") { $action = ""; if (!empty($poll_id)) { $result = $db->execute("DELETE FROM " . PM_TABLE_POLLS . " WHERE p_id='" . $poll_id . "'"); $result = $db->execute("DELETE FROM " . PM_TABLE_POLL_OPTIONS . " WHERE o_poll_id='" . $poll_id . "'"); $result = $db->execute("DELETE FROM " . PM_TABLE_POLL_RESULTS . " WHERE r_poll_id='" . $poll_id . "'"); } } // Delete a poll's votes and go on to the list page if ($action == "delete_votes") { $action = ""; if (!empty($poll_id)) { $result = $db->execute("DELETE FROM " . PM_TABLE_POLL_RESULTS . " WHERE r_poll_id='" . $poll_id . "'"); } } // Delete a poll's votes and go on to the list page if ($action == "delete_IP_votes") { $action = ""; if (!empty($poll_id) && !empty($ip)) { $result = $db->execute("DELETE FROM " . PM_TABLE_POLL_RESULTS . " WHERE r_poll_id='" . $poll_id . "' AND r_ip='" . $ip . "'"); } } // New polls start with two empty options if ($action == "new_poll") { $headertext = PM_ADD_HEADER; for ($i = 1; $i < 3; $i++) { $options[$i]["option"] = $i; $options[$i]["text"] = ""; $options[$i]["inpf"] = ""; } } // Either with adding the poll, or with adding an option, we compress the option list // For add option we add an empty option if ($action == "add_option" || $action == "add_poll") { $i = 1; $j = 1; $option = "option_id_1"; // Compress the options array and remove any empty options while (isset($_GET[$option])) { $text = "option_text_" . $i; if (isset($_GET[$text]) && !empty($_GET[$text])) { $options[$j]["option"] = $j; $options[$j]["text"] = $_GET[$text]; $inpf = "option_inpf_" . $i; if (isset($_GET[$inpf])) $options[$j]["inpf"] = $_GET[$inpf]; else $options[$j]["inpf"] = ""; $j++; } $i++; $option = "option_id_". $i; } // At this stage we have a renumbered array with valid options. if ($action == "add_option") { if (empty($poll_id)) $headertext = PM_ADD_HEADER; else $headertext = PM_EDIT_HEADER; // Add an empty option $options = PMAddEmptyOption($options); $j++; $action = "new_poll"; } // We cannot have a poll with only 1 answer! So we fill up with empty options and go in edit mode again if ($j <= 2) { if ($action == "add_poll") $messageStack->add(PM_REQ_2_OPTIONS,'error'); while ($j <= 2) { $options = PMAddEmptyOption($options); $j++; } $action = "new_poll"; } } // We can add the poll to the DB, IF the option fields are ok. if ($action == "add_poll") { // Check input fields if (empty($poll_descr) || empty($poll_type) || empty($poll_status) || empty($poll_question) || empty($poll_prim)) { $messageStack->add(PM_REQ_FIELDS, 'error'); $action = "new_poll"; $options = PMAddEmptyOption($options); } else { // It's a new poll if (empty($poll_id)) { // Insert the stuff in the DB $poll_insert = $db->Execute("INSERT INTO " . PM_TABLE_POLLS . " VALUES('0', '" . $db->prepare_input($poll_descr) . "', '" . $db->prepare_input($poll_question) . "', '" . ($poll_status == "I" ? 0 : 1) . "', '" . ($poll_type == "R" ? 0 : 1) . "', '" . $poll_multivote . "', '" . ($poll_prim == "Y" ? "Y" : "N") . "', '" . ($poll_show_cust == "Y" ? "Y" : "N") . "')"); $poll_id = $db->insert_ID(); foreach ($options as $number => $option) { $option_insert = $db->Execute("INSERT INTO " . PM_TABLE_POLL_OPTIONS . " VALUES('0', '" . $poll_id . "', '" . $number . "', '" . $db->prepare_input($option["text"]) . "', '" . ($option["inpf"] == "" ? 0 : 1) . "')"); } $action = ""; } else { // It's an update for a poll $poll_update = $db->Execute("UPDATE " . PM_TABLE_POLLS . " SET p_description='" . $db->prepare_input($poll_descr) . "', p_question='" . $db->prepare_input($poll_question) . "', p_status='" . ($poll_status == "I" ? 0 : 1) . "', p_type='" . ($poll_type == "R" ? 0 : 1). "', p_multivote='" . $poll_multivote . "', p_prim='" . ($poll_prim == "Y" ? "Y" : "N") . "', p_show_cust='" . ($poll_show_cust == "Y" ? "Y" : "N") . "' WHERE p_id='" . $poll_id . "'"); $poll_delete = $db->Execute("DELETE FROM " . PM_TABLE_POLL_OPTIONS . " WHERE o_poll_id='" . $poll_id . "'"); foreach ($options as $number => $option) { $option_insert = $db->Execute("INSERT INTO " . PM_TABLE_POLL_OPTIONS . " VALUES('0', '" . $poll_id . "', '" . $number . "', '" . $db->prepare_input($option["text"]) . "', '" . ($option["inpf"] == "" ? 0 : 1) . "')"); } $action = ""; } // If the primary poll indicator is set to yes, update the indication of all other polls to no. if ($poll_prim == "Y") $poll_primary = $db->Execute("UPDATE " . PM_TABLE_POLLS . " SET p_prim='N' WHERE p_id!='" . $poll_id . "'"); } } if ($action == "edit_poll") { $headertext = PM_EDIT_HEADER; if (!empty($poll_id)) { $action = "new_poll"; // Get the poll info from the DB $poll_list = $db->Execute("SELECT * FROM " . PM_TABLE_POLLS . ", " . PM_TABLE_POLL_OPTIONS . " WHERE p_id=o_poll_id AND p_id='" . $poll_id . "' ORDER BY o_option"); if ($poll_list->RecordCount() > 0) { $options = array(); while(!$poll_list->EOF) { $oid = $poll_list->fields['o_option']; $poll_id = $poll_list->fields['p_id']; $poll_descr = $poll_list->fields['p_description']; $poll_question = $poll_list->fields['p_question']; $poll_status = ($poll_list->fields['p_status'] == 0 ? "I" : "A"); $poll_type = ($poll_list->fields['p_type'] == 0 ? "R" : "C"); $poll_multivote = $poll_list->fields['p_multivote']; $poll_prim = ($poll_list->fields['p_prim'] == "Y" ? "Y" : "N"); $poll_show_cust = ($poll_list->fields['p_show_cust'] == "Y" ? "Y" : "N"); $options[$oid]["option"] = $poll_list->fields['o_option']; $options[$oid]["text"] = $poll_list->fields['o_text']; $options[$oid]["inpf"] = $poll_list->fields['o_inpf']; $poll_list->MoveNext(); } } } } // No action, just display the existing polls if ($action == "") { $poll_list = $db->Execute("SELECT * FROM " . PM_TABLE_POLLS . ", " . PM_TABLE_POLL_OPTIONS . " WHERE p_id=o_poll_id ORDER BY p_id, o_option"); $polls = array(); if ($poll_list->RecordCount() > 0) { while(!$poll_list->EOF) { $id = $poll_list->fields['p_id']; $oid = $poll_list->fields['o_option']; $polls[$id]['id'] = $poll_list->fields['p_id']; $polls[$id]['descr'] = $poll_list->fields['p_description']; $polls[$id]['question'] = $poll_list->fields['p_question']; $polls[$id]['status'] = ($poll_list->fields['p_status'] == 0 ? "I" : "A"); $polls[$id]['type'] = ($poll_list->fields['p_type'] == 0 ? "R" : "C"); $polls[$id]['multivote'] = $poll_list->fields['p_multivote']; $polls[$id]['prim'] = $poll_list->fields['p_prim']; $polls[$id]['show_cust'] = $poll_list->fields['p_show_cust']; $polls[$id]['votes'] = 0; $polls[$id]['options'][$oid]['option'] = $poll_list->fields['o_option']; $polls[$id]['options'][$oid]['votes'] = 0; $polls[$id]['options'][$oid]['text'] = $poll_list->fields['o_text']; $polls[$id]['options'][$oid]['inpf'] = $poll_list->fields['o_inpf']; $poll_list->MoveNext(); } // Add the votes foreach ($polls as $id => $poll) { $tvotes = 0; $votes = $db->Execute("SELECT r_option, count(r_option) as r_count FROM " . PM_TABLE_POLL_RESULTS . " WHERE r_poll_id='" . $id . "' GROUP BY r_option"); while (!$votes->EOF) { $polls[$id]['options'][$votes->fields['r_option']]['votes'] = $votes->fields['r_count']; $tvotes += $votes->fields['r_count']; if ($polls[$id]['options'][$votes->fields['r_option']]['inpf'] == 1) { $votetexts = $db->Execute("SELECT r_option_text, count(r_option_text) as r_count FROM " . PM_TABLE_POLL_RESULTS . " WHERE r_poll_id='" . $id . "' AND r_option='" . $votes->fields['r_option'] . "' GROUP BY r_option_text ORDER BY r_count"); while (!$votetexts->EOF) { $polls[$id]['options'][$votes->fields['r_option']]['texts'][] = array('text' => $votetexts->fields['r_option_text'], 'count' =>$votetexts->fields['r_count']); $votetexts->MoveNext(); } } $votes->MoveNext(); } // Also add the voters if multiple choice $polls[$id]['votes'] = $tvotes; if ($polls[$id]['type'] == "C") { $voters = 0; $rvoters = $db->Execute("SELECT count(ip) i_count FROM (SELECT DISTINCT(r_ip) as ip FROM " . PM_TABLE_POLL_RESULTS . " WHERE r_poll_id='" . $id . "' GROUP BY r_ip) as t"); while(!$rvoters->EOF) { $voters = $rvoters->fields['i_count']; $rvoters->MoveNext(); } $polls[$id]['voters'] = $voters; } // Add the timestamps of the first and last vote $stamps = $db->Execute("SELECT DISTINCT(r_poll_id), DATE_FORMAT(MIN(r_timestamp),'" . PM_VOTE_DATE_FORMAT . "') as first, DATE_FORMAT(MAX(r_timestamp),'" . PM_VOTE_DATE_FORMAT . "') as last FROM " . PM_TABLE_POLL_RESULTS . " WHERE 1 GROUP BY r_poll_id"); while (!$stamps->EOF) { $polls[$stamps->fields['r_poll_id']]['firstvote'] = $stamps->fields['first']; $polls[$stamps->fields['r_poll_id']]['lastvote'] = $stamps->fields['last']; $stamps->MoveNext(); } } } } // Start output ?> > <?php print TITLE; ?>

" . PM_HELP . ""; ?>

"; // Print heading row print "
". $headertext . "

"; print "
"; print "\n"; print "\n"; // Print description field print ""; print ""; // Print question field print ""; print ""; // Print type field print ""; print ""; print ""; print ""; // Print active/inactive field print ""; print ""; print ""; print ""; // Print active/inactive field print ""; print ""; print ""; print ""; // Print the primary yes/no field print ""; print ""; print ""; print ""; // Print the show poll to customers yes/no field print ""; print ""; print ""; print ""; print "
" . PM_POLL_INFO . "
" . PM_POLL_DESCR . "" . zen_draw_input_field('poll_descr', $poll_descr, 'size="64" maxlength="64"', true) . "
" . PM_POLL_QUESTION . "" . zen_draw_input_field('poll_question', $poll_question, 'size="64" maxlength="64"', true) . "
" . PM_POLL_TYPE . "" . zen_draw_radio_field('poll_type', 'R') . PM_POLL_TYPE_SINGLE . "
" . zen_draw_radio_field('poll_type', 'C') . PM_POLL_TYPE_MULTIPLE . "
" . PM_POLL_STATUS . "" . zen_draw_radio_field('poll_status', 'A', ($poll_status == "A" ? true : false)) . PM_POLL_STATUS_ACTIVE . "
" . zen_draw_radio_field('poll_status', 'I', ($poll_status == "I" ? true : false)) . PM_POLL_STATUS_INACTIVE . "
" . PM_ALLOW_MULTIPLE_VOTES . "" . zen_draw_radio_field('poll_multivote', 'Y', ($poll_multivote == "Y" ? true : false)) . PM_YES . "
" . zen_draw_radio_field('poll_multivote', 'N', ($poll_multivote == "N" ? true : false)) . PM_NO . "
" . PM_POLL_PRIM . "" . zen_draw_radio_field('poll_prim', 'Y', ($poll_prim != "N" ? true : false)) . PM_YES . "
" . zen_draw_radio_field('poll_prim', 'N', ($poll_prim == "N" ? true : false)) . PM_NO . "
" . PM_SHOW_RESULTS_TO_CUST . "" . zen_draw_radio_field('poll_show_cust', 'Y', ($poll_show_cust != "N" ? true : false)) . PM_YES . "
" . zen_draw_radio_field('poll_show_cust', 'N', ($poll_show_cust == "N" ? true : false)) . PM_NO . "

"; // Print the options print "\n"; print "\n"; foreach($options as $option => $fields) { if ($option == 1) { // Option header print "\n"; } print ""; print ""; print ""; print "\n"; } print "
" . PM_POLL_OPTION_INFO . "
" . PM_POLL_OPTION_ID . "" . PM_POLL_OPTION_TEXT . "" . PM_POLL_OPTION_INPUT . "
"; print zen_draw_hidden_field("option_id_".$option, $fields["option"]); print $fields["option"] . "" . zen_draw_input_field("option_text_" . $fields["option"], $fields["text"], 'size="64" maxlength="64"', true) . "" . zen_draw_checkbox_field("option_inpf_" . $fields["option"], "1", ($fields["inpf"] == 1), "", "id=\"option_inpf_" . $fields["option"] . "\" onclick=\"if(counttextinput(" . count($options) . ") > 1){alert('". PM_USER_INPUT_WARNING . "'); this.checked=0;};\"") . "
"; print "\n"; print "\n"; print "
"; // Print submit/add option buttons // Submit button print zen_image_submit(PM_FILENAME_SEND_BUTTON, PM_POLL_ADD); // Add option button, if less than 6 options present if (count($options) < 6) print "  " . zen_image_button(PM_FILENAME_ADD_OPTION_BUTTON, PM_POLL_OPTION_ADD) . ""; // Back button print "  " . zen_image_button(PM_FILENAME_BACK_BUTTON, PM_POLL_BACK) . ""; print "
"; print ""; break; default: ?> "; print "
". $headertext . "

"; print "\n
"; foreach ($polls as $poll_key => $poll) { print "
"; print "
 " . zen_image(($poll['status'] == "I" && $poll_id != $poll_key ? PM_FILENAME_PLUS : PM_FILENAME_MINUS), PM_CLICK_FOR_POLL, 0, 0, "id=\"plusmin" . $poll_key . "\" onclick=\"return switchblock('poll_block_div', '" . $poll_key . "')\""); print "  " . $poll['descr'] . " (" . ($poll['status'] == "I" ? PM_POLL_STATUS_INACTIVE : PM_POLL_STATUS_ACTIVE) . ($poll['prim'] == "Y" ? ", " . PM_POLL_PRIM_SHORT : "") . ")"; print "

"; print "
"; print "\n"; print "\n"; // Print description field print ""; print ""; // Print question field print ""; print ""; // Print type field print ""; print ""; print ""; // Print active/inactive field print ""; print ""; print ""; // Print multivote field print ""; print ""; print ""; // Print primary / not primary field print ""; print ""; print ""; // Print show to customers / not field print ""; print ""; print ""; // Print number of votes field print ""; print ""; print ""; // For multiple choice, also display the number of voters if (isset($poll['voters'])) { print ""; print ""; print ""; } // Print date/time of first and last vote print ""; print ""; print ""; print ""; print ""; print ""; print "
" . PM_POLL_INFO . "
" . PM_POLL_DESCR . "" . $poll['descr'] . "
" . PM_POLL_QUESTION . "" . $poll['question'] . "
" . PM_POLL_TYPE . "" . ($poll['type'] == "R" ? PM_POLL_TYPE_SINGLE : PM_POLL_TYPE_MULTIPLE) . "
" . PM_POLL_STATUS . "" . ($poll['status'] == "I" ? PM_POLL_STATUS_INACTIVE : PM_POLL_STATUS_ACTIVE) . "
" . PM_ALLOW_MULTIPLE_VOTES . "" . ($poll['multivote'] == "Y" ? PM_YES : PM_NO) . "
" . PM_POLL_PRIM . "" . ($poll['prim'] == "Y" ? PM_YES : PM_NO) . "
" . PM_SHOW_RESULTS_TO_CUST . "" . ($poll['show_cust'] == "Y" ? PM_YES : PM_NO) . "
" . PM_POLL_VOTES . "" . $poll['votes'] . "
" . PM_POLL_VOTERS . "" . $poll['voters'] . "
" . PM_FIRST_VOTE . "" . (isset($poll['firstvote']) ? $poll['firstvote'] : ' ') . "
" . PM_LAST_VOTE . "" . (isset($poll['lastvote']) ? $poll['lastvote'] : ' ') . "
\n"; // Print the options print "\n"; print "\n"; foreach($poll['options'] as $option_id => $option) { if ($option_id == 1) { // Option header print "\n"; } print "\n"; print "\n"; print "\n"; print "\n"; // print "\n"; } print "\n"; print "
" . PM_POLL_OPTION_INFO . "
" . PM_POLL_OPTION_ID . "" . PM_POLL_OPTION_INPUT . "" . PM_POLL_OPTION_TEXT . "" . PM_OPTION_VOTES . "
" . $option_id . "" . ($option['inpf'] == 1 ? PM_YES : PM_NO) . ""; if (isset($option['texts'])) { print "" . $option['text'] . ""; print "\n"; } else print $option['text']; print "" . $option['votes'] . (($poll['votes'] == 0 || $poll['type'] == "C") ? "" : " (" . round($option['votes'] / $poll['votes'] * 100, 0) . "%)"); print "" . $option['votes'] . "" .(($poll['votes'] == 0) ? "" : " (" . round($option['votes'] / ($poll['type'] == "C" ? $poll['voters'] : $poll['votes']) * 100, 0) . "%)"); print "
"; // Print the edit button print "" . zen_image_button(PM_FILENAME_EDIT_BUTTON, PM_POLL_EDIT) . "  "; // Print the delete button print "" . zen_image_button(PM_FILENAME_DELETE_BUTTON, PM_POLL_DELETE) . "  "; // Print the delete votes button, if # votes > 0 if ($poll['votes'] > 0) { print "" . zen_image_button(PM_FILENAME_REMOVE_VOTE_BUTTON, PM_POLL_DELETE_VOTES) . ""; } // Check if we can delete votes of this IP, if so print the appropriate button $ip = zen_get_ip_address(); $poll_ip = $db->Execute("SELECT r_ip FROM " . PM_TABLE_POLL_RESULTS . " WHERE r_poll_id='" . $poll_key . "' AND r_ip='" . $ip . "'"); if ($poll_ip->RecordCount() > 0) { print "  " . zen_image_button(PM_FILENAME_REMOVE_IP_VOTES_BUTTON, PM_DEL_ADMIN_VOTES) . ""; } print "

"; } print "" . PM_ADDLINK . ""; break; } ?>