Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

PHP 5 CLASSE CALENDRIER QUI RENVOIE LA DATE CLIQUÉE DANS 1 ÉLÉMENT HTML CHOISI


Information sur la source

Catégorie :Date / Heure Classé sous : calendrier, formulaire, iterateurs, php5 Niveau : Débutant Date de création : 16/05/2007 Date de mise à jour : 25/07/2007 20:13:29 Vu / téléchargé: 6 929 / 837

Note :
7,5 / 10 - par 2 personnes
7,50 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (7)
Ajouter un commentaire et/ou une note

Description

Cliquez pour voir la capture en taille normale
1 nouvelle classe de calendrier:

Fonctionnalités :
- alimente par GET la date selectionnée dans le text box ou n'importe quel autre élément de votre choix .
- possibilité d'afficher le calendrier:
     soit dans la page courante :la page ne se referme pas après selection de la date
     soit dans dans 1 popup ouvrante: la popup se referme après slection de la date
     sur 1 mois navigable, sur un nombre de mois précis à partir d'une mois précis ou du mois en cours.

- possibilité d'utiliser ses apparences favorites avec les styles css définis par l'utilisateur

Fonctionnalités supplémentaires:
Mise à jour:
Ajout des itérateurs et des accesseurs de tableaux.

La version à jour est dans le ZIP
 

