5d7f379d672b94a94da463d17e8a3f1689bf2812
[plewww.git] / planetlab / includes / plc_tables.php
1 <?php
2
3   // $Id$
4
5 drupal_set_html_head('
6 <script type="text/javascript" src="/planetlab/tablesort/tablesort.js"></script>
7 <script type="text/javascript" src="/planetlab/tablesort/customsort.js"></script>
8 <script type="text/javascript" src="/planetlab/tablesort/paginate.js"></script>
9 <script type="text/javascript" src="/planetlab/js/plc_tables.js"></script>
10 <link href="/planetlab/css/plc_tables.css" rel="stylesheet" type="text/css" />
11 ');
12
13 ////////////////////////////////////////
14 // table_id: <table>'s id tag - WARNING : do not use '-' in table ids as it's used for generating javascript code
15 // headers: an associative array "label"=>"type" 
16 // column_sort: the column to sort on at load-time
17 // options : an associative array to override options 
18 //  - search_area : boolean (default true)
19 //  - pagesize_area : boolean (default true)
20 //  - notes_area : boolean (default true)
21 //  - search_width : size in chars of the search text dialog
22 //  - notes : an array of additional notes
23 //  - pagesize: the initial pagination size
24 //  - pagesize_def: the page size when one clicks the pagesize reset button
25 //  - max_pages: the max number of pages to display in the paginator
26
27 class PlcTable {
28   // mandatory
29   var $table_id;
30   var $headers;
31   var $column_sort;
32   // options
33   var $search_area;   // boolean (default true)
34   var $pagesize_area; // boolean (default true)
35   var $notes_area;    // boolean (default true)
36   var $search_width;  // size in chars of the search text dialog
37   var $pagesize;       // the initial pagination size
38   var $pagesize_def;  // the page size when one clicks the pagesize reset button
39   var $max_pages;     // the max number of pages to display in the paginator
40   var $notes;         // an array of additional notes
41   var $has_tfoot;
42
43   function PlcTable ($table_id,$headers,$column_sort,$options=NULL) {
44     $this->table_id = $table_id;
45     $this->headers = $headers;
46     $this->column_sort = $column_sort;
47     
48     $this->has_tfoot=false;
49
50     $this->search_area = true;
51     $this->pagesize_area = true;
52     $this->notes_area = true;
53     $this->search_width = 40;
54     $this->pagesize = 25;
55     $this->pagesize_def = 999;
56     $this->max_pages = 10;
57     $this->notes = array();
58
59     $this->set_options ($options);
60   }
61
62   function set_options ($options) {
63     if ( ! $options)
64       return;
65     if (array_key_exists('search_area',$options)) $this->search_area=$options['search_area'];
66     if (array_key_exists('pagesize_area',$options)) $this->pagesize_area=$options['pagesize_area'];
67     if (array_key_exists('notes_area',$options)) $this->notes_area=$options['notes_area'];
68     if (array_key_exists('search_width',$options)) $this->search_width=$options['search_width'];
69     if (array_key_exists('pagesize',$options)) $this->pagesize=$options['pagesize'];
70     if (array_key_exists('pagesize_def',$options)) $this->pagesize_def=$options['pagesize_def'];
71     if (array_key_exists('max_pages',$options)) $this->max_pages=$options['max_pages'];
72
73     if (array_key_exists('notes',$options)) $this->notes=array_merge($this->notes,$options['notes']);
74   }
75
76   public function columns () {
77     return count ($this->headers);
78   }
79
80   ////////////////////
81   public function start () {
82     $paginator=$this->table_id."_paginator";
83     $classname="paginationcallback-".$paginator;
84     $classname.=" max-pages-" . $this->max_pages;
85     $classname.=" paginate-" . $this->pagesize;
86   // instantiate paginator callback
87     print <<< EOF
88 <script type="text/javascript"> 
89 function $paginator (opts) { plc_table_paginator (opts,"$this->table_id"); }
90 </script>
91 <br/>
92 <table id="$this->table_id" cellpadding="0" cellspacing="0" border="0" 
93 class="plc_table sortable-onload-$this->column_sort rowstyle-alt colstyle-alt no-arrow $classname">
94 <thead>
95 EOF;
96
97   if ($this->pagesize_area)
98     print $this->pagesize_area_html ();
99   if ($this->search_area) 
100     print $this->search_area_html ();
101
102   print "<tr>";
103   foreach ($this->headers as $label => $type) {
104     if ($type == "none" ) {
105       $class="";
106     } else {
107       if ($type == "string") $type="";
108       if ($type == "int") $type="";
109       if ($type == "float") $type="";
110       $class="sortable";
111       if ( ! empty($type)) $class .= "-sort" . $type;
112     }
113     printf ('<th class="%s plc_table">%s</th>',$class,$label);
114   }
115
116   print <<< EOF
117 </tr>
118 </thead>
119 <tbody>
120 EOF;
121 }
122
123   ////////////////////
124   // for convenience, the options that apply to the bottom area can be passed here
125   // typically notes will add up to the ones provided so far, and to the default ones 
126   // xxx default should be used only if applicable
127   function end ($options=NULL) {
128     $this->set_options($options);
129     print $this->bottom_html();
130     if ($this->notes_area) 
131       print $this->notes_area_html();
132   }
133                     
134   ////////////////////
135   function pagesize_area_html () {
136     $width=count($this->headers);
137     $pagesize_text_id = $this->table_id . "_pagesize";
138     $result= <<< EOF
139 <tr class=pagesize_area><td class=pagesize_area colspan=$width><form class='pagesize'>
140    <input class='pagesize_input' type='text' id="$pagesize_text_id" value=$this->pagesize 
141       onkeyup='plc_pagesize_set("$this->table_id","$pagesize_text_id", $this->pagesize);' 
142       size=3 maxlength=3 /> 
143   <label class='pagesize_label'> items/page </label>   
144   <img class='table_reset' src="/planetlab/icons/clear.png" 
145       onmousedown='plc_pagesize_reset("$this->table_id","$pagesize_text_id",$this->pagesize_def);' />
146 </form></td></tr>
147 EOF;
148     return $result;
149 }
150
151   ////////////////////
152   function search_area_html () {
153     $width=count($this->headers);
154     $search_text_id = $this->table_id . "_search";
155     $search_reset_id = $this->table_id . "_search_reset";
156     $search_and_id = $this->table_id . "_search_and";
157     $result = <<< EOF
158 <tr class=search_area><td class=search_area colspan=$width><form class='table_search'>
159    <label class='table_search_label'> Search </label> 
160    <input class='table_search_input' type='text' id='$search_text_id'
161       onkeyup='plc_table_filter("$this->table_id","$search_text_id","$search_and_id");'
162       size=$this->search_width maxlength=256 />
163    <label>and</label>
164    <input id='$search_and_id' class='table_search_and' 
165       type='checkbox' checked='checked' 
166       onchange='plc_table_filter("$this->table_id","$search_text_id","$search_and_id");' />
167    <img class='table_reset' src="/planetlab/icons/clear.png" 
168       onmousedown='plc_table_filter_reset("$this->table_id","$search_text_id","$search_and_id");'>
169 </form></td></tr>
170 EOF;
171     return $result;
172   }
173
174   //////////////////// start a <tfoot> section
175   function tfoot_start () { print $this->tfoot_start_html(); }
176   function tfoot_start_html () {
177     $this->has_tfoot=true;
178     return "</tbody><tfoot>";
179   }
180
181   ////////////////////////////////////////
182   function bottom_html () {
183     $result="";
184     if ($this->has_tfoot)
185       $result .= "</tfoot>";
186     else
187       $result .= "</tbody>";
188     $result .= "</table>\n";
189     return $result;
190   }
191
192   ////////////////////////////////////////
193   function notes_area_html () {
194     $default_notes =  array(
195         "Enter & or | in the search area to alternate between <bold>AND</bold> and <bold>OR</bold> search modes",
196         "Hold down the shift key to select multiple columns to sort");
197
198     if ($this->notes)
199       $notes=$this->notes;
200     else
201       $notes=array();
202     $notes=array_merge($notes,$default_notes);
203     if (! $notes)
204       return "";
205     $result = "";
206     $result .= "<p class='plc_table_note'> <span class='plc_table_note_title'>Notes</span>\n";
207     foreach ($notes as $note) 
208       $result .= "<br/>$note\n";
209     $result .= "</p>";
210     return $result;
211   }
212
213   ////////////////////////////////////////
214   function row_start ($id=NULL,$class=NULL) {
215     print "<tr";
216     if ( $id) print (" id=\"$id\"");
217     if ( $class) print (" class=\"$class\"");
218     print ">\n";
219   }
220
221   function row_end () {
222     print "</tr>\n";
223   }
224
225   ////////////////////
226   public function cell ($text,$colspan=0,$align=NULL) { print $this->cell_html ($text,$colspan,$align); }
227   public function cell_html ($text,$colspan=0,$align=NULL) {
228     $result="";
229     $result .= "<td";
230     if ($colspan) $result .= " colspan=$colspan";
231     if ($align) $result .= " style='text-align:$align'";
232     $result .= ">$text</td>";
233     return $result;
234   }
235
236 }
237
238 ?>