Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.TableLayout.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.TableLayout.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 // $Id: Class.TableLayout.php,v 1.2 2003/08/18 15:46:42 eric Exp $
20 // $Source: /home/cvsroot/anakeen/freedom/core/Class/Layout/Class.TableLayout.php,v $
21 // ---------------------------------------------------------------
22 // $Log: Class.TableLayout.php,v $
23 // Revision 1.2 2003/08/18 15:46:42 eric
24 // phpdoc
25 //
26 // Revision 1.1 2002/01/08 12:41:34 eric
27 // first
28 //
29 // Revision 1.13 2001/02/26 15:10:32 yannick
30 // Optimization
31 //
32 // Revision 1.12 2001/02/09 13:53:26 yannick
33 // Release 0.3.0
34 //
35 // Revision 1.11 2001/02/07 16:41:22 yannick
36 // Gestion des header et tris
37 //
38 // Revision 1.10 2001/02/07 13:16:17 yannick
39 // Retour à la page 0 sur tri
40 //
41 // Revision 1.9 2001/02/07 11:30:44 yannick
42 // Traitement résultat vide, 1 seule page
43 //
44 // Revision 1.8 2001/02/07 09:22:17 yannick
45 // Nombre de pages arrondi
46 //
47 // Revision 1.7 2001/02/06 16:23:28 yannick
48 // QueryGen : first release
49 //
50 // Revision 1.6 2001/01/15 11:29:25 marianne
51 // Parametrage du nom de la table
52 //
53 // Revision 1.5 2000/10/26 08:10:50 yannick
54 // Nettoyage
55 //
56 // Revision 1.4 2000/10/24 17:44:55 yannick
57 // Ajout du download
58 //
59 // Revision 1.3 2000/10/19 16:49:20 marc
60 // TableLayout utilise un lay fournit
61 //
62 // Revision 1.2 2000/10/19 10:58:54 yannick
63 // Gestion des utilisateurs
64 //
65 // Revision 1.1 2000/10/13 14:21:41 yannick
66 // Création
67 //
68 //
69 //
70 // ---------------------------------------------------------------------------
71 //
72 include_once ('Class.Log.php');
73 
75 {
76  // ---------------------------------------------------------------------------
77  // Public var
78  //
80  var $array; // THE Array (2 dimensionnal) or
81  // array of objects
82  // Fields attributes
83  var $fields; // array of the field names to show
84  var $order_by; // the current ordering field
85  var $desc = ""; // the ordering =up or down
86  var $sort_link; // the URL used to perform a reordering of the table
87  // this URL is a format string with a %s where we
88  // should give the column name
89  // Header attributes
90  var $headcontent; // the content of the header
91  // if not set, the field string is used
92  var $headsortfields; // column with sorting capabilities
93  // Footer attributes
94  var $footcontent; // content of the footer
95  // if not set, the field string is used
96  // Hyperlinks
97  var $links; // array of links associated with fields, each link
98  // is a composed with a dynamic url + an array of
99  // value that should replace %s format tag
100  // in the url using an sprintff function
101  // Paging attributes
102  var $start = 0; // the start index
103  var $slice = 20; // the slice size, zero means all
104  var $page_numbering = 0; // if true a page number is displayed
105  var $prev = "prev"; // the text (can be <img...) used to link to the
106  // previous page
107  var $next = "next"; // the text (can be <img...) used to link to the
108  // next page
109  var $first = "first"; // the text (can be <img...) used to link to the
110  // first page
111  var $last = "last"; // the text (can be <img...) used to link to the
112  // last page
113  var $page_link; // the URL used to turn pages. This URL is a format
114  // string with two %s in it the first on gives the
115  // index of the page start, the second gives the
116  // page size (number of elements in the page
117  var $nb_tot = 0; // Total number of elements
118 
119  // ---------------------------------------------------------------------------
120  // Private var
121  var $row = 0; // index of the current displayed row
122  var $out; // the output string
127  //
128  // ---------------------------------------------------------------------------
129  // Public methods
130  // ---------------------------------------------------------------------------
131  //
132  function TableLayout(&$lay, $table_name = 'TABLE')
133  {
134  $this->table_name = $table_name;
135  $this->log = new Log("", "TableLayout", "");
136  $this->lay = & $lay;
137  }
138  //
139  // ---------------------------------------------------------------------------
140  // Private methods
141  // ---------------------------------------------------------------------------
142  //
143  function Set()
144  {
145  if ($this->start == "") $this->start = 0;
146  // check the table
147  if (!is_array($this->array)) {
148  return;
149  }
150  // init the fields to display
151  $this->SelectColnames();
152  // show the table
153  $this->GenPaging();
154  $this->GenHeader();
155  $this->GenTable();
156  $this->GenFooter();
157  }
158 
159  function GenHeader()
160  {
161 
162  if (!isset($this->headcontent)) return;
163  reset($this->headcontent);
164  while (list($k, $v) = each($this->headcontent)) {
165  /* link ? */
166  if (isset($this->headsortfields[$k])) {
167  $value[0] = $this->headsortfields[$k];
168  $value[1] = "down";
169  $value[2] = 0;
170  if ($this->order_by == $this->headsortfields[$k]) {
171  $value[2] = 0;
172  if ($this->desc == "down") {
173  $value[1] = "up";
174  }
175  }
176  $v = $this->create_link($this->sort_link, $value, $v);
177  }
178  $this->lay->set("$k", $v);
179  }
180  }
181  // ----------------------------------------------
182  function GenTable()
183  {
184  $ind = 0;
185  reset($this->array);
186  $tmparray = "";
187  while (list($key, $val) = each($this->array)) {
188  if ($ind > $this->slice) break;
189 
190 
191  if ((!is_array($val)) && (!is_object($val))) continue;
192 
193  reset($this->fields);
194  while (list($k, $v) = each($this->fields)) {
195  if (is_object($val)) {
196  $curval = $val->$v;
197  } else {
198  if (isset($val[$v])) {
199  $curval = $val[$v];
200  } else {
201  $curval = "";
202  }
203  }
204  if (!isset($this->links[$v])) {
205  $tmparray[$ind][$v] = $curval;
206  } else {
207  reset($this->links[$v][1]);
208  while (list($kk, $var) = each($this->links[$v][1])) {
209  if (is_object($val)) {
210  $value[$kk] = $val->$var;
211  } else {
212  if (isset($val[$var])) {
213  $value[$kk] = $val[$var];
214  } else {
215  $value[$kk] = "";
216  }
217  }
218  }
219  $link = $this->create_link($this->links[$v][0], $value, $curval);
220  $tmparray[$ind][$v] = $link;
221  }
222  }
223  $ind++;
224  }
225  reset($this->fields);
226  while (list($k, $v) = each($this->fields)) {
227  $this->lay->SetBlockCorresp($this->table_name . "BODY", $v, $v);
228  }
229 
230  $this->lay->SetBlockData($this->table_name . "BODY", $tmparray);
231  }
232  // ----------------------------------------------
233  function GenFooter()
234  {
235  reset($this->fields);
236  while (list($k, $v) = each($this->fields)) {
237  if (isset($this->footcontent)) {
238  if (isset($this->footcontent[$v])) {
239  $val = $this->footcontent[$v];
240  } else {
241  continue;
242  }
243  } else {
244  $val = $v;
245  }
246 
247  $this->lay->set($v, $val);
248  }
249  return;
250  }
251  // ----------------------------------------------
252  function GenPaging()
253  {
254 
255  $link_first = "";
256  $link_last = "";
257  $link_next = "";
258  $link_prev = "";
259  $page_num = 1;
260  $page_tot = 1;
261  // Next/Prev pages
262  if ($this->slice && ($this->slice < $this->nb_tot) && isset($this->page_link)) {
263 
264  $page_tot = (ceil(($this->nb_tot / $this->slice) * $this->slice) == $this->nb_tot) ? ceil($this->nb_tot / $this->slice) : ceil($this->nb_tot / $this->slice + 1);
265  $page_num = (int)($this->start / $this->slice) + 1;
266 
267  $values_first[0] = 0;
268  $values_first[1] = $this->slice;
269  $values_last[0] = $this->nb_tot - ($this->nb_tot - (($page_tot - 1) * ($this->slice)));
270  $values_last[1] = $this->slice;
271  if ($this->start - $this->slice >= 0) {
272  $value[0] = $this->start - $this->slice;
273  $value[1] = $this->slice;
274  $link_first = $this->create_link($this->page_link, $values_first, $this->first);
275  $link_prev = $this->create_link($this->page_link, $value, $this->prev);
276  }
277  if ($this->start + $this->slice < $this->nb_tot) {
278  $value[0] = $this->start + $this->slice;
279  $value[1] = $this->slice;
280  $link_next = $this->create_link($this->page_link, $value, $this->next);
281  $link_last = $this->create_link($this->page_link, $values_last, $this->last);
282  }
283  }
284  $this->lay->set($this->table_name . "_PREV", $link_prev);
285  $this->lay->set($this->table_name . "_NEXT", $link_next);
286  $this->lay->set($this->table_name . "_FIRST", $link_first);
287  $this->lay->set($this->table_name . "_LAST", $link_last);
288  $this->lay->set($this->table_name . "_NUM", $page_num);
289  $this->lay->set($this->table_name . "_NB", $page_tot);
290  }
291  // ----------------------------------------------
292  // Used if fields are not provided
293  function SelectColnames()
294  {
295  if (isset($this->fields)) return;
296  reset($this->array);
297  list($key, $val) = each($this->array);
298  if (is_object($val)) $val = get_object_vars($val);
299  reset($val);
300  while (list($k, $v) = each($val)) {
301  $this->fields[] = $k;
302  }
303  }
304  ////////////////////////////////////////////////////////////////
305  // create_link : should be usefull for other classes
306  // this function is here because we don't know where we should put it
307  // so !!
308  //
309  function create_link($template, $values, $text)
310  {
311  $link = "<a href=\"" . $template . "\">";
312  for ($i = 0; $i < 9; $i++) {
313  if (!isset($values[$i])) $values[$i] = "";
314  }
315  $link = sprintf($link, $values[0], $values[1], $values[2], $values[3], $values[4], $values[5], $values[6], $values[7], $values[8]);
316  $link = $link . $text . "</a>";
317  return ($link);
318  }
319 }
320 ?>
← centre documentaire © anakeen - published under CC License - Dynacase