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