00001 <?php
00002
00003 class Selecteur
00004 {
00005 var
00007 $titre,
00009 $nom,
00011 $rep,
00013 $type,
00015 $target ,
00017 $initVal ,
00019 $defVal ,
00021 $listeVal,
00023 $extension,
00025 $img,
00027 $imgExt ;
00028
00030 function Selecteur($titre='selecteur',$nom='selecteur',$rep='',$def='',$type='select',$extension='',$target='index.php',$img=false, $imgExt='ico')
00031 {
00032 $this->setTitre($titre);
00033 $this->setNom($nom);
00034 $this->setRep($rep);
00035 $this->setType($type);
00036 $this->setTarget($target);
00037 if($rep!='')
00038 {
00039 $this->setExtension($extension);
00040 $this->setListeVal($this->getListFromRep());
00041 $this->setInitVal($this->selectInitVal());
00042 }
00043 $this->setDefVal($def);
00044 $this->setImg($img);
00045 $this->setImgExt($imgExt);
00046 }
00047
00049 function toString()
00050 {
00051 $str= " Selecteur { ";
00052 $str.= " titre=\"".$this->getTitre()."\";";
00053 $str.= " nom=\"".$this->getNom()."\";";
00054 $str.= " rep=\"".$this->getRep()."\";";
00055 $str.= " type=\"".$this->getType()."\"; ";
00056 $str.= " target=\"".$this->getType()."\"; ";
00057 $str.= " listeVal=\"".$this->getListeVal()."\"; ";
00058 $str.= " initVal=\"".$this->getInitVal()."\"; ";
00059 $str.= " defVal=\"".$this->getDefVal()."\"; ";
00060 $str.= " extension=\"".$this->getExtension()."\"; ";
00061 $str.= " img=\"".$this->getImg()."\"; ";
00062 $str.= " imgExt=\"".$this->getImgExt()."\"; ";
00063 $str.= " }";
00064 return $str;
00065 }
00066
00067 function getTitre()
00068 {
00069 return $this->titre;
00070 }
00071
00072 function setTitre($titre)
00073 {
00074 $this->titre = $titre;
00075 }
00076
00077 function getNom()
00078 {
00079 return $this->nom;
00080 }
00081
00082 function setNom($nom)
00083 {
00084 $this->nom = $nom;
00085 }
00086
00087 function getRep()
00088 {
00089 return $this->rep;
00090 }
00091
00092 function setRep($rep)
00093 {
00094 $this->rep = $rep;
00095 }
00096
00097 function getType()
00098 {
00099 return $this->type;
00100 }
00101
00102 function setType($type)
00103 {
00104 $this->type = $type;
00105 }
00106
00107 function getTarget()
00108 {
00109 return $this->target;
00110 }
00111
00112 function setTarget($target)
00113 {
00114 $this->target = $target;
00115 }
00116
00117 function getInitVal()
00118 {
00119 return $this->initVal;
00120 }
00121
00122 function setInitVal($initiale)
00123 {
00124 $this->initVal = $initiale;
00125 }
00126
00127 function getDefVal()
00128 {
00129 return $this->defVal;
00130 }
00131
00132 function setDefVal($def)
00133 {
00134 $this->defVal = $def;
00135 }
00136
00137 function getListeVal()
00138 {
00139 return $this->listeVal;
00140 }
00141
00142 function setListeVal($liste)
00143 {
00144 $this->listeVal = $liste;
00145 }
00146
00147 function getExtension()
00148 {
00149 return $this->extension;
00150 }
00151
00152 function setExtension($extension)
00153 {
00154 $this->extension = $extension;
00155 }
00156
00157 function getImg()
00158 {
00159 return $this->img;
00160 }
00161
00162 function setImg($img)
00163 {
00164 $this->img = $img;
00165 }
00166
00167 function getImgExt()
00168 {
00169 return $this->imgExt;
00170 }
00171
00172 function setImgExt($imgExt)
00173 {
00174 $this->imgExt = $imgExt;
00175 }
00176
00183 function selectInitVal()
00184 {
00185 $val = "";
00186
00187 if(isset($_GET[$this->getNom()]))
00188 {
00189 $val = $_GET[$this->getNom()];
00190 }
00191 else
00192 {
00193 if(isset($_COOKIE[$this->getNom()]))
00194 {
00195 $val = $_COOKIE[$this->getNom()];
00196 }
00197 else
00198 {
00199 if(isset($HTTP_SERVER_VARS))
00200 {
00201 $val = substr($HTTP_SERVER_VARS['HTTP_ACCEPT_LANGUAGE'],0,2);
00202 }
00203 }
00204 }
00205
00206 if(trim($val)=="")
00207 {
00208 $val = $this->getListeVal();
00209 $val = $val[0];
00210 }
00211
00212 $this->setCookize( $this->getNom() , $val ) ;
00213
00214 return $val;
00215 }
00216
00217
00224 function getListFromRep()
00225 {
00226 $rep = "";
00227 $nomRep = $this->getRep();
00228 $contenu = "";
00229 $list = array();
00230
00231 if(is_dir($nomRep))
00232 {
00233 if ( $rep = opendir($nomRep) )
00234 {
00235 while ( false !== ($contenu = readdir($rep)) )
00236 {
00237 if( $contenu!='.' && $contenu!='..' )
00238 {
00239 if( $this->getExtension() == "" ||
00240 ( $this->getExtension() != "" && $this->goodExt( $contenu , $this->getExtension() ) ))
00241 {
00242 $fic = $this->ficSansExt($contenu);
00243 if( trim($fic) != "" )
00244 {
00245 array_push($list,$fic);
00246 }
00247 }
00248 }
00249 }
00250 closedir($rep);
00251 }
00252 }
00253 sort($list);
00254 return $list ;
00255 }
00256
00263 function toHtml($img="")
00264 {
00265 $liststr = "";
00266 $resultStr = "";
00267 $script = "onchange=\"document.getElementById('".$this->getNom()."Val').value = document.getElementById('".$this->getNom()."List').options[document.getElementById('".$this->getNom()."List').selectedIndex].value;
00268 document.getElementById('".$this->getNom()."Form').submit();\" " ;
00269
00270
00271 $contenu = $this->getListeVal();
00272 $liststr = "";
00273 switch($this->getType())
00274 {
00275
00276 case 'table':
00277 $nb = count($this->getListeVal());
00278 $liststr .= "<table id='".$this->getNom()."List' >\n ";
00279 $liststr .= "<tr><th colspan=".$nb.'>'.$this->getTitre(). "</th></tr>\n ";
00280 $liststr .= "<tr> \n";
00281 foreach ( $contenu as $i=> $ligne)
00282 {
00283 $imageStr = $this->getImage($ligne);
00284 $liststr .= "<td><a href='".$this->getTarget()."?".$this->getNom()."=".$ligne."'>$imageStr</a></td>\n";
00285 }
00286 $liststr .= "</tr>\n";
00287 $liststr .= "</table>\n";
00288 break;
00289
00290 case 'ul':
00291 $liststr .= "<ul id='".$this->getNom()."List' >\n";
00292 $liststr .= '<li>'.$this->getTitre() ."</li>\n";
00293 foreach ( $contenu as $i=> $ligne)
00294 {
00295 $imageStr = $this->getImage($ligne);
00296 $liststr .= "<li><a href='".$this->getTarget()."?".$this->getNom()."=".$ligne."'>$imageStr</a></li>\n";
00297 }
00298 $liststr .= "</ul>\n";
00299
00300 break;
00301
00302
00303 default:
00304 $liststr .= '<select id="'.$this->getNom().'List" '.$script." > \n";
00305 $liststr .= "<option value='".$this->getTitre()."'>".$this->getTitre()."</option>\n";
00306 foreach ( $contenu as $i=> $ligne)
00307 {
00308 $liststr .= "<option value='$ligne'>".$ligne."</option>\n";
00309 }
00310 $liststr .= "</select>\n";
00311
00312 }
00313
00314 $resultStr .= "<div class='Selecteur' >\n";
00315 $resultStr .= "<form id='".$this->getNom()."Form' action='". $this->getTarget() . "' method='get' >\n";
00316 $resultStr .= "<fieldset>\n";
00317 $resultStr .= $liststr;
00318 $resultStr .= "<input type='hidden' name='".$this->getNom()."' id='".$this->getNom()."Val' value='".$this->getInitVal()."' />\n";
00319 $resultStr .= "</fieldset> \n";
00320 $resultStr .= "</form> \n";
00321 $resultStr .= "</div> \n";
00322
00323 return $resultStr;
00324 }
00325
00326
00335 function setCookize( $name, $val )
00336 {
00337
00338 $expire = 365*24*3600;
00339
00340
00341 setcookie($name, $val, time() + $expire) ;
00342 }
00343
00351 function ficSansExt( $fileName )
00352 {
00353 $fic = "";
00354 $lenFicName = strlen($fileName);
00355 $posPoint = strpos($fileName,'.');
00356 return substr($fileName,0,$lenFicName - ( $lenFicName-$posPoint)) ;
00357 }
00358
00359
00368 function goodExt( $fileName , $ext )
00369 {
00370 $lenFicName = strlen($fileName);
00371 $posPoint = strpos($fileName,'.');
00372 return (substr($fileName,$posPoint+1, $lenFicName-$posPoint ) == $ext) ;
00373 }
00374
00375
00383 function getImage($nom)
00384 {
00385 $image = array( "<img src='",
00386 "' title='",
00387 "' caption='",
00388 "' />");
00389 if($this->getImg()==true)
00390 {
00391 $imageStr = $image[0].$this->getRep().'/img/'.$nom.'.'.$this->getImgExt().$image[1].$nom.$image[2].$nom.$image[3];
00392 }
00393 else
00394 {
00395 $imageStr = $nom;
00396 }
00397
00398 return $imageStr;
00399 }
00400
00401
00402
00403 }
00404 ?>