Page 1 sur 1

Aide à l adaptation d'une class à mes besoins

Posté : 27 mars 2014, 11:39
par petitux
Bonjour,

Afin de faciliter l'utilisation de mon application web, je cherche à remplacer un formulaire de saisie d'une date par l'affichage d'un calendrier.
Je me suis donc mis en quête d'un code source d affichage de calendrier ( sans Js ni Ajax pour une meilleur compatibilité ).
J'ai trouvé une class phpCal dont le code est ci-dessous :
<?php
/********************************************************************/
/*
	@File Name: phpCal
	@Author: Rick Hopkins
	@Date: 11.07.2003
	@Description:
		[ PHP based calendar program which will create a calendar. It will display ]
		[ months, days, current day, navigation arrows to move from month to month, ]
		[ and year to year, and will also have the ability to color in days which contain ]
		[ events. The calendar will also be scalable in size, and have style editability. ]
		[ More features will be forth coming. ]
*/
/********************************************************************/
class phpCal
{ //class in session
	//declare variables
	// These variables all have to do with setting up calendar
	var $curMonth; //current month
	var $nextMonth; //next month
	var $prevMonth; //previous month
	var $curYear; //current year
	var $nextYear; //next year
	var $prevYear; //previous year
	var $monthTitleKey; //Key representing which month to pull out of $monthTitles array
	var $monthTitles; //Array of month titles
	var $curMonthTitle; //Title of current month
	var $daysOfWeek; //titles of days in week
	var $day; //numerical value of first day of month (0 = Sunday, 6 = Saturday)
	var $daysToCreate; //number of days to create
	var $eventDays; //array variable containing list of days with scheduled events
	
	// These variables all have to do with styling the calendar
	var $tableWidth; //width of calendar table
	var $tableColor; //color of calendar background
	var $tableBorder; //designates if table has border or not (0 = No, 0< = Yes, Width)
	var $tableBorderColor; //color of border
	var $tableSpacing; //space between table cells
	var $fontName; //font to be used
	var $fontSize; //size of font in pixels ex. 10px
	var $fontColor; //color of text
	var $numColor; //color of day numbers
	var $normalDayColor; //color of box on normal day
	var $normalDayRollOverColor; //color of normal box when rolled over
	var $curDayColor; //color of current day's box
	var $curDayRollOverColor; //color of current day's box when rolled over
	var $eventDayColor; //color of day's box which has events scheduled
	var $eventDayRollOverColor; //color of day's box which has events scheduled and has been rolled over
	var $dayLink; //name of file to link calendar days to
	var $calendarLink; //name of file to link calendar to
	
	var $fontStyleOne; //font style for month name and arrows
	var $fontStyleTwo; //font style for calendar days
	var $styleTable; //style commands for table
	var $borderStyleTdOne; //border style: bottom, right
	var $borderStyleTdTwo; //border style: bottom
	var $borderStyleTdThree; //border style: bottom left
	
	/************************************************/
	//function phpCal() will be the constructor class for phpCal class
	function phpCal($mois, $annee)
	{
		$this->curMonth = $mois;
		$this->curYear = $annee;
		$this->monthTitleKey = date("m", mktime(0, 0, 0, $this->curMonth, 1, $this->curYear)) - 1;
		$this->nextMonth = date("m", mktime(0, 0, 0, $this->curMonth + 1, 1, $this->curYear));
		$this->prevMonth = date("m", mktime(0, 0, 0, $this->curMonth - 1, 1, $this->curYear));
		$this->nextYear = date("Y", mktime(0, 0, 0, $this->curMonth + 1, 1, $this->curYear));
		$this->prevYear = date("Y", mktime(0, 0, 0, $this->curMonth - 1, 1, $this->curYear));
		$this->day = 1 - date("w", mktime(0, 0, 0, $this->curMonth, 1, $this->curYear));
		$this->daysToCreate = date("t", mktime(0, 0, 0, $this->curMonth, 1, $this->curYear));
		$this->eventDays = array();
		
		//setup default calendar styles
		$this->setStyleVars();
		$this->setStyles();
	}
	
