class.multipage.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. Classe d'affichage d'éléments sur plusieurs pages
  27. */
  28. class multipage
  29. {
  30. var $options;
  31. var $data;
  32. var $func_name;
  33. var $var_page;
  34. var $virtual;
  35. var $nb_elements;
  36. var $nb_pages;
  37. var $nb_groups;
  38. var $env;
  39. var $index_start;
  40. var $index_end;
  41. var $env_group;
  42. var $index_group_start;
  43. var $index_group_end;
  44. function multipage($env,$func_name=NULL,$data,$virtual=0,$nb_per_page='',$nb_pages_per_group='',$nb_cols='')
  45. {
  46. # Initialisation
  47. $this->virtual = $virtual;
  48. $this->_init();
  49. $this->_setData($data);
  50. $this->_setFunction($func_name);
  51. if($nb_per_page != '') {
  52. $this->setOption('nb_per_page',$nb_per_page);
  53. }
  54. if($nb_pages_per_group != '') {
  55. $this->setOption('nb_pages_per_group',$nb_pages_per_group);
  56. }
  57. if($nb_cols != "") {
  58. $this->setOption('nb_cols',$nb_cols);
  59. }
  60. # Calcul des données
  61. #
  62. # Nombre d'éléments
  63. if (!$this->virtual) {
  64. $this->nb_elements = count($this->data);
  65. } else {
  66. $this->nb_elements = $this->virtual;
  67. }
  68. # Nombre de pages possibles
  69. $this->nb_pages = ceil($this->nb_elements/$this->options['nb_per_page']);
  70. # On vérifie que env ne sort pas du nombre de pages
  71. if ($env <= $this->nb_pages && $env != "") {
  72. $this->env = $env;
  73. } else {
  74. $this->env = 1;
  75. }
  76. # Nombre de groupes
  77. $this->nb_groups = (integer) ceil($this->nb_pages/$this->options['nb_pages_per_group']);
  78. # Index de début de page
  79. $this->index_start = ($this->env-1)*$this->options['nb_per_page'];
  80. # Index de fin de page
  81. $this->index_end = $this->index_start+$this->options['nb_per_page']-1;
  82. if($this->index_end >= $this->nb_elements) {
  83. $this->index_end = $this->nb_elements-1;
  84. }
  85. # Index du groupe en cours
  86. $this->env_group = (integer) ceil($this->env/$this->options['nb_pages_per_group']);
  87. # Index de la première page du groupe
  88. $this->index_group_start = ($this->env_group-1)*$this->options['nb_pages_per_group']+1;
  89. # Index de la dernière page du groupe
  90. $this->index_group_end = $this->index_group_start+$this->options['nb_pages_per_group']-1;
  91. if($this->index_group_end > $this->nb_pages) {
  92. $this->index_group_end = $this->nb_pages;
  93. }
  94. }
  95. function setOption($name,$value)
  96. {
  97. if (!in_array($name,array_keys($this->options))) {
  98. trigger_error('Multipage: '.sprintf(_rc_undefined_option,$name),E_USER_NOTICE);
  99. return false;
  100. }
  101. $this->options[$name] = $value;
  102. }
  103. function getPage()
  104. {
  105. $res = '';
  106. $nb_elements = $this->index_end-$this->index_start+1;
  107. $i = ($this->virtual==0) ? $this->index_start : 0;
  108. $end = ($this->virtual==0) ? $this->index_end : $i+count($this->data)-1;
  109. $function = $this->func_name;
  110. if ($this->_emptyData())
  111. {
  112. return $this->options['html_empty'];
  113. }
  114. else
  115. {
  116. while ($i<=$end)
  117. {
  118. $tmp_res = '';
  119. for ($j=0;$j<$this->options['nb_cols'];$j++)
  120. {
  121. if ($i<=$end) {
  122. $tmp_res .= sprintf($this->options['html_cell'],$function($this->data[$i],$i));
  123. } elseif ($this->options['draw_empty_cells']) {
  124. $tmp_res .= sprintf($this->options['html_cell'],$this->options['html_empty_cell']);
  125. }
  126. $i++;
  127. }
  128. $res .= sprintf($this->options['html_row'],$tmp_res);
  129. }
  130. return sprintf($this->options['html_block'],$res);
  131. }
  132. }
  133. function getLinks()
  134. {
  135. # Création des liens
  136. $htmlLinks = '';
  137. $htmlPrev = '';
  138. $htmlNext = '';
  139. $htmlPrevGrp = '';
  140. $htmlNextGrp = '';
  141. for($i=$this->index_group_start; $i<=$this->index_group_end; $i++)
  142. {
  143. if($i == $this->env) {
  144. $htmlLinks .= sprintf($this->options['html_cur_page'],$i);
  145. } else {
  146. $htmlLinks .= '<a href="'.$this->_setURL($i).'">'.$i.'</a>';
  147. }
  148. if($i != $this->index_group_end) {
  149. $htmlLinks .= $this->options['html_links_sep'];
  150. }
  151. }
  152. # Page précédente
  153. if($this->env != 1) {
  154. $htmlPrev = '<a href="'.$this->_setURL($this->env-1).'">'.
  155. $htmlPrev .= $this->options['html_prev'].'</a>&nbsp;';
  156. }
  157. # Page suivante
  158. if($this->env != $this->nb_pages) {
  159. $htmlNext = '&nbsp;<a href="'.$this->_setURL($this->env+1).'">';
  160. $htmlNext .= $this->options['html_next'].'</a>';
  161. }
  162. //Groupe précédent
  163. if($this->env_group != 1) {
  164. $htmlPrevGrp = '&nbsp;<a href="'.$this->_setURL($this->index_group_start - $this->options['nb_pages_per_group']).'">';
  165. $htmlPrevGrp .= $this->options['html_prev_grp'].'</a>&nbsp;';
  166. }
  167. if($this->env_group != $this->nb_groups) {
  168. $htmlNextGrp = '&nbsp;<a href="'.$this->_setURL($this->index_group_end+1).'">';
  169. $htmlNextGrp .= $this->options['html_next_grp'].'</a>&nbsp;';
  170. }
  171. $res = $htmlPrev.
  172. $htmlPrevGrp.
  173. $htmlLinks.
  174. $htmlNextGrp.
  175. $htmlNext;
  176. if (count($this->data)>0) {
  177. return sprintf($this->options['html_links'],$res);
  178. }
  179. }
  180. function _setURL($pageNum)
  181. {
  182. $strLink = $_SERVER['REQUEST_URI'];
  183. # Suppression de l'information de session
  184. if(ereg(session_name().'='.session_id().'([&]){1}',$strLink)) {
  185. $strLink = ereg_replace(session_name()."=".session_id().'([&]){1}','',$strLink);
  186. } else {
  187. $strLink = ereg_replace('([?&]){1}'.session_name().'='.session_id(),'',$strLink);
  188. }
  189. if(ereg('([?&]){1}'.$this->var_page.'=([0-9])+',$strLink)) {
  190. $strLink = ereg_replace('([?&]){1}'.$this->var_page.'=([0-9])+', '\\1'.$this->var_page.'='.$pageNum, $strLink);
  191. } else {
  192. if(ereg('\?',$strLink)) {
  193. $strLink = $strLink.'&'.$this->var_page.'='.$pageNum;
  194. } else {
  195. $strLink = $strLink.'?'.$this->var_page.'='.$pageNum;
  196. }
  197. }
  198. return str_replace('&','&amp;',$strLink);
  199. }
  200. function _setData($data)
  201. {
  202. $this->data = $data;
  203. }
  204. function _emptyData()
  205. {
  206. return (count($this->data) > 0) ? false : true;
  207. }
  208. function _setFunction($str)
  209. {
  210. if (!function_exists($str)) {
  211. trigger_error('Multipage: '.sprintf(_rc_undefined_function,$str),E_USER_ERROR);
  212. return false;
  213. }
  214. $this->func_name = $str;
  215. }
  216. function _setVarPage($str)
  217. {
  218. $this->var_page = $str;
  219. }
  220. function _init()
  221. {
  222. $this->_setData(array());
  223. $this->_setVarPage('env');
  224. $this->options = array(
  225. # Comptage
  226. 'nb_per_page' => 15,
  227. 'nb_pages_per_group'=> 10,
  228. 'nb_cols' => 1,
  229. 'draw_empty_cells' => true,
  230. # Formatage HTML
  231. 'html_block' => '<table>%s</table>',
  232. 'html_row' => '<tr>%s</tr>'."\n",
  233. 'html_cell' => '<td>%s</td>'."\n",
  234. 'html_empty_cell' => '&nbsp;',
  235. 'html_links' => '<p>Page(s)&nbsp;: %s</p>',
  236. 'html_links_sep' => '-',
  237. 'html_cur_page' => '<strong>%s</strong>',
  238. 'html_prev' => '&#171;prev.',
  239. 'html_next' => 'next&#187;',
  240. 'html_prev_grp' => '...',
  241. 'html_next_grp' => '...',
  242. 'html_empty' => '<p><strong>No result</strong></p>'
  243. );
  244. }
  245. function _debug()
  246. {
  247. return '<pre>'.
  248. "Nombre d'éléments par page ........... ".$this->options['nb_per_page']."\n".
  249. 'Nombre de pages par groupe ........... '.$this->options['nb_pages_per_group']."\n".
  250. 'Nombre de colonnes ....................'.$this->options['nb_cols']."\n".
  251. "Nombre d'éléments .................... ".$this->nb_elements."\n".
  252. 'Nombre de pages ...................... '.$this->nb_pages."\n".
  253. 'Nombre de groupes .................... '.$this->nb_groups."\n\n".
  254. 'Page en cour ..........................'.$this->env."\n".
  255. 'Index de départ ...................... '.$this->index_start."\n".
  256. 'Index de fin ......................... '.$this->index_end."\n".
  257. 'Groupe en cours ...................... '.$this->env_group."\n".
  258. 'Index de la première page du groupe .. '.$this->index_group_start."\n".
  259. 'Index de la dernière page du groupe .. '.$this->index_group_end."\n".
  260. '</pre>';
  261. }
  262. }
  263. ?>