segunda-feira, 9 de agosto de 2010

Calendário customizável em ASP (destaque por data) e exemplo de cálculos com datas

Mexendo em umas aplicações webs mais antigas por aqui, encontrei um script q desenvolvi há um tempo atras e que me custou um tempinho p/ fazer.
Resolvi compartilhá-lo.

Segue o código fonte abaixo , salve o como
mcezzare_calendar.asp, ou como quiser,   e aonde está o código
<a href="http://REPLACE_YOUR_SITE/agenda_itens.php?data=<%=x%>/<%=mes%>/<%=ano%>" target="miolo"><%=x%></a>

1 - Acerte o link e o target caso esteja dentro de um frame ou o detalhe do evento esteja num iframe. se não remova o texto target="miolo".
 
2 - Este calendario pode ser usado p/ varios sites :
veja um exemplo chamando script dessa maneira na url : 
http://REPLACE_YOUR_SITE/mcezzare_calendar.asp?data=01/3/2005&days=1-9-14-25-27 



<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Option Explicit
'Mcezzare's  Calendar
'mcezzare@gmail.com last update Mon Aug  9 10:21:23 on ttys000
'version 1.3 date Fri Nov 25 08:31:32 BRST 2005
'this file display a calendar in table acording to the variable data on url ?data=dd/mm/yy
'if no value given, the default system time (now) wiill be used
' use : http://your_domain.com/calendar_final.asp?data=01/3/2005&days=1-9-14-25-27
'this calendar is mathematically correct, just chnage the line  Session.lcid = xxx to you timezone/language
'from the value data on url the script builds a ccalendar based on Year and Month
'If you want to flag some days 
Dim mes_st,mes,dia_st,dia_ini,dia_fim,dtx,mes_fresco,data,mesname,proximomes,ano,dia,mesanterior
Dim link,linkano,linkano_prox,linkmes_prox,linkmes_ant,linkano_ant,linksmes_prox
Dim i,x,z
Dim days,days_aux,high_days, color,teste,check_high
Dim diasr, diasr_aux,writeaux
Dim aux_file
Session.lcid = 1046 ' pt-BR
    if request("data") = "" then
        data = now
    else
        data = request("data")
    end if
    if not isdate(data) then data = now
' date variables
mes_st = month(data)
mes = month(data)
mesname = monthname(mes)
dia_st=1
ano = year(data)
dia_ini = dateserial(ano,mes,1)
dia_fim =dateserial(ano,mes +1,1)-1
dtx =weekday(dateserial(ano,mes,dia_st))
mes_fresco = Ucase(Left(mesname,1)) & Lcase(right(mesname,len(mesname)-1))

' file name , you can give any name.
link = Request.ServerVariables("SCRIPT_NAME") & "?data=01/"
aux_file= "http://REPLACE_YOUR_SITE/agenda_itens.php"

' if there is a other file you want to save data on database for example, you can call this page for your site
' like this
proximomes = mes + 1
mesanterior= mes - 1


'tem q fazer um select case p/ o mes qdo for 01 e 12
        linkano = ano
        linkano_prox = ano
        linkano_ant = ano
        linkmes_ant = mes
        linksmes_prox = mes

select case mes
    case 1
        linkano_ant = ano - 1
        linkmes_ant = 12
        linkmes_prox = mes + 1
    case 12
        linkano_prox= ano + 1
        linkmes_prox = 1
        linkmes_ant = mes -1
        linkano_ant = ano
    case else
        linkano = ano
        linkano_prox = ano
        linkano_ant = ano
        linkmes_ant = mes - 1
        linkmes_prox = mes + 1
end select

days=Request("days")
days_aux = split(days,"-")
diasr= Request("dias")
diasr_aux = split(diasr,"-")
 check_high = false
 color = "#FFFFFF"
function testa(x)   
testa = false
     for z = lbound(days_aux) to Ubound(days_aux)
    '    if   cdbl(days_aux(z)) =  cdbl(x)  then check_high = true
    '    if   cdbl(days_aux(z)) =  cdbl(x)  then color = "#FF0000"
        if   cdbl(days_aux(z)) =  cdbl(x)  then testa = true
        'response.write typename(vartype(days_aux(z))) & "-" & typename(vartype(x)) &   " " & check_high & "<br>"
        response.write cdbl(days_aux(z)) & "-" & cdbl(x) &   " --> " & check_high & " " & color & "<br>"
        'response.write z & "-" & days_aux(z) & "-" & x
  next

