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