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