Source

  • <?php
  • define ('MSG_WRONG_STYLE_TYPE','Style non paramètrable');
  • /**
  • * class myIterator implements Iterator,Countable,Seekableiterator,ArrayAccess
  • */
  • class myIterator implements Iterator,Countable,Seekableiterator,ArrayAccess
  • {
  • /**
  • * property protected array $oItems:
  • *
  • * desc: tableau d'éléments .
  • */
  • protected $oItems=null;
  • /**
  • * property protected int $ipos
  • *
  • * desc: indice itérative de Position relative
  • */
  • protected $iPos=0;
  • /**
  • * property protected int $iMin
  • *
  • * desc: indice minimale de bornage
  • */
  • protected $iMin=0;
  • /**
  • * property protected int $iLimit
  • *
  • * desc: nombre limite d'occurences bornées
  • */
  • protected $iLimit;
  • /**
  • * property int $iOffset
  • *
  • * desc: Indice itérative de Position absolue
  • */
  • protected $iOffset=0;
  • /**
  • * public function constructor
  • *
  • * param int $iMin
  • * param int $iLimit
  • */
  • public function __construct($iMin,$iLimit)
  • {
  • $this->oItems=array();
  • $this->iLimit=$iLimit;
  • $this->iMin=$iMin;
  • }
  • /**
  • * public function add
  • *
  • * desc: empile le tableau passé en paramètres sur la pile existante statique
  • *
  • * param int $iMin
  • * param int $iLimit
  • */
  • public function add($array)
  • {
  • $this->oItems[]=$array;
  • }
  • /**
  • * public function setLimit
  • *
  • * desc: Définit un bornage $it => indice de départ , $l => longueur de la plage en occurences
  • *
  • * param int $it
  • * param int $l
  • */
  • public function setLimit($it,$l)
  • {
  • if (is_int($it)&&is_int($l))
  • {
  • if ($l>0)
  • {
  • $this->iMin=$it;
  • $this->iLimit=$l;
  • $this->iOffset=$it;
  • }
  • }
  • }
  • /**
  • * public function current
  • *
  • * desc: Methode itérative interface iterator
  • */
  • public function current()
  • {
  • if ($this->valid())
  • return current($this->oItems);
  • }
  • /**
  • * public function valid
  • *
  • * desc: Methode itérative interface iterator
  • */
  • public function valid()
  • {
  • return ( $this->iOffset >= $this->iMin && $this->iPos < ($this->iLimit) ) ;
  • }
  • /**
  • * public function next
  • *
  • * desc: Methode itérative interface iterator
  • */
  • public function next()
  • {
  • $this->iPos++;
  • $this->iOffset++;
  • return next($this->oItems);
  • }
  • /**
  • * public function key
  • *
  • * desc: Methode itérative interface iterator
  • */
  • public function key()
  • {
  • if ($this->valid())
  • return key($this->oItems);
  • }
  • /**
  • * public function rewind
  • *
  • * desc: Methode itérative interface iterator
  • */
  • public function rewind()
  • {
  • reset ($this->oItems);
  • $position = 0;
  • while($position < $this->iMin)
  • {
  • next($this->oItems);
  • $position++;
  • }
  • $this->iPos=0;
  • $this->iOffset=$this->iMin;
  • if (!$this->valid())
  • throw new Exception('Invalid seek position');
  • }
  • /**
  • * public function count
  • *
  • * desc: Methode de comptage interface countable
  • */
  • public function count()
  • {
  • return count($this->oItems);
  • }
  • /**
  • * public function seek
  • *
  • * desc: Methode itérative interface seekableIterator
  • */
  • public function seek($index)
  • {
  • $this->rewind();
  • $position = $this->iMin;
  • while($position < $index && $this->valid())
  • {
  • $this->next();
  • $position++;
  • }
  • if (!$this->valid())
  • {
  • throw new Exception('Invalid seek position');
  • }
  • }
  • /**
  • * public function OffsetExists
  • *
  • * desc: Methode d' acces aux tableaux, interface arrayAccess
  • *
  • * param $iOffset int
  • */
  • public function OffsetExists($iOffset)
  • {
  • if ($iOffset < $this->iLimit && $iOffset >= $this->iMin)
  • return true;
  • else return false;
  • }
  • /**
  • * public function OffsetGet
  • *
  • * desc: Methode itérative interface arrayAccess
  • */
  • public function OffsetGet($iOffset)
  • {
  • if ($this->OffsetExists($iOffset))
  • return $this->oItems[$iOffset];
  • }
  • /**
  • * public function OffsetSet
  • * desc: Methode itérative interface arrayAccess
  • */
  • public function OffsetSet($iOffset,$val)
  • {
  • }
  • public function OffsetUnset($iOffset)
  • {
  • }
  • }
  • /**
  • * class monthIterator extends myIterator
  • */
  • class monthIterator extends myIterator
  • {
  • /**
  • * property int $startmonth
  • * indice minimale du bornage au mois
  • */
  • private $startmonth;
  • /**
  • * property int $startyear
  • * indice minimale du bornage à l'année
  • */
  • private $startyear;
  • /**
  • * property int $iM
  • * indice de position du mois
  • */
  • private $iM;
  • /**
  • * property int $iM
  • * indice de position de l'année
  • */
  • private $iY;
  • /**
  • * public function __construct
  • * Constructeur.
  • *
  • * @param string $start au format mm/yyyy
  • * @param int $period en mois
  • */
  • public function __construct($start,$period=100)
  • {
  • if (substr($start,2,1)!=='/')
  • throw new Exception('Paramètre Invalide pour constructeur format mm/yyyy');
  • $expl=explode('/',$start);
  • if( ((int)$expl[0]) <= 12 && ((int)$expl[0]) > 0 )
  • $this->startmonth=(int)$expl[0];
  • else throw new Exception('Format du mois invalide') ;
  • if ( ((int)$expl[1]) >= 1000 && ((int)$expl[1]) < 9999 )
  • $this->startyear=(int)$expl[1];
  • else throw new Exception('Format année invalide');
  • parent::__construct(0,$period);
  • }
  • /**
  • * public function
  • *
  • * desc: Getter
  • * param: mixed $prop
  • * return object
  • */
  • public function __get ($prop)
  • {
  • if (isset($this->$prop))
  • return $this->$prop;
  • }
  • /**
  • * public Function nextM
  • *
  • * desc: positionne les indices iM et iY (mois et année) de l'iterateur.
  • */
  • public function NextM()
  • {
  • if( $this->iM < 12)
  • {
  • $this->iM++;
  • }
  • else
  • {
  • $this->iM = 1;
  • $this->iY++;
  • }
  • }
  • /**
  • * public Function next
  • *
  • * desc: methode iterative.
  • */
  • public function next()
  • {
  • parent::next();
  • $this->NextM();
  • }
  • /**
  • * public Function rewind
  • *
  • * desc: methode iterative.
  • */
  • public function rewind()
  • {
  • parent::rewind();
  • $this->iM = $this->startmonth;
  • $this->iY = $this->startyear;
  • }
  • }
  • /**
  • * Class calendar
  • */
  • class calendar
  • {
  • /**
  • * property $monthIterator
  • * object monthIterator
  • */
  • private $monthIterator;
  • /**
  • * function Constructor
  • *
  • * param string $start format 'mm/yyyy'
  • * param int $period
  • *
  • */
  • public function __construct ($start,$period)
  • {
  • $this->monthIterator=new monthIterator($start,$period);
  • $i=$this->monthIterator->iMin;
  • $this->monthIterator->rewind();
  • while ( $i < $this->monthIterator->iLimit)
  • {
  • $this->monthIterator->add(self::createmonth($this->monthIterator->iM,$this->monthIterator->iY));
  • $this->monthIterator->NextM();
  • $i++;
  • }
  • }
  • /**
  • * Function
  • * desc: Getter
  • * param: mixed $prop
  • * return: property object
  • */
  • public function __get($prop)
  • {
  • if (isset($this->$prop))
  • return $this->$prop;
  • else return false;
  • }
  • /**
  • * function getCurrentMonth
  • *
  • * desc:renvoit le mois et l'année en cours
  • *
  • * return string format ('mm/yyyy')
  • */
  • public function getCurrentMonth()
  • {
  • return (($this->monthIterator->iM<10)?'0'.$this->monthIterator->iM.'/'.$this->monthIterator->iY:$this->monthIterator->iM.'/'.$this->monthIterator->iY);
  • }
  • public function getNextM($mode)
  • {
  • switch ($mode)
  • {
  • case 'M':
  • return(date('m',mktime(0, 0, 0, $this->monthIterator->iM+1, 1, $this->monthIterator->iY)));
  • break;
  • case 'Y':
  • return(date('Y',mktime(0, 0, 0, $this->monthIterator->iM+1, 1, $this->monthIterator->iY)));
  • break;
  • }
  • }
  • public function getPrevM($mode)
  • {
  • switch ($mode)
  • {
  • case 'M':
  • return (date('m',mktime(0, 0, 0, $this->monthIterator->iM-1, 1, $this->monthIterator->iY)));
  • break;
  • case 'Y':
  • return(date('Y',mktime(0, 0, 0, $this->monthIterator->iM-1, 1, $this->monthIterator->iY)));
  • break;
  • }
  • }
  • /**
  • * Static function
  • *
  • * desc: Methode de fabrication de calendrier de type tableau au mois
  • *
  • * param int $month
  • * param int $year
  • * return array
  • */
  • public static function createmonth($month,$year)
  • {
  • // jour de la semaine du premier du mois
  • $fom = date ("w",mktime(0, 0, 0, $month, 1, $year));
  • $day=1;
  • $start=false;
  • //combien de jours dans le mois?
  • $max=date('t',mktime(0, 0, 0, $month, 1, $year));
  • //formatage en chaine
  • if ($month < 10)
  • $month = '0'.$month;
  • //init
  • $d=0 ;
  • //Le calendrier va occuper 6 lignes(semaines) de 7 colonnes(jours) pour le mois au maximum
  • for ( $i=0 ; $i < 6 ; $i++ )
  • {
  • for ( $j=0 ; $j < 7 ;$j++ )
  • {
  • //début du comptage au 1er du mois pour la premiere semaine
  • if ($i==0)
  • {
  • if ($fom == $d)
  • {
  • $start=true;
  • $day=1;
  • }
  • }
  • if ($day < $max+1)
  • {
  • if ($start ===true)
  • {
  • if ($day<10)
  • $cal[$j][]='0'.$day;
  • else
  • $cal[$j][]=$day;
  • $day++;
  • }
  • else
  • $cal[$j][]='';
  • }
  • else
  • $cal[$j][]='';
  • $d++;
  • }
  • }
  • return $cal;
  • }
  • }
  • /**
  • * class htmlCalendar extends calendar
  • */
  • class htmlCalendar extends calendar
  • {
  • /**
  • * private property string domTextBoxId
  • *
  • * desc : l'id de la text box
  • */
  • private $domTextBoxId;
  • /**
  • * proprerty array settable
  • *
  • * desc : les propriétés modifiables
  • */
  • private $settable=array('cssHeader',
  • 'cssSunday',
  • 'cssWeekday',
  • 'cssToday',
  • 'cssLink',
  • 'cssFooter',
  • 'MonthByMonth',
  • 'tdWidth',
  • 'tdHeight',
  • 'target');
  • /**
  • * proprerty array style
  • *
  • * desc: le tableau des propriétés de style
  • */
  • private $style=array();
  • /**
  • * proprerty string html
  • *
  • * desc: la chaine html stockant le calendrier au format html
  • */
  • private $html;
  • /**
  • * public function __set
  • *
  • * desc: Setter
  • *
  • * param:string $type
  • * param string $strVal
  • */
  • public function __set($type,$strVal)
  • {
  • if (in_array($type,$this->settable))
  • {
  • $this->style[$type]=$strVal;
  • }
  • else throw new Exception (MSG_WRONG_STYLE_TYPE);
  • }
  • /**
  • * public function __construct
  • *
  • * desc: Initialisation et constrcution des objets parents
  • */
  • public function __construct($boxId=null,$month=null,$year=null,$period=1)
  • {
  • $this->domTextBoxId=$boxId;
  • $this->MonthByMonth=false;
  • $m=isset($month)?(int)$month:(int)date('m');
  • $y=isset($year)?(int)$year:(int)date('Y');
  • if ($m<10)
  • $m='0'.$m;
  • parent::__construct($m.'/'.$y,$period);
  • }
  • /**
  • * public function __get
  • *
  • * desc: Getter
  • *
  • * param: mixed $prop
  • * return: mixed value
  • */
  • public function __get($prop)
  • {
  • if (parent::__get($prop))
  • return parent::__get($prop);
  • if (array_key_exists($prop,$this->style))
  • return $this->style[$prop];
  • }
  • /**
  • * public function getHtml
  • *
  • * desc: Construit et récupére le tableau en chaine html
  • *
  • * return: string
  • */
  • public function getHtml()
  • {
  • $tdW=$this->tdWidth;
  • $tdH=$this->tdHeight;
  • $this->html='';
  • //Le calendrier va occuper 5 lignes(semaines) de 7 colonnes(jours) pour le mois au maximum
  • foreach ($this->monthIterator as $key=>$month)
  • {
  • $MONTH=date('M',mktime(0, 0, 0, $this->monthIterator->iM, 1, $this->monthIterator->iY));
  • if ($this->MonthByMonth!==false)
  • {
  • $prevm=$this->getPrevM('M');
  • $prevy=$this->getPrevM('Y');
  • $nextm=$this->getNextM('M');
  • $nexty=$this->getNextM('Y');
  • $chooseMonth='<a class="'.$this->cssLink.'" href="?month='.$prevm.'&year='.$prevy.'"> << </a> '.$MONTH.' '.$this->monthIterator->iY.' <a class="'.$this->cssLink.'" href="?month='.$nextm.'&year='.$nexty.'&box='.$this->domTextBoxId.'"> >> </a>';
  • }
  • else $chooseMonth=' '.$MONTH.' '.$this->monthIterator->iY.' ';
  • $this->html.= '
  • <table border="0" cellpadding="0" cellspacing="0" >
  • <!--DWLayoutTable-->
  • <tr>
  • <td width="'.$tdW.'" height="'.$tdH.'" valign="middle" align="center" class="'.$this->cssHeader.'">D</td>
  • <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">L</td>
  • <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">M</td>
  • <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">M</td>
  • <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">J</td>
  • <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">V</td>
  • <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">S</td>
  • </tr>';
  • for ($j=0;$j<6;$j++)
  • {
  • $this->html.='<tr>';
  • for ($k=0;$k<7;$k++)
  • {
  • $class=$this->cssWeekday;
  • $title ='click to select in the box';
  • if ($k==0)
  • $class=$this->cssSunday;
  • if ( ($month[$k][$j] == (int)date('d')) and ($this->monthIterator->iM== (int)date('m')) and ($this->monthIterator->iY == (int)date('Y')) )
  • {
  • $class = $this->cssToday;
  • }
  • if ($this->target=='opener')
  • $close='self.close();';
  • else $close='';
  • if ($this->monthIterator->iM < 10)
  • $mth='0'.$this->monthIterator->iM;
  • else $mth=$this->monthIterator->iM;
  • if($month[$k][$j]!='')
  • $this->html.= '<td width="'.$tdW.'" height="'.$tdH.'" valign="middle" align="center" class="'.$class.'"><a href = "#" class="'.$this->cssLink.'" onclick="'.$this->target.'.document.getElementById(\''.$this->domTextBoxId.'\').value=\''.$month[$k][$j].'/'.$mth.'/'.$this->monthIterator->iY.'\';'.$close.' " '.$title.'>'.(int)$month[$k][$j].'</td>';
  • else
  • $this->html.= '<td width="'.$tdW.'" height="'.$tdH.'" valign="middle" align="center" class="'.$class.'"><a href = "#" class="'.$this->cssLink.'" onclick="'.$this->target.'.document.getElementById(\''.$this->domTextBoxId.'\').value=\''.$month[$k][$j].'/'.$mth.'/'.$this->monthIterator->iY.'\';'.$close.' " '.$title.'>'.$month[$k][$j].'</td>';
  • }
  • $this->html.= '</tr>';
  • }
  • $this->html.= '</tr></table><table border="0" cellpadding="0" cellspacing="0" ><tr><td class="'.$this->cssFooter.'">'.$chooseMonth.'</td></tr></table>';
  • }
  • return $this->html;
  • }
  • }
  • ?>
  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html xmlns="http://www.w3.org/1999/xhtml">
  • <head>
  • <title>calendrier</title>
  • <style>
  • td.formtdcalendar {
  • font-family: verdana;
  • font-size: 11px;
  • font-style:normal;
  • font-weight:100;
  • color: #330099;
  • background-color: #FFFFFF;
  • }
  • td.cal_dimanche {
  • font-family: verdana;
  • font-size: 11px;
  • text-transform: lowercase;
  • color: red;
  • text-decoration: none;
  • text-align: center;
  • vertical-align: middle;
  • background: yellow;
  • }
  • td.formtdtoday {
  • font-family:Geneva, Arial, Helvetica, sans-serif;
  • font-size: 11px;
  • font-style:normal;
  • font-weight:bold;
  • text-align:center;
  • color: #FF0000;
  • background-color: #A60001;
  • background-image:url(./ricerca_box3.gif);
  • }
  • td.footer {
  • font-family: verdana;
  • font-size: 11px;
  • text-transform: lowercase;
  • color: #330099;
  • text-decoration: none;
  • text-align: center;
  • vertical-align: middle;
  • background: #FFFFFF;
  • }
  • td.date_today {
  • font-family: verdana;
  • font-size: 11px;
  • text-transform: lowercase;
  • color: #FFFFFF;
  • text-decoration: none;
  • text-align: center;
  • vertical-align: middle;
  • background: #FF0000;
  • }
  • td.headertd {
  • font-family: Arial;
  • font-size: 11px;
  • font-weight: bold;
  • color: #F5F5F5;
  • background-color: #003366;
  • font-style: italic;
  • text-decoration: blink;
  • border-top-width: 1px;
  • border-right-width: 1px;
  • border-bottom-width: 1px;
  • border-left-width: 1px;
  • border-top-style: solid;
  • border-right-style: solid;
  • border-bottom-style: solid;
  • border-left-style: solid;
  • border-color: #363636 #F5F5F5 #F5F5F5 #363636 ;
  • background-image:url(header.gif);
  • }
  • a.fld:hover {
  • text-decoration:none;
  • color:#633363;
  • font-style: italic;
  • }
  • a.fld:link {
  • text-decoration:none;
  • color:#633363;
  • font-style: italic;
  • }
  • a.fld:visited {
  • text-decoration:none;
  • color:#633363;
  • font-style: italic;
  • }
  • a.fld:active {
  • text-decoration:none;
  • color:#633363;
  • font-style: italic;
  • }
  • </style>
  • </head>
  • <body>
  • <input type="text" id="box" value=""/>
  • <?php
  • $calendar = isset($_GET['month'])?new htmlCalendar('box',$_GET['month'],$_GET['year']):new htmlCalendar('box');
  • $calendar->cssHeader='headertd';
  • $calendar->cssWeekday='formtdcalendar';
  • $calendar->cssSunday='cal_dimanche';
  • $calendar->cssToday='date_today';
  • $calendar->cssLink='fld';
  • $calendar->cssFooter='footer';
  • $calendar->tdWidth='20';
  • $calendar->target='window';
  • echo $calendar->getHtml();
  • ?>
  • </body>
  • </html>
