Created
September 20, 2011 15:39
AJAX Calendar page template and functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//////////////////////// | |
// CALENDAR FUNCTIONS // | |
//////////////////////// | |
/* | |
Function to Draw the calendar and it's controls. | |
Credit: | |
David's Walsh's PHP Event Calendar tutorial: | |
http://davidwalsh.name/php-event-calendar | |
*/ | |
// Draw the calendar | |
function draw_ajax_calendar($month,$year) { | |
// Open table | |
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">'; | |
// Table headings | |
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); | |
$calendar.= '<tr class="calendar-row" id="weekdays"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>'; | |
// Define day and week variables | |
$running_day = date('w',mktime(0,0,0,$month,1,$year)); | |
$days_in_month = date('t',mktime(0,0,0,$month,1,$year)); | |
$days_in_this_week = 1; | |
$day_counter = 0; | |
$dates_array = array(); | |
// Row for week one | |
$calendar.= '<tr class="calendar-row">'; | |
// Print "blank" days until the first of the current week (will likely change this for Onlyinpgh) | |
for($x = 0; $x < $running_day; $x++): | |
$calendar.= '<td class="calendar-day-np"> </td>'; | |
$days_in_this_week++; | |
endfor; | |
// Add the day cell, highlight current | |
for($list_day = 1; $list_day <= $days_in_month; $list_day++): | |
$calendar.= '<td class="calendar-day"><div class="cal-days-indiv">'; | |
if ($list_day == date('d') && $month == date('n') && $year == date('Y')): | |
$calendar.= '<div class="day-today">'.$list_day.'</div>'; | |
else: | |
$calendar.= '<div class="day-number">'.$list_day.'</div>'; | |
endif; | |
// Close the day cell | |
$calendar.= '</div></td>'; | |
// Create a new row for new week | |
if($running_day == 6): | |
$calendar.= '</tr>'; | |
if(($day_counter+1) != $days_in_month): | |
$calendar.= '<tr class="calendar-row">'; | |
endif; | |
$running_day = -1; | |
$days_in_this_week = 0; | |
endif; | |
$days_in_this_week++; $running_day++; $day_counter++; | |
endfor; | |
// Finish the rest of the days in the week | |
if($days_in_this_week < 8): | |
for($x = 1; $x <= (8 - $days_in_this_week); $x++): | |
$calendar.= '<td class="calendar-day-np"> </td>'; | |
endfor; | |
endif; | |
// Final row | |
$calendar.= '</tr>'; | |
// End the table | |
$calendar.= '</table>'; | |
///* DEBUG */// | |
$calendar = str_replace('</td>','</td>'."\n",$calendar); | |
$calendar = str_replace('</tr>','</tr>'."\n",$calendar); | |
// Return result | |
return $calendar; | |
} | |
function random_number() { | |
srand(time()); | |
return (rand() % 7); | |
} | |
// Date variable settings | |
$month = (int) ($_POST['month'] ? $_POST['month'] | |
: ($_GET['month'] ? $_GET['month'] | |
: date('m'))); | |
$year= (int) ($_POST['year'] ? $_POST['year'] | |
: ($_GET['year'] ? $_GET['year'] | |
: date('Y'))); | |
$prevMonthVal = ($month != 1 ? $month - 1 : 12); | |
$prevYearVal = ($month != 1 ? $year : $year - 1); | |
$nextMonthVal = ($month != 12 ? $month + 1 : 1); | |
$nextYearVal = ($month != 12 ? $year : $year + 1); | |
// Prep some variable strings to avoid a lot of messy <?php echo X; ?\> crap in the HTML | |
$month_title = date('F',mktime(0,0,0,$month,1,$year)) . ' ' . $year; | |
$prev_href = '?month=' . $prevMonthVal . '&year=' . $prevYearVal; | |
$next_href = '?month=' . $nextMonthVal . '&year=' . $nextYearVal; | |
// Adding the AJAX hooks | |
add_action('wp_ajax_nopriv_my_special_action', 'draw_ajax_calendar'); | |
add_action('wp_ajax_my_special_ajax_call', 'draw_ajax_calendar'); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Template Name: Events Calendar | |
*/ | |
// Put WP AJAX file in a variable | |
$_ajax = admin_url('admin-ajax.php'); | |
?> | |
<script type-"text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script> | |
<script type="text/javascript"> | |
function prevMonthSwap(prevMonthVal,prevYearVal) { | |
jQuery("#calendar-container").html("<h1>LOADING</h1>").show(); | |
var url="<?php echo $_ajax; ?>"; | |
jQuery.post(url, {month: prevMonthVal, year: prevYearVal}, function(data){ | |
jQuery("#calendar-container").html(data).show(); | |
}); | |
return false; | |
} | |
function nextMonthSwap(nextMonthVal,nextYearVal) { | |
jQuery("#calendar-container").html("<h1>LOADING</h1>").show(); | |
var url="<?php echo $_ajax; ?>"; | |
jQuery.post(url, {month: nextMonthVal, year: nextYearVal}, function(data){ | |
jQuery("#calendar-container").html(data).show(); | |
}); | |
return false; | |
} | |
// attach the link event handlers (remember to include return in the callbacks -- I wasn't at first and the false value was getting lost) | |
jQuery(document).ready(function() { | |
jQuery('#prev-link').click(function(){ return prevMonthSwap(<?php echo $prevMonthVal; ?>,<?php echo $prevYearVal; ?>)}); | |
jQuery('#next-link').click(function(){ return nextMonthSwap(<?php echo $nextMonthVal; ?>,<?php echo $nextYearVal; ?>)}); | |
}); | |
</script> | |
<?php | |
get_header(); | |
?> | |
<div id="content"> | |
<div class="padder" id="cal-page"> | |
<?php do_action( 'bp_before_blog_page' ) ?> | |
<div class="page" id="blog-page"> | |
<h2 class="pagetitle">Upcoming Events: <?php echo $month_title;?></h2> | |
<div id="calendar-container"> | |
<?php echo draw_ajax_calendar($month,$year); ?> | |
<a id="prev-link" href="<?php echo $prev_href; ?>">← Previous</a> | |
| |
<a id="next-link" href="<?php echo $next_href; ?>">Next →</a> | |
</div> | |
</div><!-- .page --> | |
<?php do_action( 'bp_after_blog_page' ) ?> | |
</div><!-- .padder --> | |
</div><!-- #content --> | |
<?php get_footer() ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment