<?php
/*
Plugin Name: Agenda Widget
Plugin URI: http://wordpress.org/extend/plugins/agenda/
Description: Show your events on the sidebar.
Version: 1.0
Author: Steven Goos
Author URI: http://gooos.nl
Author Email: support+wp-plugin-agenda [at] golabs [dot] nl
*/

function widget_agenda_init() {

    if ( !
function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
        return;

    
// Function to output widget content
    
function widget_agenda($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_agenda');
        
    
$title $options['title'];
    
$next $options['next'];
    
$highlighted_only $options['highlighted_only'];
    
$show_date $options['show_date'];
    
$group_by_date $options['group_by_date'];
    
$before $options['before'];
    
$after $options['after'];
    
$title_tag $options['title_tag'];
    
$print $options['print'];

    
// These lines generate the actual output.
    
echo $before_widget $before_title $title $after_title;
    echo 
'<ul>';
    if ( 
$options['group_by_date'] == ) {
      
agenda_compromissos ($next$title_tag$print);
    } else {
      
agenda_list ($next$highlighted_only$show_date$before$after$print);
    }
    echo 
'</ul>';
    echo 
$after_widget;
    }

  
// Function to output settings module
  
function widget_agenda_control() {

    
// Get our options and see if we're handling a form submission.
    
$options get_option('widget_agenda');

    
// Set the default options for the widget here.
    
if ( !is_array($options) ) {
      
$options['title'] = 'Agenda';        // Print title above the widget section in the sidebar. 
      
$options['next'] = '10';             // Defines the quantity of next events that will be showed (default: 10).
      
$options['highlighted_only'] = '0';  // If TRUE only highlighted events will be shown, if FALSE then all events are shown (hightlighted events are marked as strong) (default: FALSE).
      
$options['show_date'] = '1';         // If TRUE it will shows each date events (default: TRUE).
      
$options['group_by_date'] = '1';     // If TRUE it will show events grouped by date, if FALSE then just as a list (default: TRUE).
      
$options['before'] = '<li>';         // Will be written before each event (default: <li>). Only for agenda_list()!!
      
$options['after'] = '</li>';         // Will be written after each event (default: <li>). Only for agenda_list()!!
      
$options['title_tag'] = 'strong';    // Element tag when showing date tag's name (defaut: h3). Only for agenda_compromissos()!!
      
$options['print'] = '1';             // If TRUE then print the result, if FALSE return the result string for future manipulations (default: TRUE).    
    
}
    
    if ( 
$_POST['agenda-submit'] ) {
    
// Remember to sanitize and format use input appropriately.
      
$options['title'] = strip_tags(stripslashes($_POST['agenda-title']));
      
$options['next'] = !empty($_POST['next']) ? strip_tags(stripslashes($_POST['next'])) : '10';
      
$options['highlighted_only'] = isset($_POST['highlighted_only']) ? '1' '0';
      
$options['show_date'] = isset($_POST['show_date']) ? '1' '0';
      
$options['group_by_date'] = isset($_POST['group_by_date']) ? '1' '0';
      
$options['before'] = !empty($_POST['before']) ? $_POST['before'] : '<li>';
      
$options['after'] = !empty($_POST['after']) ? $_POST['after'] : '</li>';
      
$options['title_tag'] = !empty($_POST['title_tag']) ? strip_tags(stripslashes($_POST['title_tag'])) : 'strong';
      
$options['print'] = '1'//isset($_POST['print']) ? '1' : '0'; // Currently not in the widget.
            
      
update_option('widget_agenda'$options);
    }

    
// Be sure you format your options to be valid HTML attributes.
    
$title htmlspecialchars($options['title'], ENT_QUOTES);
    
$next =  htmlspecialchars($options['next'], ENT_QUOTES);
    
$highlighted_only $options['highlighted_only'] == '1' 'checked="checked"' '';
    
$show_date $options['show_date'] == '1' 'checked="checked"' '';
    
$group_by_date $options['group_by_date'] == '1' 'checked="checked"' '';
    
$before $options['before'];
    
$after $options['after'];
    
$title_tag htmlspecialchars($options['title_tag'], ENT_QUOTES);
    
$print $options['print'] == '1' 'checked="checked"' '';
    
    
// Form segment to change the widget settings.
    
echo '
    <p style="text-align:left;"><label for="agenda-title">'
.__('Title:''agenda').' <input style="width: 240px;" id="agenda-title" name="agenda-title" type="text" value="'.$title.'" /></label></p>
    <p style="text-align:left;"><label for="next">'
.__('Number of events to show:' 'agenda').'&nbsp;<input style="width: 25px; text-align: center;" id="next" name="next" type="text" value="'.$next.'" /></label></p>
    <p style="text-align:left;"><label for="highlighted_only">'
.__('Highlighted only?''agenda').'&nbsp;<input class="checkbox" type="checkbox" '.$highlighted_only.' id="highlighted_only" name="highlighted_only" /></label></p>
    <p style="text-align:left;"><label for="show_date">'
.__('Show date?''agenda').'&nbsp;<input class="checkbox" type="checkbox" '.$show_date.' id="show_date" name="show_date" /></label></p>
    <p style="text-align:left;"><label for="group_by_date">'
.__('Group by date?''agenda').'&nbsp;<input class="checkbox" type="checkbox" '.$group_by_date.' id="group_by_date" name="group_by_date" /></label></p>
    <p style="text-align:left; float:left;"><label for="before">'
.__('Before (&lt;li&gt;):''agenda').'<br /><input style="width: 115px;" id="before" name="before" type="text" value="'.$before.'" /></label></p>
    <p style="text-align:left; float:right;"><label for="after">'
.__('After (&lt;/li&gt;):''agenda').'<br /><input style="width: 115px;" id="after" name="after" type="text" value="'.$after.'" /></label></p>
    <p style="clear:both; text-align:left;"><label for="Title tag">'
.__('Date title element tag (strong):''agenda').'<br /><input style="width: 240px;" id="title_tag" name="title_tag" type="text" value="'.$title_tag.'" /></label></p>
    <input type="hidden" id="agenda-submit" name="agenda-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(array('Agenda''widgets'), 'widget_agenda');

  
// This registers our optional widget control form. Because of this
  // our widget will have a button that reveals a 300x100 pixel form.
  
register_widget_control(array('Agenda''widgets'), 'widget_agenda_control');
}

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

?>