<?php

define ('MSG_WRONG_STYLE_TYPE','Style non paramètrable');

/**
* class myIterator  implements Iterator,Countable,Seekableiterator,ArrayAccess
*/
class myIterator  implements Iterator,Countable,Seekableiterator,ArrayAccess
{
  /**
  * property protected array $oItems:
  *
  * desc: tableau d'éléments .
  */
  protected $oItems=null;
  
  /**
  * property protected int $ipos
  *
  * desc: indice itérative de Position relative
  */
  protected $iPos=0;
  
  /**
  * property protected int $iMin
  *
  * desc: indice minimale de bornage
  */
  protected $iMin=0;
  
  /**
  * property protected int $iLimit
  *
  * desc: nombre limite d'occurences bornées
  */
  protected $iLimit;
  
  /**
  * property int  $iOffset
  *
  * desc: Indice itérative de Position absolue
  */
  protected $iOffset=0;
  
  /**
  * public function constructor
  *  
  * param int $iMin
  * param int $iLimit
  */  
  public function __construct($iMin,$iLimit)
  {
    $this->oItems=array();
    $this->iLimit=$iLimit;
    $this->iMin=$iMin;
  }
  
  /**
  * public function add
  *
  * desc: empile le tableau passé en paramètres sur la pile existante statique
  *
  * param int $iMin
  * param int $iLimit
  */  
  public function add($array)
  {
    $this->oItems[]=$array;
  }
  
