<?php
/*
Plugin Name: WP-Polls Widget Multi
Plugin URI: http://lesterchan.net/portfolio/programming/php/
Description: Adds a multiple Poll Widgets to display single or multiple polls from WP-Polls Plugin. You will need to activate WP-Polls first.
Version: 2.40
Author: Amaury BALMER
Author URI: http://wp-box.fr
*/

/*  
Copyright 2009 Amaury BALMER (email : balmer.amaury@gmail.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

### Function: Init Multi WP-Polls Widget
function multi_widget_polls_init() {
    if (!
function_exists('register_sidebar_widget')) {
        return;
    }

    
### Function: Multi WP-Polls Widget
    
function multi_widget_polls$args$widget_args ) {
        global 
$wpdb;
        
        
extract$argsEXTR_SKIP );
        if ( 
is_numeric($widget_args) )
            
$widget_args = array( 'number' => $widget_args );
        
$widget_args wp_parse_args$widget_args, array( 'number' => -) );
        
extract$widget_argsEXTR_SKIP );
    
        
$options get_option('multi_widget_polls');
        if ( !isset(
$options[$number]) )
            return 
false;
    
        
$title            apply_filters('widget_title'$options[$number]['title']);
        
$currentpoll   $options[$number]['currentpoll'];
        
$multiplepolls $options[$number]['multiplepolls'];
        
$archive_show  $options[$number]['archive_show'];
        
        
// No poll ?
        
if(intval(get_option('poll_currentpoll')) == -1) {
            return 
false;
        }
        
        
// Random Poll
        
if( intval($currentpoll) == -) {
            
$poll_id intval($wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq WHERE pollq_active = 1 ORDER BY RAND() LIMIT 1"));
        
// Current Poll ID Is Not Specified
        
} elseif( intval($currentpoll) == ) {
            
// Get Lastest Poll ID
            
$poll_id intval(get_option('poll_latestpoll'));
        } else {
            
// Get Current Poll ID
            
$poll_id $currentpoll;
        }

        if (
function_exists('vote_poll') && !in_pollarchive()) {
        
            echo 
$before_widget;
            echo 
$before_title $title $after_title;
            
            if( 
intval($currentpoll) == -) {
            
                
$multiplepolls explode(','$multiplepolls);
                foreach( (array) 
$multiplepolls as $multiplepoll ) {
                    
get_poll($multiplepoll);
                }
                
            } else {
            
                
get_poll$poll_id );        
                        
            }
            
            
// Fake option if archive me be show
            
if ( $archive_show == '1' ) {
                
add_filter'pre_option_poll_archive_show''allow_poll_archive_show' );
            }
            
display_polls_archive_link();
            
remove_filter'pre_option_poll_archive_show''allow_poll_archive_show' );
            
            echo 
$after_widget;
        }    
        
        return 
true;    
    }
    
    function 
allow_poll_archive_show$flag '' ) {
        return 
true;
    }

    
### Function: Multi WP-Polls Widget Options
    
function multi_widget_polls_options$widget_args ) {
        global 
$wpdb;
        global 
$wp_registered_widgets;
        static 
$updated false;
    
        if ( 
is_numeric($widget_args) )
            
$widget_args = array( 'number' => $widget_args );
        
$widget_args wp_parse_args$widget_args, array( 'number' => -) );
        
extract$widget_argsEXTR_SKIP );
    
        
$options get_option('multi_widget_polls');
        if ( !
is_array($options) )
            
$options = array();

        if ( !
$updated && !empty($_POST['sidebar']) ) {
            
$sidebar = (string) $_POST['sidebar'];
    
            
$sidebars_widgets wp_get_sidebars_widgets();
            if ( isset(
$sidebars_widgets[$sidebar]) )
                
$this_sidebar =& $sidebars_widgets[$sidebar];
            else
                
$this_sidebar = array();
    
            foreach ( (array) 
$this_sidebar as $_widget_id ) {
                if ( 
'multi_widget_polls' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
                    
$widget_number $wp_registered_widgets[$_widget_id]['params'][0]['number'];
                    if ( !
in_array"poll-$widget_number"$_POST['widget-id'] ) ) // the widget has been removed.
                        
unset($options[$widget_number]);
                }
            }
    
            foreach ( (array) 
$_POST['widget-poll'] as $widget_number => $widget_poll ) {
                if ( !isset(
$widget_poll['title']) && isset($options[$widget_number]) ) // user clicked cancel
                    
continue;
                    
                
$title strip_tags(stripslashes($widget_poll['title']));
                
$currentpoll intval($widget_poll['currentpoll']);
                
$archive_show intval($widget_poll['archive_show']);    
                
                if( 
is_array($widget_poll['multiplepolls']) ) {
                    
$multiplepolls implode','$widget_poll['multiplepolls'] );
                } else {
                    
$multiplepolls $widget_poll['multiplepolls'];
                }
                    
                
$options[$widget_number] = compact'title''currentpoll''archive_show''multiplepolls' );
            }
        
            
update_option('multi_widget_polls'$options);
            
$updated true;
        }
    
        if ( -
== $number ) {
            
$title __('Polls''wp-polls');
            
$currentpoll '';
            
$archive_show '';
            
$multiplepolls '';
            
$number '%i%';
        } else {
            
$title attribute_escape($options[$number]['title']);
            
$currentpoll intval($options[$number]['currentpoll']);
            
$archive_show intval($options[$number]['archive_show']);
            
$multiplepolls $options[$number]['multiplepolls'];    
        }
        
        
// CSS
        
if( $currentpoll == -) {
            
$display 'display: block;';
            
$disabled '';
        } else {
            
$display 'display: none;';
            
$disabled 'disabled="disabled"'
        }
        
?>
        <script type="text/javascript">
            /* <![CDATA[*/
            function show_multiple_polls_<?php echo $number?>() {
                if(document.getElementById('poll_currentpoll-<?php echo $number?>').value == -3) {
                    document.getElementById('poll_multiplepolls-<?php echo $number?>').disabled = false;
                    document.getElementById('poll_multiplepolls_text-<?php echo $number?>').style.display = 'block';
                } else {                        
                    document.getElementById('poll_multiplepolls-<?php echo $number?>').selectedIndex = -1;
                    document.getElementById('poll_multiplepolls-<?php echo $number?>').disabled = true;
                    document.getElementById('poll_multiplepolls_text-<?php echo $number?>').style.display = 'none';
                }
            }
            /* ]]> */
        </script>
        
        <p>
            <label for="polls-title-<?php echo $number?>"><?php _e('Title''wp-polls'); ?></label>
            <input type="text" id="polls-title-<?php echo $number?>" name="widget-poll[<?php echo $number?>][title]" value="<?php echo htmlspecialchars(stripslashes($title)); ?>" />
        </p>
        
        <p>
            <label for="polls-displayarchive-<?php echo $number?>"><?php _e('Display Polls Archive Link Below Poll?''wp-polls'); ?></label>
            <select id="polls-displayarchive-<?php echo $number?>" name="widget-poll[<?php echo $number?>][archive_show]" size="1">
                <option value="0" <?php selected('0'$archive_show ); ?>><?php _e('No''wp-polls'); ?></option>
                <option value="1" <?php selected('1'$archive_show ); ?>><?php _e('Yes''wp-polls'); ?></option>
            </select>
        </p>
        
        <p>
            <label for="poll_currentpoll-<?php echo $number?>"><?php _e('Current Active Poll''wp-polls'); ?> :</label>
            <select id="poll_currentpoll-<?php echo $number?>" name="widget-poll[<?php echo $number?>][currentpoll]" size="1" onchange="show_multiple_polls_<?php echo $number?>()">
                <option value="-1" <?php selected(-1$currentpoll); ?>><?php _e('Do NOT Display Poll (Disable)''wp-polls'); ?></option>
                <option value="-2" <?php selected(-2$currentpoll); ?>><?php _e('Display Random Poll''wp-polls'); ?></option>
                <option value="0"  <?php selected0$currentpoll); ?>><?php _e('Display Latest Poll''wp-polls'); ?></option>
                <option value="-3" <?php selected(-3$currentpoll); ?>><?php _e('Display Multiple Polls''wp-polls'); ?></option>
                
                <optgroup>&nbsp;</optgroup>
                <?php
                $polls 
