<?php
/*
Plugin Name: weatherpixie widget
Description: Adds a sidebar widget for the weatherpixie from Weatherpixie.com
Author: Paul "TBBle" Hampson
Version: 1.0
Author URI: http://blog.tbble.net/

Based on gsearch.php (Google Search widget) by Automattic, Inc. (http://automattic.com)
*/

// Put functions into one big function we'll call at the plugins_loaded
// action. This ensures that all required plugin functions are defined.
function widget_weatherpixie_init() {

    
// Check for the required plugin functions. This will prevent fatal
    // errors occurring when you deactivate the dynamic-sidebar plugin.
    
if ( !function_exists('register_sidebar_widget') )
        return;

    
// This is the function that outputs our little Google search form.
    
function widget_weatherpixie($args) {
        
        
// $args is an array of strings that help widgets to conform to
        // the active theme: before_widget, before_title, after_widget,
        // and after_title are the array keys. Default tags: li and h2.
        
extract($args);

        
// Each widget can store its own options. We keep strings here.
        
$options get_option('widget_weatherpixie');
        
$title $options['title'];
        
$place $options['place'];
        
$trooper $options['trooper'];
        
$type $options['type'];

        
// These lines generate our output. Widgets can be very complex
        // but as you can see here, they can also be very, very simple.
        // (This is even simpler than the gsearch one...)
        
echo $before_widget $before_title $title $after_title;
        
// I don't know for sure that the <p> tags are neccessary... Ocadia needed 'em.
        
echo '<p><a href="http://weatherpixie.com/" target="_blank"><img src="http://weatherpixie.com/displayimg.php?place='.$place.'&amp;trooper='.$trooper.'&amp;type='.$type.'" style="width: 124px; height:175px;" alt="The Weatherpixie" /></a></p>';
        echo 
$after_widget;
    }

    
// This is the function that outputs the form to let the users edit
    // the widget's title. It's an optional feature that users cry for.
    
function widget_weatherpixie_control() {

        
// Get our options and see if we're handling a form submission.
        
$options get_option('widget_weatherpixie');
        if ( !
is_array($options) )
            
$options = array('title'=>"The Weatherpixie",
                
'place' => 'YSCB',
                
'trooper' => 'r',
                
'type' => 'C',
            );
        if ( 
$_POST['weatherpixie-submit'] ) {

            
// Remember to sanitize and format use input appropriately.
            
$options['title'] = strip_tags(stripslashes($_POST['weatherpixie-title']));
            
$options['place'] = strip_tags(stripslashes($_POST['weatherpixie-place']));
            
$options['trooper'] = strip_tags(stripslashes($_POST['weatherpixie-trooper']));
            
$options['type'] = strip_tags(stripslashes($_POST['weatherpixie-type']));
            
update_option('widget_weatherpixie'$options);
        }

        
// Be sure you format your options to be valid HTML attributes.
        
$title htmlspecialchars($options['title'], ENT_QUOTES);
        
$place htmlspecialchars($options['place'], ENT_QUOTES);
        
$trooper htmlspecialchars($options['trooper'], ENT_QUOTES);
        
$type htmlspecialchars($options['type'], ENT_QUOTES);
        
        
// Here is our little form segment. Notice that we don't need a
        // complete form. This will be embedded into the existing form.
        
echo '<p style="text-align:right;"><label for="weatherpixie-title">Title: <input style="width: 200px;" id="weatherpixie-title" name="weatherpixie-title" type="text" value="'.$title.'" /></label></p>';
        echo 
'<p style="text-align: left;">Go to <a href="http://weatherpixie.com/index.php?page=dir" target="_blank">http://weatherpixie.com/index.php?page=dir</a> and choose your country, and get the four-letter place code for your nearest weatherpixie garden. Then pick a pixie, and take the number after <tt>trooper=</tt> in the URL to get your trooper.</p>';
        echo 
'<p style="text-align:right;"><label for="weatherpixie-place">Place: <input style="width: 200px;" id="weatherpixie-place" name="weatherpixie-place" type="text" value="'.$place.'" /></label></p>';
        echo 
'<p style="text-align:right;"><label for="weatherpixie-trooper">Trooper: <input style="width: 200px;" id="weatherpixie-trooper" name="weatherpixie-trooper" type="text" value="'.$trooper.'" /> ("r" for random)</label></p>';
        echo 
'<p style="text-align:right;"><label for="weatherpixie-type">Box type: ';

        echo 
'<select id="weatherpixie-type" name="weatherpixie-type">';
        foreach (array(
'P' => 'deg C / Kts','F' => 'deg F / Mph','C' => 'deg C / Kph') as $key => $value) {
            echo 
'<option value="'.$key.'" ';
            if (
$key == $type)
                echo 
'selected="selected" ';
            echo 
'>'.$value.'</option>';
        }
        echo 
'</select>';

        echo 
'</label></p>';
        echo 
'<input type="hidden" id="weatherpixie-submit" name="weatherpixie-submit" value="1" />';
    }
    
    
// This registers our widget so it appears with the other available
    // widgets and can be dragged and dropped into any active sidebars.
    
register_sidebar_widget('The Weatherpixie''widget_weatherpixie');

    
// This registers our optional widget control form. Because of this
    // our widget will have a button that reveals a 300x400 pixel form.
    
register_widget_control('The Weatherpixie''widget_weatherpixie_control'300400);
}

// Run our code later in case this loads prior to any required plugins.
add_action('plugins_loaded''widget_weatherpixie_init');

?>