  /**
  * public function setLimit
  *
  * desc: Définit un bornage $it => indice de départ , $l => longueur de la plage en occurences
  * 
  * param int $it 
  * param int $l 
  */  
  public function setLimit($it,$l)
  {
    if (is_int($it)&&is_int($l))
    {
      if ($l>0)
      {
        $this->iMin=$it;
        $this->iLimit=$l;
        $this->iOffset=$it;
      }
    }
  }
  
  /**
  * public function current 
  * 
  * desc: Methode itérative interface iterator 
  */
  public function current()
  {
    if ($this->valid())
      return current($this->oItems);
  }
  
  /**
  * public function valid 
  * 
  * desc: Methode itérative interface iterator 
  */
  public function valid()
  {
    return ( $this->iOffset >= $this->iMin && $this->iPos < ($this->iLimit) ) ;
    
  }
  
  /**
  * public function next 
  * 
  * desc: Methode itérative interface iterator 
  */
  public function next()
  {
    $this->iPos++;
    $this->iOffset++;      
    return next($this->oItems);
    
  }
  
  /**
  * public function key 
  * 
  * desc: Methode itérative interface iterator 
  */
  public function key()
  {
    if ($this->valid())
      return key($this->oItems);
  }
  
  /**
  * public function rewind 
  * 
  * desc: Methode itérative interface iterator 
  */
  public function rewind()
  {
    reset ($this->oItems);
    $position = 0;
    while($position < $this->iMin) 
    {
      next($this->oItems);
      $position++;
    }
    $this->iPos=0;
    $this->iOffset=$this->iMin;
    if (!$this->valid()) 
      throw new Exception('Invalid seek position');
    
  }
  
