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