';
+ return $visual;
+ }
+ }
+}
+
+// Print todays events
+function todays_events()
+{
+ global $wpdb;
+
+ // This function cannot be called unless calendar is up to date
+ check_calendar();
+
+ // Find out if we should be displaying todays events
+ $display = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_todays'",0,0);
+
+ if ($display == 'true')
+ {
+ $output = '
';
+ if (count($events) != 0)
+ {
+ return $output;
+ }
+ }
+}
+
+// Function to compare time in event objects
+function time_cmp($a, $b)
+{
+ if ($a->event_time == $b->event_time) {
+ return 0;
+ }
+ return ($a->event_time < $b->event_time) ? -1 : 1;
+}
+
+// Used to draw multiple events
+function draw_events($events)
+{
+ // We need to sort arrays of objects by time
+ usort($events, "time_cmp");
+
+ // Now process the events
+ foreach($events as $event)
+ {
+ $output .= draw_event($event);
+ }
+ return $output;
+}
+
+// Widget todays events
+function todays_events_widget() {
+ global $wpdb;
+
+ // This function cannot be called unless calendar is up to date
+ check_calendar();
+
+ // Find out if we should be displaying todays events
+ $display = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_todays'",0,0);
+
+ if ($display == 'true')
+ {
+ $output = '
';
+ if (count($events) != 0)
+ {
+ return $output;
+ }
+ }
+}
+
+// Widget upcoming events
+function upcoming_events_widget() {
+ global $wpdb;
+
+ // This function cannot be called unless calendar is up to date
+ check_calendar();
+
+ // Find out if we should be displaying upcoming events
+ $display = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_upcoming'",0,0);
+
+ if ($display == 'true')
+ {
+ // Get number of days we should go into the future
+ $future_days = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_upcoming_days'",0,0);
+ $day_count = 1;
+
+ while ($day_count < $future_days+1)
+ {
+ list($y,$m,$d) = split("-",date("Y-m-d",mktime($day_count*24,0,0,date("m"),date("d"),date("Y"))));
+ $events = grab_events($y,$m,$d);
+ usort($events, "time_cmp");
+ if (count($events) != 0) {
+ $output .= '
';
+ return $visual;
+ }
+ }
+}
+
+// The widget to show todays events in the sidebar
+function widget_init_calendar_today() {
+ // Check for required functions
+ if (!function_exists('register_sidebar_widget'))
+ return;
+
+ function widget_calendar_today($args) {
+ extract($args);
+ $the_title = get_option('calendar_today_widget_title');
+ $widget_title = empty($the_title) ? __('Today\'s Events','calendar') : $the_title;
+ $the_events = todays_events_widget();
+ if ($the_events != '') {
+ echo $before_widget;
+ echo $before_title . $widget_title . $after_title;
+ echo $the_events;
+ echo $after_widget;
+ }
+ }
+
+ function widget_calendar_today_control() {
+ $widget_title = get_option('calendar_today_widget_title');
+ if (isset($_POST['calendar_today_widget_title'])) {
+ update_option('calendar_today_widget_title',strip_tags($_POST['calendar_today_widget_title']));
+ }
+ ?>
+
+
+
+
+
+
+
+ get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_author'",0,0);
+ $show_cat = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='enable_categories'",0,0);
+
+ if ($show_cat == 'true')
+ {
+ $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=".$event->event_category;
+ $cat_details = $wpdb->get_row($sql);
+ $style = "background-color:".$cat_details->category_colour.";";
+ }
+
+ $header_details .= '
'.$event->event_title.'
';
+ if ($event->event_time != "00:00:00")
+ {
+ $header_details .= ''.__('Time','calendar').': ' . date(get_option('time_format'), strtotime($event->event_time)) . ' ';
+ }
+ if ($display_author == 'true')
+ {
+ $e = get_userdata($event->event_author);
+ $header_details .= ''.__('Posted by', 'calendar').': '.$e->display_name.' ';
+ }
+ if ($display_author == 'true' || $event->event_time != "00:00:00")
+ {
+ $header_details .= '';
+ }
+ if ($event->event_link != '') { $linky = $event->event_link; }
+ else { $linky = '#'; }
+
+ $details = '
+* ' . $event->event_title . '' . $header_details . '' . $event->event_desc . '';
+
+ return $details;
+}
+
+// Draw an event but customise the HTML for use in the widget
+function draw_widget_event($event)
+{
+ global $wpdb;
+
+ // Calendar must be updated to run this function
+ check_calendar();
+
+ // Before we do anything we want to know if we
+ // should display the author and/or show categories.
+ // We check for this later
+ $display_author = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_author'",0,0);
+ $show_cat = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='enable_categories'",0,0);
+
+ if ($show_cat == 'true')
+ {
+ $sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=".$event->event_category;
+ $cat_details = $wpdb->get_row($sql);
+ $style = "background-color:".$cat_details->category_colour.";";
+ }
+
+ $header_details .= '
'.$event->event_title.'
';
+ if ($event->event_time != "00:00:00")
+ {
+ $header_details .= ''.__('Time','calendar').': ' . date(get_option('time_format'), strtotime($event->event_time)) . ' ';
+ }
+ if ($display_author == 'true')
+ {
+ $e = get_userdata($event->event_author);
+ $header_details .= ''.__('Posted by','calendar').': '.$e->display_name.' ';
+ }
+ if ($display_author == 'true' || $event->event_time != "00:00:00")
+ {
+ $header_details .= '';
+ }
+ if ($event->event_link != '') { $linky = $event->event_link; }
+ else { $linky = '#'; }
+
+ $details = '' . $event->event_title . '' . $header_details . '' . $event->event_desc . '';
+
+ return $details;
+}
+
+// Grab all events for the requested date from calendar
+function grab_events($y,$m,$d)
+{
+ global $wpdb;
+
+ $arr_events = array();
+
+ // Get the date format right
+ $date = $y . '-' . $m . '-' . $d;
+
+ // Firstly we check for conventional events. These will form the first instance of a recurring event
+ // or the only instance of a one-off event
+ $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_begin <= '$date' AND event_end >= '$date' AND event_recur = 'S' ORDER BY event_id");
+ if (!empty($events))
+ {
+ foreach($events as $event)
+ {
+ array_push($arr_events, $event);
+ }
+ }
+
+ // Even if there were results for that query, we may still have events recurring
+ // from the past on this day. We now methodically check the for these events
+
+ /*
+ The yearly code - easy because the day and month will be the same, so we return all yearly
+ events that match the date part. Out of these we show those with a repeat of 0, and fast-foward
+ a number of years for those with a value more than 0. Those that land in the future are displayed.
+ */
+
+
+ // Deal with forever recurring year events
+ $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'Y' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM event_begin) AND event_repeats = 0 ORDER BY event_id");
+
+ if (!empty($events))
+ {
+ foreach($events as $event)
+ {
+ // This is going to get complex so lets setup what we would place in for
+ // an event so we can drop it in with ease
+
+ // Technically we don't care about the years, but we need to find out if the
+ // event spans the turn of a year so we can deal with it appropriately.
+ $year_begin = date('Y',strtotime($event->event_begin));
+ $year_end = date('Y',strtotime($event->event_end));
+
+ if ($year_begin == $year_end)
+ {
+ if (date('m-d',strtotime($event->event_begin)) <= date('m-d',strtotime($date)) &&
+ date('m-d',strtotime($event->event_end)) >= date('m-d',strtotime($date)))
+ {
+ array_push($arr_events, $event);
+ }
+ }
+ else if ($year_begin < $year_end)
+ {
+ if (date('m-d',strtotime($event->event_begin)) <= date('m-d',strtotime($date)) ||
+ date('m-d',strtotime($event->event_end)) >= date('m-d',strtotime($date)))
+ {
+ array_push($arr_events, $event);
+ }
+ }
+ }
+ }
+
+ // Now the ones that happen a finite number of times
+ $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'Y' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM event_begin) AND event_repeats != 0 AND (EXTRACT(YEAR FROM '$date')-EXTRACT(YEAR FROM event_begin)) <= event_repeats ORDER BY event_id");
+ if (!empty($events))
+ {
+ foreach($events as $event)
+ {
+ // This is going to get complex so lets setup what we would place in for
+ // an event so we can drop it in with ease
+
+ // Technically we don't care about the years, but we need to find out if the
+ // event spans the turn of a year so we can deal with it appropriately.
+ $year_begin = date('Y',strtotime($event->event_begin));
+ $year_end = date('Y',strtotime($event->event_end));
+
+ if ($year_begin == $year_end)
+ {
+ if (date('m-d',strtotime($event->event_begin)) <= date('m-d',strtotime($date)) &&
+ date('m-d',strtotime($event->event_end)) >= date('m-d',strtotime($date)))
+ {
+ array_push($arr_events, $event);
+ }
+ }
+ else if ($year_begin < $year_end)
+ {
+ if (date('m-d',strtotime($event->event_begin)) <= date('m-d',strtotime($date)) ||
+ date('m-d',strtotime($event->event_end)) >= date('m-d',strtotime($date)))
+ {
+ array_push($arr_events, $event);
+ }
+ }
+ }
+ }
+
+ /*
+ The monthly code - just as easy because as long as the day of the month is correct, then we
+ show the event
+ */
+
+ // The monthly events that never stop recurring
+ $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'M' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM event_begin) AND event_repeats = 0 ORDER BY event_id");
+ if (!empty($events))
+ {
+ foreach($events as $event)
+ {
+ // This is going to get complex so lets setup what we would place in for
+ // an event so we can drop it in with ease
+
+ // Technically we don't care about the years or months, but we need to find out if the
+ // event spans the turn of a year or month so we can deal with it appropriately.
+ $month_begin = date('m',strtotime($event->event_begin));
+ $month_end = date('m',strtotime($event->event_end));
+
+ if ($month_begin == $month_end)
+ {
+ if (date('d',strtotime($event->event_begin)) <= date('d',strtotime($date)) &&
+ date('d',strtotime($event->event_end)) >= date('d',strtotime($date)))
+ {
+ array_push($arr_events, $event);
+ }
+ }
+ else if ($month_begin < $month_end)
+ {
+ if ( ($event->event_begin <= date('Y-m-d',strtotime($date))) && (date('d',strtotime($event->event_begin)) <= date('d',strtotime($date)) ||
+ date('d',strtotime($event->event_end)) >= date('d',strtotime($date))) )
+ {
+ array_push($arr_events, $event);
+ }
+ }
+ }
+ }
+
+
+ // Now the ones that happen a finite number of times
+ $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'M' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM event_begin) AND event_repeats != 0 AND (PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM '$date'),EXTRACT(YEAR_MONTH FROM event_begin))) <= event_repeats ORDER BY event_id");
+ if (!empty($events))
+ {
+ foreach($events as $event)
+ {
+ // This is going to get complex so lets setup what we would place in for
+ // an event so we can drop it in with ease
+
+ // Technically we don't care about the years or months, but we need to find out if the
+ // event spans the turn of a year or month so we can deal with it appropriately.
+ $month_begin = date('m',strtotime($event->event_begin));
+ $month_end = date('m',strtotime($event->event_end));
+
+ if ($month_begin == $month_end)
+ {
+ if (date('d',strtotime($event->event_begin)) <= date('d',strtotime($date)) &&
+ date('d',strtotime($event->event_end)) >= date('d',strtotime($date)))
+ {
+ array_push($arr_events, $event);
+ }
+ }
+ else if ($month_begin < $month_end)
+ {
+ if ( ($event->event_begin <= date('Y-m-d',strtotime($date))) && (date('d',strtotime($event->event_begin)) <= date('d',strtotime($date)) ||
+ date('d',strtotime($event->event_end)) >= date('d',strtotime($date))) )
+ {
+ array_push($arr_events, $event);
+ }
+ }
+ }
+ }
+
+
+ /*
+ Weekly - well isn't this fun! We need to scan all weekly events, find what day they fell on
+ and see if that matches the current day. If it does, we check to see if the repeats are 0.
+ If they are, display the event, if not, we fast forward from the original day in week blocks
+ until the number is exhausted. If the date we arrive at is in the future, display the event.
+ */
+
+ // The weekly events that never stop recurring
+ $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'W' AND '$date' >= event_begin AND event_repeats = 0 ORDER BY event_id");
+ if (!empty($events))
+ {
+ foreach($events as $event)
+ {
+ // This is going to get complex so lets setup what we would place in for
+ // an event so we can drop it in with ease
+
+ // Now we are going to check to see what day the original event
+ // fell on and see if the current date is both after it and on
+ // the correct day. If it is, display the event!
+ $day_start_event = date('D',strtotime($event->event_begin));
+ $day_end_event = date('D',strtotime($event->event_end));
+ $current_day = date('D',strtotime($date));
+
+ $plan = array();
+ $plan['Mon'] = 1;
+ $plan['Tue'] = 2;
+ $plan['Wed'] = 3;
+ $plan['Thu'] = 4;
+ $plan['Fri'] = 5;
+ $plan['Sat'] = 6;
+ $plan['Sun'] = 7;
+
+ if ($plan[$day_start_event] > $plan[$day_end_event])
+ {
+ if (($plan[$day_start_event] <= $plan[$current_day]) || ($plan[$current_day] <= $plan[$day_end_event]))
+ {
+ array_push($arr_events, $event);
+ }
+ }
+ else if (($plan[$day_start_event] < $plan[$day_end_event]) || ($plan[$day_start_event]== $plan[$day_end_event]))
+ {
+ if (($plan[$day_start_event] <= $plan[$current_day]) && ($plan[$current_day] <= $plan[$day_end_event]))
+ {
+ array_push($arr_events, $event);
+ }
+ }
+
+ }
+ }
+
+ // The weekly events that have a limit on how many times they occur
+ $events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_recur = 'W' AND '$date' >= event_begin AND event_repeats != 0 AND (event_repeats*7) >= (TO_DAYS('$date') - TO_DAYS(event_end)) ORDER BY event_id");
+ if (!empty($events))
+ {
+ foreach($events as $event)
+ {
+ // This is going to get complex so lets setup what we would place in for
+ // an event so we can drop it in with ease
+
+ // Now we are going to check to see what day the original event
+ // fell on and see if the current date is both after it and on
+ // the correct day. If it is, display the event!
+ $day_start_event = date('D',strtotime($event->event_begin));
+ $day_end_event = date('D',strtotime($event->event_end));
+ $current_day = date('D',strtotime($date));
+
+ $plan = array();
+ $plan['Mon'] = 1;
+ $plan['Tue'] = 2;
+ $plan['Wed'] = 3;
+ $plan['Thu'] = 4;
+ $plan['Fri'] = 5;
+ $plan['Sat'] = 6;
+ $plan['Sun'] = 7;
+
+ if ($plan[$day_start_event] > $plan[$day_end_event])
+ {
+ if (($plan[$day_start_event] <= $plan[$current_day]) || ($plan[$current_day] <= $plan[$day_end_event]))
+ {
+ array_push($arr_events, $event);
+ }
+ }
+ else if (($plan[$day_start_event] < $plan[$day_end_event]) || ($plan[$day_start_event]== $plan[$day_end_event]))
+ {
+ if (($plan[$day_start_event] <= $plan[$current_day]) && ($plan[$current_day] <= $plan[$day_end_event]))
+ {
+ array_push($arr_events, $event);
+ }
+ }
+
+ }
+ }
+
+ return $arr_events;
+}
+
+
+// Actually do the printing of the calendar
+// Compared to searching for and displaying events
+// this bit is really rather easy!
+function calendar()
+{
+ global $wpdb;
+
+ // First things first, make sure calendar is up to date
+ check_calendar();
+
+ // Deal with the week not starting on a monday
+ if (get_option('start_of_week') == 0)
+ {
+ $name_days = array(1=>__('Sunday','calendar'),__('Monday','calendar'),__('Tuesday','calendar'),__('Wednesday','calendar'),__('Thursday','calendar'),__('Friday','calendar'),__('Saturday','calendar'));
+ }
+ // Choose Monday if anything other than Sunday is set
+ else
+ {
+ $name_days = array(1=>__('Monday','calendar'),__('Tuesday','calendar'),__('Wednesday','calendar'),__('Thursday','calendar'),__('Friday','calendar'),__('Saturday','calendar'),__('Sunday','calendar'));
+ }
+
+ // Carry on with the script
+ $name_months = array(1=>__('January','calendar'),__('February','calendar'),__('March','calendar'),__('April','calendar'),__('May','calendar'),__('June','calendar'),__('July','calendar'),__('August','calendar'),__('September','calendar'),__('October','calendar'),__('November','calendar'),__('December','calendar'));
+
+ // If we don't pass arguments we want a calendar that is relevant to today
+ if (empty($_GET['month']) || empty($_GET['yr']))
+ {
+ $c_year = date("Y");
+ $c_month = date("m");
+ $c_day = date("d");
+ }
+
+ // Years get funny if we exceed 3000, so we use this check
+ if ($_GET['yr'] <= 3000 && $_GET['yr'] >= 0)
+ {
+ // This is just plain nasty and all because of permalinks
+ // which are no longer used, this will be cleaned up soon
+ if ($_GET['month'] == 'jan' || $_GET['month'] == 'feb' || $_GET['month'] == 'mar' || $_GET['month'] == 'apr' || $_GET['month'] == 'may' || $_GET['month'] == 'jun' || $_GET['month'] == 'jul' || $_GET['month'] == 'aug' || $_GET['month'] == 'sept' || $_GET['month'] == 'oct' || $_GET['month'] == 'nov' || $_GET['month'] == 'dec')
+ {
+
+ // Again nasty code to map permalinks into something
+ // databases can understand. This will be cleaned up
+ $c_year = mysql_escape_string($_GET['yr']);
+ if ($_GET['month'] == 'jan') { $t_month = 1; }
+ else if ($_GET['month'] == 'feb') { $t_month = 2; }
+ else if ($_GET['month'] == 'mar') { $t_month = 3; }
+ else if ($_GET['month'] == 'apr') { $t_month = 4; }
+ else if ($_GET['month'] == 'may') { $t_month = 5; }
+ else if ($_GET['month'] == 'jun') { $t_month = 6; }
+ else if ($_GET['month'] == 'jul') { $t_month = 7; }
+ else if ($_GET['month'] == 'aug') { $t_month = 8; }
+ else if ($_GET['month'] == 'sept') { $t_month = 9; }
+ else if ($_GET['month'] == 'oct') { $t_month = 10; }
+ else if ($_GET['month'] == 'nov') { $t_month = 11; }
+ else if ($_GET['month'] == 'dec') { $t_month = 12; }
+ $c_month = $t_month;
+ $c_day = date("d");
+ }
+ // No valid month causes the calendar to default to today
+ else
+ {
+ $c_year = date("Y");
+ $c_month = date("m");
+ $c_day = date("d");
+ }
+ }
+ // No valid year causes the calendar to default to today
+ else
+ {
+ $c_year = date("Y");
+ $c_month = date("m");
+ $c_day = date("d");
+ }
+
+ // Fix the days of the week if week start is not on a monday
+ if (get_option('start_of_week') == 0)
+ {
+ $first_weekday = date("w",mktime(0,0,0,$c_month,1,$c_year));
+ $first_weekday = ($first_weekday==0?1:$first_weekday+1);
+ }
+ // Otherwise assume the week starts on a Monday. Anything other
+ // than Sunday or Monday is just plain odd
+ else
+ {
+ $first_weekday = date("w",mktime(0,0,0,$c_month,1,$c_year));
+ $first_weekday = ($first_weekday==0?7:$first_weekday);
+ }
+
+ $days_in_month = date("t", mktime (0,0,0,$c_month,1,$c_year));
+
+ // Start the table and add the header and naviagtion
+ $calendar_body .= '
+
+';
+
+ // We want to know if we should display the date switcher
+ $date_switcher = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_jump'",0,0);
+
+ if ($date_switcher == 'true')
+ {
+ $calendar_body .= '
+
+
+
+
+';
+ }
+
+ // The header of the calendar table and the links. Note calls to link functions
+ $calendar_body .= '
+
+
+
+
' . prev_link($c_year,$c_month) . '
+
'.$name_months[(int)$c_month].' '.$c_year.'
+
' . next_link($c_year,$c_month) . '
+
+
+
+
+';
+
+ // Print the headings of the days of the week
+ $calendar_body .= '
+';
+ for ($i=1; $i<=7; $i++)
+ {
+ // Colours need to be different if the starting day of the week is different
+ if (get_option('start_of_week') == 0)
+ {
+ $calendar_body .= '
+';
+
+ // A little link to yours truely. See the README if you wish to remove this
+ $calendar_body .= '
'.__('Calendar developed and supported by ', 'calendar').'Kieran O\'Shea
+';
+
+ // Phew! After that bit of string building, spit it all out.
+ // The actual printing is done by the calling function.
+ return $calendar_body;
+}
+
+?>