  /**
  * public function count 
  * 
  * desc: Methode de comptage interface countable 
  */
  public function count()
  {
    return count($this->oItems);
  }
  
  /**
  * public function seek 
  * 
  * desc: Methode itérative interface seekableIterator 
  */
  public function seek($index)
  {
    $this->rewind();
    $position = $this->iMin;
    while($position < $index && $this->valid()) 
    {
      $this->next();
      $position++;
    }
    if (!$this->valid()) 
    {
      throw new Exception('Invalid seek position');
    }
  }
  
  /**
  * public function OffsetExists 
  *
  * desc: Methode d' acces aux tableaux, interface arrayAccess 
  *
  * param $iOffset int 
  */
  public function OffsetExists($iOffset)
  {
    if ($iOffset < $this->iLimit && $iOffset >= $this->iMin)
      return true;
    else return false;
  }
  
  /**
  * public function OffsetGet 
  * 
  * desc: Methode itérative interface arrayAccess 
  */
  public function OffsetGet($iOffset)
  {
    if ($this->OffsetExists($iOffset))
      return $this->oItems[$iOffset];
  }
  
  /**
  * public function OffsetSet 
  * desc: Methode itérative interface arrayAccess 
  */
  public function OffsetSet($iOffset,$val)
  {
    
  }
  
  public function OffsetUnset($iOffset)
  {
    
  }
  
}


/**
* class monthIterator extends myIterator
*/
class monthIterator extends myIterator
{
  /**
  * property int $startmonth 
  * indice minimale du bornage au mois 
  */
  private $startmonth;
  
  /**
  * property int $startyear 
  * indice minimale du bornage à l'année 
  */
  private $startyear;
  
  /**
  * property int $iM 
  * indice de position du mois 
  */
  private $iM;
  
  /**
  * property int $iM 
  * indice de position de l'année 
  */
  private $iY;
 