end function

'
'high_days = high_days &
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Calendario</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="calendar.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript">
<!--
function pinta(x){
document.getElementById("calendar").bgColor= "#FFFF00";
}

function pintacel(x){
var f = document.form1;
var campo =  x;
//document.form1.x.class='botao3';
//window.alert(document.getElementById(''+campo).value);
//document.getElementById(''+campo)
document.ids.cp1.color="#FFFF00";
}
function pintacelretorno(x){
var f = document.form1;
var campo =  x;

document.ids.cp1.color="#6699FF";
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}
//-->
</script>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="

<% for z = lbound(diasr_aux) to Ubound(diasr_aux)%>

MM_changeProp('cp<%=diasr_aux(z)%>','','style.backgroundColor','#6699FF','DIV');

<% next%>
<% for z = lbound(days_aux) to Ubound(days_aux)

%>
MM_changeProp('cp<%=days_aux(z)%>','','style.backgroundColor','#FFFF00','DIV');

<% next%>
">

<!-- <p>
mes : <% = mes %><br>
ano : <% = ano %><br>
primeiro dia da semana : <% = weekdayname(dtx) %><br>
primeiro dia do mes : <% = dia_ini %><br>
ultimo dia do mes : <% = dia_fim %><br>
dias selecionados : <% = days %>
<br>
<a href="#" onClick="pinta(1);">pinta</a> tabela<br>
<a href="#" onClick="pintacel('cp2');">pinta celula</a><br>
<a href="#" onClick="MM_changeProp('div2','','style.backgroundColor','#FFFF00','DIV')">cc
</a> <a href="#" onClick="MM_changeProp('cp2','','style.backgroundColor','#FFFF00','DIV')">cc </a> <a href="#" onClick="MM_changeProp('cp8','','style.backgroundColor','#FFFF00','DIV')">cc </a><br>
</p>


<div class="botao2" id="div2">hdskjhksdj</div>
<p>&nbsp; </p>
<p>&nbsp; </p>
-->
<form action="#" method="post" name="form1" id="form1">
  <table width="140"  border="1" align="center" cellpadding="0" cellspacing="0" class="tabela">
    <tr>
      <td valign="top">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" id="calendar">
<tr align="center">
            <td colspan="7" class="center11verdanaRED">
              <!--<a href="<% = link & linkmes_ant & "/" & linkano_ant %>">&lt;&lt;</a> <a href="#">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </a> -->
<% = mes_fresco %>
              -
              <% = ano %>
              <!--   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="<% = link & linkmes_prox & "/" & linkano_prox%>"> &gt;&gt;</a> -->
            </td>
          </tr>
          <tr align="center" class="center12verdanaBOLD">
            <% for i = 1 to 7 %>
            <td>
              <% =Ucase(Left(weekdayname(weekday(i)),1))%>
            </td>
            <% next%>
          </tr>
          <tr align="center">
            <%

days_aux = split(days,"-")


 Select Case  dtx 'day of week
 case 1
