[Bio] / FortyEight / Table.pm Repository:
ViewVC logotype

Annotation of /FortyEight/Table.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (view) (download) (as text)

1 : paczian 1.1 package Table;
2 :    
3 :     use strict;
4 :     use warnings;
5 :     use URI::Escape;
6 :    
7 :     1;
8 :    
9 :     =over 3
10 :    
11 :     =head1 NAME
12 :    
13 :     Table
14 :    
15 :     =head1 DESCRIPTION
16 :    
17 :     The Table Component implements an HTML/javascript table for displaying, filtering and browsing data.
18 :     It supports onClick events and popup menus for each cell. Data can also be grouped by a single column.
19 :     Like all WebApplicationComponents, configuration of columns to display is also supported.
20 :    
21 :     =head1 ARGUMENTS
22 :    
23 :     Arguments are passed as a single hash. All data must be passed as references (i.e. reference to an array
24 :     instead of an array). A table is instanciated via the I<new> method.
25 :    
26 :     =head2 Mandatory Arguments
27 :    
28 :     =item B<data>
29 :    
30 :     This must be an array of rows, each containing an array of cells. The data type of the cell contents is
31 :     detected for sorting issues. The first cell in a column will determine the data type. Three types of data
32 :     are differentiated:
33 :    
34 :     Integers and Floats
35 :     Dates in the format mm/dd/yyyy
36 :     Strings
37 :    
38 :     The cell data must not include quotation marks or linebreaks of any kind.
39 :    
40 :     =item B<columns>
41 :    
42 :     The columns are passed as an array of column headers, also no quotation marks or linebreaks are allowed.
43 :    
44 :     =head2 Optional Arguments
45 :    
46 :     =item B<id>
47 :    
48 :     The identification of the table. Only neccessary if you have more than one table on a single page. Default is
49 :     'table'.
50 :    
51 :     =item B<image_base>
52 :    
53 :     The directory relative to the cgi directory where all images are stored. 'clear.gif' needs to be present in this
54 :     directory. Default is '../images'.
55 :    
56 :     =item B<offset>
57 :    
58 :     The array index of the first item in the list to be displayed. Default is 0.
59 :    
60 :     =item B<perpage>
61 :    
62 :     Number of items to be displayed on each page. Default is 20.
63 :    
64 :     =item B<show_perpage>
65 :    
66 :     Boolean to determine whether the user may change the number of items to be displayed on each
67 :     page. Default is false.
68 :    
69 :     =item B<show_topbrowse>
70 :    
71 :     Boolean to determine whether the browsing functions are displayed above the table. Default is
72 :     false.
73 :    
74 :     =item B<show_bottombrowse>
75 :    
76 :     Boolean to determine whether the browsing functions are displayed below the table. Default is
77 :     false.
78 :    
79 :     =item B<enable_grouping>
80 :    
81 :     Boolean to determine whether the grouping of a single column should be supported. Grouped
82 :     columns are collapsed if they contain consecutive identical values. They can be expanded
83 :     and collapsed by the user via a click on the symbol in the according cell. Default is false.
84 :    
85 :     =item B<group_col>
86 :    
87 :     Array index of the column to be the grouping column. Default is 0.
88 :    
89 :     =item B<sortable>
90 :    
91 :     Boolean to determine whether the user is allowed to sort the columns. Default is false.
92 :    
93 :     =item B<show_filter>
94 :    
95 :     Boolean to determine whether the user should be able to filter the columns. Default is false.
96 :    
97 :     =item B<available_operators>
98 :    
99 :     An array of operators available for filtering. Must be passed in the following format, which
100 :     is also the default:
101 :    
102 :     ( [ 'like', '&cong;' ],
103 :     [ 'unlike', '!&cong;' ],
104 :     [ 'equal', '=' ],
105 :     [ 'unequal', '!=' ],
106 :     [ 'less', '&lt;' ],
107 :     [ 'more', '&gt;' ] )
108 :    
109 :     Where the first entry is the operator and the second entry is the html to be displayed in the
110 :     select box.
111 :    
112 :     =item B<preselected_operators>
113 :    
114 :     A hash containing the column names as key and the operator to be pre-selected as a value. As
115 :     default, the first operator in the list is selected.
116 :    
117 :     =item B<operands>
118 :    
119 :     A hash containing the column names as key and the operand as values. If show_filter is true,
120 :     columns which are not a key of this hash will not receive a filter box.
121 :    
122 :     =item B<onclicks>
123 :    
124 :     If passed, this must be an array of rows, each containing an array of cells with the same
125 :     dimensions as the data array. The values should be urls that a click on the according cell
126 :     should link to.
127 :    
128 :     =item B<popup_menu>
129 :    
130 :     A hash containing the keys 'titles', 'infos' and 'menus'. Each of these have an array of arrays,
131 :     matching the dimensions of the data array, as a value. For usage of the popup menu, consult
132 :     the popup menu manual.
133 :    
134 :     =back
135 :    
136 :     =cut
137 :    
138 :     # initialize the table
139 :     sub new {
140 :     my ($params) = @_;
141 :    
142 :     # initialize variables
143 :     my $table = "";
144 :    
145 :     # retrieve mandatory data from params
146 :     my @data;
147 :     if (defined($params->{data})) {
148 :     @data = @{$params->{data}};
149 :     } else {
150 :     return "No data passed to table creator!";
151 :     }
152 :     my @columns;
153 :     if (defined($params->{columns})) {
154 :     @columns = @{$params->{columns}};
155 :     } else {
156 :     return "No columns passed to table creator!";
157 :     }
158 :    
159 :     # check for optional parameters in params
160 :     my @available_operators = (
161 :     [ 'like', '&cong;' ],
162 :     [ 'unlike', '!&cong;' ],
163 :     [ 'equal', '=' ], [ 'unequal', '!=' ],
164 :     [ 'less', '&lt;' ], [ 'more', '&gt;' ]
165 :     );
166 :     if (defined($params->{available_operators})) {
167 :     @available_operators = @{$params->{available_operators}};
168 :     }
169 :    
170 :     my %preselected_operators = ();
171 :     if (defined($params->{preselected_operators})) {
172 :     %preselected_operators = %{$params->{preselected_operators}};
173 :     }
174 :    
175 :     my %operands = ();
176 :     if (defined($params->{operands})) {
177 :     %operands = %{$params->{operands}};
178 :     }
179 :     unless (scalar(keys(%operands))) {
180 :     if (defined($params->{show_filter})) {
181 :     foreach my $column (@columns) {
182 :     $operands{$column} = "";
183 :     }
184 :     }
185 :     }
186 :    
187 :     my $complex_filter = $params->{complex_filter} || 0;
188 :     my $offset = $params->{offset} || 0;
189 :     my $total = scalar(@data) || 0;
190 :     $params->{total} = $total;
191 :     my $perpage = $params->{perpage} || -1;
192 :     $params->{perpage} = $perpage;
193 :     my $id = $params->{id} || "table";
194 :     $id =~ s/_//g;
195 :     $params->{id} = $id;
196 :     my $img_path = $params->{image_base} || "./Html/";
197 :     $params->{img_path} = $img_path;
198 :     my $show_perpage = $params->{show_perpage} || 0;
199 :     my $show_topbrowse = $params->{show_topbrowse} || 0;
200 :     my $show_bottombrowse = $params->{show_bottombrowse} || 0;
201 :     my $group_col = $params->{group_col} || 0;
202 :     my $enable_grouping = $params->{enable_grouping} || 0;
203 :     my $sortable = $params->{sortable} || 0;
204 :     my $column_widths = $params->{column_widths};
205 :     unless (defined($column_widths)) {
206 :     foreach (@columns) {
207 :     push(@$column_widths, -1);
208 :     }
209 :     }
210 :     for (my $i=0; $i<scalar(@columns); $i++) {
211 :     unless (defined($column_widths->[$i])) {
212 :     $column_widths->[$i] = -1;
213 :     }
214 :     }
215 :    
216 :     # create an empty data array
217 :     my $empty_array;
218 :     my @good_data;
219 :     foreach my $row (@data) {
220 :     my $empty_row;
221 :     my $good_row;
222 :     foreach my $cell (@$row) {
223 :     if (defined($cell)) {
224 :     push(@$good_row, $cell);
225 :     } else {
226 :     push(@$good_row, "");
227 :     }
228 :     push(@$empty_row, "");
229 :     }
230 :     push(@$empty_array, $empty_row);
231 :     push(@good_data, $good_row);
232 :     }
233 :     @data = @good_data;
234 :     @good_data = undef;
235 :    
236 :     # create onclick event string
237 :     my $onclicks = "";
238 :     {
239 :     my $onclicks_array = $params->{onclicks} || $empty_array;
240 :     my $rows = [];
241 :     foreach my $row (@$onclicks_array) {
242 :     push(@$rows, join("^", @$row));
243 :     }
244 :     $onclicks = join("~", @$rows);
245 :     }
246 :    
247 :     # create popup menu strings if requested
248 :     my $titles = "";
249 :     my $infos = "";
250 :     my $menus = "";
251 :     my $highlights = "";
252 :     my ($titles_array, $infos_array, $menus_array, $highlights_array);
253 :     {
254 :     $titles_array = $params->{popup_menu}->{titles} || $empty_array;
255 :     $infos_array = $params->{popup_menu}->{infos} || $empty_array;
256 :     $menus_array = $params->{popup_menu}->{menus} || $empty_array;
257 :     $highlights_array = $params->{highlights} || $empty_array;
258 :    
259 :     my $rows = [];
260 :     foreach my $row (@$titles_array) {
261 :     push(@$rows, join("^", @$row));
262 :     }
263 :     $titles = join("~", @$rows);
264 :    
265 :     $rows = [];
266 :     foreach my $row (@$infos_array) {
267 :     push(@$rows, join("^", @$row));
268 :     }
269 :     $infos = join("~", @$rows);
270 :    
271 :     $rows = [];
272 :     foreach my $row (@$menus_array) {
273 :     push(@$rows, join("^", @$row));
274 :     }
275 :     $menus = join("~", @$rows);
276 :    
277 :     $rows = [];
278 :     foreach my $row (@$highlights_array) {
279 :     push(@$rows, join("^", @$row));
280 :     }
281 :     $highlights = join("~", @$rows);
282 :    
283 :     # check for unwanted symbols
284 :     $infos =~ s/\n//g;
285 :     $infos =~ s/'/&quot;/g;
286 :     $infos =~ s/"/\\"/g;
287 :     $menus =~ s/\n//g;
288 :     $menus =~ s/'/&quot;/g;
289 :     $menus =~ s/"/\\"/g;
290 :     }
291 :    
292 :     # create stringified data
293 :     my $rows = [];
294 :     foreach my $row (@data) {
295 :     my $quoted_row;
296 :     foreach my $cell (@$row) {
297 :     $cell =~ s/\^/ /g;
298 :     $cell =~ s/\~/ /g;
299 :     push(@$quoted_row, $cell);
300 :     }
301 :     push(@$rows, join("^", @$quoted_row));
302 :     }
303 :     my $data_source = join("~", @$rows);
304 :    
305 :     $data_source =~ s/'/\@1/g;
306 :     $data_source =~ s/"/\@2/g;
307 :    
308 :     # insert hidden fields
309 :     $table .= "\n<input type='hidden' id='table_data_" . $id . "' value='" . $data_source . "'>\n";
310 :     $table .= "<input type='hidden' id='table_onclicks_" . $id . "' value='" . $onclicks . "'>\n";
311 :     $table .= "<input type='hidden' id='table_titles_" . $id . "' value='" . $titles . "'>\n";
312 :     $table .= "<input type='hidden' id='table_infos_" . $id . "' value='" . $infos . "'>\n";
313 :     $table .= "<input type='hidden' id='table_menus_" . $id . "' value='" . $menus . "'>\n";
314 :     $table .= "<input type='hidden' id='table_highlights_" . $id . "' value='" . $highlights . "'>\n";
315 :     $table .= "<input type='hidden' id='table_filtereddata_" . $id . "' value=''>\n";
316 :     $table .= "<input type='hidden' id='table_rows_" . $id . "' value='" . $total . "'>\n";
317 :     $table .= "<input type='hidden' id='table_cols_" . $id . "' value='" . scalar(@columns) . "'>\n";
318 :     $table .= "<input type='hidden' id='table_start_" . $id . "' value='0'>\n";
319 :     $table .= "<input type='hidden' id='table_sortdirection_" . $id . "' value='up'>\n";
320 :    
321 :     # check for title
322 :     if (defined($params->{title})) {
323 :     $table .= "<p class='table_title'>" . $params->{title} . "</p>\n";
324 :     }
325 :    
326 :     # check if table width was passed
327 :     my $table_width = "";
328 :     if (defined($params->{table_width})) {
329 :     $table_width = "width: " . $params->{table_width} . "px;";
330 :     }
331 :    
332 :     # check for display options - select entries per page
333 :     if ($show_perpage) {
334 :     $table .= "<table class='table_table' style='$table_width'>\n<tr><td align=center><span class='table_perpage'>display&nbsp;<input type='text' id='table_perpage_" . $id . "' name='table_perpage_" . $id . "' size='3' value='" . $perpage . "' onkeypress='check_submit_filter(event, \"" . $id . "\")'>&nbsp;items per page</span></td></tr>\n";
335 :     } elsif ($perpage == -1) {
336 :     $table .= "<input type='hidden' id='table_perpage_" . $id . "' name='table_perpage_" . $id . "' value='" . scalar(@data) . "' >\n<table class='table_table' style='$table_width'>\n";
337 :     } else {
338 :     $table .= "<input type='hidden' id='table_perpage_" . $id . "' name='table_perpage_" . $id . "' value='" . $perpage . "' >\n<table class='table_table' style='$table_width'>\n";
339 :     }
340 :    
341 :     # check for display options - display browsing element at the top
342 :     if ($show_topbrowse) {
343 :     $table .= get_browse($params);
344 :     }
345 :    
346 :     # start data table
347 :     $table .= "<tr><td><table id='table_" . $id . "' class='table_table' style='$table_width'>";
348 :    
349 :     # check for display options - display column filters
350 :     if ($complex_filter) {
351 :     if (scalar(keys(%operands)) > 0) {
352 :     $table .= "<tr>";
353 :    
354 :     # check each value for an existing filter field
355 :     my $i = 0;
356 :     foreach my $col (@columns) {
357 :     if (exists($operands{$col})) {
358 :    
359 :     # check if there is a preselected value for this filter field
360 :     my $preselected_operand = "";
361 :     if (defined($operands{$col})) {
362 :     $preselected_operand = $operands{$col};
363 :     }
364 :    
365 :     # check for filter comparison operator
366 :     $table .= "<td><select name='" . $id . "_" . $col . "_operator' id='table_" . $id . "_operator_" . $i . "'>";
367 :    
368 :     foreach my $operator (@available_operators) {
369 :     my $preselected_operator = "";
370 :    
371 :     if (defined($preselected_operators{$col})) {
372 :     if ($preselected_operators{$col} eq $operator->[0]) {
373 :     $preselected_operator = " selected='selected'";
374 :     }
375 :     }
376 :    
377 :     $table .= "<option value='" . $operator->[0] . "'" . $preselected_operator . ">" . $operator->[1] . "</option>";
378 :     }
379 :     $table .= "</select><input type='text' name='" . $id . $col . "' class='filter_item' value='" . $preselected_operand . "' id='table_" . $id . "_operand_" . $i . "' onkeypress='check_submit_filter(event, \"" . $id . "\")'></td>";
380 :     } else {
381 :     $table .= "<td></td>";
382 :     }
383 :     $i++;
384 :     }
385 :    
386 :     $table .= "</tr>";
387 :     }
388 :     }
389 :    
390 :     # write table header
391 :     $table .= "<tr>";
392 :     my $i = 1;
393 :    
394 :     # check column widths
395 :     my $colwidths;
396 :    
397 :     # iterate through columns
398 :     foreach my $col (@columns) {
399 :     if ($column_widths->[$i - 1] ne -1) {
400 :     push(@$colwidths, "width: " . $column_widths->[$i - 1] . "px;");
401 :     } else {
402 :     push(@$colwidths, "");
403 :     }
404 :    
405 :     # count up column name
406 :     my $name = $id . "_col_" . $i;
407 :    
408 :     # prepare html for sorting according to column
409 :     my $order_img = "<a href='javascript: table_sort(\"" . $id . "\", \"" . $i . "\", \"ASC\");' class='table_first_row' title='Click to sort'>";
410 :    
411 :     # create config button
412 :     my $conf_button = qq~<span style="background-color: white; border: 1px solid black; font-size: 7pt; padding: 1px; cursor: pointer; width: 13px; vertical-align: top; text-align: left;" class="hideme" onclick="change_group('$id\_col_$i');" id="$id\_col_$i" name="conf">+/-</span>~;
413 :    
414 :     # check for simple filter
415 :     my $filter = "";
416 :     if (defined($operands{$col})) {
417 :     $filter = "<br/><input type=hidden name='" . $id . "_" . $col . "_operator' value='like' id='table_" . $id . "_operator_" . $i . "'><input type='text' name='" . $id . $col . "' class='filter_item' value='' size=5 id='table_" . $id . "_operand_" . $i . "' onkeypress='check_submit_filter(event, \"" . $id . "\")' style='width: 100%;' title='Enter Search Text'>";
418 :     }
419 :    
420 :     # check if colum header click should sort
421 :     if ($sortable) {
422 :     $table .= "<td name='" . $name . "' class='table_first_row' style='" . $colwidths->[$i - 1] . "'>" . $conf_button . $order_img . $col . "&nbsp;<img src='./Html/up-arrow.gif'><img src='./Html/down-arrow.gif'></a>" . $filter . "</td>";
423 :     } else {
424 :     $table .= "<td name='" . $name . "' class='table_first_row' style='" . $colwidths->[$i - 1] . "'>" . $conf_button . $col . $filter . "</td>";
425 :     }
426 :    
427 :     # increase column counter
428 :     $i ++;
429 :     }
430 :     $table .= "</tr>";
431 :    
432 :     # end data table
433 :     $table .= "</table></td></tr>";
434 :    
435 :     # check for display options - display browse element at the bottom
436 :     if ($show_bottombrowse) {
437 :     $table .= get_browse($params);
438 :     }
439 :    
440 :     # end surrounding table
441 :     $table .= "</table>";
442 :    
443 :     # include image for table initialization
444 :     $table .= "<img src='" . $img_path . "clear.gif' onload='initialize_table(\"" . $id . "\")'>";
445 :    
446 :     return $table;
447 :     }
448 :    
449 :     # get the browsing html
450 :     sub get_browse {
451 :     my ($params) = @_;
452 :    
453 :     my $browse = "";
454 :     my $left = "";
455 :     my $right = "";
456 :     my $offset = $params->{offset} || 1;
457 :     my $id = $params->{id} || "table";
458 :     my $perpage = $params->{perpage} || 0;
459 :     my $total = $params->{total} || 2;
460 :    
461 :     if ($offset > 0) {
462 :     $left .= "<a href='javascript: table_first(\"" . $id . "\");' name='table_first_" . $id . "'>&laquo;first</a>&nbsp;&nbsp;<a href='javascript: table_prev(\"" . $id . "\");' name='table_prev_" . $id . "'>&laquo;prev</a>";
463 :     }
464 :    
465 :     if (($offset + $perpage) < $total) {
466 :     $right = "<a href='javascript: table_next(\"" . $id . "\");' name='table_next_" . $id . "'>next&raquo;</a>&nbsp;&nbsp;<a href='javascript: table_last(\"" . $id . "\");' name='table_last_" . $id . "'>last&raquo;</a>";
467 :     }
468 :    
469 :     my $to = $offset + $perpage;
470 :     if (($offset + $perpage) > $total) {
471 :     $to = $total;
472 :     }
473 :    
474 :     $browse .= "<tr><td><table class='table_browse'><tr><td align='left' width='20%'>" . $left . "</td><td align='center' width='60%'>displaying <input type='text' name='table_start_" . $id . "' class='disp' readonly=1 value='" . ($offset + 1) . "'> - <input type='text' name='table_stop_" . $id . "' class='disp' readonly=1 value='" . $to . "'> of <input type='text' name='table_total_" . $id . "' class='disp' readonly=1 value='" . $total . "'></td><td align='right' width='20%'>" . $right . "</td></tr></table></td></tr>";
475 :    
476 :     return $browse;
477 :     }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3