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