trailing spaces
[plewww.git] / plekit / php / table2.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   var $configurable;  // boolen (whether the column configuration option is set or not)
60
61   // internal
62   var $has_tfoot;
63
64   function __construct ($table_id,$headers,$sort_column,$options=NULL) {
65     $this->table_id = $table_id;
66     $this->headers = $headers;
67     $this->sort_column = $sort_column;
68
69     $this->bullets1 = true;
70     $this->stripes=true;
71     $this->caption='';
72     $this->search_area = true;
73     $this->pagesize_area = true;
74     $this->notes_area = true;
75     $this->search_width = 40;
76     $this->pagesize = 25;
77     $this->pagesize_def = 9999;
78     $this->max_pages = 10;
79     $this->notes = array();
80     $this->debug = false;
81     $this->configurable = false;
82
83     $this->set_options ($options);
84
85     // internal
86     $this->has_tfoot=false;
87   }
88
89   function set_options ($options) {
90     if ( ! $options)
91       return;
92     if (array_key_exists('bullets1',$options)) $this->bullets1=$options['bullets1'];
93     if (array_key_exists('stripes',$options)) $this->stripes=$options['stripes'];
94     if (array_key_exists('caption',$options)) $this->caption=$options['caption'];
95     if (array_key_exists('search_area',$options)) $this->search_area=$options['search_area'];
96     if (array_key_exists('pagesize_area',$options)) $this->pagesize_area=$options['pagesize_area'];
97     if (array_key_exists('notes_area',$options)) $this->notes_area=$options['notes_area'];
98     if (array_key_exists('search_width',$options)) $this->search_width=$options['search_width'];
99     if (array_key_exists('pagesize',$options)) $this->pagesize=$options['pagesize'];
100     if (array_key_exists('pagesize_def',$options)) $this->pagesize_def=$options['pagesize_def'];
101     if (array_key_exists('max_pages',$options)) $this->max_pages=$options['max_pages'];
102     if (array_key_exists('notes',$options)) $this->notes=array_merge($this->notes,$options['notes']);
103     if (array_key_exists('debug',$options)) $this->debug=$options['debug'];
104     if (array_key_exists('configurable',$options)) $this->configurable=$options['configurable'];
105   }
106
107   public function columns () {
108     return count ($this->headers);
109   }
110
111   ////////////////////
112   public function start () {
113     $paginator=$this->table_id."_paginator";
114     $classname="paginationcallback-".$paginator;
115     $classname.=" max-pages-" . $this->max_pages;
116     $classname.=" paginate-" . $this->pagesize;
117     if ($this->bullets1) { $classname .= " bullets1"; }
118     if ($this->stripes) { $classname .= " rowstyle-alt"; }
119     if ($this->sort_column >= 0) { $classname .= " sortable-onload-$this->sort_column"; }
120
121     // instantiate paginator callback
122     print "<script type='text/javascript'> function $paginator (opts) { plekit_table_paginator (opts,'$this->table_id'); } </script>\n";
123
124     // instantiate debug hooks if needed
125     if ($this->debug) {
126       $cb_init = $this->table_id."_init";
127       print "<script type='text/javascript'> function $cb_init () { plc_message ('sorting table $this->table_id'); } </script>\n";
128       $classname .= " sortinitiatedcallback-$cb_init";
129       $cb_comp = $this->table_id."_comp";
130       print "<script type='text/javascript'> function $cb_comp () { plc_message ('table $this->table_id sorted'); } </script>\n";
131       $classname .= " sortcompletecallback-$cb_comp";
132     }
133     // start actual table
134     print "<table id='$this->table_id' class='plekit_table colstyle-alt no-arrow $classname'><thead>\n";
135
136     if ($this->pagesize_area)
137       print $this->pagesize_area_html ();
138     if ($this->search_area)
139       print $this->search_area_html ();
140
141     if ($this->caption)
142       print "<caption> $this->caption </caption>";
143     print "<tr>";
144
145 //a hidden column to store the node_id (used for the dynamic update of column data but not sure if
146 //it is necessary)
147 if ($this->configurable)
148       print ("<th class=\"plekit_table\" style=\"display:none\">hiddenID</th>\n");
149
150     foreach ($this->headers as $label => $colspec) {
151       // which form is being used
152       if (is_array($colspec)) {
153         $type=$colspec['type'];
154         $title=ucfirst($colspec['title']);
155         if ($this->configurable) {
156                 $visible=$colspec['visible'];
157                 $header = $colspec['header'];
158                 $tlabel=$colspec['label'];
159         }
160       } else {
161         // simple/legacy form
162         $type=$colspec;
163         $title=NULL;
164         if ($this->configurable) {
165                 $visible = true;
166                 $tlabel=$label;
167                 $header=$hkey;
168         }
169       }
170       switch ($type) {
171       case "none" :
172         $class=""; break;
173       case "string": case "int": case "float":
174         $class="sortable"; break;
175       case ( strpos($type,"date-") == 0):
176         $class="sortable-" . $type; break;
177       default:
178         $class="sortable-sort" . $type; break;
179       }
180       $title_part=$title ? "title=\"$title\"" : "";
181
182       if ($this->configurable) {
183         if ($visible) print ("<th class=\"$class plekit_table\" $title_part name=\"$header\" style=\"display:table-cell\">".$tlabel."</th>\n");
184         else  print ("<th class=\"$class plekit_table\" $title_part name=\"$header\" style=\"display:none\">".$tlabel."</th>\n");
185       }
186       else
187         print ("<th class=\"$class plekit_table\" $title_part name=\"$label\" style=\"display:table-cell\">$label</th>\n");
188         //print ("<th class=\"$class plekit_table\" $title_part>$label</th>\n");
189     }
190
191     print "</tr></thead><tbody>";
192   }
193
194   ////////////////////
195   // for convenience, the options that apply to the bottom area can be passed here
196   // typically notes will add up to the ones provided so far, and to the default ones
197   // xxx default should be used only if applicable
198   function end ($options=NULL) {
199     $this->set_options($options);
200     print $this->bottom_html();
201     if ($this->notes_area)
202       print $this->notes_area_html();
203   }
204
205   ////////////////////
206   function pagesize_area_html () {
207     $width=count($this->headers);
208     $pagesize_text_id = $this->table_id . "_pagesize";
209     $result= <<< EOF
210 <tr class='pagesize_area'><td class='pagesize_area' colspan='$width'>
211 <form class='pagesize' action='satisfy_xhtml_validator'><fieldset>
212    <input class='pagesize_input' type='text' id="$pagesize_text_id" value='$this->pagesize'
213       onkeyup='plekit_pagesize_set("$this->table_id","$pagesize_text_id", $this->pagesize);'
214       size='3' maxlength='4' />
215   <label class='pagesize_label'> items/page </label>
216   <img class='reset' src="/planetlab/icons/clear.png" alt="reset visible size"
217       onmousedown='plekit_pagesize_reset("$this->table_id","$pagesize_text_id",$this->pagesize_def);' />
218 </fieldset></form></td></tr>
219 EOF;
220     return $result;
221 }
222
223   ////////////////////
224   function search_area_html () {
225     $width=count($this->headers);
226     $search_text_id = $this->table_id . "_search";
227     $search_reset_id = $this->table_id . "_search_reset";
228     $search_and_id = $this->table_id . "_search_and";
229     $result = <<< EOF
230 <tr class='search_area'><td class='search_area' colspan='$width'>
231 <div class='search'><fieldset>
232    <label class='search_label'> Search </label>
233    <input class='search_input' type='text' id='$search_text_id'
234       onkeyup='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");'
235       size='$this->search_width' maxlength='256' />
236    <label>and</label>
237    <input id='$search_and_id' class='search_and'
238       type='checkbox' checked='checked'
239       onchange='plekit_table_filter("$this->table_id","$search_text_id","$search_and_id");' />
240    <img class='reset' src="/planetlab/icons/clear.png" alt="reset search"
241       onmousedown='plekit_table_filter_reset("$this->table_id","$search_text_id","$search_and_id");' />
242 </fieldset></div></td></tr>
243 EOF;
244     return $result;
245   }
246
247   //////////////////// start a <tfoot> section
248   function tfoot_start () { print $this->tfoot_start_html(); }
249   function tfoot_start_html () {
250     $this->has_tfoot=true;
251     return "</tbody><tfoot>";
252   }
253
254   ////////////////////////////////////////
255   function bottom_html () {
256     $result="";
257     if ($this->has_tfoot)
258       $result .= "</tfoot>";
259     else
260       $result .= "</tbody>";
261     $result .= "</table>\n";
262     return $result;
263   }
264
265   ////////////////////////////////////////
266   function notes_area_html () {
267     $search_notes =
268       array("Enter &amp; or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes");
269     $sort_notes =
270       array ("Hold down the shift key to select multiple columns to sort");
271
272     if ($this->notes)
273       $notes=$this->notes;
274     else
275       $notes=array();
276     $notes=array_merge($notes,$sort_notes);
277     if ($this->search_area)
278       $notes=array_merge($notes,$search_notes);
279     if (! $notes)
280       return "";
281     $result = "";
282     $result .= "<p class='table_note'> <span class='table_note_title'>Notes</span>\n";
283     foreach ($notes as $note)
284       $result .= "<br/>$note\n";
285     $result .= "</p>";
286     return $result;
287   }
288
289   ////////////////////////////////////////
290   function row_start ($id=NULL,$class=NULL) {
291     print "<tr";
292     if ( $id) print (" id=\"$id\"");
293     if ( $class) print (" class=\"$class\"");
294     print ">\n";
295   }
296
297   function row_end () {
298     print "</tr>\n";
299   }
300
301   ////////////////////
302   // supported options:
303   // (*) only-if : if set and false, then print 'n/a' instead of (presumably void) $text
304   // (*) class
305   // (*) columns
306   // (*) hfill
307   // (*) align
308   public function cell ($text,$options=NULL) { print $this->cell_html ($text,$options); }
309   public function cell_html ($text,$options=NULL) {
310     if (isset ($options['only-if']) && ! $options['only-if'] )
311       $text="n/a";
312     $html="";
313     $html .= "<td";
314     $option=$options['class'];  if ($option) $html .= " class='$option'";
315     $option=$options['columns'];if ($option) $html .= " colspan='$option'";
316     $option=$options['hfill'];  if ($option) $html .= " colspan='" . $this->columns() . "'";
317     $option=$options['align'];  if ($option) $html .= " style='text-align:$option'";
318     $option=$options['color'];  if ($option) $html .= " style='color:$option'";
319     $option=$options['display'];  if ($option) $html .= " style='display: $option'";
320     $option=$options['name'];   if ($option) $html .= " name='$option'";
321     $html .= ">$text</td>";
322     return $html;
323   }
324
325 }
326
327 ?>