Código:
<?php
/*
* Function requested by Ajax
*/
if(isset($_POST['func']) && !empty($_POST['func'])){
switch($_POST['func']){
case 'getCalender':
getCalender($_POST['year'],$_POST['month']);
break;
case 'getEvents':
getEvents($_POST['date']);
break;
default:
break;
}
}
/*
* Get calendar full HTML
*/
function getCalender($year = '',$month = '')
{
$dateYear = ($year != '')?$year:date("Y");
$dateMonth = ($month != '')?$month:date("m");
$date = $dateYear.'-'.$dateMonth.'-01';
$currentMonthFirstDay = date("N",strtotime($date));
$totalDaysOfMonth = cal_days_in_month(CAL_GREGORIAN,$dateMonth,$dateYear);
$totalDaysOfMonthDisplay = ($currentMonthFirstDay == 7)?($totalDaysOfMonth):($totalDaysOfMonth + $currentMonthFirstDay);
$boxDisplay = ($totalDaysOfMonthDisplay <= 35)?35:42;
?>
<div id="calender_section">
<h2>
<div id="seleccion_mes">
<a href="javascript:void(0);" onclick="getCalendar('calendar_div','<?php echo date("Y",strtotime($date.' - 1 Month')); ?>','<?php echo date("m",strtotime($date.' - 1 Month')); ?>');" id="flecha_izquierda"><<</a>
<select name="month_dropdown" class="month_dropdown dropdown" face="segoe script"><?php echo getAllMonths($dateMonth); ?></select>
<select name="year_dropdown" class="year_dropdown dropdown" face="segoe script"><?php echo getYearList($dateYear); ?></select>
<a href="javascript:void(0);" onclick="getCalendar('calendar_div','<?php echo date("Y",strtotime($date.' + 1 Month')); ?>','<?php echo date("m",strtotime($date.' + 1 Month')); ?>');" id="flecha_derecha">>></a>
</div>
</h2>
<div id="event_list" class="none"></div>
<div id="calender_section_top">
<ul id="dias_semana" class="dias_semana1">
<li id="dia1">Dom.</li>
<li id="dia2">Lun.</li>
<li id="dia3">Mar.</li>
<li id="dia4">Mié.</li>
<li id="dia5">Jue.</li>
<li id="dia6">Vie.</li>
<li id="dia7">Sáb.</li>
</ul>
</div>
<div id="calender_section_bot">
<ul>
<?php
$dayCount = 1;
for($cb=1;$cb<=$boxDisplay;$cb++){
if(($cb >= $currentMonthFirstDay+1 || $currentMonthFirstDay == 7) && $cb <= ($totalDaysOfMonthDisplay)){
//Current date
$currentDate = $dateYear.'-'.$dateMonth.'-'.$dayCount;
$eventNum = 0;
//Include db configuration file
include 'dbConfig.php';
//Get number of events based on the current date
$result = $db->query("SELECT * FROM 1esoacalendar WHERE fecha = '".$currentDate."'");
$result1 = $db->query("SELECT * FROM inicio_de_curso WHERE data = '".$currentDate."'");
$result2 = $db->query("SELECT * FROM vacaciones_de_navidad WHERE data = '".$currentDate."'");
$result3 = $db->query("SELECT * FROM vacaciones_de_pascua WHERE data = '".$currentDate."'");
$result4 = $db->query("SELECT * FROM dias_festivos WHERE data = '".$currentDate."'");
$eventNum = $result->num_rows;
$eventNum1 = $result1->num_rows;
$eventNum2 = $result2->num_rows;
$eventNum3 = $result3->num_rows;
$eventNum4 = $result4->num_rows;
//Define date cell color
if(strtotime($currentDate) == strtotime(date("Y-m-d"))){
echo '<li date="'.$currentDate.'" class="grey date_cell">';
}elseif($eventNum > 0){
echo '<li date="'.$currentDate.'" class="light_sky date_cell">';
}elseif($eventNum1 > 0){
echo '<li date="'.$currentDate.'" style="background-color: #F78181 !important;">';
}elseif($eventNum2 > 0){
echo '<li date="'.$currentDate.'" style="background-color: #81F781 !important;">';
}elseif($eventNum3 > 0){
echo '<li date="'.$currentDate.'" style="background-color: #F2F5A9 !important;">';
}elseif($eventNum4 > 0){
echo '<li date="'.$currentDate.'" style="background-color: #E2A9F3 !important;">';
}else{
echo '<li date="'.$currentDate.'" class="date_cell">';
}
//Date cell
echo '<span>';
echo $dayCount;
echo '</span>';
//Hover event popup
echo '<div id="date_popup_'.$currentDate.'" class="date_popup_wrap none">';
echo '<div class="date_window">';
echo '<div class="popup_event">Eventos ('.$eventNum.')</div>';
echo ($eventNum > 0)?'<a href="javascript:;" onclick="getEvents(\''.$currentDate.'\');" style="color:#0040FF; font-size:1em; padding:10px 0px 0px 0px;" class="evento_texto">ver eventos</a>':'';
echo '</div></div>';
echo '</li>';
$dayCount++;
?>
<?php }else{ ?>
<li><span> </span></li>
<?php } } ?>
</ul>
</div>
</div>
<script type="text/javascript">
function getCalendar(target_div,year,month){
$.ajax({
type:'POST',
url:'functions.php',
data:'func=getCalender&year='+year+'&month='+month,
success:function(html){
$('#'+target_div).html(html);
}
});
}
function getEvents(date){
$.ajax({
type:'POST',
url:'functions.php',
data:'func=getEvents&date='+date,
success:function(html){
$('#event_list').html(html);
$('#event_list').slideDown('slow');
}
});
}
function addEvent(date){
$.ajax({
type:'POST',
url:'functions.php',
data:'func=addEvent&date='+date,
success:function(html){
$('#event_list').html(html);
$('#event_list').slideDown('slow');
}
});
}
$(document).ready(function(){
$('.date_cell').mouseenter(function(){
date = $(this).attr('date');
$(".date_popup_wrap").fadeOut();
$("#date_popup_"+date).fadeIn();
});
$('.date_cell').mouseleave(function(){
$(".date_popup_wrap").fadeOut();
});
$('.month_dropdown').on('change',function(){
getCalendar('calendar_div',$('.year_dropdown').val(),$('.month_dropdown').val());
});
$('.year_dropdown').on('change',function(){
getCalendar('calendar_div',$('.year_dropdown').val(),$('.month_dropdown').val());
});
$(document).click(function(){
$('#event_list').slideUp('slow');
});
});
</script>
<?php
}
/*
* Get months options list.
*/
function getAllMonths($selected = ''){
$options = '';
for($i=1;$i<=12;$i++)
{
$value = ($i < 10)?'0'.$i:$i;
$selectedOpt = ($value == $selected)?'selected':'';
$options .= '<option value="'.$value.'" '.$selectedOpt.' >'.date("F", mktime(0, 0, 0, $i+1, 0, 0)).'</option>';
}
return $options;
}
/*
* Get years options list.
*/
function getYearList($selected = ''){
$options = '';
for( $i=2017; $i <= 2050; $i++)
{
$selectedOpt = ($i == $selected)?'selected':'';
$options .= '<option value="'.$i.'" '.$selectedOpt.' >'.$i.'</option>';
}
return $options;
}
/*
* Get events by date
*/
function getEvents($date = ''){
//Include db configuration file
include 'dbConfig.php';
$eventListHTML = '';
$date = $date?$date:date("Y-m-d");
//Get events based on the current date
$result = $db->query("SELECT * FROM 1esoacalendar WHERE fecha = '".$date."'");
if($result->num_rows > 0){
$eventListHTML = '<h2 id="seleccion_mes1"><p class="texto_dato">Events on '.date("l, d M Y",strtotime($date)).'</p></h2>';
$eventListHTML .= '<ul>';
while($row = $result->fetch_assoc()){
$eventListHTML .= '<center><font size="5" class="texto_dato"><b>Evento</b></font></center>';
$eventListHTML .= '<left class="texto_dato"><b>Tipo de evento:</b> '.$row['tipoevento'].'</left>';
$eventListHTML .= '<br>';
$eventListHTML .= '<left class="texto_dato"><b>otro:</b> '.$row['otro'].'</left>';
$eventListHTML .= '<br>';
$eventListHTML .= '<left class="texto_dato"><b>materia:</b> '.$row['materia'].'</left>';
$eventListHTML .= '<br>';
$eventListHTML .= '<left class="texto_dato"><b>asunto:</b> '.$row['asunto'].'</left>';
$eventListHTML .= '<br>';
$eventListHTML .= '<left class="texto_dato"><b>tarea:</b> '.$row['tarea'].'</left>';
$eventListHTML .= '<br>';
$eventListHTML .= '<left class="texto_dato"><b>evento:</b> '.$row['evento'].'</left>';
$eventListHTML .= '<br>';
}
$eventListHTML .= '</ul>';
}
echo $eventListHTML;
}
?>