  /**
  * public function __construct
  * Constructeur.
  * 
  * @param string $start au format mm/yyyy  
  * @param int $period en mois
  */
  public function __construct($start,$period=100)
  {
    if (substr($start,2,1)!=='/')
      throw new Exception('Paramètre Invalide pour constructeur format mm/yyyy');
    
    $expl=explode('/',$start);
    
    if( ((int)$expl[0]) <= 12 && ((int)$expl[0]) > 0 )
      $this->startmonth=(int)$expl[0];
    else throw new Exception('Format du mois invalide') ;
    
    if ( ((int)$expl[1]) >= 1000 && ((int)$expl[1]) < 9999 )
      $this->startyear=(int)$expl[1];
    else throw new Exception('Format année invalide');
    
    parent::__construct(0,$period);
  }
  
  /**
  * public function 
  * 
  * desc: Getter
  * param: mixed $prop 
  * return object
  */
  public function __get ($prop)
  {
    if (isset($this->$prop))
    return $this->$prop;
  }
  
  /**
  * public Function nextM 
  *
  * desc: positionne les indices iM et iY (mois et année) de l'iterateur.   
  */
  public function NextM()
  {
    if( $this->iM < 12)
    {
      $this->iM++;
    }
    else
    {
      $this->iM = 1;
      $this->iY++;
    }
  }
  
  /**
  * public Function next 
  *
  * desc: methode iterative.   
  */
  public function next()
  {
    parent::next();
    $this->NextM();
  }
  
  /**
  * public Function rewind 
  *
  * desc: methode iterative.   
  */
  public function rewind()
  {
    parent::rewind();
    $this->iM = $this->startmonth;
    $this->iY = $this->startyear;
  }
}

/**
* Class calendar
*/
class calendar 
{
  /**
  * property $monthIterator
  * object monthIterator
  */
  private $monthIterator;
  
  /**
  * function Constructor
  * 
  * param string $start format 'mm/yyyy'
  * param int $period  
  *
  */
  public function __construct ($start,$period)
  {
    $this->monthIterator=new monthIterator($start,$period);
    $i=$this->monthIterator->iMin;
    $this->monthIterator->rewind();
    while ( $i < $this->monthIterator->iLimit)
    { 
      $this->monthIterator->add(self::createmonth($this->monthIterator->iM,$this->monthIterator->iY));
      $this->monthIterator->NextM();
      $i++;
    }
  }
  
  /**
  * Function 
  * desc: Getter
  * param: mixed $prop
  * return: property object 
  */
  public function __get($prop)
  {
    if (isset($this->$prop))
      return $this->$prop;
    else return false;
  }
  
  /**
  * function getCurrentMonth
  * 
  * desc:renvoit le mois et l'année en cours
  * 
  * return string format ('mm/yyyy') 
  */
  public function getCurrentMonth()
  {
    return (($this->monthIterator->iM<10)?'0'.$this->monthIterator->iM.'/'.$this->monthIterator->iY:$this->monthIterator->iM.'/'.$this->monthIterator->iY);
  }
  
  public function getNextM($mode)
  {
    switch ($mode)
    { 
      case 'M':
        return(date('m',mktime(0, 0, 0, $this->monthIterator->iM+1, 1, $this->monthIterator->iY)));
        break;
      case 'Y':
        return(date('Y',mktime(0, 0, 0, $this->monthIterator->iM+1, 1, $this->monthIterator->iY)));
        break;
    }
  }
  
  public function getPrevM($mode)
  {
    switch ($mode)
    { 
      case 'M':
        return (date('m',mktime(0, 0, 0, $this->monthIterator->iM-1, 1, $this->monthIterator->iY)));
        break;
      case 'Y':
        return(date('Y',mktime(0, 0, 0, $this->monthIterator->iM-1, 1, $this->monthIterator->iY)));
        break;
    }
  }
  /**
  * Static function
  * 
  * desc: Methode de fabrication de calendrier de type tableau au mois
  * 
  * param int $month
  * param int $year
  * return array
  */
  public static function createmonth($month,$year)
  {
    // jour de la semaine du premier du mois
    $fom = date ("w",mktime(0, 0, 0, $month, 1, $year));
    $day=1;
    $start=false;
    //combien de jours dans le mois?
    $max=date('t',mktime(0, 0, 0, $month, 1, $year));
    //formatage en chaine
    if ($month < 10)
      $month = '0'.$month;
    //init
    $d=0 ;
    //Le calendrier va occuper 6 lignes(semaines) de 7 colonnes(jours) pour le mois au maximum
    for ( $i=0 ; $i < 6 ; $i++ ) 
    {
      for ( $j=0 ; $j < 7 ;$j++ ) 
      {
        //début du comptage au 1er du mois pour la premiere semaine
        if ($i==0) 
        {
          if ($fom == $d)
          {
            $start=true;
            $day=1;
          }
        }
        if ($day < $max+1) 
        {
          if ($start ===true)
          {
            if ($day<10)
              $cal[$j][]='0'.$day;
            else 
              $cal[$j][]=$day;
            $day++;
          }
          else
            $cal[$j][]='';
        }
        else 
          $cal[$j][]='';
        $d++;
      }
    }
    return $cal;    
  }
}

/**
* class htmlCalendar extends calendar
*/
class htmlCalendar extends calendar
{
  /**
  * private property string domTextBoxId
  * 
  * desc : l'id de la text box
  */
  private $domTextBoxId;
  
  /**
  * proprerty array settable
  * 
  * desc : les propriétés modifiables
  */
  private $settable=array('cssHeader',
                          'cssSunday',
                          'cssWeekday',
                          'cssToday',
                          'cssLink',
                          'cssFooter',
                          'MonthByMonth',
                          'tdWidth',
                          'tdHeight',
                          'target');
  /**
  * proprerty array style
  * 
  * desc: le tableau des propriétés de style 
  */
  private $style=array();
  
