Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Wilker

Class para gerar Calendario

Recommended Posts

e ae galera!! oa soh, vo avisando aki pra vcs q eu vo passa 1 mes de ferias (finalmente...), ai vo fica off nesse mes, n q va faze tanta falta assim neh :P

 

bom, antes de viaja eu vo dexa um presentinho aki pra vcs, criei agorinha uma classe para a geracao de calendarios para sites, tem gente q acha isso meio complicado, mas usando essa classe você consegue isso facinho :)

 

calendar.php

PHP

[*]<?

[*]

[*]class Calendar {

[*] var $bgColor = "#FFFFFF";

[*] var $mouseOverColor = "#9999FF";

[*] var $lineColor = "#000000";

[*] var $fontColor = "#000000";

[*] var $eventBgColor = "#BBBBFF";

[*] var $eventStyle = "";

[*] var $nowDateStyle = "bold";

[*] var $tableWidth = 150;

[*]

[*] var $day;

[*] var $month;

[*] var $year;

[*]

[*] var $events = array();

[*]

[*] function Calendar($d = 0, $m = 0, $y = 0) {

[*] $this->day = (int) $d ? (int) $d : date("j");

[*] $this->month = (int) $m ? (int) $m : date("n");

[*] $this->year = (int) $y ? (int) $y : date("Y");

[*] }

[*]

[*] function setEvent($d) {

[*] $this->events[] = $d;

[*] }

[*]

[*] function defineEvents($listEvents) {

[*] $this->events = $listEvents;

[*] }

[*]

[*] function show($write = true) {

[*] $return = "";

[*] $return .= "<table width='" . $this->tableWidth . "' cellspacing='1' cellpadding='2' bgcolor='" . $this->lineColor . "' style='font-family: Verdana; font-size: 11px;'>\r\n";

[*] $return .= "\t<tr>\r\n";

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>D</b></td>\r\n";

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>S</b></td>\r\n";

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>T</b></td>\r\n";

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>Q</b></td>\r\n";

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>Q</b></td>\r\n";

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>S</b></td>\r\n";

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>S</b></td>\r\n";

[*] $return .= "\t</tr>\r\n";

[*] $return .= "\t<tr>\r\n";

[*]

[*] $tempo = mktime(0, 0, 0, $this->month, 1, $this->year);

[*]

[*] $fwd = date("w", $tempo);

[*] $td = date("t", $tempo);

[*]

[*] $iDay = 1;

[*] $iTmp = 0;

[*]

[*] for($i = 0; $i < $fwd; $i++) {

[*] $return .= "\t\t<td bgcolor='" . $this->bgColor . "'><span></span></td>\r\n";

[*] $iTmp++;

[*] }

[*]

[*] while($iDay <= $td) {

[*] $tmp = $iTmp % 7;

[*]

[*] if($tmp == 0 && $iTmp != 0)

[*] $return .= "\t</tr>\r\n\t<tr>\r\n";

[*]

[*] if(in_array($iDay, $this->events)) {

[*] $thisBg = $this->eventBgColor;

[*]

[*] switch($this->eventStyle) {

[*] case "bold":

[*] $aDay = "<b>{$iDay}</b>";

[*] break;

[*] case "italic":

[*] $aDay = "<i>{$iDay}</i>";

[*] break;

[*] case "bold-italic":

[*] $aDay = "<b><i>{$iDay}</i></b>";

[*] break;

[*] default:

[*] $aDay = $iDay;

[*] }

[*] } else {

[*] $thisBg = $this->bgColor;

[*] $aDay = $iDay;

[*] }

[*]

[*] if($iDay == $this->day) {

[*] switch($this->nowDateStyle) {

[*] case "bold":

[*] $aDay = "<b>{$iDay}</b>";

[*] break;

[*] case "italic":

[*] $aDay = "<i>{$iDay}</i>";

[*] break;

[*] case "bold-italic":

[*] $aDay = "<b><i>{$iDay}</i></b>";

[*] break;

[*] default:

[*] $aDay = $iDay;

[*] }

[*] }

[*]

[*] $return .= "\t\t<td ";

[*] $return .= "align='center' ";

[*] $return .= "bgcolor='" . $thisBg . "' ";

[*] $return .= "style='cursor: pointer; color: " . $this->fontColor . ";' ";

[*] $return .= "onmouseover=\"this.bgColor = '" . $this->mouseOverColor . "'\" ";

[*] $return .= "onmouseout=\"this.bgColor = '" . $thisBg . "'\">";

[*] $return .= $aDay;

[*] $return .= "</td>\r\n";

[*]

[*] $iDay++;

[*] $iTmp++;

[*] }

[*]

[*] while(($iTmp % 7) > 0) {

[*] $return .= "\t\t<td bgcolor='" . $this->bgColor . "'><span></span></td>\r\n";

[*] $iTmp++;

[*] }

[*]

[*] $return .= "\t</tr>\r\n</table>\r\n";

[*]

[*] if($write) {

[*] echo $return;

[*] } else {

[*] return $return;

[*] }

[*] }

[*]}

