Core  3.2
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  * @package FDL
5 */
6 /**
7  * Generated Header (not documented yet)
8  *
9  * @author Anakeen
10  * @version $Id: Class.TableLayout.php,v 1.2 2003/08/18 15:46:42 eric Exp $
11  * @package FDL
12  * @subpackage CORE
13  */
14 /**
15  */
16 // ---------------------------------------------------------------------------
17 // $Id: Class.TableLayout.php,v 1.2 2003/08/18 15:46:42 eric Exp $
18 // $Source: /home/cvsroot/anakeen/freedom/core/Class/Layout/Class.TableLayout.php,v $
19 // ---------------------------------------------------------------
20 // $Log: Class.TableLayout.php,v $
21 //
22 //
23 // ---------------------------------------------------------------------------
24 //
25 include_once ('Class.Log.php');
26 
28 {
29  // ---------------------------------------------------------------------------
30  // Public var
31  //
33  var $array; // THE Array (2 dimensionnal) or
34  // array of objects
35  // Fields attributes
36  var $fields; // array of the field names to show
37  var $order_by; // the current ordering field
38  var $desc = ""; // the ordering =up or down
39  var $sort_link; // the URL used to perform a reordering of the table
40  // this URL is a format string with a %s where we
41  // should give the column name
42  // Header attributes
43  var $headcontent; // the content of the header
44  // if not set, the field string is used
45  var $headsortfields; // column with sorting capabilities
46  // Footer attributes
47  var $footcontent; // content of the footer
48  // if not set, the field string is used
49  // Hyperlinks
50  var $links; // array of links associated with fields, each link
51  // is a composed with a dynamic url + an array of
52  // value that should replace %s format tag
53  // in the url using an sprintff function
54  // Paging attributes
55  var $start = 0; // the start index
56  var $slice = 20; // the slice size, zero means all
57  var $page_numbering = 0; // if true a page number is displayed
58  var $prev = "prev"; // the text (can be <img...) used to link to the
59  // previous page
60  var $next = "next"; // the text (can be <img...) used to link to the
61  // next page
62  var $first = "first"; // the text (can be <img...) used to link to the
63  // first page
64  var $last = "last"; // the text (can be <img...) used to link to the
65  // last page
66  var $page_link; // the URL used to turn pages. This URL is a format
67  // string with two %s in it the first on gives the
68  // index of the page start, the second gives the
69  // page size (number of elements in the page
70  var $nb_tot = 0; // Total number of elements
71  // ---------------------------------------------------------------------------
72  // Private var
73  var $row = 0; // index of the current displayed row
74  var $out; // the output string
79  //
80  // ---------------------------------------------------------------------------
81  // Public methods
82  // ---------------------------------------------------------------------------
83  //
84  function __construct(&$lay, $table_name = 'TABLE')
85  {
86  $this->table_name = $table_name;
87  $this->log = new Log("", "TableLayout", "");
88  $this->lay = & $lay;
89  }
90  //
91  // ---------------------------------------------------------------------------
92  // Private methods
93  // ---------------------------------------------------------------------------
94  //
95  function Set()
96  {
97  if ($this->start == "") $this->start = 0;
98  // check the table
99  if (!is_array($this->array)) {
100  return;
101  }
102  // init the fields to display
103  $this->SelectColnames();
104  // show the table
105  $this->GenPaging();
106  $this->GenHeader();
107  $this->GenTable();
108  $this->GenFooter();
109  }
110 
111  function GenHeader()
112  {
113 
114  if (!isset($this->headcontent)) return;
115  reset($this->headcontent);
116  foreach ($this->headcontent as $k => $v) {
117  /* link ? */
118  if (isset($this->headsortfields[$k])) {
119  $value[0] = $this->headsortfields[$k];
120  $value[1] = "down";
121  $value[2] = 0;
122  if ($this->order_by == $this->headsortfields[$k]) {
123  $value[2] = 0;
124  if ($this->desc == "down") {
125  $value[1] = "up";
126  }
127  }
128  $v = $this->create_link($this->sort_link, $value, $v);
129  }
130  $this->lay->set("$k", $v);
131  }
132  }
133  // ----------------------------------------------
134  function GenTable()
135  {
136  $ind = 0;
137  reset($this->array);
138  $tmparray = "";
139  foreach ($this->array as $key => $val) {
140  if ($ind > $this->slice) break;
141 
142 
143  if ((!is_array($val)) && (!is_object($val))) continue;
144 
145  reset($this->fields);
146  foreach ($this->fields as $k => $v) {
147  if (is_object($val)) {
148  $curval = $val->$v;
149  } else {
150  if (isset($val[$v])) {
151  $curval = $val[$v];
152  } else {
153  $curval = "";
154  }
155  }
156  if (!isset($this->links[$v])) {
157  $tmparray[$ind][$v] = $curval;
158  } else {
159  reset($this->links[$v][1]);
160  foreach ($this->links[$v][1] as $kk => $var) {
161  if (is_object($val)) {
162  $value[$kk] = $val->$var;
163  } else {
164  if (isset($val[$var])) {
165  $value[$kk] = $val[$var];
166  } else {
167  $value[$kk] = "";
168  }
169  }
170  }
171  $link = $this->create_link($this->links[$v][0], $value, $curval);
172  $tmparray[$ind][$v] = $link;
173  }
174  }
175  $ind++;
176  }
177  reset($this->fields);
178  foreach ($this->fields as $k => $v) {
179  $this->lay->SetBlockCorresp($this->table_name . "BODY", $v, $v);
180  }
181 
182  $this->lay->SetBlockData($this->table_name . "BODY", $tmparray);
183  }
184  // ----------------------------------------------
185  function GenFooter()
186  {
187  reset($this->fields);
188  foreach ($this->fields as $k => $v) {
189  if (isset($this->footcontent)) {
190  if (isset($this->footcontent[$v])) {
191  $val = $this->footcontent[$v];
192  } else {
193  continue;
194  }
195  } else {
196  $val = $v;
197  }
198 
199  $this->lay->set($v, $val);
200  }
201  return;
202  }
203  // ----------------------------------------------
204  function GenPaging()
205  {
206 
207  $link_first = "";
208  $link_last = "";
209  $link_next = "";
210  $link_prev = "";
211  $page_num = 1;
212  $page_tot = 1;
213  // Next/Prev pages
214  if ($this->slice && ($this->slice < $this->nb_tot) && isset($this->page_link)) {
215 
216  $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);
217  $page_num = (int)($this->start / $this->slice) + 1;
218 
219  $values_first[0] = 0;
220  $values_first[1] = $this->slice;
221  $values_last[0] = $this->nb_tot - ($this->nb_tot - (($page_tot - 1) * ($this->slice)));
222  $values_last[1] = $this->slice;
223  if ($this->start - $this->slice >= 0) {
224  $value[0] = $this->start - $this->slice;
225  $value[1] = $this->slice;
226  $link_first = $this->create_link($this->page_link, $values_first, $this->first);
227  $link_prev = $this->create_link($this->page_link, $value, $this->prev);
228  }
229  if ($this->start + $this->slice < $this->nb_tot) {
230  $value[0] = $this->start + $this->slice;
231  $value[1] = $this->slice;
232  $link_next = $this->create_link($this->page_link, $value, $this->next);
233  $link_last = $this->create_link($this->page_link, $values_last, $this->last);
234  }
235  }
236  $this->lay->set($this->table_name . "_PREV", $link_prev);
237  $this->lay->set($this->table_name . "_NEXT", $link_next);
238  $this->lay->set($this->table_name . "_FIRST", $link_first);
239  $this->lay->set($this->table_name . "_LAST", $link_last);
240  $this->lay->set($this->table_name . "_NUM", $page_num);
241  $this->lay->set($this->table_name . "_NB", $page_tot);
242  }
243  // ----------------------------------------------
244  // Used if fields are not provided
245  function SelectColnames()
246  {
247  if (isset($this->fields)) return;
248  reset($this->array);
249  list($key, $val) = each($this->array);
250  if (is_object($val)) $val = get_object_vars($val);
251  reset($val);
252  foreach ($val as $k => $v) {
253  $this->fields[] = $k;
254  }
255  }
256  ////////////////////////////////////////////////////////////////
257  // create_link : should be usefull for other classes
258  // this function is here because we don't know where we should put it
259  // so !!
260  //
261  function create_link($template, $values, $text)
262  {
263  $link = "<a href=\"" . $template . "\">";
264  for ($i = 0; $i < 9; $i++) {
265  if (!isset($values[$i])) $values[$i] = "";
266  }
267  $link = sprintf($link, $values[0], $values[1], $values[2], $values[3], $values[4], $values[5], $values[6], $values[7], $values[8]);
268  $link = $link . $text . "</a>";
269  return ($link);
270  }
271 }
272 ?>
$s slice
$s start
__construct(&$lay, $table_name= 'TABLE')
$query order_by
create_link($template, $values, $text)
$value
first($a)
← centre documentaire © anakeen