class PlcTable replaces functions plc_table_*
[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 //  - footers: an array of table rows (<tr> will be added) for building the table's tfoot area
25
26 class PlcTable {
27   // mandatory
28   var $table_id;
29   var $headers;
30   var $column_sort;
31   // options
32   var $search_area;   // boolean (default true)
33   var $pagesize_area; // boolean (default true)
34   var $notes_area;    // boolean (default true)
35   var $search_width;  // size in chars of the search text dialog
36   var $pagesize;       // the initial pagination size
37   var $pagesize_def;  // the page size when one clicks the pagesize reset button
38   var $max_pages;     // the max number of pages to display in the paginator
39   var $notes;         // an array of additional notes
40   var $footers;       // an array of table rows (<tr> will be added) for building the table's tfoot area
41
42   function PlcTable ($table_id,$headers,$column_sort,$options=NULL) {
43     $this->table_id = $table_id;
44     $this->headers = $headers;
45     $this->column_sort = $column_sort;
46
47     $this->search_area = true;
48     $this->pagesize_area = true;
49     $this->notes_area = true;
50     $this->search_width = 40;
51     $this->pagesize = 25;
52     $this->pagesize_def = 999;
53     $this->max_pages = 10;
54     $this->notes = array();
55     $this->footers = 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     if (array_key_exists('footers',$options)) $this->footers=array_merge($this->footers,$options['footers']);
73   }
74
75   ////////////////////
76   public function start () {
77     $paginator=$this->table_id."_paginator";
78     $classname="paginationcallback-".$paginator;
79     $classname.=" max-pages-" . $max_pages;
80     $classname.=" paginate-" . $pagesize;
81   // instantiate paginator callback
82     print <<< EOF
83 <script type="text/javascript"> 
84 function $paginator (opts) { plc_table_paginator (opts,"$this->table_id"); }
85 </script>
86 <br/>
87 <table id="$this->table_id" cellpadding="0" cellspacing="0" border="0" 
88 class="plc_table sortable-onload-$this->column_sort rowstyle-alt colstyle-alt no-arrow $classname">
89 <thead>
90 EOF;
91
92   if ($this->pagesize_area)
93     print $this->pagesize_area_text ();
94   if ($this->search_area) 
95     print $this->search_area_text ();
96
97   print "<tr>";
98   foreach ($this->headers as $label => $type) {
99     if ($type == "none" ) {
100       $class="";
101     } else {
102       if ($type == "string") $type="";
103       if ($type == "int") $type="";
104       if ($type == "float") $type="";
105       $class="sortable";
106       if ( ! empty($type)) $class .= "-sort" . $type;
107     }
108     printf ('<th class="%s plc_table">%s</th>',$class,$label);
109   }
110
111   print <<< EOF
112 </tr>
113 </thead>
114 <tbody>
115 EOF;
116 }
117
118   ////////////////////
119   // for convenience, the options that apply to the footer can be passed here
120   // typically footers or notes will add up to the ones provided so far
121   // makes code more readable, as preparing the footer before the table is displayed is confusing
122   function end ($options=NULL) {
123     $this->set_options($options);
124     print $this->foot_text();
125     if ($this->notes_area) 
126       print $this->notes_area_text();
127   }
128                     
129   ////////////////////
130   function pagesize_area_text () {
131     $width=count($this->headers);
132     $pagesize_text_id = $this->table_id . "_pagesize";
133     $result= <<< EOF
134 <tr class=pagesize_area><td class=pagesize_area colspan=$width><form class='pagesize'>
135    <input class='pagesize_input' type='text' id="$pagesize_text_id" value=$this->pagesize 
136       onkeyup='plc_pagesize_set("$this->table_id","$this->pagesize_text_id", $this->pagesize);' 
137       size=3 maxlength=3 /> 
138   <label class='pagesize_label'> items/page </label>   
139   <img class='table_reset' src="/planetlab/icons/clear.png" 
140       onmousedown='plc_pagesize_reset("$this->table_id","$pagesize_text_id",$this->pagesize_def);' />
141 </form></td></tr>
142 EOF;
143     return $result;
144 }
145
146   ////////////////////
147   function search_area_text () {
148     $width=count($this->headers);
149     $search_text_id = $this->table_id . "_search";
150     $search_reset_id = $this->table_id . "_search_reset";
151     $search_and_id = $this->table_id . "_search_and";
152     $result = <<< EOF
153 <tr class=search_area><td class=search_area colspan=$width><form class='table_search'>
154    <label class='table_search_label'> Search </label> 
155    <input class='table_search_input' type='text' id='$search_text_id'
156       onkeyup='plc_table_filter("$this->table_id","$search_text_id","$search_and_id");'
157       size=$this->search_width maxlength=256 />
158    <label>and</label>
159    <input id='$search_and_id' class='table_search_and' 
160       type='checkbox' checked='checked' 
161       onchange='plc_table_filter("$this->table_id","$search_text_id","$search_and_id");' />
162    <img class='table_reset' src="/planetlab/icons/clear.png" 
163       onmousedown='plc_table_filter_reset("$this->table_id","$search_text_id","$search_and_id");'>
164 </form></td></tr>
165 EOF;
166     return $result;
167   }
168
169   ////////////////////////////////////////
170   function foot_text () {
171     $result="";
172     $result .= "</tbody><tfoot>\n";
173     if ($this->footers) 
174       foreach ($this->footers as $footer) 
175         $result .="<tr>$footer</tr>\n";
176     $result .= "</tfoot></table>\n";
177     return $result;
178   }
179
180   ////////////////////////////////////////
181   function notes_area_text () {
182     $default_notes =  array(
183         "Enter & or | in the search area to alternate between <bold>AND</bold> and <bold>OR</bold> search modes",
184         "Hold down the shift key to select multiple columns to sort");
185
186     if ($this->notes)
187       $notes=$this->notes;
188     else
189       $notes=array();
190     $notes=array_merge($notes,$default_notes);
191     if (! $notes)
192       return "";
193     $result = "";
194     $result .= "<p class='plc_table_note'> <span class='plc_table_note_title'>Notes</span>\n";
195     foreach ($notes as $note) 
196       $result .= "<br/>$note\n";
197     $result .= "</p>";
198     return $result;
199   }
200
201   ////////////////////////////////////////
202   function row_start ($id=NULL,$class=NULL) {
203     print "<tr";
204     if ( $id) print (" id=\"$id\"");
205     if ( $class) print (" class=\"$class\"");
206     print ">\n";
207   }
208
209   function row_end () {
210     print "</tr>\n";
211   }
212
213   ////////////////////
214   public function cell_text ($cell) {
215     return "<td>$cell</td>";
216   }
217   public function cell ($cell) {
218     print $this->cell_text($cell);
219   }
220   
221   public static function td_text ($text,$colspan=0,$align=NULL) {
222     $result="";
223     $result .= "<td";
224     if ($colspan) $result .= " colspan=$colspan";
225     if ($align) $result .= " style='text-align:$align'";
226     $result .= ">$text</td>";
227     return $result;
228   }
229
230 }
231
232 ?>