[*]

[*]?>

 

exemplo de uso:

 

calendarTest.php

PHP

[*]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

[*]<html>

[*]<head>

[*]<title>Untitled Document</title>

[*]<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

[*]</head>

[*]

[*]<body>

[*]<?

[*]

[*]require("calendar.php");

[*]

[*]$cal = new Calendar();

[*]$cal->setEvent(5);

[*]$cal->setEvent(15);

[*]$cal->show();

[*]

[*]?>

[*]</body>

[*]</html>

 

vou explicar os metodos:

 

Calendar:

esse é o metodo de inicializacao, você pode nele auterar a data que o calendario vai mostrar, por padrao ele pega o dia atual

exemplo: $cal = new Calendar(5, 3, 2005); //dia 5 de marco de 2005

 

setEvent:

definir eventos no calendario, qdo uma data é definida, a cor de fundo desse dia fica marcada (fica diferente dos outros dias), você pode marcar quantos dias voce quizer

exemplo: $cal->setEvent(12); //marcar o dia 12

 

defineEvents:

essa é outra maneira de definir os eventos, com esse você passa um array com os dias, e todos os dias q estiverem no array serao marcados

exemplo:

$dias = array(3, 6, 10, 20, 25);

$cal->defineEvents($dias);

ATENCAO - ESSA FUNCAO SOBRESCREVE TODOS OS DIAS DEFINIDOS POR setEvent

 

show:

essa funcao server para exibir o calendario, ou retornar o codigo gerador do mesmo, isso vai depender do 1 argumento passado, c for true (padrao) ele vai escrever diretamente o codigo do calendario, em caso de false ele vai retornar esse valor

exemplo:

$ret = $cal->show(false);

echo $ret;

 

bom, eh isso galera, vcs tb podem definir as cores:

exemplo:

$cal->bgColor = "#CCCCCC";

$cal->lineColor = "#FF0000";

 

veja no proprio codigo as cores possiveis de definir (estao logo no inicio da class)

 

os parametros eventStyle e nowDateStyle podem ser definidos com 3 valores:

bold - negrito

italic - italico

bold-italic - negrito e italico

 

qq outro valor (como nada por exemplo) vai resultar em nenhum estilo

 

qq duvida, posta ai, eu to viajando terca fera (dia 5)

 

flws

Compartilhar este post


Link para o post
Compartilhar em outros sites

Olha, fiz uma modificação no seu código caro wilker, agora você pode atribuir links para os dias. http://forum.imasters.com.br/public/style_emoticons/default/assobiando.gif

 

PHP

[*]<? 

[*]

