tables layout
[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 //// hash to retrieve the headers and options as passed at table-creation time
13 // this means that table_id's need to be different across the page,
14 // which is required anyway for the search and pagesize areas to work properly
15 $plc_table_hash=array();
16
17 ////////////////////////////////////////
18 function plc_table_cell($cell) {
19   printf ('<td class="plc_table"> %s </td>',$cell);
20 }
21
22 ////////////////////////////////////////
23 // table_id: <table>'s id tag
24 // headers: an associative array "label"=>"type" 
25 // column_sort: the column to sort on at load-time
26 // options : an associative array to override options (should be passed to both _stsart and _end)
27 //  - search_area : boolean (default true)
28 //  - pagesize_area : boolean (default true)
29 //  - notes_area : boolean (default true)
30 //  - notes : an array of additional notes
31 //  - pagesize: the initial pagination size
32 //  - pagesize_def: the page size when one clicks the pagesize reset button
33 //  - max_pages: the max number of pages to display in the paginator
34 //  - footers: a list of table rows (<tr> will be added) for building the table's tfoot area
35 function plc_table_start ($table_id, $headers, $column_sort, $options=NULL) {
36   if ( ! $options ) $options = array();
37   global $plc_table_hash;
38   $plc_table_hash[$table_id]=array($headers,$options);
39   $search_area = array_key_exists('search_area',$options) ? $options['search_area'] : true;
40   $pagesize_area = array_key_exists('pagesize_area',$options) ? $options['pagesize_area'] : true;
41   $max_pages = array_key_exists('max_pages',$options) ? $options['max_pages'] : 10;
42   $pagesize = array_key_exists('pagesize',$options) ? $options['pagesize'] : 25;
43   $pagesize_def = array_key_exists('pagesize_def',$options) ? $options['pagesize_def'] : 999;
44
45   $paginator=$table_id."_paginator";
46   $classname="paginationcallback-".$paginator;
47   $classname.=" max-pages-" . $max_pages;
48   $classname.=" paginate-" . $pagesize;
49   print <<< EOF
50 <!-- instantiate paginator callback -->
51 <script type="text/javascript"> 
52 function $paginator (opts) { plc_table_paginator (opts,"$table_id"); }
53 </script>
54 <br/>
55 <table id="$table_id" cellpadding="0" cellspacing="0" border="0" 
56 class="plc_table sortable-onload-$column_sort rowstyle-alt colstyle-alt no-arrow $classname">
57 <thead>
58 EOF;
59
60   if ($pagesize_area) plc_table_pagesize_area ($table_id,$headers,$pagesize, $pagesize_def);
61   if ($search_area) plc_table_search_area ($table_id, $headers);
62
63   print "<tr>";
64   foreach ($headers as $label => $type) {
65     if ($type == "none" ) {
66       $class="";
67     } else {
68       if ($type == "string") $type="";
69       if ($type == "int") $type="";
70       if ($type == "float") $type="";
71       $class="sortable";
72       if ( ! empty($type)) $class .= "-sort" . $type;
73     }
74     printf ('<th class="%s plc_table">%s</th>',$class,$label);
75   }
76
77   print <<< EOF
78 </tr>
79 </thead>
80 <tbody>
81 EOF;
82 }
83
84 ////////////////////
85 function plc_table_pagesize_area ($table_id,$headers,$pagesize,$pagesize_def) {
86   $width=count($headers);
87   $pagesize_text_id = $table_id . "_pagesize";
88   print <<< EOF
89 <tr class=pagesize_area><td class=pagesize_area colspan=$width><form class='pagesize'>
90    <input class='pagesize_input' type='text' id="$pagesize_text_id" value=$pagesize 
91       onkeyup='plc_pagesize_set("$table_id","$pagesize_text_id", $pagesize);' 
92       size=3 maxlength=3 /> 
93   <label class='pagesize_label'> items/page </label>   
94   <img class='table_reset' src="/planetlab/icons/clear.png" 
95       onmousedown='plc_pagesize_reset("$table_id","$pagesize_text_id",$pagesize_def);' />
96 </form></td></tr>
97 EOF;
98 }
99
100 ////////////////////
101 function plc_table_search_area ($table_id,$headers) {
102   $width=count($headers);
103   $search_text_id = $table_id . "_search";
104   $search_reset_id = $table_id . "_search_reset";
105   $search_and_id = $table_id . "_search_and";
106   print <<< EOF
107 <tr class=search_area><td class=search_area colspan=$width><form class='table_search'>
108    <label class='table_search_label'> Search </label> 
109    <input class='table_search_input' type='text' id='$search_text_id'
110       onkeyup='plc_table_filter("$table_id","$search_text_id","$search_and_id");'
111       size=40 maxlength=256 />
112    <label>and</label>
113    <input id='$search_and_id' class='table_search_and' 
114       type='checkbox' checked='checked' 
115       onchange='plc_table_filter("$table_id","$search_text_id","$search_and_id");' />
116    <img class='table_reset' src="/planetlab/icons/clear.png" 
117       onmousedown='plc_table_filter_reset("$table_id","$search_text_id","$search_and_id");'>
118 </form></td></tr>
119 EOF;
120 }
121
122 ////////////////////////////////////////
123 // for convenience, the options that apply to the footer only can be passed in plc_table_end()
124 // they add up to the ones provided to the begin clause
125 // makes code more readable, as preparing the footer before the table is displayed is confusing
126 function plc_table_end ($table_id,$options_end=NULL) {
127   global $plc_table_hash;
128   list($headers,$options) = $plc_table_hash[$table_id];
129   if ($options_end) 
130     $options=array_merge($options,$options_end);
131
132   plc_table_foot($options);
133   $notes_area = array_key_exists('notes_area',$options) ? $options['notes_area'] : true;
134   if ($notes_area) 
135     plc_table_notes($options);
136 }
137                     
138 ////////////////////////////////////////
139 function plc_table_foot ($options) {
140   print "</tbody><tfoot>";
141   if ($options['footers']) 
142     foreach ($options['footers'] as $footer) 
143       print "<tr> $footer </tr>";
144   print "</tfoot></table>\n";
145 }
146
147 ////////////////////////////////////////
148 function plc_table_notes ($options) {
149   print <<< EOF
150 <p class='plc_filter_note'> 
151 Notes: Enter & or | in the search area to alternate between <bold>AND</bold> and <bold>OR</bold> search modes
152 <br/> 
153 Hold down the shift key to select multiple columns to sort 
154 EOF;
155   if (array_key_exists('notes',$options)) {
156     foreach ($options['notes'] as $line) {
157       print "<br/>" . $line . "\n";
158     }
159   }
160   print "</p>";
161 }
162
163 ////////////////////////////////////////
164 function plc_table_row_start ($id="") {
165   if ( $id) {
166     printf ('<tr id="%s">',$id);
167   } else {
168     print '<tr>';
169   }
170 }
171 function plc_table_row_end () {
172   print "</tr>\n";
173 }
174
175 function plc_table_td_text ($text,$colspan=0,$align=NULL) {
176   $result="";
177   $result .= "<td";
178   if ($colspan) $result .= " colspan=$colspan";
179   if ($align) $result .= " style='text-align:$align'";
180   $result .= ">$text</td>";
181   return $result;
182 }
183
184 ?>
185