prefix . 'calendar');
define('WP_CALENDAR_CONFIG_TABLE', $wpdb->prefix . 'calendar_config');
define('WP_CALENDAR_CATEGORIES_TABLE', $wpdb->prefix . 'calendar_categories');
// Check ensure calendar is installed and install it if not - required for
// the successful operation of most functions called from this point on
check_calendar();
// Create a master category for Calendar and its sub-pages
add_action('admin_menu', 'calendar_menu');
// Enable the ability for the calendar to be loaded from pages
add_filter('the_content','calendar_insert');
add_filter('the_content','minical_insert');
// Enable the ability for the lists to be loaded from pages
add_filter('the_content','upcoming_insert');
add_filter('the_content','todays_insert');
// Add the function that puts style information in the header
add_action('wp_head', 'calendar_wp_head');
// Add the function that deals with deleted users
add_action('delete_user', 'deal_with_deleted_user');
// Add the widgets if we are using version 2.8
add_action('widgets_init', 'widget_init_calendar_today');
add_action('widgets_init', 'widget_init_calendar_upcoming');
add_action('widgets_init', 'widget_init_events_calendar');
// Before we get on with the functions, we need to define the initial style used for Calendar
// Function to deal with events posted by a user when that user is deleted
function deal_with_deleted_user($id)
{
global $wpdb;
// Do the query
$wpdb->get_results("UPDATE ".WP_CALENDAR_TABLE." SET event_author=".$wpdb->get_var("SELECT MIN(ID) FROM ".$wpdb->prefix."users",0,0)." WHERE event_author=".mysql_escape_string($id));
}
// Function to provide time with WordPress offset, localy replaces time()
function ctwo()
{
return (time()+(3600*(get_option('gmt_offset'))));
}
// Function to add the calendar style into the header
function calendar_wp_head()
{
global $wpdb;
$style = $wpdb->get_var("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='calendar_style'");
if ($style != '')
{
echo '
';
}
}
// Function to deal with adding the calendar menus
function calendar_menu()
{
global $wpdb;
// Set admin as the only one who can use Calendar for security
$allowed_group = 'manage_options';
// Use the database to *potentially* override the above if allowed
$configs = $wpdb->get_results("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='can_manage_events'");
if (!empty($configs))
{
foreach ($configs as $config)
{
$allowed_group = $config->config_value;
}
}
// Add the admin panel pages for Calendar. Use permissions pulled from above
if (function_exists('add_menu_page'))
{
add_menu_page(__('Calendar','calendar'), __('Calendar','calendar'), $allowed_group, 'calendar', 'edit_calendar');
}
if (function_exists('add_submenu_page'))
{
add_submenu_page('calendar', __('Manage Calendar','calendar'), __('Manage Calendar','calendar'), $allowed_group, 'calendar', 'edit_calendar');
add_action( "admin_head", 'calendar_add_javascript' );
// Note only admin can change calendar options
add_submenu_page('calendar', __('Manage Categories','calendar'), __('Manage Categories','calendar'), 'manage_options', 'calendar-categories', 'manage_categories');
add_submenu_page('calendar', __('Calendar Config','calendar'), __('Calendar Options','calendar'), 'manage_options', 'calendar-config', 'edit_calendar_config');
}
}
// Function to add the javascript to the admin header
function calendar_add_javascript()
{
echo '
';
}
// Function to deal with loading the calendar into pages
function calendar_insert($content)
{
if (preg_match('/\{CALENDAR*.+\}/',$content))
{
$cat_list = preg_split('/\{CALENDAR\;/',$content);
if (sizeof($cat_list) > 1) {
$cat_list = preg_split('/\}/',$cat_list[1]);
$cat_list = $cat_list[0];
$cal_output = calendar($cat_list);
} else {
$cal_output = calendar();
}
$content = preg_replace('/\{CALENDAR*.+\}/',$cal_output,$content);
}
return $content;
}
// Function to show a mini calendar in pages
function minical_insert($content)
{
if (preg_match('/\{MINICAL*.+\}/',$content))
{
$cat_list= preg_split('/\{MINICAL\;/',$content);
if (sizeof($cat_list) > 1) {
$cat_list = preg_split('/\}/',$cat_list[1]);
$cat_list= $cat_list[0];
$cal_output = minical($cat_list);
} else {
$cal_output = minical();
}
$content = preg_replace('/\{MINICAL*.+\}/',$cal_output,$content);
}
return $content;
}
// Functions to allow the widgets to be inserted into posts and pages
function upcoming_insert($content)
{
if (preg_match('/\{UPCOMING_EVENTS*.+\}/',$content))
{
$cat_list= preg_split('/\{UPCOMING_EVENTS\;/',$content);
if (sizeof($cat_list) > 1) {
$cat_list = preg_split('/\}/',$cat_list[1]);
$cat_list= $cat_list[0];
$cal_output = ''.upcoming_events($cat_list).'';
} else {
$cal_output = ''.upcoming_events().'';
}
$content = preg_replace('/\{UPCOMING_EVENTS*.+\}/',$cal_output,$content);
}
return $content;
}
function todays_insert($content)
{
if (preg_match('/\{TODAYS_EVENTS*.+\}/',$content))
{
$cat_list= preg_split('/\{TODAYS_EVENTS\;/',$content);
if (sizeof($cat_list) > 1) {
$cat_list = preg_split('/\}/',$cat_list[1]);
$cat_list= $cat_list[0];
$cal_output = ''.todays_events($cat_list).'';
} else {
$cal_output = ''.todays_events().'';
}
$content = preg_replace('/\{TODAYS_EVENTS*.+\}/',$cal_output,$content);
}
return $content;
}
// Function to check what version of Calendar is installed and install if needed
function check_calendar()
{
// Checks to make sure Calendar is installed, if not it adds the default
// database tables and populates them with test data. If it is, then the
// version is checked through various means and if it is not up to date
// then it is upgraded.
// Lets see if this is first run and create us a table if it is!
global $wpdb, $initial_style;
// All this style info will go into the database on a new install
// This looks nice in the TwentyTen theme
$initial_style = " .calnk a:hover {
background-position:0 0;
text-decoration:none;
color:#000000;
border-bottom:1px dotted #000000;
}
.calnk a:visited {
text-decoration:none;
color:#000000;
border-bottom:1px dotted #000000;
}
.calnk a {
text-decoration:none;
color:#000000;
border-bottom:1px dotted #000000;
}
.calnk a span {
display:none;
}
.calnk a:hover span {
color:#333333;
background:#F6F79B;
display:block;
position:absolute;
margin-top:1px;
padding:5px;
width:150px;
z-index:100;
line-height:1.2em;
}
.calendar-table {
border:0 !important;
width:100% !important;
border-collapse:separate !important;
border-spacing:2px !important;
}
.calendar-heading {
height:25px;
text-align:center;
border:1px solid #D6DED5;
background-color:#E4EBE3;
}
.calendar-next {
width:25%;
text-align:center;
}
.calendar-prev {
width:25%;
text-align:center;
}
.calendar-month {
width:50%;
text-align:center;
font-weight:bold;
}
.normal-day-heading {
text-align:center;
width:25px;
height:25px;
font-size:0.8em;
border:1px solid #DFE6DE;
background-color:#EBF2EA;
}
.weekend-heading {
text-align:center;
width:25px;
height:25px;
font-size:0.8em;
border:1px solid #DFE6DE;
background-color:#EBF2EA;
color:#FF0000;
}
.day-with-date {
vertical-align:text-top;
text-align:left;
width:60px;
height:60px;
border:1px solid #DFE6DE;
}
.no-events {
}
.day-without-date {
width:60px;
height:60px;
border:1px solid #E9F0E8;
}
span.weekend {
color:#FF0000;
}
.current-day {
vertical-align:text-top;
text-align:left;
width:60px;
height:60px;
border:1px solid #BFBFBF;
background-color:#E4EBE3;
}
span.event {
font-size:0.75em;
}
.kjo-link {
font-size:0.75em;
text-align:center;
}
.calendar-date-switcher {
height:25px;
text-align:center;
border:1px solid #D6DED5;
background-color:#E4EBE3;
}
.calendar-date-switcher form {
margin:2px;
}
.calendar-date-switcher input {
border:1px #D6DED5 solid;
margin:0;
}
.calendar-date-switcher select {
border:1px #D6DED5 solid;
margin:0;
}
.calnk a:hover span span.event-title {
padding:0;
text-align:center;
font-weight:bold;
font-size:1.2em;
margin-left:0px;
}
.calnk a:hover span span.event-title-break {
width:96%;
text-align:center;
height:1px;
margin-top:5px;
margin-right:2%;
padding:0;
background-color:#000000;
margin-left:0px;
}
.calnk a:hover span span.event-content-break {
width:96%;
text-align:center;
height:1px;
margin-top:5px;
margin-right:2%;
padding:0;
background-color:#000000;
margin-left:0px;
}
.page-upcoming-events {
font-size:80%;
}
.page-todays-events {
font-size:80%;
}
.calendar-table table,tbody,tr,td {
margin:0 !important;
padding:0 !important;
}
table.calendar-table {
margin-bottom:5px !important;
}
.cat-key {
width:100%;
margin-top:30px;
padding:5px;
border:0 !important;
}
.cal-separate {
border:0 !important;
margin-top:10px;
}
table.cat-key {
margin-top:5px !important;
border:1px solid #DFE6DE !important;
border-collapse:separate !important;
border-spacing:4px !important;
margin-left:2px !important;
width:99.5% !important;
margin-bottom:5px !important;
}
.cat-key td {
border:0 !important;
}";
// Assume this is not a new install until we prove otherwise
$new_install = false;
$vone_point_one_upgrade = false;
$vone_point_two_beta_upgrade = false;
$wp_calendar_exists = false;
$wp_calendar_config_exists = false;
$wp_calendar_config_version_number_exists = false;
// Determine the calendar version
$tables = $wpdb->get_results("show tables");
foreach ( $tables as $table )
{
foreach ( $table as $value )
{
if ( $value == WP_CALENDAR_TABLE )
{
$wp_calendar_exists = true;
}
if ( $value == WP_CALENDAR_CONFIG_TABLE )
{
$wp_calendar_config_exists = true;
// We now try and find the calendar version number
// This will be a lot easier than finding other stuff
// in the future.
$version_number = $wpdb->get_var("SELECT config_value FROM " . WP_CALENDAR_CONFIG_TABLE . " WHERE config_item='calendar_version'");
if ($version_number == "1.2")
{
$wp_calendar_config_version_number_exists = true;
}
}
}
}
if ($wp_calendar_exists == false && $wp_calendar_config_exists == false)
{
$new_install = true;
}
else if ($wp_calendar_exists == true && $wp_calendar_config_exists == false)
{
$vone_point_one_upgrade = true;
}
else if ($wp_calendar_exists == true && $wp_calendar_config_exists == true && $wp_calendar_config_version_number_exists == false)
{
$vone_point_two_beta_upgrade = true;
}
// Now we've determined what the current install is or isn't
// we perform operations according to the findings
if ( $new_install == true )
{
$sql = "CREATE TABLE " . WP_CALENDAR_TABLE . " (
event_id INT(11) NOT NULL AUTO_INCREMENT ,
event_begin DATE NOT NULL ,
event_end DATE NOT NULL ,
event_title VARCHAR(30) NOT NULL ,
event_desc TEXT NOT NULL ,
event_time TIME ,
event_recur CHAR(1) ,
event_repeats INT(3) ,
event_author BIGINT(20) UNSIGNED ,
event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1 ,
event_link TEXT ,
PRIMARY KEY (event_id)
)";
$wpdb->get_results($sql);
$sql = "CREATE TABLE " . WP_CALENDAR_CONFIG_TABLE . " (
config_item VARCHAR(30) NOT NULL ,
config_value TEXT NOT NULL ,
PRIMARY KEY (config_item)
)";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='can_manage_events', config_value='edit_posts'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_style', config_value='".$initial_style."'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_author', config_value='false'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_jump', config_value='false'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_todays', config_value='true'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming', config_value='true'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming_days', config_value=7";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
$wpdb->get_results($sql);
$sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
category_id INT(11) NOT NULL AUTO_INCREMENT,
category_name VARCHAR(30) NOT NULL ,
category_colour VARCHAR(30) NOT NULL ,
PRIMARY KEY (category_id)
)";
$wpdb->get_results($sql);
$sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
$wpdb->get_results($sql);
}
else if ($vone_point_one_upgrade == true)
{
$sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_author BIGINT(20) UNSIGNED";
$wpdb->get_results($sql);
$sql = "UPDATE ".WP_CALENDAR_TABLE." SET event_author=".$wpdb->get_var("SELECT MIN(ID) FROM ".$wpdb->prefix."users",0,0);
$wpdb->get_results($sql);
$sql = "ALTER TABLE ".WP_CALENDAR_TABLE." MODIFY event_desc TEXT NOT NULL";
$wpdb->get_results($sql);
$sql = "CREATE TABLE " . WP_CALENDAR_CONFIG_TABLE . " (
config_item VARCHAR(30) NOT NULL ,
config_value TEXT NOT NULL ,
PRIMARY KEY (config_item)
)";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='can_manage_events', config_value='edit_posts'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_style', config_value='".$initial_style."'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_author', config_value='false'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_jump', config_value='false'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_todays', config_value='true'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming', config_value='true'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='display_upcoming_days', config_value=7";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
$wpdb->get_results($sql);
$sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1";
$wpdb->get_results($sql);
$sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_link TEXT";
$wpdb->get_results($sql);
$sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
category_id INT(11) NOT NULL AUTO_INCREMENT,
category_name VARCHAR(30) NOT NULL ,
category_colour VARCHAR(30) NOT NULL ,
PRIMARY KEY (category_id)
)";
$wpdb->get_results($sql);
$sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
$wpdb->get_results($sql);
}
else if ($vone_point_two_beta_upgrade == true)
{
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='calendar_version', config_value='1.2'";
$wpdb->get_results($sql);
$sql = "INSERT INTO ".WP_CALENDAR_CONFIG_TABLE." SET config_item='enable_categories', config_value='false'";
$wpdb->get_results($sql);
$sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_category BIGINT(20) UNSIGNED NOT NULL DEFAULT 1";
$wpdb->get_results($sql);
$sql = "ALTER TABLE ".WP_CALENDAR_TABLE." ADD COLUMN event_link TEXT ";
$wpdb->get_results($sql);
$sql = "CREATE TABLE " . WP_CALENDAR_CATEGORIES_TABLE . " (
category_id INT(11) NOT NULL AUTO_INCREMENT,
category_name VARCHAR(30) NOT NULL ,
category_colour VARCHAR(30) NOT NULL ,
PRIMARY KEY (category_id)
)";
$wpdb->get_results($sql);
$sql = "INSERT INTO " . WP_CALENDAR_CATEGORIES_TABLE . " SET category_id=1, category_name='General', category_colour='#F6F79B'";
$wpdb->get_results($sql);
$sql = "UPDATE " . WP_CALENDAR_CONFIG_TABLE . " SET config_value='".$initial_style."' WHERE config_item='calendar_style'";
$wpdb->get_results($sql);
}
}
// Used on the manage events admin page to display a list of events
function wp_events_display_list(){
global $wpdb;
$events = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " ORDER BY event_begin DESC");
if ( !empty($events) )
{
?>
";
return;
}
else
{
$data = $wpdb->get_results("SELECT * FROM " . WP_CALENDAR_TABLE . " WHERE event_id='" . mysql_escape_string($event_id) . "' LIMIT 1");
if ( empty($data) )
{
echo "
".__("An event with that ID couldn't be found",'calendar')."
";
return;
}
$data = $data[0];
}
// Recover users entries if they exist; in other words if editing an event went wrong
if (!empty($users_entries))
{
$data = $users_entries;
}
}
// Deal with possibility that form was submitted but not saved due to error - recover user's entries here
else
{
$data = $users_entries;
}
?>
= strtotime($begin))
{
$start_date_ok = 1;
$end_date_ok = 1;
}
else
{
?>
';
return $visual;
}
}
}
// Print todays events
function todays_events($cat_list = '')
{
global $wpdb;
// 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");
$output = '';
// Now process the events
foreach($events as $event)
{
$output .= '* '.draw_event($event).' ';
}
return $output;
}
// The widget to show the mini calendar
function widget_init_events_calendar() {
// Check for required functions
if (!function_exists('wp_register_sidebar_widget'))
return;
function widget_events_calendar($args) {
extract($args);
$the_title = stripslashes(get_option('events_calendar_widget_title'));
$the_cats = stripslashes(get_option('events_calendar_widget_cats'));
$widget_title = empty($the_title) ? __('Calendar','calendar') : $the_title;
$the_events = minical($the_cats);
if ($the_events != '') {
echo $before_widget;
echo $before_title . $widget_title . $after_title;
echo ' '.$the_events;
echo $after_widget;
}
}
function widget_events_calendar_control() {
$widget_title = stripslashes(get_option('events_calendar_widget_title'));
$widget_cats = stripslashes(get_option('events_calendar_widget_cats'));
if (isset($_POST['events_calendar_widget_title']) || isset($_POST['events_calendar_widget_cats'])) {
update_option('events_calendar_widget_title',strip_tags($_POST['events_calendar_widget_title']));
update_option('events_calendar_widget_cats',strip_tags($_POST['events_calendar_widget_cats']));
}
?>
'A calendar of your events'));
wp_register_widget_control('events_calendar','events_calendar','widget_events_calendar_control');
}
// The widget to show todays events in the sidebar
function widget_init_calendar_today() {
// Check for required functions
if (!function_exists('wp_register_sidebar_widget'))
return;
function widget_calendar_today($args) {
extract($args);
$the_title = stripslashes(get_option('calendar_today_widget_title'));
$the_cats = stripslashes(get_option('calendar_today_widget_cats'));
$widget_title = empty($the_title) ? __('Today\'s Events','calendar') : $the_title;
$the_events = todays_events($the_cats);
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 = stripslashes(get_option('calendar_today_widget_title'));
$widget_cats = stripslashes(get_option('calendar_today_widget_cats'));
if (isset($_POST['calendar_today_widget_title']) || isset($_POST['calendar_today_widget_cats'])) {
update_option('calendar_today_widget_title',strip_tags($_POST['calendar_today_widget_title']));
update_option('calendar_today_widget_cats',strip_tags($_POST['calendar_today_widget_cats']));
}
?>
'A list of your events today'));
wp_register_widget_control('todays_events_calendar','todays_events_calendar','widget_calendar_today_control');
}
// The widget to show todays events in the sidebar
function widget_init_calendar_upcoming() {
// Check for required functions
if (!function_exists('wp_register_sidebar_widget'))
return;
function widget_calendar_upcoming($args) {
extract($args);
$the_title = stripslashes(get_option('calendar_upcoming_widget_title'));
$the_cats = stripslashes(get_option('calendar_upcoming_widget_cats'));
$widget_title = empty($the_title) ? __('Upcoming Events','calendar') : $the_title;
$the_events = upcoming_events($the_cats);
if ($the_events != '') {
echo $before_widget;
echo $before_title . $widget_title . $after_title;
echo $the_events;
echo $after_widget;
}
}
function widget_calendar_upcoming_control() {
$widget_title = stripslashes(get_option('calendar_upcoming_widget_title'));
$widget_cats = stripslashes(get_option('calendar_upcoming_widget_cats'));
if (isset($_POST['calendar_upcoming_widget_title']) || isset($_POST['calendar_upcoming_widget_cats'])) {
update_option('calendar_upcoming_widget_title',strip_tags($_POST['calendar_upcoming_widget_title']));
update_option('calendar_upcoming_widget_cats',strip_tags($_POST['calendar_upcoming_widget_cats']));
}
?>
'A list of your upcoming events'));
wp_register_widget_control('upcoming_events_calendar','upcoming_events_calendar','widget_calendar_upcoming_control');
}
// Used to draw an event to the screen
function draw_event($event)
{
global $wpdb;
// 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);
$style = '';
if ($show_cat == 'true')
{
$sql = "SELECT * FROM " . WP_CALENDAR_CATEGORIES_TABLE . " WHERE category_id=".mysql_escape_string($event->event_category);
$cat_details = $wpdb->get_row($sql);
$style = 'style="background-color:'.stripslashes($cat_details->category_colour).';"';
}
$header_details = ''.stripslashes($event->event_title).'
';
if ($event->event_time != "00:00:00")
{
$header_details .= ''.__('Time','calendar').': ' . date(get_option('time_format'), strtotime(stripslashes($event->event_time))) . ' ';
}
if ($display_author == 'true')
{
$e = get_userdata(stripslashes($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 = stripslashes($event->event_link); }
else { $linky = '#'; }
$details = '' . stripslashes($event->event_title) . '' . $header_details . '' . stripslashes($event->event_desc) . '';
return $details;
}
// Grab all events for the requested date from calendar
function grab_events($y,$m,$d,$typing,$cat_list = '')
{
global $wpdb;
$arr_events = array();
// Get the date format right
$date = $y . '-' . $m . '-' . $d;
// Format the category list
if ($cat_list == '') { $cat_sql = ''; }
else { $cat_sql = 'AND event_category in ('.$cat_list.')'; }
// The collated SQL code
$sql = "SELECT a.*,'Normal' AS type FROM " . WP_CALENDAR_TABLE . " AS a WHERE a.event_begin <= '$date' AND a.event_end >= '$date' AND a.event_recur = 'S' ".$cat_sql."
UNION ALL
SELECT b.*,'Yearly' AS type FROM " . WP_CALENDAR_TABLE . " AS b WHERE b.event_recur = 'Y' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM b.event_begin) AND b.event_repeats = 0 ".$cat_sql."
UNION ALL
SELECT c.*,'Yearly' AS type FROM " . WP_CALENDAR_TABLE . " AS c WHERE c.event_recur = 'Y' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM c.event_begin) AND c.event_repeats != 0 AND (EXTRACT(YEAR FROM '$date')-EXTRACT(YEAR FROM c.event_begin)) <= c.event_repeats ".$cat_sql."
UNION ALL
SELECT d.*,'Monthly' AS type FROM " . WP_CALENDAR_TABLE . " AS d WHERE d.event_recur = 'M' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM d.event_begin) AND d.event_repeats = 0 ".$cat_sql."
UNION ALL
SELECT e.*,'Monthly' AS type FROM " . WP_CALENDAR_TABLE . " AS e WHERE e.event_recur = 'M' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM e.event_begin) AND e.event_repeats != 0 AND (PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM '$date'),EXTRACT(YEAR_MONTH FROM e.event_begin))) <= e.event_repeats ".$cat_sql."
UNION ALL
SELECT f.*,'MonthSun' AS type FROM " . WP_CALENDAR_TABLE . " AS f WHERE f.event_recur = 'U' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM f.event_begin) AND f.event_repeats = 0 ".$cat_sql."
UNION ALL
SELECT g.*,'MonthSun' AS type FROM " . WP_CALENDAR_TABLE . " AS g WHERE g.event_recur = 'U' AND EXTRACT(YEAR FROM '$date') >= EXTRACT(YEAR FROM g.event_begin) AND g.event_repeats != 0 AND (PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM '$date'),EXTRACT(YEAR_MONTH FROM g.event_begin))) <= g.event_repeats ".$cat_sql."
UNION ALL
SELECT h.*,'Weekly' AS type FROM " . WP_CALENDAR_TABLE . " AS h WHERE h.event_recur = 'W' AND '$date' >= h.event_begin AND h.event_repeats = 0 ".$cat_sql."
UNION ALL
SELECT i.*,'Weekly' AS type FROM " . WP_CALENDAR_TABLE . " AS i WHERE i.event_recur = 'W' AND '$date' >= i.event_begin AND i.event_repeats != 0 AND (i.event_repeats*7) >= (TO_DAYS('$date') - TO_DAYS(i.event_end)) ".$cat_sql."
ORDER BY event_id";
// Run the collated code
$events =$wpdb->get_results($sql);
if (!empty($events))
{
foreach($events as $event)
{
if ($event->type == 'Normal')
{
array_push($arr_events, $event);
}
else if ($event->type == 'Yearly')
{
// 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);
}
}
}
else if ($event->type == 'Monthly')
{
// 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) && (strtotime($event->event_begin) <= strtotime($date)))
{
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) && (strtotime($event->event_begin) <= strtotime($date)))
{
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);
}
}
}
else if ($event->type == 'MonthSun')
{
// This used to be complex but writing the dt_of_sun() function helped loads!
// 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));
// Setup some variables and get some values
$dow = date('w',strtotime($event->event_begin));
if ($dow == 0) { $dow = 7; }
$start_ent_this = dt_of_sun($date,np_of_day($event->event_begin),$dow);
$start_ent_prev = dt_of_sun(date('Y-m-d',strtotime($date.'-1 month')),np_of_day($event->event_begin),$dow);
$len_ent = strtotime($event->event_end)-strtotime($event->event_begin);
// The grunt work
if (($month_begin == $month_end) && (strtotime($event->event_begin) <= strtotime($date)))
{
// The checks
if (strtotime($event->event_begin) <= strtotime($date) && strtotime($event->event_end) >= strtotime($date)) // Handle the first occurance
{
array_push($arr_events, $event);
}
else if (strtotime($start_ent_this) <= strtotime($date) && strtotime($date) <= strtotime($start_ent_this)+$len_ent) // Now remaining items
{
array_push($arr_events, $event);
}
}
else if (($month_begin < $month_end) && (strtotime($event->event_begin) <= strtotime($date)))
{
// The checks
if (strtotime($event->event_begin) <= strtotime($date) && strtotime($event->event_end) >= strtotime($date)) // Handle the first occurance
{
array_push($arr_events, $event);
}
else if (strtotime($start_ent_prev) <= strtotime($date) && strtotime($date) <= strtotime($start_ent_prev)+$len_ent) // Remaining items from prev month
{
array_push($arr_events, $event);
}
else if (strtotime($start_ent_this) <= strtotime($date) && strtotime($date) <= strtotime($start_ent_this)+$len_ent) // Remaining items starting this month
{
array_push($arr_events, $event);
}
}
}
else if ($event->type == 'Weekly')
{
// 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;
}
// Setup comparison functions for building the calendar later
function calendar_month_comparison($month)
{
$current_month = strtolower(date("M", ctwo()));
if (isset($_GET['yr']) && isset($_GET['month']))
{
if ($month == $_GET['month'])
{
return ' selected="selected"';
}
}
elseif ($month == $current_month)
{
return ' selected="selected"';
}
}
function calendar_year_comparison($year)
{
$current_year = strtolower(date("Y", ctwo()));
if (isset($_GET['yr']) && isset($_GET['month']))
{
if ($year == $_GET['yr'])
{
return ' selected="selected"';
}
}
else if ($year == $current_year)
{
return ' selected="selected"';
}
}
// Actually do the printing of the calendar
// Compared to searching for and displaying events
// this bit is really rather easy!
function calendar($cat_list = '')
{
global $wpdb;
// Deal with the week not starting on a monday
if (get_option('start_of_week') == 0)
{
$name_days = array(1=>__('Nedelja','calendar'),__('Ponedeljek','calendar'),__('Torek','calendar'),__('Sreda','calendar'),__('Četrtek','calendar'),__('Petek','calendar'),__('Sobota','calendar'));
}
// Choose Monday if anything other than Sunday is set
else
{
$name_days = array(1=>__('Ponedeljek','calendar'),__('Torek','calendar'),__('Sreda','calendar'),__('Četrtek','calendar'),__('Petek','calendar'),__('Sobota','calendar'),__('Nedelja','calendar'));
}
// Carry on with the script
$name_months = array(1=>__('Januar','calendar'),__('Februar','calendar'),__('Marec','calendar'),__('April','calendar'),__('Maj','calendar'),__('Junij','calendar'),__('Julij','calendar'),__('Avgust','calendar'),__('September','calendar'),__('Oktober','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",ctwo());
$c_month = date("m",ctwo());
$c_day = date("d",ctwo());
}
// Years get funny if we exceed 3000, so we use this check
if (isset($_GET['yr']))
{
if ($_GET['yr'] <= 3000 && $_GET['yr'] >= 0 && (int)$_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",ctwo());
}
// No valid month causes the calendar to default to today
else
{
$c_year = date("Y",ctwo());
$c_month = date("m",ctwo());
$c_day = date("d",ctwo());
}
}
}
// No valid year causes the calendar to default to today
else
{
$c_year = date("Y",ctwo());
$c_month = date("m",ctwo());
$c_day = date("d",ctwo());
}
// 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 = '';
$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 .= '
';
for ($ii=1; $ii<=7; $ii++)
{
if ($ii==$first_weekday && $i==1)
{
$go = TRUE;
}
elseif ($i > $days_in_month )
{
$go = FALSE;
}
if ($go)
{
// Colours again, this time for the day numbers
if (get_option('start_of_week') == 0)
{
// This bit of code is for styles believe it or not.
$grabbed_events = grab_events($c_year,$c_month,$i,'calendar',$cat_list);
$no_events_class = '';
if (!count($grabbed_events))
{
$no_events_class = ' no-events';
}
$calendar_body .= '
';
$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 . " ORDER BY category_name ASC";
$cat_details = $wpdb->get_results($sql);
$calendar_body .= '
'.__('Kategorije','calendar').'
';
foreach($cat_details as $cat_detail)
{
$calendar_body .= '
'.$cat_detail->category_name.'
';
}
$calendar_body .= '
';
}
// A little link to yours truly. 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;
}
// Used to create a hover will all a day's events in for minical
function minical_draw_events($events,$day_of_week = '')
{
// We need to sort arrays of objects by time
usort($events, "time_cmp");
// Only show anything if there are events
$output = '';
if (count($events)) {
// Setup the wrapper
$output = ''.$day_of_week.'';
// Now process the events
foreach($events as $event)
{
if ($event->event_time == '00:00:00') { $the_time = 'all day'; } else { $the_time = 'at '.date(get_option('time_format'), strtotime(stripslashes($event->event_time))); }
$output .= '* '.$event->event_title.' '.$the_time.' ';
}
// The tail
$output .= '';
} else {
$output .= $day_of_week;
}
return $output;
}
function minical($cat_list = '') {
global $wpdb;
// Deal with the week not starting on a monday
if (get_option('start_of_week') == 0)
{
$name_days = array(1=>__('Su','calendar'),__('Mo','calendar'),__('Tu','calendar'),__('We','calendar'),__('Th','calendar'),__('Fr','calendar'),__('Sa','calendar'));
}
// Choose Monday if anything other than Sunday is set
else
{
$name_days = array(1=>__('Mo','calendar'),__('Tu','calendar'),__('We','calendar'),__('Th','calendar'),__('Fr','calendar'),__('Sa','calendar'),__('Su','calendar'));
}
// Carry on with the script
$name_months = array(1=>__('Januar','calendar'),__('Februar','calendar'),__('Marec','calendar'),__('April','calendar'),__('Maj','calendar'),__('Junij','calendar'),__('Julij','\
calendar'),__('Avgust','calendar'),__('September','calendar'),__('Oktober','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",ctwo());
$c_month = date("m",ctwo());
$c_day = date("d",ctwo());
}
// Years get funny if we exceed 3000, so we use this check
if (isset($_GET['yr']))
{
if ($_GET['yr'] <= 3000 && $_GET['yr'] >= 0 && (int)$_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",ctwo());
}
// No valid month causes the calendar to default to today
else
{
$c_year = date("Y",ctwo());
$c_month = date("m",ctwo());
$c_day = date("d",ctwo());
}
}
}
// No valid year causes the calendar to default to today
else
{
$c_year = date("Y",ctwo());
$c_month = date("m",ctwo());
$c_day = date("d",ctwo());
}
// 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 = '';
$calendar_body .= '
';
// The header of the calendar table and the links. Note calls to link functions
$calendar_body .= '
' . prev_link($c_year,$c_month,true) . '
'.$name_months[(int)$c_month].' '.$c_year.'
' . next_link($c_year,$c_month,true) . '
';
// 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 .= '
';
for ($ii=1; $ii<=7; $ii++)
{
if ($ii==$first_weekday && $i==1)
{
$go = TRUE;
}
elseif ($i > $days_in_month )
{
$go = FALSE;
}
if ($go)
{
// Colours again, this time for the day numbers
if (get_option('start_of_week') == 0)
{
// This bit of code is for styles believe it or not.
$grabbed_events = grab_events($c_year,$c_month,$i,'calendar',$cat_list);
$no_events_class = '';
if (!count($grabbed_events))
{
$no_events_class = ' no-events';
}
$calendar_body .= '