	/************************************************/
	//function setStlyeVars() will set default values to all styles variables
	function setStyleVars()
	{
		$this->daysOfWeek = array(0 => "D", 1 => "L", 2 => "M", 3 => "M", 4 => "J", 5 => "V", 6 => "S");
		$this->monthTitles = array(0 => "Janvier", 1 => "Fevrier", 2 => "Mars", 3 => "Avril", 4 => "Mai", 5 => "Juin", 6 => "Juillet", 7 => "Aout", 8 => "Septembre", 9 => "Octobre", 10 => "Novembre", 11 => "Decembre");
		
		$this->tableWidth = 260;
		$this->tableColor = "#FFFFFF";
		$this->tableBorder = 1;
		$this->tableBorderColor = "#CCCCCC";
		$this->tableSpacing = 0;
		$this->fontName = "Verdana";
		$this->fontSize = "14";
		$this->fontColor = "#993300";
		$this->numColor = "#000000";
		$this->normalDayColor = "#FFFFFF";
		$this->normalDayRollOverColor = "#EEEEEE";
		$this->curDayColor = "#CCCCCC";
		$this->curDayRollOverColor = "#BBBBBB";
		$this->eventDayColor = "#C8C8AC";
		$this->eventDayRollOverColor = "B8B89B";
		$this->dayLink = sprintf("phpCal.php");
		$this->calendarLink = sprintf("phpCal.php");
	}
	
	/************************************************/
	//function setStyles() will set default values to all styles in calendar
	function setStyles()
	{
		$this->curMonthTitle = sprintf("%s %s", $this->monthTitles[$this->monthTitleKey], substr($this->curYear, 2, 4));
		
		$this->fontStyleOne = sprintf(" style=\"font-family: %s; font-size: %spx; font-weight: normal; color: %s; margin: 0px; padding: 0px;\"", $this->fontName, $this->fontSize, $this->fontColor);
		$this->fontStyleTwo = sprintf(" style=\"font-family: %s; font-size: %spx; font-weight: normal; color: %s; margin: 0px; padding: 0px;\"", $this->fontName, $this->fontSize, $this->numColor);
		if ($this->tableBorder > 0){
			$this->styleTable = sprintf(" style=\"background-color: %s;border-top: %spx solid %s; border-left: %spx solid %s; border-right: %spx solid %s;\"", $this->tableColor, $this->tableBorder, $this->tableBorderColor, $this->tableBorder, $this->tableBorderColor, $this->tableBorder, $this->tableBorderColor);
			$this->borderStyleTdOne = sprintf(" style=\"border-bottom: %spx solid %s; border-right: %spx solid %s;\"", $this->tableBorder, $this->tableBorderColor, $this->tableBorder, $this->tableBorderColor);
			$this->borderStyleTdTwo = sprintf(" style=\"border-bottom: %spx solid %s;\"", $this->tableBorder, $this->tableBorderColor);
			$this->borderStyleTdThree = sprintf(" style=\"border-bottom: %spx solid %s; border-left: %spx solid %s;\"", $this->tableBorder, $this->tableBorderColor, $this->tableBorder, $this->tableBorderColor);
			} else {
			$this->styleTable = sprintf("");
			$this->borderStyleTdOne = sprintf("");
			$this->borderStyleTdTwo = sprintf("");
			$this->borderStyleTdThree = sprintf("");
			}
	}
	
