__('Displays events upcoming in the following weeks.', AEC_PLUGIN_NAME));
parent::WP_Widget(false, __('Upcoming Events', AEC_PLUGIN_NAME), $widget_ops);
}
function widget($args, $instance){
extract($args, EXTR_SKIP);
$weeks = ($instance['weeks']) ? apply_filters('widget_weeks', $instance['weeks']) : 3;
$category = ($instance['category']) ? apply_filters('widget_category', $instance['category']) : 'all';
$events = $this->get_events($weeks,$category);
echo $before_widget;
$es = sizeof($events);
echo $before_title . sprintf(_n('(%d) Upcoming Event','(%d) Upcoming Events', $es, AEC_PLUGIN_NAME), $es) . $after_title;
$out = '
';
if ($events){
foreach ($events as $event){
list($start_date, $start_time) = str_split($event->start, 10);
list($end_date, $end_time) = str_split($event->end, 10);
$start_time = trim($start_time);
$end_time = trim($end_time);
//$out .= '- ';
$out .= '
- ';
if ($start_date != $end_date) {
if ($event->allday) {
$out .= $start_date;
$out .= ' - ' . $end_date;
} else {
$out .= $event->start;
$out .= '
' . $event->end;
}
} else {
if ($event->allday) {
$out .= $start_date . ' (' . __('All Day', AEC_PLUGIN_NAME) . ')';
} else {
$out .= $start_date . ' (' . $start_time . ' - ' . $end_time . ')';
}
}
$out .= '
' . $event->title . ' ';
}
}else{
$out .= '- ';
$out .= sprintf(_n('No events listed in the next week', 'No events listed in the next %d weeks', $weeks, AEC_PLUGIN_NAME), $weeks);
$out .= '
';
}
$out .= '
';
echo $out;
echo $after_widget;
}
function get_events($duration, $category_id){
global $wpdb;
$week = 604800;
$start = date('Y-m-d');
$end = date('Y-m-d', strtotime($start) + ($duration * $week));
$andcategory = ($category_id=="all") ? '' : ' AND category_id = ' . $category_id;
$results = $wpdb->get_results('SELECT
id,
title,
start,
end,
allday,
category_id
FROM ' . $wpdb->prefix . AEC_EVENT_TABLE . '
WHERE ((start >= "' . $start . '"
AND start < "' . $end . '")
OR (end >= "' . $start . '"
AND end < "' . $end . '")
OR (start < "' . $start . '"
AND end > "' . $end . '"))' .
$andcategory .
' ORDER BY start;'
);
if ($results !== false) return $results;
}
function get_categories(){
global $wpdb;
$results = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . AEC_CATEGORY_TABLE . ' ORDER BY id;');
if ($results !== false) return $results;
}
function update($new_instance, $old_instance){
$instance = $old_instance;
$instance['weeks'] = $new_instance['weeks'];
$instance['category'] = $new_instance['category'];
return $instance;
}
/** @see WP_Widget::form */
function form($instance){
$instance = wp_parse_args( (array) $instance, array( 'weeks' => '2', 'category' => 'all') );
$weeks = $instance['weeks'];
$category = $instance['category'];
?>