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