[*]class Calendar { 

[*] var $bgColor = "#FFFFFF"

[*] var $mouseOverColor = "#f4f4f4"

[*] var $lineColor = "#cccccc"

[*] var $fontColor = "#555555"

[*] var $eventBgColor = "#f1f1f1"

[*] var $eventStyle = ""

[*] var $nowDateStyle = "bold"

[*] var $tableWidth = 100; 

[*]  

[*] var $day; 

[*] var $month; 

[*] var $monthName; 

[*] var $year; 

[*]  

[*] var $events = array()

[*] var $links = array()

[*]  

[*] function Calendar($d = 0, $m = 0, $y = 0) { 

[*] $this->day = (int) $d ? (int) $d : date("j")

[*] $this->month = (int) $m ? (int) $m : date("n")

[*] $this->monthName = date("M - Y")

[*] $this->year = (int) $y ? (int) $y : date("Y")

[*] } 

[*]  

[*] function setEvent($d, $link) { 

[*] $this->events[] = $d; 

[*] $this->links[] = $link; 

[*] } 

[*]  

[*] function defineEvents($listEvents) { 

[*] $this->events = $listEvents; 

[*] } 

[*]

[*] function defineLinks($listLinks) { 

[*] $this->links = $listLinks; 

[*] } 

[*]

[*] function show($write = true) { 

[*] $return = ""

[*] $return .= "<table width='" . $this->tableWidth . "' cellspacing='1' cellpadding='2' bgcolor='" . $this->lineColor . "' style='font-family: Small Fonts, Verdana; font-size: 9px;'>\r\n"

[*] $return .= "\t<tr>\r\n"

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "' colspan=\"7\"><b>".$this->monthName."</b></td>\r\n"

[*] $return .= "\t</tr>\r\n"

[*] $return .= "\t<tr>\r\n"

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>D</b></td>\r\n"

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>S</b></td>\r\n"

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>T</b></td>\r\n"

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>Q</b></td>\r\n"

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>Q</b></td>\r\n"

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>S</b></td>\r\n"

[*] $return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>S</b></td>\r\n"

[*] $return .= "\t</tr>\r\n"

[*] $return .= "\t<tr>\r\n"

[*]  

[*] $tempo = mktime(0, 0, 0, $this->month, 1, $this->year)

[*]  

[*] $fwd = date("w", $tempo)

[*] $td = date("t", $tempo)

[*]  

[*] $iDay = 1; 

[*] $iTmp = 0; 

[*] $contLinks = 0;

[*]  

[*] for($i = 0; $i < $fwd; $i++) { 

[*] $return .= "\t\t<td bgcolor='" . $this->bgColor . "'><span></span></td>\r\n"

[*] $iTmp++

[*] } 

[*]  

[*] while($iDay <= $td) { 

[*] $tmp = $iTmp % 7; 

[*]  

[*] if($tmp == 0 && $iTmp != 0) 

[*] $return .= "\t</tr>\r\n\t<tr>\r\n"

[*]  

[*] if(in_array($iDay, $this->events)) { 

[*] $thisBg = $this->eventBgColor; 

[*]  

[*] switch($this->eventStyle) { 

[*] case "bold"

[*] $aDay = "<b>{$iDay}</b>"

[*] break

[*] case "italic"

[*] $aDay = "<i>{$iDay}</i>"

[*] break

[*] case "bold-italic"

[*] $aDay = "<b><i>{$iDay}</i></b>"

[*] break

[*] default: 

[*] $aDay = "<a href=\"".$this->links[$contLinks]."\">".$iDay."</a>"

[*] $contLinks++;

[*] } 

[*] } else { 

[*] $thisBg = $this->bgColor; 

[*] $aDay = $iDay; 

[*] } 

[*]  

[*] if($iDay == $this->day) { 

[*] switch($this->nowDateStyle) { 

[*] case "bold"

[*] $aDay = "<b>{$iDay}</b>"

[*] break

[*] case "italic"

[*] $aDay = "<i>{$iDay}</i>"

[*] break

[*] case "bold-italic"

[*] $aDay = "<b><i>{$iDay}</i></b>"

[*] break

[*] default: 

[*] $aDay = $iDay; 

[*] } 

[*] } 

[*]  

[*] $return .= "\t\t<td "

[*] $return .= "align='center' "

[*] $return .= "bgcolor='" . $thisBg . "' "

[*] $return .= "style='cursor: pointer; color: " . $this->fontColor . ";' "

[*] $return .= "onmouseover=\"this.bgColor = '" . $this->mouseOverColor . "'\" "

[*] $return .= "onmouseout=\"this.bgColor = '" . $thisBg . "'\">"

[*] $return .= $aDay; 

[*] $return .= "</td>\r\n"

[*]  

[*] $iDay++

[*] $iTmp++

[*] } 

[*]  

[*] while(($iTmp % 7) > 0) { 

[*] $return .= "\t\t<td bgcolor='" . $this->bgColor . "'><span></span></td>\r\n"

[*] $iTmp++

[*] } 

[*]  

[*] $return .= "\t</tr>\r\n</table>\r\n"

[*]  

[*] if($write) { 

[*] echo $return

[*] } else { 

[*] return $return

[*] } 

[*] } 

[*]} 

[*]

[*]?>

[*]

 

Utilização:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>Untitled Document</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 

<body> 
<? 
require("class-calendar.php"); 

$cal = new Calendar(); 
$cal->setEvent(5, '01.php'); 
$cal->setEvent(6, '02.php'); 
$cal->setEvent(31, '04.php'); 
$cal->show(); 

?> 
</body> 
</html>

 

http://forum.imasters.com.br/public/style_emoticons/default/thumbsup.gif NeoN

Compartilhar este post


Link para o post
Compartilhar em outros sites

Aonde tem:

 

