handles urls with settings
[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 //  - 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 //  - footers: a list of table rows (<tr> will be added) for building the table's tfoot area
34 function plc_table_start ($table_id, $headers, $column_sort, $options=NULL) {
35   if ( ! $options ) $options = array();
36   global $plc_table_hash;
37   $plc_table_hash[$table_id]=array($headers,$options);
38   $search_area = array_key_exists('search_area',$options) ? $options['search_area'] : true;
39   $max_pages = array_key_exists('max_pages',$options) ? $options['max_pages'] : 10;
40   $pagesize = array_key_exists('pagesize',$options) ? $options['pagesize'] : 25;
41   $pagesize_def = array_key_exists('pagesize_def',$options) ? $options['pagesize_def'] : 999;
42
43   if ($search_area) 
44     plc_table_search_area($table_id,$pagesize,$pagesize_def);
45   plc_table_head($table_id,$headers,$column_sort,$max_pages,$pagesize);
46 }
47
48 // for convenience, the options that apply to the footer only can be passed in plc_table_end()
49 // they add up to the ones provided to the begin clause
50 // makes code more readable, as preparing the footer before the table is displayed is confusing
51 function plc_table_end ($table_id,$options_end=NULL) {
52   global $plc_table_hash;
53   list($headers,$options) = $plc_table_hash[$table_id];
54   if ($options_end) 
55     $options=array_merge($options,$options_end);
56
57   plc_table_foot($options);
58   $notes_area = array_key_exists('notes_area',$options) ? $options['notes_area'] : true;
59   if ($notes_area) 
60     plc_table_notes($options);
61 }
62                     
63 ////////////////////
64 function plc_table_search_area ($table_id,$pagesize,$pagesize_def) {
65   $pagesize_text_id = $table_id . "_pagesize";
66   $search_text_id = $table_id . "_search";
67   $search_reset_id = $table_id . "_search_reset";
68   $search_and_id = $table_id . "_search_and";
69   print <<< EOF
70 <table class='table_dialogs'> <tr>
71 <td class='table_flushleft'>
72 <form class='pagesize'>
73    <input class='pagesize_input' type='text' id="$pagesize_text_id" value=$pagesize 
74       onkeyup='plc_pagesize_set("$table_id","$pagesize_text_id", $pagesize);' 
75       size=3 maxlength=3 /> 
76   <label class='pagesize_label'> items/page </label>   
77   <img class='table_reset' src="/planetlab/icons/clear.png" 
78       onmousedown='plc_pagesize_reset("$table_id","$pagesize_text_id",$pagesize_def);'>
79 </form>
80 </td>
81
82 <td class='table_flushright'> 
83 <form class='table_search'>
84    <label class='table_search_label'> Search </label> 
85    <input class='table_search_input' type='text' id='$search_text_id'
86       onkeyup='plc_table_filter("$table_id","$search_text_id","$search_and_id");'
87       size=40 maxlength=256 />
88    <label>and</label>
89    <input id='$search_and_id' class='table_search_and' 
90       type='checkbox' checked='checked' 
91       onchange='plc_table_filter("$table_id","$search_text_id","$search_and_id");' />
92    <img class='table_reset' src="/planetlab/icons/clear.png" 
93       onmousedown='plc_table_filter_reset("$table_id","$search_text_id","$search_and_id");'>
94 </form>
95 </td>
96 </tr></table>
97 EOF;
98 }
99
100 ////////////////////////////////////////
101 function plc_table_head ($table_id,$headers,$column_sort,$max_pages,$pagesize) {
102   $paginator=$table_id."_paginator";
103   $classname="paginationcallback-".$paginator;
104   $classname.=" max-pages-" . $max_pages;
105   $classname.=" paginate-" . $pagesize;
106   print <<< EOF
107 <!-- instantiate paginator callback -->
108 <script type="text/javascript"> 
109 function $paginator (opts) { plc_table_paginator (opts,"$table_id"); }
110 </script>
111 <br/>
112 <table id="$table_id" cellpadding="0" cellspacing="0" border="0" 
113 class="plc_table sortable-onload-$column_sort rowstyle-alt colstyle-alt no-arrow $classname">
114 <thead>
115 <tr>
116 EOF;
117
118   foreach ($headers as $label => $type) {
119     if ($type == "none" ) {
120       $class="";
121     } else {
122       if ($type == "string") $type="";
123       if ($type == "int") $type="";
124       if ($type == "float") $type="";
125       $class="sortable";
126       if ( ! empty($type)) $class .= "-sort" . $type;
127     }
128     printf ('<th class="%s plc_table">%s</th>',$class,$label);
129   }
130
131   print <<< EOF
132 </tr>
133 </thead>
134 <tbody>
135 EOF;
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_title ($text) {
165   print "<h2> $text </h2>\n";
166 }
167
168 function plc_table_row_start ($id="") {
169   if ( $id) {
170     printf ('<tr id="%s">',$id);
171   } else {
172     print '<tr>';
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