[ Index ] |
|
Code source de Dotclear 1.2.5 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of DotClear. 4 # Copyright (c) 2004 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # DotClear is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # DotClear is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with DotClear; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 # 23 # Contributor(s): 24 # Kevyn Lebouille 25 26 /** 27 Classe d'affichage d'éléments sur plusieurs pages 28 */ 29 30 class multipage 31 { 32 var $options; 33 var $data; 34 var $func_name; 35 var $var_page; 36 37 var $virtual; 38 var $nb_elements; 39 var $nb_pages; 40 var $nb_groups; 41 var $env; 42 var $index_start; 43 var $index_end; 44 var $env_group; 45 var $index_group_start; 46 var $index_group_end; 47 48 function multipage($env,$func_name=NULL,$data,$virtual=0,$nb_per_page='',$nb_pages_per_group='',$nb_cols='') 49 { 50 # Initialisation 51 $this->virtual = $virtual; 52 53 $this->_init(); 54 55 $this->_setData($data); 56 $this->_setFunction($func_name); 57 58 if($nb_per_page != '') { 59 $this->setOption('nb_per_page',$nb_per_page); 60 } 61 62 if($nb_pages_per_group != '') { 63 $this->setOption('nb_pages_per_group',$nb_pages_per_group); 64 } 65 66 if($nb_cols != "") { 67 $this->setOption('nb_cols',$nb_cols); 68 } 69 70 # Calcul des données 71 # 72 73 # Nombre d'éléments 74 if (!$this->virtual) { 75 $this->nb_elements = count($this->data); 76 } else { 77 $this->nb_elements = $this->virtual; 78 } 79 80 # Nombre de pages possibles 81 $this->nb_pages = ceil($this->nb_elements/$this->options['nb_per_page']); 82 83 84 # On vérifie que env ne sort pas du nombre de pages 85 if ($env <= $this->nb_pages && $env != "") { 86 $this->env = (integer)$env; 87 } else { 88 $this->env = 1; 89 } 90 91 # Nombre de groupes 92 $this->nb_groups = (integer) ceil($this->nb_pages/$this->options['nb_pages_per_group']); 93 94 # Index de début de page 95 $this->index_start = ($this->env-1)*$this->options['nb_per_page']; 96 97 # Index de fin de page 98 $this->index_end = $this->index_start+$this->options['nb_per_page']-1; 99 if($this->index_end >= $this->nb_elements) { 100 $this->index_end = $this->nb_elements-1; 101 } 102 103 # Index du groupe en cours 104 $this->env_group = (integer) ceil($this->env/$this->options['nb_pages_per_group']); 105 106 # Index de la première page du groupe 107 $this->index_group_start = ($this->env_group-1)*$this->options['nb_pages_per_group']+1; 108 109 # Index de la dernière page du groupe 110 $this->index_group_end = $this->index_group_start+$this->options['nb_pages_per_group']-1; 111 if($this->index_group_end > $this->nb_pages) { 112 $this->index_group_end = $this->nb_pages; 113 } 114 } 115 116 function setOption($name,$value) 117 { 118 if (!in_array($name,array_keys($this->options))) { 119 trigger_error('Multipage: '.sprintf(_rc_undefined_option,$name),E_USER_NOTICE); 120 return false; 121 } 122 123 $this->options[$name] = $value; 124 } 125 126 function getPage() 127 { 128 $res = ''; 129 $nb_elements = $this->index_end-$this->index_start+1; 130 $i = ($this->virtual==0) ? $this->index_start : 0; 131 $end = ($this->virtual==0) ? $this->index_end : $i+count($this->data)-1; 132 $function = $this->func_name; 133 134 if ($this->_emptyData()) 135 { 136 return $this->options['html_empty']; 137 } 138 else 139 { 140 while ($i<=$end) 141 { 142 $tmp_res = ''; 143 for ($j=0;$j<$this->options['nb_cols'];$j++) 144 { 145 if ($i<=$end) { 146 $tmp_res .= sprintf($this->options['html_cell'],$function($this->data[$i],$i)); 147 } elseif ($this->options['draw_empty_cells']) { 148 $tmp_res .= sprintf($this->options['html_cell'],$this->options['html_empty_cell']); 149 } 150 $i++; 151 } 152 153 $res .= sprintf($this->options['html_row'],$tmp_res); 154 } 155 return sprintf($this->options['html_block'],$res); 156 } 157 } 158 159 function getLinks() 160 { 161 # Création des liens 162 $htmlLinks = ''; 163 $htmlPrev = ''; 164 $htmlNext = ''; 165 $htmlPrevGrp = ''; 166 $htmlNextGrp = ''; 167 168 for($i=$this->index_group_start; $i<=$this->index_group_end; $i++) 169 { 170 if($i == $this->env) { 171 $htmlLinks .= sprintf($this->options['html_cur_page'],$i); 172 } else { 173 $htmlLinks .= '<a href="'.$this->_setURL($i).'">'.$i.'</a>'; 174 } 175 176 if($i != $this->index_group_end) { 177 $htmlLinks .= $this->options['html_links_sep']; 178 } 179 } 180 181 # Page précédente 182 if($this->env != 1) { 183 $htmlPrev = '<a href="'.$this->_setURL($this->env-1).'">'. 184 $htmlPrev .= $this->options['html_prev'].'</a> '; 185 } 186 187 # Page suivante 188 if($this->env != $this->nb_pages) { 189 $htmlNext = ' <a href="'.$this->_setURL($this->env+1).'">'; 190 $htmlNext .= $this->options['html_next'].'</a>'; 191 } 192 193 //Groupe précédent 194 if($this->env_group != 1) { 195 $htmlPrevGrp = ' <a href="'.$this->_setURL($this->index_group_start - $this->options['nb_pages_per_group']).'">'; 196 $htmlPrevGrp .= $this->options['html_prev_grp'].'</a> '; 197 } 198 199 if($this->env_group != $this->nb_groups) { 200 $htmlNextGrp = ' <a href="'.$this->_setURL($this->index_group_end+1).'">'; 201 $htmlNextGrp .= $this->options['html_next_grp'].'</a> '; 202 } 203 204 $res = $htmlPrev. 205 $htmlPrevGrp. 206 $htmlLinks. 207 $htmlNextGrp. 208 $htmlNext; 209 210 211 if (count($this->data)>0) { 212 return sprintf($this->options['html_links'],$res); 213 } 214 } 215 216 function _setURL($pageNum) 217 { 218 $strLink = $_SERVER['REQUEST_URI']; 219 220 # Suppression de l'information de session 221 if(ereg(session_name().'='.session_id().'([&]){1}',$strLink)) { 222 $strLink = ereg_replace(session_name()."=".session_id().'([&]){1}','',$strLink); 223 } else { 224 $strLink = ereg_replace('([?&]){1}'.session_name().'='.session_id(),'',$strLink); 225 } 226 227 if(ereg('([?&]){1}'.$this->var_page.'=([0-9])+',$strLink)) { 228 $strLink = ereg_replace('([?&]){1}'.$this->var_page.'=([0-9])+', '\\1'.$this->var_page.'='.$pageNum, $strLink); 229 } else { 230 if(ereg('\?',$strLink)) { 231 $strLink = $strLink.'&'.$this->var_page.'='.$pageNum; 232 } else { 233 $strLink = $strLink.'?'.$this->var_page.'='.$pageNum; 234 } 235 } 236 return htmlspecialchars($strLink); 237 } 238 239 function _setData($data) 240 { 241 $this->data = $data; 242 } 243 244 function _emptyData() 245 { 246 return (count($this->data) > 0) ? false : true; 247 } 248 249 function _setFunction($str) 250 { 251 if (!function_exists($str)) { 252 trigger_error('Multipage: '.sprintf(_rc_undefined_function,$str),E_USER_ERROR); 253 return false; 254 } 255 $this->func_name = $str; 256 } 257 258 function _setVarPage($str) 259 { 260 $this->var_page = $str; 261 } 262 263 function _init() 264 { 265 $this->_setData(array()); 266 $this->_setVarPage('env'); 267 268 $this->options = array( 269 # Comptage 270 'nb_per_page' => 15, 271 'nb_pages_per_group'=> 10, 272 'nb_cols' => 1, 273 'draw_empty_cells' => true, 274 275 # Formatage HTML 276 'html_block' => '<table>%s</table>', 277 'html_row' => '<tr>%s</tr>'."\n", 278 'html_cell' => '<td>%s</td>'."\n", 279 'html_empty_cell' => ' ', 280 281 'html_links' => '<p>Page(s) : %s</p>', 282 'html_links_sep' => '-', 283 'html_cur_page' => '<strong>%s</strong>', 284 'html_prev' => '«prev.', 285 'html_next' => 'next»', 286 'html_prev_grp' => '...', 287 'html_next_grp' => '...', 288 289 'html_empty' => '<p><strong>No result</strong></p>' 290 ); 291 } 292 293 function _debug() 294 { 295 return '<pre>'. 296 "Nombre d'éléments par page ........... ".$this->options['nb_per_page']."\n". 297 'Nombre de pages par groupe ........... '.$this->options['nb_pages_per_group']."\n". 298 'Nombre de colonnes ....................'.$this->options['nb_cols']."\n". 299 "Nombre d'éléments .................... ".$this->nb_elements."\n". 300 'Nombre de pages ...................... '.$this->nb_pages."\n". 301 'Nombre de groupes .................... '.$this->nb_groups."\n\n". 302 'Page en cour ..........................'.$this->env."\n". 303 'Index de départ ...................... '.$this->index_start."\n". 304 'Index de fin ......................... '.$this->index_end."\n". 305 'Groupe en cours ...................... '.$this->env_group."\n". 306 'Index de la première page du groupe .. '.$this->index_group_start."\n". 307 'Index de la dernière page du groupe .. '.$this->index_group_end."\n". 308 '</pre>'; 309 } 310 } 311 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 21:40:15 2007 | par Balluche grâce à PHPXref 0.7 |