	/************************************************/
	//function createCalendar() will piece the calendar together
	function createCal()
	{
		$day = $this->day;
		
		$cal = sprintf("<table width=\"%s\" cellpadding=\"%s\" cellspacing=\"%s\"%s>\n", $this->tableWidth, $this->tableSpacing, $this->tableSpacing, $this->styleTable);
		$cal .= sprintf("<tr onmouseover=\"style.cursor='pointer';\">\n");
		$cal .= sprintf("<td bgcolor=\"%s\" align=\"center\" onclick=\"window.location.href='%s?mois=%s&annee=%s';\" onmouseover=\"style.backgroundColor='%s'\" onmouseout=\"style.backgroundColor='%s'\"%s><h1%s><</h1></td>\n", $this->normalDayRollOverColor, $this->calendarLink, $this->prevMonth, $this->prevYear, $this->curDayColor, $this->normalDayRollOverColor, $this->borderStyleTdOne, $this->fontStyleOne);
		$cal .= sprintf("<td bgcolor=\"%s\" colspan=\"5\" align=\"center\" onclick=\"window.location.href='%s';\" onmouseover=\"style.backgroundColor='%s'\" onmouseout=\"style.backgroundColor='%s'\"%s><h1%s>%s</h1></td>\n", $this->normalDayRollOverColor, $this->calendarLink, $this->curDayColor, $this->normalDayRollOverColor, $this->borderStyleTdTwo, $this->fontStyleOne, $this->curMonthTitle);
		$cal .= sprintf("<td bgcolor=\"%s\" align=\"center\" onclick=\"window.location.href='%s?mois=%s&annee=%s';\" onmouseover=\"style.backgroundColor='%s'\" onmouseout=\"style.backgroundColor='%s'\"%s><h1%s>></h1></td>\n", $this->normalDayRollOverColor, $this->calendarLink, $this->nextMonth, $this->nextYear, $this->curDayColor, $this->normalDayRollOverColor, $this->borderStyleTdThree, $this->fontStyleOne);
		$cal .= sprintf("</tr>\n");
		$cal .= sprintf("<tr>\n");
		$width = round($this->tableWidth / 7);
		for ($i = 0; $i < count($this->daysOfWeek); $i++){
			switch ($i){
				case 5:
					$cal .= sprintf("<td bgcolor=\"%s\" width=\"%s\" align=\"center\"%s><h1%s>%s</h1></td>\n", $this->normalDayRollOverColor, $width, $this->borderStyleTdTwo, $this->fontStyleTwo, $this->daysOfWeek[$i]);
					break;
				case 6:
					$cal .= sprintf("<td bgcolor=\"%s\" width=\"%s\" align=\"center\"%s><h1%s>%s</h1></td>\n", $this->normalDayRollOverColor, $width, $this->borderStyleTdThree, $this->fontStyleTwo, $this->daysOfWeek[$i]);
					break;
				default:
					$cal .= sprintf("<td bgcolor=\"%s\" width=\"%s\" align=\"center\"%s><h1%s>%s</h1></td>\n", $this->normalDayRollOverColor, $width, $this->borderStyleTdOne, $this->fontStyleTwo, $this->daysOfWeek[$i]);
					break;
				}
			}
		$cal .= sprintf("</tr>\n");
		
		$rows = 0;
		while ($day <= $this->daysToCreate){
			$cal .= sprintf("<tr onmouseover=\"style.cursor='pointer';\">\n");
			for ($i = 0; $i < 7; $i++){
				switch ($i){
					case 5:
						$borderStyle = $this->borderStyleTdTwo;
						break;
					case 6:
						$borderStyle = $this->borderStyleTdThree;
						break;
					default:
						$borderStyle = $this->borderStyleTdOne;
						break;
					}
				if (($day <= $this->daysToCreate) && ($day > 0)){
					$cal .= $this->setUpDay($borderStyle, $day);
					} else {
					$cal .= $this->setUpDay($borderStyle);
					}
				$day++;
				}
			$cal .= sprintf("</tr>\n");
			$rows++;
			}
		
		$cal .= sprintf("</table>\n");
		
		return $cal;
	}
	