  /**
  * proprerty string html
  * 
  * desc: la chaine html stockant le calendrier au format html 
  */
  private $html;
  
  /**
  * public function __set 
  * 
  * desc: Setter
  * 
  * param:string $type
  * param string $strVal
  */
  public function __set($type,$strVal)
  {
    if (in_array($type,$this->settable))
    {
      $this->style[$type]=$strVal;
    }
    else throw new Exception (MSG_WRONG_STYLE_TYPE);
  }
  
  /**
  * public function __construct
  * 
  * desc: Initialisation et constrcution des objets parents
  */
  public function __construct($boxId=null,$month=null,$year=null,$period=1)
  {
    $this->domTextBoxId=$boxId;
    $this->MonthByMonth=false;
    $m=isset($month)?(int)$month:(int)date('m');
    $y=isset($year)?(int)$year:(int)date('Y');
    if ($m<10)
      $m='0'.$m;
    parent::__construct($m.'/'.$y,$period);
   }
  
  /**
  * public function __get 
  * 
  * desc: Getter
  * 
  * param: mixed $prop
  * return: mixed value 
  */
  public function __get($prop)
  {
    if (parent::__get($prop))
      return parent::__get($prop);
    if (array_key_exists($prop,$this->style))
      return $this->style[$prop];
  }
  