$wpdb->get_results("SELECT pollq_id, pollq_question FROM $wpdb->pollsq ORDER BY pollq_id DESC");
                if( 
$polls != false ) {
                    foreach( (array) 
$polls as $poll ) {
                        if ( 
intval($poll->pollq_id) == intval($currentpoll) ) {
                            echo 
'<option value="'intval($poll->pollq_id) .'" selected="selected">'.stripslashes($poll->pollq_question).'</option>'."\n";
                        } else {
                            echo 
'<option value="'intval($poll->pollq_id) .'">'.stripslashes($poll->pollq_question).'</option>'."\n";
                        }
                    }
                }
                
?>
            </select>
        </p>
        
        <p id="poll_multiplepolls_text-<?php echo $number?>" style="<?php echo $display?>">
            <label for="poll_multiplepolls-<?php echo $number?>"><?php _e('Select Multiple Polls''wp-polls'); ?>: </label>
            <select id="poll_multiplepolls-<?php echo $number?>" name="widget-poll[<?php echo $number?>][multiplepolls][]" size="5" multiple="true" style="height: 100px; vertical-align: text-top;" <?php echo $disabled?>>
                <?php
                $polls 
$wpdb->get_results("SELECT pollq_id, pollq_question FROM $wpdb->pollsq ORDER BY pollq_id DESC");
                if( 
$polls != false ) {
                    foreach( (array) 
$polls as $poll ) {
                        if( 
in_arrayintval($poll->pollq_id), explode(','$multiplepolls)) ) {
                            echo 
'<option value="'.intval($poll->pollq_id).'" selected="selected">'stripslashes($poll->pollq_question) .'</option>'."\n";
                        } else {
                            echo 
'<option value="'.intval($poll->pollq_id).'">'stripslashes($poll->pollq_question) .'</option>'."\n";
                        }
                    }
                }
                
