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