  /**
  * public function getHtml 
  * 
  * desc: Construit et récupére le tableau en chaine html
  * 
  * return: string  
  */
  public function getHtml()
  {
    $tdW=$this->tdWidth;
    $tdH=$this->tdHeight;
    $this->html='';
    
    
    //Le calendrier va occuper 5 lignes(semaines) de 7 colonnes(jours) pour le mois au maximum
    foreach ($this->monthIterator as $key=>$month)
    { 
      $MONTH=date('M',mktime(0, 0, 0, $this->monthIterator->iM, 1, $this->monthIterator->iY));
      if ($this->MonthByMonth!==false)
      {
        $prevm=$this->getPrevM('M');
        $prevy=$this->getPrevM('Y');
        $nextm=$this->getNextM('M');
        $nexty=$this->getNextM('Y');
        $chooseMonth='<a class="'.$this->cssLink.'" href="?month='.$prevm.'&year='.$prevy.'">  <<  </a> '.$MONTH.'  '.$this->monthIterator->iY.' <a class="'.$this->cssLink.'" href="?month='.$nextm.'&year='.$nexty.'&box='.$this->domTextBoxId.'">  >>  </a>';
      }
      else $chooseMonth=' '.$MONTH.'  '.$this->monthIterator->iY.' ';
      $this->html.= '
      <table border="0" cellpadding="0" cellspacing="0" >
      <!--DWLayoutTable-->
      <tr>
        <td width="'.$tdW.'" height="'.$tdH.'" valign="middle" align="center" class="'.$this->cssHeader.'">D</td>
        <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">L</td>
        <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">M</td>
        <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">M</td>
        <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">J</td>
        <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">V</td>
        <td width="'.$tdW.'" valign="middle" align="center" class="'.$this->cssHeader.'">S</td>
      </tr>';
        for ($j=0;$j<6;$j++)
        {
          $this->html.='<tr>';
          for ($k=0;$k<7;$k++)  
          {
            $class=$this->cssWeekday;
            $title ='click to select in the box';
            if ($k==0)
              $class=$this->cssSunday;
      
            if ( ($month[$k][$j] == (int)date('d')) and ($this->monthIterator->iM== (int)date('m')) and ($this->monthIterator->iY == (int)date('Y')) )
            { 
              $class = $this->cssToday;
            }
            
            if ($this->target=='opener')
              $close='self.close();';
            else $close='';
            
            if ($this->monthIterator->iM < 10)
              $mth='0'.$this->monthIterator->iM;
            else $mth=$this->monthIterator->iM;
              
            if($month[$k][$j]!='')
              $this->html.= '<td width="'.$tdW.'" height="'.$tdH.'" valign="middle" align="center" class="'.$class.'"><a href = "#" class="'.$this->cssLink.'" onclick="'.$this->target.'.document.getElementById(\''.$this->domTextBoxId.'\').value=\''.$month[$k][$j].'/'.$mth.'/'.$this->monthIterator->iY.'\';'.$close.' " '.$title.'>'.(int)$month[$k][$j].'</td>';
            else 
              $this->html.= '<td width="'.$tdW.'" height="'.$tdH.'" valign="middle" align="center" class="'.$class.'"><a href = "#" class="'.$this->cssLink.'" onclick="'.$this->target.'.document.getElementById(\''.$this->domTextBoxId.'\').value=\''.$month[$k][$j].'/'.$mth.'/'.$this->monthIterator->iY.'\';'.$close.' " '.$title.'>'.$month[$k][$j].'</td>';
          }
          $this->html.= '</tr>';
        }
        $this->html.= '</tr></table><table border="0" cellpadding="0" cellspacing="0" ><tr><td class="'.$this->cssFooter.'">'.$chooseMonth.'</td></tr></table>';
      }
      return $this->html;
   }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/    TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>calendrier</title>
<style>
td.formtdcalendar {
  
  font-family: verdana;
  font-size: 11px;
  font-style:normal;
  font-weight:100;
  color: #330099;
  background-color: #FFFFFF;
}

td.cal_dimanche { 

    font-family: verdana; 
    font-size: 11px; 
    text-transform: lowercase; 
    color: red; 
    text-decoration: none; 
    text-align: center; 
    vertical-align: middle; 
    background: yellow; 
} 
td.formtdtoday {
  font-family:Geneva, Arial, Helvetica, sans-serif;
  font-size: 11px;
  font-style:normal;
  font-weight:bold;
  text-align:center;
  color: #FF0000;
  background-color: #A60001;
  background-image:url(./ricerca_box3.gif);
}
td.footer {
  font-family: verdana; 
    font-size: 11px; 
    text-transform: lowercase; 
    color: #330099; 
    text-decoration: none; 
    text-align: center; 
    vertical-align: middle; 
    background: #FFFFFF; 
}
td.date_today { 
    font-family: verdana; 
    font-size: 11px; 
    text-transform: lowercase; 
    color: #FFFFFF; 
    text-decoration: none; 
    text-align: center; 
    vertical-align: middle; 
    background: #FF0000; 
} 
td.headertd {
  font-family: Arial;
  font-size: 11px;
  font-weight: bold;
  color: #F5F5F5;
  background-color: #003366;
  font-style: italic;
  text-decoration: blink;
  border-top-width: 1px;
  border-right-width: 1px;
  border-bottom-width: 1px;
  border-left-width: 1px;
  border-top-style: solid;
  border-right-style: solid;
  border-bottom-style: solid;
  border-left-style: solid;
  border-color:  #363636 #F5F5F5 #F5F5F5 #363636 ;
  background-image:url(header.gif);
}
a.fld:hover {
  text-decoration:none;
  color:#633363;
  font-style: italic;
}
a.fld:link {
  text-decoration:none;
  color:#633363;
  font-style: italic;
}
a.fld:visited {
  text-decoration:none;
  color:#633363;
  font-style: italic;
}
a.fld:active {
  text-decoration:none;
  color:#633363;
  font-style: italic;
} 
</style>
</head>
<body>
<input type="text" id="box" value=""/>
<?php
$calendar = isset($_GET['month'])?new htmlCalendar('box',$_GET['month'],$_GET['year']):new htmlCalendar('box');
$calendar->cssHeader='headertd';
$calendar->cssWeekday='formtdcalendar';
$calendar->cssSunday='cal_dimanche';
$calendar->cssToday='date_today';
$calendar->cssLink='fld';
$calendar->cssFooter='footer';
$calendar->tdWidth='20';
$calendar->target='window';
echo $calendar->getHtml();
?>
</body>
</html>

Conclusion

Ajout de classe itérateurs et arrayaccess.
Refonte de la classe principale ajout de classe spécialisée Html.  
Gestion des plages de périodes.
Plus plein d'autres possibilités.
  
 

Fichier Zip

Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

Historique

16 mai 2007 21:57:13 :
oubli
16 mai 2007 23:06:41 :
exemple ajouté + zip
27 mai 2007 17:23:49 :
Ajout de classe itérateurs et arrayaccess. Refonte de la classe principale ajout de classe spécialisée Html. Gestion des plages de périodes. Plus plein d'autres possibilités.
27 mai 2007 18:04:21 :
exemple modifié
27 mai 2007 20:32:27 :
MAJ des exemples.
28 mai 2007 23:21:43 :
Bugs HTML corrigés Ajout des jours fériés.
30 mai 2007 21:46:50 :
manquait paques
31 mai 2007 23:32:17 :
arrangement
25 juillet 2007 20:13:29 :
Ajout screenshot et modification categorie =>date/heure

Commentaires et avis

signaler à un administrateur
Commentaire de coucou747 le 17/05/2007 02:21:23

tu te limites au html c'est domage, t'aurais pu faire une classe tableau, une tableauhtml abstraite, et passer un objet tableau en parametre pour ta fonction qui fait le calendrier...

sinon c'est pas mal,

signaler à un administrateur
Commentaire de malalam le 26/05/2007 11:15:42 administrateur CS

C'est effectivement pas mal.
Chuis sûr qu'on peut monter une classe calendrier très sympa avec des itérateurs, moi... :-)
Et effectivement, faudrait pousser la personnalisation, ça peut être agréable.

signaler à un administrateur
Commentaire de malalam le 26/05/2007 11:16:59 administrateur CS

Par contre, ça manque d'options : si je ne veux pas que les week-end soient sélectionnables? Ou les jours fériés ? Si je veux commencer à telle date et terminer à telle date ? Si je veux un intervalle de temps (d'aujourd'hui sur 1 an ?) etc ?

signaler à un administrateur
Commentaire de guill76 le 26/05/2007 14:34:42

Merci, pour vos remarques à tous les 2.
Je vais réfléchir dans ce sens pour une future MAJ.
    

signaler à un administrateur
Commentaire de el_novitch le 30/12/2007 15:48:51

J'aime bien le survol de Pentecôte... ;-)

signaler à un administrateur
Commentaire de benoitXV le 07/08/2008 15:14:22

Merci pour ce code.
J'essai de recuperer l'id de ma textbox depuis calendar.php dans la fonction gethtml()
Ceci afin de m