<?php
/*
Plugin Name: Irontec - Language Widget
Plugin URI: http://www.irontec.com
Description: Adds a language drop down menu to display the languages. Uses cookies.
Version: 0.1
Author: Irontec
Author URI: http://www.irontec.com
License: GNU General Public License 2.0 (GPL) http://www.gnu.org/licenses/gpl.html
*/
ob_start();

$availableLanguages = array('eu', 'es', 'en'); // EN orden de prioridad, por si un navegador acepta todos, cual escogemos antes.
$_lang = "es";
$langISOS = array(
						'eu'=>'eu_EU',
						'es'=>'es_ES',
						'en'=>'en_EN');


switch(true) {
	case ( (isset($_POST['lang'])) && (in_array($_POST['lang'],$availableLanguages)) ) :
		$_lang = $_POST['lang'];
	break;
	case ( (isset($_GET['lang'])) && (in_array($_GET['lang'],$availableLanguages)) ) :
		$_lang = $_GET['lang'];
	break;
	case ( (isset($_COOKIE['LANG'])) && (in_array($_COOKIE['LANG'],$availableLanguages)) ):
		$_lang = $_COOKIE['LANG'];
	break;

	case (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])):
		$_temp_langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
		foreach ($_temp_langs as $value){
			$choice=substr($value,0,2);
         if(in_array($choice, $availableLanguages)){
         	$_lang = $choice;
				break;
         }
     }
   break;
}

setcookie('LANG', '', 0, '/');
setcookie('LANG', $_lang, 0, '/' );
$_COOKIE['LANG']=$_lang; // EN el primer viaje, la superglobal $_COOKIE no se llena.
define('WPLANG', $langISOS[$_lang]);

ob_end_flush();

class Irontec_Language_Widget extends WP_Widget {
    /** constructor */
    function Irontec_Language_Widget() {
        parent::WP_Widget(false, $name = 'Irontec Language');
    }

    /** @see WP_Widget::widget */
	function widget($args, $instance) {
        extract( $args );
        ?>

        <?php echo $before_widget; ?>

        <?php
        // change the title
        if ($instance['display_title'] == 1) {
        	$title = apply_filters('widget_title', $instance['title']);
        	if ( $title ) echo $before_title . $title . $after_title;
        }
		?>

		<?php // Display the Language Drop Down ?>
		<div class="sidebar-content">
			<form name="lang-form" method="post" action="">
				<select name="lang">
					<option value="es" <?php if ($_COOKIE['LANG'] == 'es' || $_COOKIE['LANG'] == '' || $_POST['lang'] == 'es') echo ' selected'; ?>>Castellano</option>
					<option value="eu" <?php if ($_COOKIE['LANG'] == 'eu' || $_POST['lang'] == 'eu') echo ' selected'; ?>>Euskera</option>
				</select>
				<input type="submit" name="lang-form-submit" value="<?php _e( 'Cambiar', 'buddypress' ); ?>">
			</form>
		</div>
		<?php echo $after_widget; ?>

	<?php
	}

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['number'] = strip_tags($new_instance['number']);
		$instance['avatar_size'] = strip_tags($new_instance['avatar_size']);
		$instance['display_showall'] = intval($new_instance['display_showall']);
		$instance['display_search'] = intval($new_instance['display_search']);
		$instance['links_valign'] = strip_tags($new_instance['links_valign']);
		$instance['links_align'] = strip_tags($new_instance['links_align']);

		return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {
		$title = esc_attr($instance['title']);
		$display_title = esc_attr($instance['display_title']);
        ?>
		<p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php if ($title) echo $title; ?>" />
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('display_title'); ?>"><?php _e('Display Title ?'); ?></label>
          <input id="<?php echo $this->get_field_id('display_title'); ?>" name="<?php echo $this->get_field_name('display_title'); ?>" type="checkbox" value="1" <?php if ($display_title==1) echo "checked"; ?> />
        </p>
	<?php }
}

add_action('widgets_init', create_function('', 'return register_widget("Irontec_Language_Widget");'));
?>