if($iDay == $this->day) {                switch($this->nowDateStyle) {                    case "bold":                        $aDay = "<b>{$iDay}</b>";                        break;                    case "italic":                        $aDay = "<i>{$iDay}</i>";                        break;                    case "bold-italic":                        $aDay = "<b><i>{$iDay}</i></b>";                        break;                    default:                        $aDay = $iDay;                }            }

Você coloca o link no $aDay nos casos. ;)

Compartilhar este post


Link para o post
Compartilhar em outros sites

Dentro dos cases, aonde tem negrito, itálico, negrito-e-itálico, você coloca a tag do link também em cada um. ;)

 

 

case "bold":                       $aDay = "<a href='link'><b>{$iDay}</b></a>";                       break;   //........

Compartilhar este post


Link para o post
Compartilhar em outros sites

to vendo q a galera ta viajando na minha class de calendario ^^ainda to aki soh curtindo as ferias e talzqdo volta eu explico umas duvidas da galera aiflww

Compartilhar este post


Link para o post
Compartilhar em outros sites

Olha, fiz uma modificação no seu código caro wilker, agora você pode atribuir links para os dias.  http://forum.imasters.com.br/public/style_emoticons/default/assobiando.gif

Aeeeeee, copie conforme você postou, mas quando eu coloquei no servidor tah dando erro na linha 28, como eu faço pra arrumar?Obs, no código original não da erro, mas gostei mais dessa modificação, pois dá pra colocar link :P Olha aqui como aparece o código, quase perfeito, se alguém puder me ajudar, eu fico grato :D

Compartilhar este post


Link para o post
Compartilhar em outros sites

fiz uma alteração para mostrar o mês em português e não em inglês, quem quizer testar tá aí, em vermelho foi o que eu acrescentei ou mexi:

 

<?

 

class Calendar {

var $bgColor = "#FFFFFF";

var $mouseOverColor = "#f4f4f4";

var $lineColor = "#cccccc";

var $fontColor = "#555555";

var $eventBgColor = "#ffcc00";

var $eventStyle = "";

var $nowDateStyle = "bold";

var $tableWidth = 140;

 

var $day;

var $month;

var $monthName;

var $year;

 

var $events = array();

var $links = array();

 

function mes($a) {

switch($a) {

case 1: $mes = "Janeiro - ".date("Y"); break;

case 2: $mes = "Fevereiro - ".date("Y"); break;

case 3: $mes = "Março - ".date("Y"); break;

case 4: $mes = "Abril - ".date("Y"); break;

case 5: $mes = "Maio - ".date("Y"); break;

case 6: $mes = "Junho - ".date("Y"); break;

case 7: $mes = "Julho - ".date("Y"); break;

case 8: $mes = "Agosto - ".date("Y"); break;

case 9: $mes = "Setembro - ".date("Y"); break;

case 10: $mes = "Outubro - ".date("Y"); break;

case 11: $mes = "Novembro - ".date("Y"); break;

case 12: $mes = "Dezembro - ".date("Y"); break;

}

return $mes;

}

 

function Calendar($d = 0, $m = 0, $y = 0) {

$this->day = (int) $d ? (int) $d : date("j");

$this->month = (int) $m ? (int) $m : date("n");

$this->monthName = $this->mes(date("m"));

$this->year = (int) $y ? (int) $y : date("Y");

}

 

function setEvent($d, $link) {

$this->events[] = $d;

$this->links[] = $link;

}

 

function defineEvents($listEvents) {

$this->events = $listEvents;

}

 

function defineLinks($listLinks) {

$this->links = $listLinks;

}

 

function show($write = true) {

$return = "";

$return .= "<table width='" . $this->tableWidth . "' cellspacing='1' cellpadding='2' bgcolor='" . $this->lineColor . "' style='font-family: verdana, tahoma, Small Fonts; font-size: 9px;'>\r\n";

$return .= "\t<tr>\r\n";

$return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "' colspan=\"7\"><b>".$this->monthName."</b></td>\r\n";

$return .= "\t</tr>\r\n";

$return .= "\t<tr>\r\n";

$return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>D</b></td>\r\n";

$return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>S</b></td>\r\n";

$return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>T</b></td>\r\n";

$return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>Q</b></td>\r\n";

$return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>Q</b></td>\r\n";

$return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>S</b></td>\r\n";

$return .= "\t\t<td align='center' bgcolor='" . $this->bgColor . "'><b>S</b></td>\r\n";

$return .= "\t</tr>\r\n";

$return .= "\t<tr>\r\n";

 

$tempo = mktime(0, 0, 0, $this->month, 1, $this->year);

 

$fwd = date("w", $tempo);

$td = date("t", $tempo);

 

$iDay = 1;

$iTmp = 0;

$contLinks = 0;

 

for($i = 0; $i < $fwd; $i++) {

$return .= "\t\t<td bgcolor='" . $this->bgColor . "'><span></span></td>\r\n";

$iTmp++;

}

 

while($iDay <= $td) {

$tmp = $iTmp % 7;

 

if($tmp == 0 && $iTmp != 0)

$return .= "\t</tr>\r\n\t<tr>\r\n";

 

if(in_array($iDay, $this->events)) {

$thisBg = $this->eventBgColor;

 

switch($this->eventStyle) {

case "bold":

$aDay = "<b>{$iDay}</b>";

break;

case "italic":

$aDay = "<i>{$iDay}</i>";

break;

case "bold-italic":

$aDay = "<b><i>{$iDay}</i></b>";

break;

default:

$aDay = "<a href=\"".$this->links[$contLinks]."\">".$iDay."</a>";

$contLinks++;

}

} else {

$thisBg = $this->bgColor;

$aDay = $iDay;

}

 

if($iDay == $this->day) {

switch($this->nowDateStyle) {

case "bold":

$aDay = "<b>{$iDay}</b>";

break;

case "italic":

$aDay = "<i>{$iDay}</i>";

break;

case "bold-italic":

$aDay = "<b><i>{$iDay}</i></b>";

break;

default:

$aDay = $iDay;

}

}

 

$return .= "\t\t<td ";

$return .= "align='center' ";

$return .= "bgcolor='" . $thisBg . "' ";

$return .= "style='cursor: pointer; color: " . $this->fontColor . ";' ";

$return .= "onmouseover=\"this.bgColor = '" . $this->mouseOverColor . "'\" ";

$return .= "onmouseout=\"this.bgColor = '" . $thisBg . "'\">";

$return .= $aDay;

$return .= "</td>\r\n";

 

$iDay++;

$iTmp++;

}

 

while(($iTmp % 7) > 0) {

$return .= "\t\t<td bgcolor='" . $this->bgColor . "'><span></span></td>\r\n";

$iTmp++;

}

 

$return .= "\t</tr>\r\n</table>\r\n";

 

if($write) {

echo $return;

} else {

return $return;

}

}

}

 