	/************************************************/
	//function setUpDay() will color the given day accordingly
	function setUpDay($borderStyle, $day = "")
	{
		if (strlen($day) > 0){
			if (strlen($day) == 1){
				$day = sprintf("0%s", $day);
				}
			$dateCheck = sprintf("%s/%s/%s", $this->curMonth, $day, $this->curYear);
			if (in_array($dateCheck, $this->eventDays)){
				if (($day == date("j")) && ($this->curMonth == date("m")) && ($this->curYear == date("Y"))){
					$cell = sprintf("<td align=\"center\" bgcolor=\"%s\" onclick=\"window.location.href='%s?mois=%s&jour=%s&annee=%s';\" onmouseover=\"style.backgroundColor='%s'\" onmouseout=\"style.backgroundColor='%s'\"%s><h1%s><b>%s</b></h1></td>\n", $this->eventDayColor, $this->dayLink, $this->curMonth, $day, $this->curYear, $this->eventDayRollOverColor, $this->eventDayColor, $borderStyle, $this->fontStyleTwo, $day);
					} else {
					$cell = sprintf("<td align=\"center\" bgcolor=\"%s\" onclick=\"window.location.href='%s?mois=%s&jour=%s&annee=%s';\" onmouseover=\"style.backgroundColor='%s'\" onmouseout=\"style.backgroundColor='%s'\"%s><h1%s>%s</h1></td>\n", $this->eventDayColor, $this->dayLink, $this->curMonth, $day, $this->curYear, $this->eventDayRollOverColor, $this->eventDayColor, $borderStyle, $this->fontStyleTwo, $day);
					}
				} else {
				if (($day == date("j")) && ($this->curMonth == date("m")) && ($this->curYear == date("Y"))){
					$cell = sprintf("<td align=\"center\" bgcolor=\"%s\" onclick=\"window.location.href='%s?mois=%s&jour=%s&annee=%s';\" onmouseover=\"style.backgroundColor='%s'\" onmouseout=\"style.backgroundColor='%s'\"%s><h1%s><b>%s</b></h1></td>\n", $this->curDayColor, $this->dayLink, $this->curMonth, $day, $this->curYear, $this->curDayRollOverColor, $this->curDayColor, $borderStyle, $this->fontStyleTwo, $day);
					} else {
					$cell = sprintf("<td align=\"center\" bgcolor=\"%s\" onclick=\"window.location.href='%s?mois=%s&jour=%s&annee=%s';\" onmouseover=\"style.backgroundColor='%s'\" onmouseout=\"style.backgroundColor='%s'\"%s><h1%s>%s</h1></td>\n", $this->normalDayColor, $this->dayLink, $this->curMonth, $day, $this->curYear, $this->normalDayRollOverColor, $this->normalDayColor, $borderStyle, $this->fontStyleTwo, $day);
					}
				}
			} else {
			$cell = sprintf("<td align=\"center\"%s><h1%s>&nbsp;</h1></td>\n", $borderStyle, $this->fontStyleTwo);
			}
		
		return $cell;
	}
	
	/************************************************/
	//function setEventDays() will allow a user to load an array of days with events for calendar coloration
	function setEventDays($events)
	{
		$this->eventDays = $events;
	}
	
	/************************************************/
	//function setFontVars() will allow the user to set the font variables
	function setFontVars($name, $size, $fColor, $nColor = "")
	{
		$this->fontName = $name;
		$this->fontSize = $size;
		$this->fontColor = $fColor;
		if (strlen($nColor) < 1){
			$this->numColor = $fColor;
			} else {
			$this->numColor = $nColor;
			}
		$this->setStyles();
	}
	
	/************************************************/
	//function setTableVars() will allow for editing the table values for size, color, and spacing
	function setTableVars($size, $color, $spacing)
	{
		$this->tableWidth = $size;
		$this->tableColor = $color;
		$this->tableSpacing = $spacing;
		$this->setStyles();
	}
	
	/************************************************/
	//function setTableBorders() will set the table border variables
	function setTableBorders($thickness, $color)
	{
		$this->tableBorder = $thickness;
		$this->tableBorderColor = $color;
		$this->setStyles();
	}
	
	/************************************************/
	//function setLinks() will allow for setting the calendar links
	function setLinks($dayLink, $calLink)
	{
		$this->dayLink = $dayLink;
		$this->calendarLink = $calLink;
	}
	
	/************************************************/
	//function setDaysOfWeek() will allow the user to change the titles of the days of the week
	function setDaysOfWeek($dayTitles)
	{
		$this->daysOfWeek = $dayTitles;
	}
	
	/************************************************/
	//function setMonthTitles() will allow the user to change the titles of months
	function setMonthTitles($monthTitles)
	{
		$this->monthTitles = $monthTitles;
		$this->setStyles();
	}
	
	/************************************************/
	//function setNormalDayColors() will allow for setting normal day background colors
	function setNormalDayColors($reg, $roll)
	{
		$this->normalDayColor = $reg;
		$this->normalDayRollOverColor = $roll;
		$this->setStyles();
	}
	
	/************************************************/
	//function setCurrentDayColors() will allow for setting current day background colors
	function setCurrentDayColors($reg, $roll)
	{
		$this->curDayColor = $reg;
		$this->curDayRollOverColor = $roll;
		$this->setStyles();
	}
	
	/************************************************/
	//function setEventDayColors() will allow for setting event day background colors
	function setEventDayColors($reg, $roll)
	{
		$this->eventDayColor = $reg;
		$this->eventDayRollOverColor = $roll;
		$this->setStyles();
	}
	