?>
            </select>
        </p>
        
        <input type="hidden" name="widget-poll[<?php echo $number?>][submit]" value="1" />
        <?php
    
}

    
/**
     * Register poll widget on startup.
     */
    
function multi_widget_poll_register() {
        if ( !
$options get_option('multi_widget_polls') )
            
$options = array();
        
$widget_ops = array('classname' => 'widget_poll''description' => __('Display a poll (multi-widget).'));
        
$control_ops = array('width' => 400'height' => 300'id_base' => 'poll');
        
$name __('Poll');
    
        
$id false;
        foreach ( (array) 
array_keys($options) as $o ) {
            
// Old widgets can have null values for some reason
            
if ( !isset($options[$o]['title']) )
                continue;
                
            
$id "poll-$o"// Never never never translate an id
            
wp_register_sidebar_widget($id$name'multi_widget_polls'$widget_ops, array( 'number' => $o ));
            
wp_register_widget_control($id$name'multi_widget_polls_options'$control_ops, array( 'number' => $o ));
        }
    
        
// If there are none, we register the widget's existance with a generic template
        
if ( !$id ) {
            
wp_register_sidebar_widget'poll-1'$name'multi_widget_polls'$widget_ops, array( 'number' => -) );
            
wp_register_widget_control'poll-1'$name'multi_widget_polls_options'$control_ops, array( 'number' => -) );
        }
    }
    
    
multi_widget_poll_register();
}

### Function: Load The Multi WP-Polls Widget
add_action('plugins_loaded''multi_widget_polls_init');
?>