?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

bom, eu descobri onde se alterar para os links funcionarem no dia atual tb

mas como to mto no basico de oop http://forum.imasters.com.br/public/style_emoticons/default/blush.gif ainda não consegui fazer funcionar.

 

bom...

 

switch($this->eventStyle) {case "bold":$aDay = "<b>{$iDay}</b>";break;case "italic":$aDay = "<i>{$iDay}</i>";break;case "bold-italic":$aDay = "<b><i>{$iDay}</i></b>";break;default:$aDay = "<a href=\"".$this->links[$contLinks]."\">".$iDay."</a>";$contLinks++;}} else {$thisBg = $this->bgColor;$aDay = $iDay;}if($iDay == $this->day) {switch($this->nowDateStyle) {case "bold":$aDay = "<b>{$iDay}</b>";break;case "italic":$aDay = "<i>{$iDay}</i>";break;case "bold-italic":$aDay = "<b><i>{$iDay}</i></b>";break;default:$aDay = $iDay;

o negócio está nessa parte do código

 

existe uma variavel $contlinks q calcula qual valor do array esta sendo usado ['0'], ['1] e assim por diante.

 

se fizermos isso

 

if($iDay == $this->day) {switch($this->nowDateStyle) {case "bold":$aDay = "<a href=\"".$this->links[$contLinks]."\"><b>".$iDay."</b></a>";break;case "italic":$aDay = "<a href=\"".$this->links[$contLinks]."\"><i>".$iDay."</i></a>";break;case "bold-italic":$aDay = "<a href=\"".$this->links[$contLinks]."\"><b><i>".$iDay."</i></b></a>";break;default:$aDay = $iDay;

não vai funcionar pq a variavel $contlinks não está auto incrementando seu valor, e sendo assim não está passando o valor nescessario para o array, pq é fechado o bloco anterior pouco antes parando assim o auto incremento.

 

se for possivel continuar autoincrementando o valor do array nesse trecho o calendario vai funcionar perfeitamente com os links no dia atual.

 

o problema é q eu não to conseguindo fazer isso... http://forum.imasters.com.br/public/style_emoticons/default/blush.gif

 

bom, pelo menos achei o q tem q se fazer.

espero q tenha ajudado pelo menos um pouco.

:)

 

[]s!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.