1 | <?php |
---|
2 | |
---|
3 | define('CAL_NB_MOIS', 5) ; |
---|
4 | |
---|
5 | require_once 'Mois.php' ; |
---|
6 | |
---|
7 | class CalendrierException extends Exception |
---|
8 | {} |
---|
9 | |
---|
10 | class Calendrier |
---|
11 | { |
---|
12 | protected $_id = 0 ; |
---|
13 | protected $_moisDebut = 0 ; |
---|
14 | protected $_anneeDebut = 0 ; |
---|
15 | protected $_nbMois = 0 ; |
---|
16 | protected $_mois = array() ; |
---|
17 | protected $_cbReserve = null ; |
---|
18 | protected $_cbFormat = null ; |
---|
19 | |
---|
20 | public function __construct($id, $mois=0, $annee=0, $nbMois=0) |
---|
21 | { |
---|
22 | $this -> _id = (int) $id ; |
---|
23 | $this -> _moisDebut = (int) $mois == 0 ? date('n') : $mois ; |
---|
24 | $this -> _anneeDebut = (int) $annee == 0 ? date('Y') : $annee ; |
---|
25 | $this -> _nbMois = (int) $nbMois == 0 ? CAL_NB_MOIS : $nbMois ; |
---|
26 | |
---|
27 | $this -> _init() ; |
---|
28 | } |
---|
29 | |
---|
30 | protected function _init() |
---|
31 | { |
---|
32 | if( $this -> _moisDebut > 0 && $this -> _moisDebut <= 12 && $this -> _anneeDebut > 0 ) |
---|
33 | { |
---|
34 | if( $this -> _nbMois > 0 ) |
---|
35 | { |
---|
36 | $annee = $this -> _anneeDebut ; |
---|
37 | $mois = $this -> _moisDebut ; |
---|
38 | for( $i = 0 ; $i < $this -> _nbMois ; $i++ ) |
---|
39 | { |
---|
40 | if( $mois > 12 ) { |
---|
41 | $annee++ ; $mois = 1 ; |
---|
42 | } |
---|
43 | $this -> _mois[] = new Mois($mois, $annee) ; |
---|
44 | $mois++ ; |
---|
45 | } |
---|
46 | } else throw new CalendrierException('Le nombre de mois à afficher est nul.') ; |
---|
47 | } else throw new CalendrierException('Le mois de départ n\'est pas correctement renseigné.') ; |
---|
48 | } |
---|
49 | |
---|
50 | public function setCallbackReserve($func) |
---|
51 | { |
---|
52 | $this -> _cbReserve = $func ; |
---|
53 | } |
---|
54 | |
---|
55 | public function setCallbackFormat($func) |
---|
56 | { |
---|
57 | $this -> _cbFormat = $func ; |
---|
58 | } |
---|
59 | |
---|
60 | public function __toString() |
---|
61 | { |
---|
62 | if( $this -> _cbReserve === null || $this -> _cbFormat === null ) throw new CalendrierException('Les callbacks nécessaires n\'ont pas été fournies.') ; |
---|
63 | |
---|
64 | $cbReserve = $this -> _cbReserve ; |
---|
65 | $cbFormat = $this -> _cbFormat ; |
---|
66 | $output = '' ; |
---|
67 | $output .= '<table id="calendrier-resa">' ; |
---|
68 | $output .= '<thead>' ; |
---|
69 | $output .= '<tr>' ; |
---|
70 | $output .= '<th class="empty"></th>' ; |
---|
71 | foreach( $this -> _mois as $mois ) { |
---|
72 | $output .= '<th>' . $mois . '</th>' ; |
---|
73 | } |
---|
74 | $output .= '</tr>' ; |
---|
75 | $output .= '</thead>' ; |
---|
76 | $output .= '<tbody>' ; |
---|
77 | $jourCourant = $i = Mois::$minJour ; |
---|
78 | for( ; $i < Mois::$maxJour ; $i++ ) |
---|
79 | { |
---|
80 | if( $jourCourant > 7 ) $jourCourant = 1 ; |
---|
81 | $output .= '<tr>' ; |
---|
82 | $output .= '<td class="jours">' . utf8_encode(Jour::$joursFr[$jourCourant]) . '</td>' ; |
---|
83 | for( $j = 0 ; $j < $this -> _nbMois ; $j++ ) |
---|
84 | { |
---|
85 | if( ($jour = $this -> _mois[$j] -> jour($i)) !== null ) |
---|
86 | $output .= '<td id="td_' . $jour -> getTS() . '" class="' . ($cbReserve($jour -> getTS()) ? 'reserve' : 'libre') . '">' . $cbFormat($this -> _id, $jour -> getTS()) . '</td>' ; |
---|
87 | else |
---|
88 | $output .= '<td class="empty"></td>' ; |
---|
89 | } |
---|
90 | $output .= '</tr>' ; |
---|
91 | $jourCourant++ ; |
---|
92 | } |
---|
93 | $output .= '</tbody>' ; |
---|
94 | $output .= '</table>' ; |
---|
95 | |
---|
96 | return $output ; |
---|
97 | } |
---|
98 | } |
---|