relbase=$url['path']; $this->read_event_category(); $this->read_show_only_events(); $this->read_num_months(); $this->read_day_length(); $this->read_hide_logo(); $this->read_advanced(); $this->read_nav_below(); $this->read_disable_popups(); } // READ functions function read_event_category() { $this->event_category=intval( get_option('ec3_event_category') ); if($this->event_category==0) $this->event_category=1; } function read_show_only_events() { $this->show_only_events=intval(get_option('ec3_show_only_events')); } function read_num_months() { $this->num_months =abs(intval(get_option('ec3_num_months'))); if(!$this->num_months) $this->num_months=1; } function read_day_length() { $this->day_length=intval(get_option('ec3_day_length')); if($this->day_length==0) $this->day_length=1; } function read_hide_logo() { $this->hide_logo=intval(get_option('ec3_hide_logo')); } function read_advanced() { $this->advanced=intval(get_option('ec3_advanced')); } function read_nav_below() { $this->nav_below=intval(get_option('ec3_nav_below')); } function read_disable_popups() { $this->disable_popups=intval(get_option('ec3_disable_popups')); } // SET functions function set_event_category($val) { if($this->event_category!=$val) { update_option('ec3_event_category',$val); $this->read_event_category(); } } function set_show_only_events($val) { if($this->show_only_events!=$val) { update_option('ec3_show_only_events',$val); $this->read_show_only_events(); } } function set_num_months($val) { if($this->num_months!=$val) { update_option('ec3_num_months',$val); $this->read_num_months(); } } function set_day_length($val) { if($this->day_length!=$val) { update_option('ec3_day_length',$val); $this->read_day_length(); } } function set_hide_logo($val) { if($this->hide_logo!=$val) { update_option('ec3_hide_logo',$val); $this->read_hide_logo(); } } function set_advanced($val) { if($this->advanced!=$val) { update_option('ec3_advanced',$val); $this->read_advanced(); } } function set_nav_below($val) { if($this->nav_below!=$val) { update_option('ec3_nav_below',$val); $this->read_nav_below(); } } function set_disable_popups($val) { if($this->disable_popups!=$val) { update_option('ec3_disable_popups',$val); $this->read_disable_popups(); } } } // end class ec3_Options /** Singleton instance of ec3_Options. */ $ec3=new ec3_Options(); function ec3_action_wp_head() { global $ec3,$month,$month_abbrev; $myfiles=$ec3->relbase.'/wp-content/plugins/eventcalendar3/'; ?> nocss): ?> from_current_page(); } else { $this->year_num =$year_num; $this->month_num=$month_num; $this->day_num =$day_num; } } /** Utility function. Calculates the value of month/year for the current * page. Code block from wp-includes/template-functions-general.php * (get_calendar function). */ function from_current_page() { global $m, $monthnum, $wpdb, $year; if (isset($_GET['w'])) { $w = ''.intval($_GET['w']); } // Let's figure out when we are if (!empty($monthnum) && !empty($year)) { $thismonth = ''.zeroise(intval($monthnum), 2); $thisyear = ''.intval($year); } elseif (!empty($w)) { // We need to get the month from MySQL $thisyear = ''.intval(substr($m, 0, 4)); $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('${thisyear}0101', INTERVAL $d DAY) ), '%m')"); } elseif (!empty($m)) { // $calendar = substr($m, 0, 6); $thisyear = ''.intval(substr($m, 0, 4)); if (strlen($m) < 6) { $thismonth = '01'; } else { $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2); } } else { $thisyear = gmdate('Y', current_time('timestamp') + get_settings('gmt_offset') * 3600); $thismonth = gmdate('m', current_time('timestamp') + get_settings('gmt_offset') * 3600); } $this->year_num =intval($thisyear); $this->month_num=intval($thismonth); $this->day_num =1; } /** Month arithmetic. Returns a new date object. */ function plus_months($month_count) { $result=new ec3_Date($this->year_num,$this->month_num,$this->day_num); $result->month_num += $month_count; if($month_count>0) { while($result->month_num>12) { $result->month_num -= 12; $result->year_num++; } } else { while($result->month_num<1) { $result->month_num += 12; $result->year_num--; } } return $result; } /** Convenience function for accessing plus_months(). */ function prev_month() { return $this->plus_months(-1); } function next_month() { return $this->plus_months( 1); } /** Modifies the current object to be one day in the future. */ function increment_day() { $this->day_num++; if($this->day_num > $this->days_in_month()) { $this->day_num=1; $this->month_num++; if($this->month_num>12) { $this->month_num=1; $this->year_num++; } $this->_days_in_month=0; } $this->_unixdate=0; } function month_id() // e.g. ec3_2005_06 { return 'ec3_' . $this->year_num . '_' . $this->month_num; } function day_id() // e.g. ec3_2005_06_25 { $result='ec3_'.$this->year_num.'_'.$this->month_num.'_'.$this->day_num; global $ec3_today_id; if($result==$ec3_today_id) return 'today'; else return $result; } function day_link() { global $ec3; if($ec3->show_only_events) { return get_option('siteurl') . '/index.php?m=' . $this->year_num . zeroise($this->month_num, 2) . zeroise($this->day_num, 2) . "&cat=" . $ec3->event_category; } else return get_day_link($this->year_num,$this->month_num,$this->day_num); } function month_name() // e.g. June { global $month; return $month[zeroise($this->month_num,2)]; } function month_abbrev() // e.g. Jun { global $month_abbrev; return $month_abbrev[ $this->month_name() ]; } function month_link() { global $ec3; if($ec3->show_only_events) { return get_option('siteurl') . '/index.php?m=' . $this->year_num . zeroise($this->month_num, 2) . "&cat=" . $ec3->event_category; } else return get_month_link($this->year_num,$this->month_num); } function days_in_month() { if(0==$this->_days_in_month) $this->_days_in_month=intval(date('t', $this->to_unixdate())); return $this->_days_in_month; } function week_day() { return intval(date('w', $this->to_unixdate())); } function to_unixdate() { if(0==intval($this->_unixdate)) { $this->_unixdate = mktime(0,0,0, $this->month_num,$this->day_num,$this->year_num); } return $this->_unixdate; } } // end class ec3_Date /** Calculate the header (days of week). */ function ec3_util_thead() { global $ec3, $weekday, $weekday_abbrev, $weekday_initial; $result="\n"; $start_of_week =intval( get_option('start_of_week') ); for($i=0; $i<7; $i++) { $full_day_name=$weekday[ ($i+$start_of_week) % 7 ]; if(3==$ec3->day_length) $display_day_name=$weekday_abbrev[$full_day_name]; elseif($ec3->day_length<3) $display_day_name=$weekday_initial[$full_day_name]; else $display_day_name=$full_day_name; $result.="\t" . "$display_day_name\n"; } $result.="\n"; return $result; } /** Echos the event calendar navigation controls. */ function ec3_get_calendar_nav($date,$num_months) { global $ec3; echo "\n"; // Previous $prev=$date->prev_month(); echo "\t\n"; echo "\t\n"; // Next $next=$date->plus_months($num_months); echo "\t\n"; echo "\n"; } /** Represents all posts from a particular day. * Generated by ec3_util_calendar_days(). */ class ec3_Day { var $is_event =False; var $titles =array(); function ec3_Day(){} function add_post($title,$time,$is_event) { $safe_title= str_replace( array(',','@'), ' ', htmlspecialchars( stripslashes($title), ENT_QUOTES, get_option('blog_charset') ) ); if($is_event) { $safe_title.=' @'.$time; $this->is_event=True; } $this->titles[]=$safe_title; } function get_titles() { return implode(', ',$this->titles); } } /** Generates an array of all 'ec3_Day's between the start of * begin_month & end_month. * month_id is in the form: ec3__ */ function ec3_util_calendar_days($begin_month_id,$end_month_id) { $begin_date=substr($begin_month_id,4) . '_1'; $end_date =substr($end_month_id,4) . '_1'; global $ec3, $tablepost2cat, $tableposts, $wpdb; // Which posts are we interested in? if($ec3->show_only_events) { // Category ID number for event posts. $where_post = "category_id = $ec3->event_category"; } else { $now = gmdate('Y-m-d H:i:59'); $where_post = "(post_date_gmt<='$now' OR category_id=$ec3->event_category)"; } $calendar_entries = $wpdb->get_results( "SELECT DISTINCT post_title, post_date, DAYOFMONTH(post_date) AS day, MONTH(post_date) AS month, YEAR(post_date) AS year, (category_id = $ec3->event_category) AS is_event FROM $tableposts,$tablepost2cat WHERE post_date >= '$begin_date' AND post_date < '$end_date' AND post_status = 'publish' AND id = post_id AND $where_post ORDER BY post_date ASC" ); $calendar_days = array(); // result if($calendar_entries) { $time_format=get_settings('time_format'); foreach($calendar_entries as $ent) { $date=new ec3_Date($ent->year,$ent->month,$ent->day); $day_id=$date->day_id(); if(empty($calendar_days[$day_id])) $calendar_days[$day_id] = new ec3_Day(); $calendar_days[$day_id]->add_post( "$ent->post_title", mysql2date($time_format,$ent->post_date), $ent->is_event ); } } return $calendar_days; } /** Echos one event calendar month table. */ function ec3_get_calendar_month($date,$calendar_days,$thead) { global $ec3; // // Table start. $title= sprintf(__('View posts for %1$s %2$s'),$date->month_name(),$date->year_num); echo "\n\n"; echo $thead; // // Table body echo "\n\t"; $days_in_month =$date->days_in_month(); $week_day=( $date->week_day() + 7 - intval(get_option('start_of_week')) ) % 7; $col =0; while(True) { if($col>6) { echo "\n\t"; $col=0; } if($col<$week_day) { // insert padding $pad=$week_day-$col; echo ""; $col=$week_day; } // insert day $day_id = $date->day_id(); echo "'; $col++; $date->increment_day(); if(1==$date->day_num) break; $week_day=($week_day+1) % 7; } // insert padding $pad=7-$col; if($pad>1) echo ""; elseif($pad) echo ""; echo "\n\n
" . '' . $date->month_name() . ' ' . $date->year_num . "
 '; echo 'is_event) echo ' class="eventday"'; echo ">$date->day_num"; } else { echo '>' . $date->day_num; } echo '" . "hide_logo? " style='display:none'>": ">") . "EC 
