fix remaining glitches for php8
[plewww.git] / plekit / php / table.php
1 <?php
2
3   // $Id$
4
5 require_once 'prototype.php';
6
7 drupal_set_html_head('
8 <script type="text/javascript" src="/plekit/tablesort/tablesort.js"></script>
9 <script type="text/javascript" src="/plekit/tablesort/customsort.js"></script>
10 <script type="text/javascript" src="/plekit/tablesort/plc_customsort.js"></script>
11 <script type="text/javascript" src="/plekit/tablesort/paginate.js"></script>
12 <script type="text/javascript" src="/plekit/table/table.js"></script>
13 <link href="/plekit/table/table.css" rel="stylesheet" type="text/css" />
14 ');
15
16 ////////////////////////////////////////
17 // table_id: <table>'s id tag - WARNING : do not use '-' in table ids as it's used for generating javascript code
18 // headers: an associative array; the values can take several forms
19 //      simple/legacy form is "label"=>"type"
20 //      more advanced form is "label"=>options, it self a dict with the following known keys
21 //         (*) 'type': the type for sorting; this is passed to the javascript layer for custom sorting
22 //              default is to use 'text', custom sort functions can be specified with e.g. 'type'=>'sortAlphaNumericBottom'
23 //              a special case is for type to be 'date-format' like e.g. 'type'=>'date-dmy'
24 //              setting type to 'none' gives an non-sortable column
25 //         (*) 'title': if set, this is used in the "Sort on ``<title>''" bubble
26 // sort_column: the column to sort on at load-time - set to negative number for no onload- sorting
27 // options : an associative array to override options
28 //  - bullets1 : set to true if you want decorative bullets in column 1 (need white background)
29 //  - stripes : use diferent colors for odd and even rows
30 //  - caption : a caption for the table -- never used I'm afraid
31 //  - search_area : boolean (default true)
32 //  - pagesize_area : boolean (default true)
33 //  - notes_area : boolean (default true)
34 //  - search_width : size in chars of the search text dialog
35 //  - pagesize: the initial pagination size
36 //  - pagesize_def: the page size when one clicks the pagesize reset button
37 //  - max_pages: the max number of pages to display in the paginator
38 //  - notes : an array of additional notes
39 //  - debug: enables debug callbacks (prints out on console.log)
40
41 class PlekitTable {
42   // mandatory
43   var $table_id;
44   var $headers;
45   var $sort_column;
46   // options
47   var $bullets1;      // boolean - default false - display decorative bullets in column 1
48   var $stripes;       // boolean - default true - use different colors for odd and even rows
49   var $caption;       // string - never used so far
50   var $search_area;   // boolean (default true)
51   var $pagesize_area; // boolean (default true)
52   var $notes_area;    // boolean (default true)
53   var $search_width;  // size in chars of the search text dialog
54   var $pagesize;      // the initial pagination size
55   var $pagesize_def;  // the page size when one clicks the pagesize reset button
56   var $max_pages;     // the max number of pages to display in the paginator
57   var $notes;         // an array of additional notes
58   var $debug;         // set to true for enabling various log messages on console.log
59
60   // internal
61   var $has_tfoot;
62
63   function __construct ($table_id,$headers,$sort_column,$options=NULL) {
64     $this->table_id = $table_id;
65     $this->headers = $headers;
66     $this->sort_column = $sort_column;
67
68     $this->bullets1 = true;
69     $this->stripes=true;
70     $this->caption='';
71     $this->search_area = true;
72     $this->pagesize_area = true;
73     $this->notes_area = true;
74     $this->search_width = 40;
75     $this->pagesize = 25;
76     $this->pagesize_def = 9999;
77     $this->max_pages = 10;
78     $this->notes = array();
79     $this->debug = false;
80
81     $this->set_options ($options);
82
83     // internal
84     $this->has_tfoot=false;
85   }
86
87   function set_options ($options) {
88     if ( ! $options)
89       return;
90     if (array_key_exists('bullets1',$options)) $this->bullets1=$options['bullets1'];
91     if (array_key_exists('stripes',$options)) $this->stripes=$options['stripes'];
92     if (array_key_exists('caption',$options)) $this->caption=$options['caption'];
93     if (array_key_exists('search_area',$options)) $this->search_area=$options['search_area'];
94     if (array_key_exists('pagesize_area',$options)) $this->pagesize_area=$options['pagesize_area'];
95     if (array_key_exists('notes_area',$options)) $this->notes_area=$options['notes_area'];
96     if (array_key_exists('search_width',$options)) $this->search_width=$options['search_width'];
97     if (array_key_exists('pagesize',$options)) $this->pagesize=$options['pagesize'];
98     if (array_key_exists('pagesize_def',$options)) $this->pagesize_def=$options['pagesize_def'];
99     if (array_key_exists('max_pages',$options)) $this->max_pages=$options['max_pages'];
100     if (array_key_exists('notes',$options)) $this->notes=array_merge($this->notes,$options['notes']);
101     if (array_key_exists('debug',$options)) $this->debug=$options['debug'];
102   }
103
104   public function columns () {
105     return count ($this->headers);
106   }
107
108   ////////////////////
109   public function start () {
110     $paginator=$this->table_id."_paginator";
111     $classname="paginationcallback-".$paginator;
112     $classname.=" max-pages-" . $this->max_pages;
113     $classname.=" paginate-" . $this->pagesize;
114     if ($this->bullets1) { $classname .= " bullets1"; }
115     if ($this->stripes) { $classname .= " rowstyle-alt"; }
116     if ($this->sort_column >= 0) { $classname .= " sortable-onload-$this->sort_column"; }
117
118     // instantiate paginator callback
119     print "<script type='text/javascript'> function $paginator (opts) { plekit_table_paginator (opts,'$this->table_id'); } </script>\n";
120
121     // instantiate debug hooks if needed
122     if ($this->debug) {
123       $cb_init = $this->table_id."_init";
124       print "<script type='text/javascript'> function $cb_init () { plc_message ('sorting table $this->table_id'); } </script>\n";
125       $classname .= " sortinitiatedcallback-$cb_init";
126       $cb_comp = $this->table_id."_comp";
127       print "<script type='text/javascript'> function $cb_comp () { plc_message ('table $this->table_id sorted'); } </script>\n";
128       $classname .= " sortcompletecallback-$cb_comp";
129     }
130     // start actual table
131     print "<table id='$this->table_id' class='plekit_table colstyle-alt no-arrow $classname'><thead>\n";
132
133     if ($this->pagesize_area)
134       print $this->pagesize_area_html ();
135     if ($this->search_area)
136       print $this->search_area_html ();
137
138     if ($this->caption)
139       print "<caption> $this->caption </caption>";
140     print "<tr>";
141     foreach ($this->headers as $label => $colspec) {
142       // which form is being used
143       if (is_array($colspec)) {
144         $type=$colspec['type'];
145         $title=ucfirst($colspec['title']);
146       } else {
147         // simple/legacy form
148         $type=$colspec;
149         $title=NULL;
150       }
151       switch ($type) {
152       case "none" :
153         $class=""; break;
154       case "string": case "int": case "float":
155         $class="sortable"; break;
156       case ( strpos($type,"date-") == 0):
157         $class="sortable-" . $type; break;
158       default:
159         $class="sortable-sort" . $type; break;
160       }
161       $title_part=$title ? "title=\"$title\"" : "";
162       print ("<th class=\"$class plekit_table\" $title_part>$label</th>\n");
163     }
164
165     print "</tr></thead><tbody>";
166   }
167
168   ////////////////////
169   // for convenience, the options that apply to the bottom area can be passed here
170   // typically notes will add up to the ones provided so far, and to the default ones
171   // xxx default should be used only if applicable
172   function end ($options=NULL) {
173     $this->set_options($options);
174     print $this->bottom_html();
175     if ($this->notes_area)
176       print $this->notes_area_html();
177   }
178
179   ////////////////////
180   function pagesize_area_html () {
181     $width=count($this->headers);
182     $pagesize_text_id = $this->table_id . "_pagesize";
183     $result= <<< EOF
184 <tr class='pagesize_area'><td class='pagesize_area' colspan='$width'>
185 <form class='pagesize' action='satisfy_xhtml_validator'><fieldset>
186    <input class='pagesize_input' type='text' id="$pagesize_text_id" value='$this->pagesize'
187       onkeyup='plekit_pagesize_set("$this->table_id","$pagesize_text_id", $this->pagesize);'
188       size='3' maxlength='4' />
189   <label class='pagesize_label'> items/page </label>
190   <img class='reset' src="/planetlab/icons/clear.png" alt="reset visible size"
191       onmousedown='plekit_pagesize_reset("$this->table_id","$pagesize_text_id",$this->pagesize_def);' />
192 </fieldset></form></td></tr>
193 EOF;
194     return $result;
195 }
196
197   ////////////////////
198   function search_area_html () {
199     $width=count($this->headers);
200     $search_text_id = $this->table_id . "_search";
201     $search_reset_id = $this->table_id . "_search_reset";
202     $search_and_id = $this->table_id . "_search_and";
203     $result = <<< EOF
204 <tr class='search_area'><td class='search_area' colspan='$width'>
205 <div class='search'><fieldset>
206    <label class='search_label'> Search </label>
207    <input class='search_input' type='text' id='$search_text_id'
208       onkeyup='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");'
209       size='$this->search_width' maxlength='256' />
210    <label>and</label>
211    <input id='$search_and_id' class='search_and'
212       type='checkbox' checked='checked'
213       onchange='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");' />
214    <img class='reset' src="/planetlab/icons/clear.png" alt="reset search"
215       onmousedown='plekit_table_filter_reset("$this->table_id","$search_text_id","$search_and_id");' />
216 </fieldset></div></td></tr>
217 EOF;
218     return $result;
219   }
220
221   //////////////////// start a <tfoot> section
222   function tfoot_start () { print $this->tfoot_start_html(); }
223   function tfoot_start_html () {
224     $this->has_tfoot=true;
225     return "</tbody><tfoot>";
226   }
227
228   ////////////////////////////////////////
229   function bottom_html () {
230     $result="";
231     if ($this->has_tfoot)
232       $result .= "</tfoot>";
233     else
234       $result .= "</tbody>";
235     $result .= "</table>\n";
236     return $result;
237   }
238
239   ////////////////////////////////////////
240   function notes_area_html () {
241     $search_notes =
242       array("Enter &amp; or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes");
243     $sort_notes =
244       array ("Hold down the shift key to select multiple columns to sort");
245
246     if ($this->notes)
247       $notes=$this->notes;
248     else
249       $notes=array();
250     $notes=array_merge($notes,$sort_notes);
251     if ($this->search_area)
252       $notes=array_merge($notes,$search_notes);
253     if (! $notes)
254       return "";
255     $result = "";
256     $result .= "<p class='table_note'> <span class='table_note_title'>Notes</span>\n";
257     foreach ($notes as $note)
258       $result .= "<br/>$note\n";
259     $result .= "</p>";
260     return $result;
261   }
262
263   ////////////////////////////////////////
264   function row_start ($id=NULL,$class=NULL) {
265     print "<tr";
266     if ( $id) print (" id=\"$id\"");
267     if ( $class) print (" class=\"$class\"");
268     print ">\n";
269   }
270
271   function row_end () {
272     print "</tr>\n";
273   }
274
275   ////////////////////
276   // supported options:
277   // (*) only-if : if set and false, then print 'n/a' instead of (presumably void) $text
278   // (*) class
279   // (*) columns
280   // (*) hfill
281   // (*) align
282   public function cell ($text,$options=NULL) { print $this->cell_html ($text,$options); }
283   public function cell_html ($text, $options=NULL) {
284     if (isset ($options['only-if']) && ! $options['only-if'] )
285       $text = "n/a";
286     $html = "";
287     $html .= "<td";
288     // xxx this should not happen, but we see it sometimes - ignoring for now
289     if ($options && ! is_array($options)) {
290       //print_r("<br>options=");
291       //var_dump($options);
292     }
293   if ($options && is_array($options)) {
294       $option = get_array($options, 'class');
295       if ($option)
296         $html .= " class='$option'";
297       $option = get_array($options, 'columns');
298         if ($option) $html .= " colspan='$option'";
299       $option = get_array($options, 'hfill');
300         if ($option) $html .= " colspan='" . $this->columns() . "'";
301       $option = get_array($options, 'align');
302         if ($option) $html .= " style='text-align:$option'";
303     }
304     $html .= ">$text</td>";
305     return $html;
306   }
307
308 }
309
310 ?>