';
} } } // end new poll question + end show start_new_poll form
} // 1 2 3 checks for allowed settings to start/edit poll
} else { // there's a saved poll question with options
if (isset($_POST['poll_vote'])) { // save new poll vote from _post data
bb_polls_add_vote($user_id,$topic_id);
bb_polls_show_poll_results($topic_id);
} else {
if (isset($_GET['show_poll_results'])) { // override to show poll results
bb_polls_show_poll_results($topic_id);
} else { // obey per page setting
if ( $bb_polls['show_poll_on_which_pages']=="all"
|| ($page==1 && ($bb_polls['show_poll_on_which_pages']=="first" || $bb_polls['show_poll_on_which_pages']=="both" ))
|| ($page==get_page_number( $topic->topic_posts ) && ($bb_polls['show_poll_on_which_pages']=="last" || $bb_polls['show_poll_on_which_pages']=="both" ))
) {
if (!$user_id || bb_polls_has_voted($user_id,$topic_id)) {
bb_polls_show_poll_results($topic_id); // they voted, show results
} else {
bb_polls_show_poll_vote_form($topic_id); // let them vote
} } } }
}
endif;
remove_action('topicmeta','bb_polls_pre_poll',200); // NullFix ?
}
function bb_polls_check_cache($topic_id) {
global $bb_polls,$topic,$poll_options;
if (!$topic_id || $topic_id != $topic->topic_id) {if (!isset($topic)) {bb_repermalink();} $topic_id=get_topic_id(); $topic = get_topic($topic_id);}
if (isset($topic->poll_options)) {if (!isset($poll_options)) {$poll_options=$topic->poll_options;}} else {$poll_options=NULL;}
if (isset($poll_options) && !is_array($poll_options)) {$poll_options=unserialize(substr($poll_options,2));} // trick bb_press to keep poll data unserialized
return intval($topic_id);
}
function bb_polls_has_voted($user_id,$topic_id) {
global $bb_polls,$topic,$poll_options;
if ($bb_polls['test_mode']) {return false;} // for testing only, allows multiple votes by anyone
$topic_id=bb_polls_check_cache($topic_id);
if (!$user_id) {$user_id=bb_get_current_user_info( 'id' );}
$votes=''; for ($i=1; $i<=$bb_polls['max_options']; $i++) {if (isset($poll_options['poll_vote_'.$i])) {$votes.=",".$poll_options['poll_vote_'.$i].",";}}
return (strpos($votes,",".$user_id.",")===false ? false : true);
}
function bb_polls_add_vote($user_id,$topic_id) {
global $bb_polls,$topic,$poll_options;
if (bb_current_user_can($bb_polls['minimum_vote_level'])) :
$topic_id=bb_polls_check_cache($topic_id);
$voted_flag=false;
if (!bb_polls_has_voted($user_id,$topic_id)) {
for ($i=0; $i<=$bb_polls['max_options']; $i++) {
$test=0;
if (isset($_POST['poll_vote_'.$i])) {$test=intval($_POST['poll_vote_'.$i]);}
elseif (isset($_GET['poll_vote_'.$i])) {$test=intval($_GET['poll_vote_'.$i]);}
if ($test>0) :
if (!isset($poll_options['poll_count_'.$test])) { // initialise counters
$poll_options['poll_count_'.$test]=0;
$poll_options['poll_vote_'.$test]='';
if (!isset($poll_options['poll_count_0'])) {$poll_options['poll_count_0']=0;}
} else { $poll_options['poll_vote_'.$test].=",";}
$poll_options['poll_vote_'.$test].=$user_id; // add user's vote, single or multiple
$poll_options['poll_count_'.$test]++; // update count for option
$voted_flag=true; // set flag to update overall count
if (!$poll_options['poll_multiple_choice']) {break;} // don't allow single choice votes to count multiple choices, only first answer
endif;
}
if ($voted_flag) {$poll_options['poll_count_0']++;} // update count for overall
bb_update_topicmeta( $topic_id, 'poll_options', '..'.serialize($poll_options)); // save and trick bb_press to not deserialize unless necessary
return true;
}
else {return false;} // has voted already
endif;
}
function bb_polls_show_poll_results($topic_id,$display=1) {
global $bb_polls,$topic,$poll_options;
$administrator=bb_current_user_can('administrate');
if ($bb_polls['minimum_view_level']=="read" || bb_current_user_can($bb_polls['minimum_view_level']) ) {
$topic_id=bb_polls_check_cache($topic_id);
$output='
';
}
$output.=bb_polls_edit_link();
$output=stripslashes($output);
if ($display) {echo '
'.$output.'
';} else {return $output;}
}
}
function bb_polls_show_poll_vote_form($topic_id,$display=1) {
global $bb_polls,$topic,$poll_options;
if (bb_current_user_can($bb_polls['minimum_vote_level'])) {
$topic_id=bb_polls_check_cache($topic_id);
if ($poll_options['poll_multiple_choice']==1) {$poll_type="checkbox";} else {$poll_type="radio";}
$output='';
$output.=bb_polls_edit_link();
$output=stripslashes($output);
if ($display) {echo '
'.$output.'
';} else {return $output;}
}
}
function bb_polls_edit_link() {
global $bb_polls,$topic,$poll_options; $output=''; $can_edit=false; $can_delete=false;
$administrator=bb_current_user_can('administrate');
$user_id=bb_get_current_user_info( 'id' );
if (bb_current_user_can($bb_polls['minimum_edit_level'])
&& !($bb_polls['only_topic_author_can_add'] && $topic->topic_poster!=$user_id)
&& !($bb_polls['edit_within_hours'] && $bb_polls['edit_within_hours']<(time()-bb_gmtstrtotime($topic->topic_start_time))/3600)) {$can_edit=true;}
if ($topic->topic_poster==$user_id && bb_current_user_can($bb_polls['minimum_delete_level'])
&& !($bb_polls['edit_within_hours'] && $bb_polls['edit_within_hours']<(time()-bb_gmtstrtotime($topic->topic_start_time))/3600)) {$can_delete=true;}
if ($administrator || $can_delete) {
$output.= ''.$bb_polls['label_delete_text'].' | ';
}
if ($administrator || $can_edit) {
$output.= ''.$bb_polls['label_edit_text'].'';
}
return $output;
}
function bb_polls_delete_poll() {
global $bb_polls; $can_delete=false;
$administrator=bb_current_user_can('administrate');
$user_id=bb_get_current_user_info( 'id' );
$topic_id=bb_polls_check_cache($topic_id);
$topic=get_topic($topic_id);
if (empty($topic_id) || empty($topic)) {return;}
if ($topic->topic_poster==$user_id && bb_current_user_can($bb_polls['minimum_delete_level'])
&& !($bb_polls['edit_within_hours'] && $bb_polls['edit_within_hours']<(time()-bb_gmtstrtotime($topic->topic_start_time))/3600)) {$can_delete=true;}
if ($administrator || $can_delete) { bb_delete_topicmeta($topic_id, 'poll_options');}
}
function bb_polls_save_poll_setup($topic_id=0) {
global $bb_polls,$topic,$poll_options;
if (bb_current_user_can('administrate') || bb_current_user_can($bb_polls['minimum_add_level'])) {
$topic_id=bb_polls_check_cache($topic_id);
$poll_options['poll_question']=trim(substr(strip_tags(stripslashes($_POST['poll_question'])),0,$bb_polls['max_length']));
$poll_options['poll_multiple_choice']=intval($_POST['poll_multiple_choice']);
$options=0;
for ($i=1; $i<=$bb_polls['max_options']; $i++) {
if ($test=trim(substr(strip_tags(stripslashes($_POST['poll_option_'.$i])),0,$bb_polls['max_length']))) {$options++; $poll_options[$options]=$test;}
} // loop
bb_update_topicmeta( $topic_id, 'poll_options', '..'.serialize($poll_options)); // save and trick bb_press to not deserialize unless necessary
// echo get_topic_id( $topic_id )." - ".$topic_id." : ".serialize($poll_options); exit();
}
}
function bb_polls_show_poll_setup_form($topic_id,$display=1,$edit_poll=0) {
global $bb_polls,$topic,$poll_options;
if (($edit_poll==0 && bb_current_user_can($bb_polls['minimum_add_level']))
|| ($edit_poll && bb_current_user_can($bb_polls['minimum_edit_level']))
|| bb_current_user_can('administrate')) {
$is_topic=is_topic(); $topic_id=bb_polls_check_cache($topic_id); $output="";
if ($is_topic) {
$output.='';
} else {
$output.=' ';
}
$output=stripslashes($output);if ($display) {echo '
'.$output.'
';} else {return $output;}
}
}
function bb_polls_title( $title ) {
global $bb_polls, $topic;
if (isset($topic->poll_options)) {
if ($bb_polls['use_icon']=="left") {$title=" ".$title;}
elseif ($bb_polls['use_icon']=="right") {$title=$title." ";}
if ($bb_polls['label_poll_text']) {$title='['.$bb_polls['label_poll_text'].'] '.$title;}
}
return $title;
}
function bb_polls_save_on_new($post_id=0) {
if (isset($_POST['poll_question'])) { // save new poll setup from _post data
bb_polls_save_poll_setup(0);
}
}
function bb_polls_add_header() {
if (isset($_POST['poll_question'])) { // save new poll setup from _post data
bb_polls_save_poll_setup(0);
// header("HTTP/1.1 307 Temporary redirect");
wp_redirect($_SERVER["REQUEST_URI"]); // I *really* don't like this technique but it's the only way to clear post data?
// exit(); // not sure why but this makes it fail?
}
if (isset($_POST['poll_vote'])) { // save new poll vote from _post data
bb_polls_add_vote(bb_get_current_user_info( 'id' ),'');
// header("HTTP/1.1 307 Temporary redirect");
wp_redirect($_SERVER["REQUEST_URI"]); // I *really* don't like this technique but it's the only way to clear post data?
// exit(); // not sure why but this makes it fail?
}
if (isset($_GET['delete_poll']) && intval($_GET['delete_poll'])) {
bb_polls_delete_poll();
wp_redirect(remove_query_arg(array('start_new_poll','edit_poll','delete_poll','show_poll_vote_form_ajax','show_poll_setup_form_ajax','bb_polls_cache'))); // I *really* don't like this technique but it's the only way to clear post data?
}
if (isset($_GET['show_poll_results_ajax'])) {
$topic_id=intval($_GET['show_poll_results_ajax']);
header("Content-Type: application/x-javascript; charset=utf-8");
echo 'bb_polls_insert_ajax("'.mysql_escape_string(bb_polls_show_poll_results($topic_id,0)).'");';
exit();
}
if (isset($_GET['show_poll_vote_form_ajax'])) {
$topic_id=intval($_GET['show_poll_vote_form_ajax']);
header("Content-Type: application/x-javascript; charset=utf-8");
echo 'bb_polls_insert_ajax("'.mysql_escape_string(bb_polls_show_poll_vote_form($topic_id,0)).'");';
exit();
}
if (isset($_GET['show_poll_setup_form_ajax'])) {
$topic_id=intval($_GET['show_poll_setup_form_ajax']);
header("Content-Type: application/x-javascript; charset=utf-8");
echo 'bb_polls_insert_ajax("'.mysql_escape_string(bb_polls_show_poll_setup_form($topic_id,0,0)).'");';
echo 'setTimeout("document.forms.postform.poll_question.focus()",50);';
exit();
}
if (isset($_GET['add_vote_ajax'])) {
$topic_id=intval($_GET['add_vote_ajax']);
bb_polls_add_vote(bb_get_current_user_info( 'id' ),$topic_id);
header("Content-Type: application/x-javascript; charset=utf-8");
echo 'bb_polls_insert_ajax("'.mysql_escape_string(bb_polls_show_poll_results($topic_id,0)).'");';
exit();
}
}
function bb_polls_add_javascript($topic_id=0) {
global $bb_polls, $topic;
if ($bb_polls['use_ajax'] && bb_current_user_can($bb_polls['minimum_vote_level']) ) :
$topic_id=bb_polls_check_cache($topic_id);
echo '';
endif;
}
function bb_polls_add_css() {
global $bb_polls;
if (is_topic()) {echo ''; }
}
if ((defined('BB_IS_ADMIN') && BB_IS_ADMIN) || strpos($_SERVER['REQUEST_URI'],"/bb-admin/")!==false) { // "stub" only load functions if in admin
function bb_polls_add_admin_page() {bb_admin_add_submenu(__('Polls'), 'administrate', 'bb_polls_admin');}
add_action( 'bb_admin_menu_generator', 'bb_polls_add_admin_page' );
if (isset($_GET['plugin']) && $_GET['plugin']=="bb_polls_admin") {require_once("bb-polls-admin.php");} // load entire core only when needed
}
?>