"; } /** Template function. Call this from your template to insert the * Event Calendar. */ function ec3_get_calendar() { // Can't cope with more than one calendar on the same page. Everything has // a unique ID, so it can't be duplicated. // Simple fix for problem: Just ignore all calls after the first. $ec3->call_count++; if($ec3->call_count>1) { echo "\n"; return; } echo "
\n"; $this_month = new ec3_Date(); global $ec3; // Display navigation panel. if(!$ec3->nav_below) ec3_get_calendar_nav($this_month,$ec3->num_months); // Get entries $end_month=$this_month->plus_months($ec3->num_months); $calendar_days = ec3_util_calendar_days( $this_month->month_id(), $end_month->month_id() ); // Display months. $thead=ec3_util_thead(); for($i=0; $i<$ec3->num_months; $i++) { ec3_get_calendar_month($this_month,$calendar_days,$thead); $this_month=$this_month->next_month(); } // Display navigation panel. if($ec3->nav_below) ec3_get_calendar_nav(new ec3_Date(),$ec3->num_months); echo "
\n"; if(!$ec3->disable_popups) echo "\t\n"; } /** Substitutes placeholders like '%key%' in $format with 'value' from $data * array. */ function ec3_format_str($format,$data) { foreach($data as $k=>$v) $format=str_replace("%$k%",$v,$format); return $format; } /** Template function. Call this from your template to insert a list of * forthcoming events. Available template variables are: * - template_day: %DATE% %SINCE% (only with Time Since plugin) * - template_event: %DATE% %TIME% %LINK% %TITLE% %AUTHOR% */ function ec3_get_events( $limit, $template_event='%TITLE% (%TIME%)', $template_day='%DATE%:', $date_format='j F') { global $ec3,$wpdb,$tableposts,$tablepost2cat,$tableusers; if(!$date_format) $date_format=get_settings('date_format'); // Start at midnight to show all of today's events $now = gmdate('Y-m-d 00:00:00'); $calendar_entries = $wpdb->get_results( "SELECT p.id AS id, post_title, post_date, u.user_nickname AS author FROM $tableposts p, $tablepost2cat p2c, $tableusers u WHERE post_status = 'publish' AND post_date_gmt > '$now' AND p.id = post_id AND p.post_author = u.id AND category_id = $ec3->event_category ORDER BY post_date LIMIT $limit" ); echo "\n"; echo "
  • " . ec3_format_str($template_day,$data)."\n
      \n"; $current_date=$data['DATE']; } $data['TIME'] =mysql2date($time_format,$entry->post_date); $data['TITLE'] =$entry->post_title; $data['LINK'] =get_permalink($entry->id); $data['AUTHOR']=$entry->author; echo "
    • ".ec3_format_str($template_event,$data)."
    • \n"; } if($template_day) echo "
  • \n"; } else { echo "
  • ".__('No events.','ec3')."
  • \n"; } echo "\n"; } /** DO NOT USE. This function is for backwards compatibility. */ function ec2_get_calendar() { ec3_get_calendar(); } /** DO NOT USE. This function is for backwards compatibility. */ function ec2_get_events($limit, $date_format) { ec3_get_events( $limit, '%TITLE% (%TIME%)', '%DATE%:', $date_format ); } function ec3_action_admin_menu() { if(function_exists('add_options_page')) { add_options_page( 'Event Calendar Options', 'EventCalendar', 6, basename(__FILE__), 'ec3_options_subpanel' ); } } function ec3_options_subpanel() { global $ec3; if(isset($_POST['info_update'])) { echo '

    '; if(isset($_POST['ec3_event_category'])) $ec3->set_event_category( intval($_POST['ec3_event_category']) ); if(isset($_POST['ec3_num_months'])) $ec3->set_num_months( intval($_POST['ec3_num_months']) ); if(isset($_POST['ec3_show_only_events'])) $ec3->set_show_only_events( intval($_POST['ec3_show_only_events']) ); if(isset($_POST['ec3_day_length'])) $ec3->set_day_length( intval($_POST['ec3_day_length']) ); if(isset($_POST['ec3_hide_logo'])) $ec3->set_hide_logo( intval($_POST['ec3_hide_logo']) ); if(isset($_POST['ec3_advanced'])) $ec3->set_advanced( intval($_POST['ec3_advanced']) ); if(isset($_POST['ec3_nav_below'])) $ec3->set_nav_below( intval($_POST['ec3_nav_below']) ); if(isset($_POST['ec3_disable_popups'])) $ec3->set_disable_popups( intval($_POST['ec3_disable_popups']) ); _e('Options set.','ec3'); echo '

    '; } ?>

    :
    :
    :
    :
    :
    :
    :
    :

    EXAMPLE SIDEBAR CODE:

            <li>
               <?php ec3_get_calendar(); ?>
            </li>
            <li><?php _e('Events'); ?>
               <?php ec3_get_events(5); ?>
            </li>
    event_category); $event_cat_nicename = $event_cat_record->category_nicename; $ec3->is_listing = (preg_match("/\bcategory_id\s*=\s*$ec3->event_category\b/",$where) || preg_match("/\bcategory_nicename\s*=\s*'$event_cat_nicename'/",$where)); if($ec3->is_listing && $ec3->advanced && !$wp_query->is_date) { // reverse date restriction for event category listing $now = gmdate('Y-m-d 00:00:00'); $result=preg_replace( "/\bAND\s+post_date_gmt\s*<=\s*'([-0-9]+) [:0-9]+'/", "AND post_date_gmt > '\$1 00:00:00'",$where ); } elseif($wp_query->is_date || $wp_query->is_single || $ec3->is_listing || $ec3->advanced) { // (This should be a sub-select, but older versions of MySQL can't cope.) $post_list=$wpdb->get_results( "SELECT DISTINCT(post_id) FROM $tablepost2cat " . "WHERE category_id=$ec3->event_category" ); if(count($post_list)) { $post_list_str='-1'; // start with a dummy value foreach($post_list as $p) $post_list_str.=",$p->post_id"; if($wp_query->is_date || $wp_query->is_single || $ec3->is_listing) // remove date restriction for events $result=preg_replace( "/AND +(post_date_gmt *<= *'[-: 0-9]+') +/", 'AND ($1 OR id IN ('.$post_list_str.') ) ',$where ); else // ($ec3->advanced) // remove events from blog. $result=preg_replace( "/AND +(post_date_gmt *<= *'[-: 0-9]+') +/", 'AND ($1 AND id NOT IN ('.$post_list_str.') ) ',$where ); } // end if(count($post_list)) } return $result; } /** Change the order of event listings (only advanced mode). */ function ec3_filter_posts_orderby($orderby) { global $ec3; if($ec3->is_listing) $orderby=preg_replace("/\bDESC\b/",'',$orderby); return $orderby; } /** If the parameter ec3_xml is set, then brutally hijack the page and replace * it with XML calendar data. This is used by XmlHttpRequests from the * active calendar JavaScript. */ function ec3_filter_query_vars_xml($wpvarstoreset) { if(isset($_GET['ec3_xml'])) { $components=explode('_',$_GET['ec3_xml']); if(count($components)==2) { $date=new ec3_Date($components[0],$components[1]); $end=$date->next_month(); $calendar_days=ec3_util_calendar_days($date->month_id(),$end->month_id()); @header('Content-type: text/xml'); echo ''; echo "\n"; foreach($calendar_days as $day_id=>$day) { if('today'==$day_id) $dc=explode('_', gmdate(':_Y_m_d') ); else $dc=explode('_',$day_id); if(count($dc)==4) { $date->day_num=$dc[3]; $titles=$day->get_titles(); echo "\n"; } } echo "\n"; exit(0); } } // else... return $wpvarstoreset; } /** If the parameter ec3_vcal is set, then brutally hijack the page and replace * it with vCalendar data. * (Includes fixes contributed by Matthias Tarasiewicz.)*/ function ec3_filter_query_vars_vcal($wpvarstoreset) { if(isset($_GET['ec3_vcal'])) { // // Generate the vCalendar $default_duration=3; // hours $name=preg_replace('/[^0-9a-zA-Z]/','',get_bloginfo_rss('name')); header("Content-Type: text/x-vCalendar"); header("Content-Disposition: inline; filename=$name.vcs"); header('Expires: Wed, 11 Jan 1984 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-cache, must-revalidate, max-age=0'); header('Pragma: no-cache'); echo "BEGIN:VCALENDAR\r\n"; echo "VERSION:1.0\r\n"; global $ec3,$tableposts,$tablepost2cat,$tableusers,$wpdb; $calendar_entries = $wpdb->get_results( "SELECT p.id as id, post_title, post_excerpt, DATE_FORMAT(post_date_gmt,'%Y%m%dT%H%i%sZ') as dt_start, user_nickname FROM $tableposts p, $tablepost2cat p2c LEFT JOIN $tableusers u ON u.ID=p.post_author WHERE post_status = 'publish' AND p.id = post_id AND category_id = $ec3->event_category" ); foreach($calendar_entries as $entry) { global $id; $id=$entry->id; $meta=get_post_custom($entry->id); $duration=$default_duration; if(is_array($meta) && array_key_exists('duration',$meta)) { $values=$meta['duration']; if(count($values)) $duration=end($values); } echo "BEGIN:VEVENT\r\n"; echo "SUMMARY:$entry->post_title\r\n"; echo "URL;VALUE=URI:". get_permalink($entry->id) . "\r\n"; if(strlen($entry->post_excerpt)>0) { // I can't get iCal to understand vCalendar encoding. // So just strip out newlines here: echo "DESCRIPTION:" . preg_replace('/[\r\n]+/',' ',$entry->post_excerpt) . "\r\n"; } if($duration) { echo "DURATION:PT$duration"."H\r\n"; } echo "DTSTART:$entry->dt_start\r\n"; echo "DTEND:$entry->dt_start\r\n"; echo "END:VEVENT\r\n"; } echo "END:VCALENDAR\r\n"; exit(0); } // else... return $wpvarstoreset; } // // Hook in... add_action('wp_head', 'ec3_action_wp_head'); add_action('admin_menu', 'ec3_action_admin_menu'); add_filter('posts_where', 'ec3_filter_where',11); add_filter('query_vars', 'ec3_filter_query_vars_xml'); add_filter('query_vars', 'ec3_filter_query_vars_vcal'); if($ec3->advanced) add_filter('posts_orderby','ec3_filter_posts_orderby'); ?>