Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Table.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Generated Header (not documented yet)
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.Table.php,v 1.2 2003/08/18 15:46:42 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage CORE
15  */
16 /**
17  */
18 // ---------------------------------------------------------------------------
19 // PHP Table Class
20 // ---------------------------------------------------------------------------
21 // 1999 anakeen - Yannick Le Briquer
22 // ---------------------------------------------------------------------------
23 //
24 // $Id: Class.Table.php,v 1.2 2003/08/18 15:46:42 eric Exp $
25 //
26 // $Log: Class.Table.php,v $
27 // Revision 1.2 2003/08/18 15:46:42 eric
28 // phpdoc
29 //
30 // Revision 1.1 2002/01/08 12:41:34 eric
31 // first
32 //
33 // Revision 1.6 2001/10/17 09:08:49 eric
34 // mise en place de i18n via gettext
35 //
36 // Revision 1.5 2001/02/10 10:07:39 yannick
37 // Abandon de libphpinclude
38 //
39 // Revision 1.4 2001/02/10 09:45:51 yannick
40 // Ajout de vieilles classes
41 //
42 // Revision 1.17 2000/09/04 17:50:11 yannick
43 // Traitement des valeurs non initialisees
44 //
45 // Revision 1.16 2000/07/04 16:24:38 marianne
46 // suppression trace : reste bug...
47 //
48 // Revision 1.15 2000/06/05 16:14:25 yannick
49 // Trace dans Table
50 //
51 // Revision 1.14 2000/04/18 08:54:19 marianne
52 // Retour arriere
53 //
54 // Revision 1.12 2000/03/21 14:33:27 marianne
55 // Ajout head_class
56 //
57 // Revision 1.11 2000/03/21 14:22:53 marianne
58 // Correction bug sur class et aout index_class
59 //
60 // Revision 1.10 2000/02/15 14:47:47 marianne
61 // Maj affichage footer
62 //
63 // Revision 1.9 2000/02/15 09:29:49 marianne
64 // Suppression warnings
65 //
66 // Revision 1.8 2000/02/10 10:40:16 marianne
67 // Ajout Incidents
68 //
69 // Revision 1.7 2000/01/27 17:52:19 marianne
70 // Suppression warnings
71 //
72 // Revision 1.6 1999/12/06 16:33:19 marianne
73 // Correction Bug sur index_alpha quand initiale=''
74 //
75 // Revision 1.5 1999/12/03 14:36:13 marc
76 // Integration de l'index dans un tableau
77 //
78 // Revision 1.4 1999/12/02 17:45:37 marc
79 // Commentaires et indentation
80 //
81 // Revision 1.3 1999/12/02 17:42:32 marc
82 // Traitement de la barre de navigation inter-pages.
83 //
84 //
85 // ---------------------------------------------------------------------------
86 //
87 include_once ('Class.Log.php');
88 
90 
91 class Table
92 {
93  // ---------------------------------------------------------------------------
94  // Public var
95  //
96  var $array; // THE Array (2 dimensionnal)
97  var $arrayobj = "FALSE"; // if set to TRUE, Table can manage array of i
98  // Objects, it means that fields are considered as
99  // Object Attributes
100  // Fields attributes
101  var $fields; // array of the field names to show
102  var $filter = "/[A-Za-z0-9][A-Za-z0-9_]*/"; // Regexp: Field names to show
103  var $sortable_fields; // array of the fields you can sort using the
104  // sort_link
105  var $ordered_by; // the current ordering field
106  var $sort_link; // the URL used to perform a reordering of the table
107  // this URL is a format string with a %s where we
108  // should give the column name
109  var $fieldsattr; // array, foreach field gives the align attr
110  // Header attributes
111  var $heading = 0; // if set, create a <th> section
112  var $headcontent; // the content of the header
113  // if not set, the field string is used
114  var $headattr; // foreach element of the header gives the spanning
115  // and align attributes
116  var $headcolor; // Background color of header line, if not set,
117  // we use the first color of colortab
118  var $indexcolor; // Background color of index line
119  // Footer attributes
120  var $footing = 0; // Do we use a footer ?
121  var $footcontent; // content of the footer
122  // if not set, the field string is used
123  var $footattr; // foreach element of the footer gives the spanning
124  // and align attributes
125  var $footcolor; // background color of the footer, if not set,
126  // we use the color from colortab
127  // Hyperlinks
128  var $links; // array of links associated with fields, each link
129  // is a composed with a dynamic url + an array of
130  // value that should replace %s format tag
131  // in the url using an sprintff function
132  // General Layout
133  var $class; // the class (CSS) associated with <table, <tr, <td
134  var $index_class; // the class (CSS) associated with the index
135  var $head_class; // the class (CSS) associated with the header
136  var $width = "100%"; // table width
137  var $colortab; // table of alternate colors used for each row
138  var $border = 0; // border size
139  var $cellspacing = 0; // cellspacing size
140  var $defaultcolor = "#FFFFFF"; // too easy
141  var $page_bgcolor = "#FFFFFF"; // background color for nav fields
142  // Page Layout
143  var $start = 0; // the start index
144  var $slice = 20; // the slice size, zero means all
145  var $alpha_index = 0; // do you want an alphabetical index before the table
146  // it's a bitstream :
147  // 1 : UpperCase letter
148  // 2 : lowerCase letter
149  var $page_numbering = 0; // if true a page number is displayed
150  var $previous = N_("prev"); // the text (can be <img...) used to link to the
151  // previous page
152  var $next = N_("next"); // the text (can be <img...) used to link to the
153  // next page
154  var $first = N_("first"); // the text (can be <img...) used to link to the
155  // first page
156  var $last = N_("last"); // the text (can be <img...) used to link to the
157  // last page
158  var $page_link; // the URL used to turn pages. This URL is a format
159  // string with two %s in it the first on gives the
160  // index of the page start, the second gives the
161  // page size
162  // ---------------------------------------------------------------------------
163  // Private var
164  var $row = 0; // index of the current displayed row
165  var $out; // the output string
166  //
167  // ---------------------------------------------------------------------------
168  // Public methods
169  // ---------------------------------------------------------------------------
170  //
171  function show()
172  {
173  $this->construct_table();
174  print $this->out;
175  }
176 
177  function get()
178  {
179  $this->construct_table();
180  return ($this->out);
181  }
182  //
183  // ---------------------------------------------------------------------------
184  // Private methods
185  // ---------------------------------------------------------------------------
186  //
187  function construct_table()
188  {
189  $this->out = "";
190  // check the table
191  if (!is_array($this->array)) return;
192  if (($this->arrayobj == "FALSE") && !is_array($this->array[$this->start])) {
193  return;
194  }
195  if (($this->arrayobj == "TRUE") && !is_object($this->array[$this->start])) {
196  return;
197  }
198  // init the fields to display
199  $this->select_colnames();
200  // show the table
201  $this->show_alpha();
202 
203  $this->table_open();
204  $this->show_header();
205  $this->show_table();
206  $this->show_footer();
207  $this->table_close();
208 
209  $this->show_navbar();
210 
211  return;
212  }
213  // ----------------------------------------------
214  function show_alpha()
215  {
216 
217  if (!isset($this->alpha_index)) return;
218  if (!isset($this->ordered_by)) return;
219  $ind = 0;
220  $lettre = array();
221  reset($this->array);
222  while (list($k, $v) = each($this->array)) {
223  if ($this->arrayobj == "TRUE") {
224  $initiale = substr($v->$this->ordered_by, 0, 1);
225  } else {
226  $initiale = substr($v[$this->ordered_by], 0, 1);
227  }
228 
229  if ($initiale != '') {
230  if (!isset($lettre[$initiale])) {
231  $lettre[$initiale] = $this->slice * (int)($ind / $this->slice);
232  }
233  }
234  $ind++;
235  }
236 
237  $alpha_up = array(
238  "A",
239  "B",
240  "C",
241  "D",
242  "E",
243  "F",
244  "G",
245  "H",
246  "I",
247  "J",
248  "K",
249  "L",
250  "M",
251  "N",
252  "O",
253  "P",
254  "Q",
255  "R",
256  "S",
257  "T",
258  "U",
259  "V",
260  "W",
261  "X",
262  "Y",
263  "Z"
264  );
265  $alpha_low = array(
266  "a",
267  "b",
268  "c",
269  "d",
270  "e",
271  "f",
272  "g",
273  "h",
274  "i",
275  "j",
276  "k",
277  "l",
278  "m",
279  "n",
280  "o",
281  "p",
282  "q",
283  "r",
284  "s",
285  "t",
286  "u",
287  "v",
288  "w",
289  "x",
290  "y",
291  "z"
292  );
293 
294  if (($this->alpha_index & 1)) $alpha_list = $alpha_up;
295  if (($this->alpha_index & 2)) $alpha_list = $alpha_low;
296  $this->table_open();
297  $this->table_index_row_open();
298  $idx = "[";
299  $prev = 0;
300  while (list($k, $car) = each($alpha_list)) {
301  if ($prev) $idx = $idx . "|";
302  if (!isset($lettre[$car])) {
303  $idx = $idx . "&nbsp;$car&nbsp;";
304  $prev = 1;
305  } else {
306  $value[0] = $lettre[$car];
307  $value[1] = $this->slice;
308  $link = $this->create_link($this->page_link, $value, $car);
309  $idx = $idx . "&nbsp;$link&nbsp;";
310  $prev = 1;
311  }
312  }
313  $idx = $idx . "]";
314  #################
315  $this->table_cell($idx, 1, "center", "", isset($this->index_class) ? $this->index_class : '', "100%");
316  $this->table_row_close();
317  $this->table_row_open();
318  $this->table_cell("&nbsp;", 1, "", "", "", "100%");
319  $this->table_row_close();
320  $this->table_close();
321  }
322  // ----------------------------------------------
323  function show_header()
324  {
325 
326  if (!$this->heading) return;
327  $this->row = 0;
328 
329  $this->table_heading_row_open();
330 
331  reset($this->fields);
332  while (list($k, $v) = each($this->fields)) {
333  if (isset($this->headcontent)) {
334  if (isset($this->headcontent[$v])) {
335  $val = $this->headcontent[$v];
336  } else {
337  continue;
338  }
339  } else {
340  $val = $v;
341  }
342  /* link ? */
343  if (isset($this->sortable_fields[$v])) {
344  $value[0] = $v;
345  $val = $this->create_link($this->sort_link, $value, $val);
346  }
347 
348  $this->table_heading_cell($val, isset($this->headattr[$v]["span"]) ? $this->headattr[$v]["span"] : '', isset($this->headattr[$v]["class"]) ? $this->headattr[$v]["class"] : '', isset($this->headattr[$v]["align"]) ? $this->headattr[$v]["align"] : '');
349  }
350  $this->table_row_close();
351  }
352  // ----------------------------------------------
353  function show_table()
354  {
355  $ind = 0;
356  reset($this->array);
357  while (list($key, $val) = each($this->array)) {
358  if ($ind++ < $this->start) continue;
359  if (($this->slice > 0) && ($ind > ($this->start + $this->slice))) break;
360 
361 
362  if ((!is_array($val)) && (!is_object($val))) continue;
363 
364  $this->table_row_open();
365 
366  reset($this->fields);
367  while (list($k, $v) = each($this->fields)) {
368  if ($this->arrayobj == "TRUE") {
369  $curval = $val->$v;
370  ######## BUG ########### echo $val->Parents->nomp;
371 
372  } else {
373  if (isset($val[$v])) {
374  $curval = $val[$v];
375  } else {
376  $curval = "";
377  }
378  }
379  if (!isset($this->links[$v])) {
380  $this->table_cell($curval, 1, isset($this->fieldsattr[$v]["align"]) ? $this->fieldsattr[$v]["align"] : '', isset($this->fieldsattr[$v]["wrap"]) ? $this->fieldsattr[$v]["wrap"] : '', isset($this->fieldsattr[$v]["class"]) ? $this->fieldsattr[$v]["class"] : '', isset($this->fieldsattr[$v]["width"]) ? $this->fieldsattr[$v]["width"] : '');
381  } else {
382  reset($this->links[$v][1]);
383  while (list($kk, $var) = each($this->links[$v][1])) {
384  if ($this->arrayobj == "TRUE") {
385  $value[$kk] = $val->$var;
386  } else {
387  if (isset($val[$var])) {
388  $value[$kk] = $val[$var];
389  } else {
390  $value[$kk] = "";
391  }
392  }
393  }
394  $link = $this->create_link($this->links[$v][0], $value, $curval);
395  $this->table_cell($link, 1, isset($this->fieldsattr[$v]["align"]) ? $this->fieldsattr[$v]["align"] : '', isset($this->fieldsattr[$v]["wrap"]) ? $this->fieldsattr[$v]["wrap"] : '', isset($this->fieldsattr[$v]["class"]) ? $this->fieldsattr[$v]["class"] : '', isset($this->fieldsattr[$v]["width"]) ? $this->fieldsattr[$v]["width"] : '');
396  }
397  }
398  $this->table_row_close();
399  }
400  }
401  // ----------------------------------------------
402  function show_footer()
403  {
404  if ($this->footing) {
405  $this->table_row_open("", $this->footcolor);
406  reset($this->fields);
407  while (list($k, $v) = each($this->fields)) {
408  if (isset($this->footcontent)) {
409  if (isset($this->footcontent[$v])) {
410  $val = $this->footcontent[$v];
411  } else {
412  continue;
413  }
414  } else {
415  $val = $v;
416  }
417 
418  $this->table_cell($val, isset($this->footattr[$v]["span"]) ? $this->footattr[$v]["span"] : '', isset($this->footattr[$v]["align"]) ? $this->footattr[$v]["align"] : '', isset($this->footattr[$v]["wrap"]) ? $this->footattr[$v]["wrap"] : '', isset($this->footattr[$v]["class"]) ? $this->footattr[$v]["class"] : '', isset($this->footattr[$v]["width"]) ? $this->footattr[$v]["width"] : '');
419  }
420  $this->table_row_close();
421  }
422  return;
423  }
424  // ----------------------------------------------
425  function show_navbar()
426  {
427  // Next/Prev pages
428  if (!$this->slice || ($this->slice >= sizeof($this->array))) return;
429  $this->table_open();
430  $this->table_row_open("", $this->page_bgcolor);
431  $nbcol = 5;
432  $link_prev = "&nbsp;";
433  $link_next = "&nbsp;";
434  $link_first = "&nbsp;";
435  $link_last = "&nbsp;";
436  $this->table_cell($link_prev, 5, "", "", "", "");
437  $this->table_row_close();
438 
439  $page_tot = (((int)(sizeof($this->array) / $this->slice)) * $this->slice) == sizeof($this->array) ? (int)(sizeof($this->array) / $this->slice) : (int)(sizeof($this->array) / $this->slice) + 1;
440  $page_num = (int)($this->start / $this->slice) + 1;
441 
442  $this->table_row_open("", $this->page_bgcolor);
443 
444  $values_first[0] = 0;
445  $values_first[1] = $this->slice;
446  $values_last[0] = sizeof($this->array) - (sizeof($this->array) - (($page_tot - 1) * ($this->slice)));
447  $values_last[1] = $this->slice;
448  if ($this->start - $this->slice >= 0) {
449  $value[0] = $this->start - $this->slice;
450  $value[1] = $this->slice;
451  $link_first = $this->create_link($this->page_link, $values_first, $this->first);
452  $link_prev = $this->create_link($this->page_link, $value, $this->previous);
453  }
454  if ($this->start + $this->slice < sizeof($this->array)) {
455  $value[0] = $this->start + $this->slice;
456  $value[1] = $this->slice;
457  $link_next = $this->create_link($this->page_link, $value, $this->next);
458  $link_last = $this->create_link($this->page_link, $values_last, $this->last);
459  }
460 
461  $this->table_cell($link_first, 1, "left", "", "", "10%");
462  $this->table_cell($link_prev, 1, "left", "", "", "20%");
463  $this->table_cell("$page_num/$page_tot", 1, "center", "", "", "20%");
464  $this->table_cell($link_next, 1, "right", "", "", "20%");
465  $this->table_cell($link_last, 1, "right", "", "", "10%");
466  $this->table_row_close();
467  $this->table_close();
468  }
469  // ----------------------------------------------
470  function select_colnames()
471  {
472  if (isset($this->fields) || ($this->arrayobj == "TRUE")) return;
473  reset($this->array);
474  list($key, $val) = each($this->array);
475  reset($val);
476  while (list($k, $v) = each($val)) {
477  if (preg_match($this->filter, $k)) $this->fields[] = $k;
478  }
479  }
480  // ----------------------------------------------
481  function table_open()
482  {
483  $this->out = $this->out . sprintf("<table%s %s cellpadding=\"0\" %s %s >\n", isset($this->class) ? " class=$this->class" : "", isset($this->border) ? " border=$this->border" : "", isset($this->cellspacing) ? " cellspacing=$this->cellspacing" : "", isset($this->width) ? " width=$this->width" : "");
484  }
485  // ----------------------------------------------
486  function table_close()
487  {
488  $this->out = $this->out . "</table>\n";
489  }
490  // ----------------------------------------------
491  function table_row_open($nbcol = "", $color = "")
492  {
493  if (!$color && (isset($this->colortab))) {
494  $color = $this->colortab[$this->row % sizeof($this->colortab) ];
495  }
496  $this->out = $this->out . sprintf(" <tr%s%s%s>\n", $color ? " bgcolor=$color" : "", isset($this->class) ? " class=$this->class" : "", $nbcol ? " cols=$nbcol" : "");
497  }
498  // ----------------------------------------------
499  function table_row_close()
500  {
501  $this->out = $this->out . " </tr>\n";
502  $this->row++;
503  }
504  // ----------------------------------------------
506  {
507  $this->out = $this->out . sprintf(" <tr%s%s>\n", isset($this->index_class) ? " class=\"$this->index_class\"" : "", isset($this->indexcolor) ? " bgcolor=$this->indexcolor" : "");
508  }
509  // ----------------------------------------------
511  {
512  $this->out = $this->out . sprintf(" <tr%s%s>\n", isset($this->head_class) ? " class=\"$this->head_class\"" : "", isset($this->headcolor) ? " bgcolor=$this->headcolor" : "");
513  }
514  // ----------------------------------------------
515  function table_heading_cell($val, $colspan = 1, $class = "", $align = "left")
516  {
517  $w_class = "";
518  if (isset($class)) {
519  $w_class = " class=$class";
520  } else {
521  if (isset($this->class)) {
522  $w_class = " class=$this->class";
523  }
524  }
525  $this->out = $this->out . sprintf(" <th%s%s%s><p>%s</p></th>\n", $w_class, $colspan ? " colspan=$colspan" : "", $align ? " align=$align" : "", $val);
526  }
527  // ----------------------------------------------
528  function table_cell($val, $colspan = 1, $align = "left", $wrap = "", $class = "", $width = "")
529  {
530  $w_class = "";
531  if ($class != "") {
532  $w_class = " class=$class";
533  } else {
534  if (isset($this->class)) {
535  $w_class = " class=$this->class";
536  }
537  }
538  $this->out = $this->out . sprintf(" <td%s%s%s%s%s><p>%s&nbsp;</p></td>\n", $w_class, $colspan ? " colspan=$colspan" : "", $align ? " align=$align" : "", $wrap ? " $wrap" : "", $width ? " width=$width" : "", $val);
539  }
540  ////////////////////////////////////////////////////////////////
541  // create_link : should be usefull for other classes
542  // this function is here because we don't know where we should put it
543  // so !!
544  //
545  function create_link($template, $values, $text)
546  {
547  $link = "\$link = sprintf (\"\n<a href=\\\"" . $template . "\\\">\"";
548  reset($values);
549  while (list($key, $val) = each($values)) {
550  $link = $link . ",\"" . $val . "\"";
551  }
552  $link = $link . ");";
553  eval($link);
554  $link = $link . $text . "</a>";
555  return ($link);
556  }
557 }
558 ?>
← centre documentaire © anakeen - published under CC License - Dynacase