	/************************************************/
	//function setStartDay() will allow for changing the first day of the month
	function setStartDay($day) //1 = Sunday Starting Calendar, 1< = Monday Starting Calendar
	{
		if ($day > 1){
			$this->day = 1 - (date("w", mktime(0, 0, 0, $this->curMonth, 1, $this->curYear)) - 1);
			if ($this->day == 2){
				$this->day = -5;
				}
			} else {
			$this->day = 1 - date("w", mktime(0, 0, 0, $this->curMonth, 1, $this->curYear));
			}
	}
	
	/************************************************/
} //class out
/********************************************************************/
?>


La page php qui se charge d'appeler et d'afficher le calendrier est conçu à l origine comme ceci :
<?php
	require('phpCal.php');
	
	if ($_GET['mois']){
		$cal = new phpCal($_GET['mois'], $_GET['annee']);
		} else {
		$cal = new phpCal(date('m'), date('Y'));
		}
	
	
$cal->setLinks('index.php', 'index.php');

print($cal->createCal()); 

?>
Rien à redire, tout fonctionne.
Cependant, je souhaite modifier son fonctionnement pour faire en sorte que lorsqu'une date est sélectionnée, le calendrier ne se réaffiche pas et idéalement transmette la date selectionnée vers une autre page ( via $_GET par exemple ).
Malheureusement, mes compétences en gestion de class/compréhension de classe/objet sont limitées et je ne trouve pas de solution.

Si quelqu'un à une piste, une suggestion pour me faire avancer, merci de m en faire part.

Cordialement,

Re: Aide à l adaptation d'une class à mes besoins

Posté : 27 mars 2014, 16:04
par moogli
salut,

Désolé mais 323 lignes de code c'est trop (sans parlé du style php4).

par contre ce que je peux te dire c'est qu'il faut trouver l'endroit qui affiche une case et gérer l’événement dessus (bouton ou lien ?).

Généralement ce genre de calendrier est ouvert en popup et du coup il te faut du javascrit pour interagir avec la première page.
Si la chose est entièrement intégré à ta page et qu'il s'agit d'un champ de formulaire tu dois pouvoir le récupérer simplement.
( sans Js ni Ajax pour une meilleur compatibilité ).
ton truc doit être compatible IE 2 ?

Actuellement la plus part des navigateur moderne, ou plus viens son a même d'utiliser la plus part des datepicker qui existe (par exemple celui de jQueryUi).
Un tel composant t’amène du dynamisme et un coté "user friendly" que tu n'auras pas avec ton script (dans le cas de la popup c'est la merde sur des smartphone / tablette alors que JS est utilisable) etc etc.

si tu ne cible que des navigateur récent il existe des champs html5 tous prêt pour cela http://www.alsacreations.com/tuto/lire/ ... local.html ;)


@+

Re: Aide à l adaptation d'une class à mes besoins

Posté : 27 mars 2014, 16:34
par petitux
Je comprend, je m' attendais un petit peu :)

Je souhaite ne pas utiliser de JS pour plusieurs raisons :
1/ je pars du principe que tout le monde n'active pas forcement javascript ds son navigateur ( ce qui est mon cas et d autres utilisateurs de mon appli oueb par souci de sécurité/éthique )
2/ je veux utiliser un language que je connais et souhaite à terme maîtriser.

En tout cas, l'information importante est que ma classe cité ci-dessus utilise du php4. Donc à bannir....

Ps : merci pour ce lien, je vais regarder cela de plus pret et en effet, cela pourrait suffir à répondre à mon besoin.

Ps (bis) : fausse joie, FF est le navigateur le plus utilisé par mes visiteurs, cela ne pourra donc pas fonctionner :)

Merci encore et je reste ouvert à toute piste/information.

Re: [RESOLU] Aide à l adaptation d'une class à mes besoins

Posté : 27 mars 2014, 18:30
par AB
Cependant, je souhaite modifier son fonctionnement pour faire en sorte que lorsqu'une date est sélectionnée, le calendrier ne se réaffiche pas et idéalement transmette la date selectionnée vers une autre page ( via $_GET par exemple ).
Le principe d'ajax est justement de pouvoir transmettre et recevoir des informations puis de modifier la page sans avoir besoin de la faire réafficher. Avec php seul tu ne pourras pas obtenir ce comportement.