color = "#FFFFFF"   

 for x = day(dia_ini) to day(dia_fim)

  %>
            <td> <div class="botao2" id="cp<%=x%>"><a href="<%=aux_file%>?data=<%=x%>/<%=mes%>/<%=ano%>" target="miolo"><%=x%></a></div></td>
            <%
 if x mod 7 =0 then response.write "    </tr><tr align='center'>"
 next
 %>
            <% case 2 %>
            <td><div class="botao2" id="branco"></div></td>
            <%  for x = day(dia_ini) to day(dia_fim)
  %>
            <td><div class="botao2" id="cp<%=x%>"><a href="<%=aux_file%>?data=<%=x%>/<%=mes%>/<%=ano%>" target="miolo"><%=x%></a></div></td>
            <%
 if x =6 then response.write "    </tr><tr align='center'>"
  if x =13 then response.write "    </tr><tr align='center'>"
  if x =20 then response.write "    </tr><tr align='center'>"
  if x =27 then response.write "    </tr><tr align='center'>"
 if x mod 7 =0 and x <> 7 and x <> 13  and x <> 14 and x <> 21  and x <> 28 then response.write "    </tr><tr align='center'>"
 next
 %>
            <% case 3 %>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <%
 for x = day(dia_ini) to day(dia_fim)
  %>
            <td><div class="botao2" id="cp<%=x%>"><a href="<%=aux_file%>?data=<%=x%>/<%=mes%>/<%=ano%>" target="miolo"><%=x%></a></div></td>
            <%
 if x =5 then response.write "    </tr><tr align='center'>"
  if x =12 then response.write "    </tr><tr align='center'>"
  if x =19 then response.write "    </tr><tr align='center'>"
  if x =26 then response.write "    </tr><tr align='center'>"

 if x mod 7 =0 and x <> 7  and x <> 14 and x <> 21 and x <> 28  then response.write "    </tr><tr align='center'>"
 next
 %>
            <% case 4 %>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <% 
       
    for x = day(dia_ini) to day(dia_fim)
  %>
            <td><div class="botao2" id="cp<%=x%>"><a href="<%=aux_file%>?data=<%=x%>/<%=mes%>/<%=ano%>" target="miolo"><%=x%></a></div></td>
            <%
  if x =4 then response.write "    </tr><tr align='center'>"
  if x =11 then response.write "    </tr><tr align='center'>"
  if x =18 then response.write "    </tr><tr align='center'>"
  if x =25 then response.write "    </tr><tr align='center'>"
 if x mod 7 =0 and x <> 7  and x <> 14 and x <> 21 and x <> 28  then response.write "    </tr><tr align='center'>"
 next
 %>
            <% case 5 %>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <% 
       
    for x = day(dia_ini) to day(dia_fim)
  %>
            <td><div class="botao2" id="cp<%=x%>"><a href="<%=aux_file%>?data=<%=x%>/<%=mes%>/<%=ano%>" target="miolo"><%=x%></a></div></td>
            <%
if x =3 then response.write "    </tr><tr align='center'>"
  if x =10 then response.write "    </tr><tr align='center'>"
  if x =17 then response.write "    </tr><tr align='center'>"
  if x =24 then response.write "    </tr><tr align='center'>"
 if x mod 7 =0 and x <> 7  and x <> 14 and x <> 21 and x <> 28  then response.write "    </tr><tr align='center'>"
 next%>
            <% case 6 %>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <% 
    for x = day(dia_ini) to day(dia_fim)
  %>
            <td><div class="botao2" id="cp<%=x%>"><a href="<%=aux_file%>?data=<%=x%>/<%=mes%>/<%=ano%>" target="miolo"><%=x%></a></div></td>
            <%
if x =2 then response.write "    </tr><tr align='center'>"
if x =9 then response.write "    </tr><tr align='center'>"
if x =16 then response.write "    </tr><tr align='center'>"
if x =23 then response.write "    </tr><tr align='center'>"
if x =30 then response.write "    </tr><tr align='center'>"

 if x mod 7 =0 and x <> 7  and x <> 14 and x <> 21 and x <> 28  then response.write "    </tr><tr align='center'>"
 next%>
            <% case 7 %>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <td><div class="botao2" id="branco"></div></td>
            <% 
    for x = day(dia_ini) to day(dia_fim)
  %>
            <td><div class="botao2" id="cp<%=x%>"><a href="<%=aux_file%>?data=<%=x%>/<%=mes%>/<%=ano%>" target="miolo"><%=x%></a></div></td>
            <%
if x =1 then response.write "    </tr><tr align='center'>"
if x =8 then response.write "    </tr><tr align='center'>"
if x =15 then response.write "    </tr><tr align='center'>"
if x =22 then response.write "    </tr><tr align='center'>"
if x =29 then response.write "    </tr><tr align='center'>"

if x mod 7 =0 and x <> 7  and x <> 14 and x <> 21 and x <> 28  then response.write "    </tr><tr align='center'>"
 next%>
            <% 
end Select
   %>
          </tr>
        </table></td>
    </tr>
  </table>
</form>
</body>
</html>