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

Annotation of /FortyEight/Table.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (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 : paczian 1.3 =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 : paczian 1.1 # 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 : paczian 1.3 my $group_col = $params->{group_col} || 0;
202 :     my $enable_grouping = $params->{enable_grouping} || 0;
203 : paczian 1.1 my $sortable = $params->{sortable} || 0;
204 : paczian 1.2 my $sortcols = $params->{sortcols} || { 'all' => 1 };
205 : paczian 1.1 my $column_widths = $params->{column_widths};
206 :     unless (defined($column_widths)) {
207 :     foreach (@columns) {
208 :     push(@$column_widths, -1);
209 :     }
210 :     }
211 :     for (my $i=0; $i<scalar(@columns); $i++) {
212 :     unless (defined($column_widths->[$i])) {
213 :     $column_widths->[$i] = -1;
214 :     }
215 :     }
216 :    
217 :     # create an empty data array
218 :     my $empty_array;
219 :     my @good_data;
220 :     foreach my $row (@data) {
221 :     my $empty_row;
222 :     my $good_row;
223 :     foreach my $cell (@$row) {
224 :     if (defined($cell)) {
225 :     push(@$good_row, $cell);
226 :     } else {
227 :     push(@$good_row, "");
228 :     }
229 :     push(@$empty_row, "");
230 :     }
231 :     push(@$empty_array, $empty_row);
232 :     push(@good_data, $good_row);
233 :     }
234 :     @data = @good_data;
235 :     @good_data = undef;
236 :    
237 :     # create onclick event string
238 :     my $onclicks = "";
239 :     {
240 :     my $onclicks_array = $params->{onclicks} || $empty_array;
241 :     my $rows = [];
242 :     foreach my $row (@$onclicks_array) {
243 :     push(@$rows, join("^", @$row));
244 :     }
245 :     $onclicks = join("~", @$rows);
246 :     }
247 :    
248 :     # create popup menu strings if requested
249 :     my $titles = "";
250 :     my $infos = "";
251 :     my $menus = "";
252 :     my $highlights = "";
253 :     my ($titles_array, $infos_array, $menus_array, $highlights_array);
254 :     {
255 :     $titles_array = $params->{popup_menu}->{titles} || $empty_array;
256 :     $infos_array = $params->{popup_menu}->{infos} || $empty_array;
257 :     $menus_array = $params->{popup_menu}->{menus} || $empty_array;
258 :     $highlights_array = $params->{highlights} || $empty_array;
259 :    
260 :     my $rows = [];
261 :     foreach my $row (@$titles_array) {
262 :     push(@$rows, join("^", @$row));
263 :     }
264 :     $titles = join("~", @$rows);
265 :    
266 :     $rows = [];
267 :     foreach my $row (@$infos_array) {
268 :     push(@$rows, join("^", @$row));
269 :     }
270 :     $infos = join("~", @$rows);
271 :    
272 :     $rows = [];
273 :     foreach my $row (@$menus_array) {
274 :     push(@$rows, join("^", @$row));
275 :     }
276 :     $menus = join("~", @$rows);
277 :    
278 :     $rows = [];
279 :     foreach my $row (@$highlights_array) {
280 :     push(@$rows, join("^", @$row));
281 :     }
282 :     $highlights = join("~", @$rows);
283 :    
284 :     # check for unwanted symbols
285 :     $infos =~ s/\n//g;
286 :     $infos =~ s/'/&quot;/g;
287 :     $infos =~ s/"/\\"/g;
288 :     $menus =~ s/\n//g;
289 :     $menus =~ s/'/&quot;/g;
290 :     $menus =~ s/"/\\"/g;
291 :     }
292 :    
293 :     # create stringified data
294 :     my $rows = [];
295 :     foreach my $row (@data) {
296 :     my $quoted_row;
297 :     foreach my $cell (@$row) {
298 :     $cell =~ s/\^/ /g;
299 :     $cell =~ s/\~/ /g;
300 :     push(@$quoted_row, $cell);
301 :     }
302 :     push(@$rows, join("^", @$quoted_row));
303 :     }
304 :     my $data_source = join("~", @$rows);
305 :    
306 :     $data_source =~ s/'/\@1/g;
307 :     $data_source =~ s/"/\@2/g;
308 :    
309 :     # insert hidden fields
310 :     $table .= "\n<input type='hidden' id='table_data_" . $id . "' value='" . $data_source . "'>\n";
311 :     $table .= "<input type='hidden' id='table_onclicks_" . $id . "' value='" . $onclicks . "'>\n";
312 :     $table .= "<input type='hidden' id='table_titles_" . $id . "' value='" . $titles . "'>\n";
313 :     $table .= "<input type='hidden' id='table_infos_" . $id . "' value='" . $infos . "'>\n";
314 :     $table .= "<input type='hidden' id='table_menus_" . $id . "' value='" . $menus . "'>\n";
315 :     $table .= "<input type='hidden' id='table_highlights_" . $id . "' value='" . $highlights . "'>\n";
316 :     $table .= "<input type='hidden' id='table_filtereddata_" . $id . "' value=''>\n";
317 :     $table .= "<input type='hidden' id='table_rows_" . $id . "' value='" . $total . "'>\n";
318 :     $table .= "<input type='hidden' id='table_cols_" . $id . "' value='" . scalar(@columns) . "'>\n";
319 :     $table .= "<input type='hidden' id='table_start_" . $id . "' value='0'>\n";
320 :     $table .= "<input type='hidden' id='table_sortdirection_" . $id . "' value='up'>\n";
321 :    
322 :     # check for title
323 :     if (defined($params->{title})) {
324 :     $table .= "<p class='table_title'>" . $params->{title} . "</p>\n";
325 :     }
326 :    
327 :     # check if table width was passed
328 :     my $table_width = "";
329 :     if (defined($params->{table_width})) {
330 :     $table_width = "width: " . $params->{table_width} . "px;";
331 :     }
332 :    
333 :     # check for display options - select entries per page
334 :     if ($show_perpage) {
335 :     $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";
336 :     } elsif ($perpage == -1) {
337 :     $table .= "<input type='hidden' id='table_perpage_" . $id . "' name='table_perpage_" . $id . "' value='" . scalar(@data) . "' >\n<table class='table_table' style='$table_width'>\n";
338 :     } else {
339 :     $table .= "<input type='hidden' id='table_perpage_" . $id . "' name='table_perpage_" . $id . "' value='" . $perpage . "' >\n<table class='table_table' style='$table_width'>\n";
340 :     }
341 :    
342 :     # check for display options - display browsing element at the top
343 :     if ($show_topbrowse) {
344 :     $table .= get_browse($params);
345 :     }
346 :    
347 :     # start data table
348 :     $table .= "<tr><td><table id='table_" . $id . "' class='table_table' style='$table_width'>";
349 :    
350 : paczian 1.3 # check for display options - display column filters
351 :     if ($complex_filter) {
352 :     if (scalar(keys(%operands)) > 0) {
353 :     $table .= "<tr>";
354 :    
355 :     # check each value for an existing filter field
356 :     my $i = 0;
357 :     foreach my $col (@columns) {
358 :     if (exists($operands{$col})) {
359 :    
360 :     # check if there is a preselected value for this filter field
361 :     my $preselected_operand = "";
362 :     if (defined($operands{$col})) {
363 :     $preselected_operand = $operands{$col};
364 :     }
365 :    
366 :     # check for filter comparison operator
367 :     $table .= "<td><select name='" . $id . "_" . $col . "_operator' id='table_" . $id . "_operator_" . $i . "'>";
368 :    
369 :     foreach my $operator (@available_operators) {
370 :     my $preselected_operator = "";
371 :    
372 :     if (defined($preselected_operators{$col})) {
373 :     if ($preselected_operators{$col} eq $operator->[0]) {
374 :     $preselected_operator = " selected='selected'";
375 :     }
376 :     }
377 :    
378 :     $table .= "<option value='" . $operator->[0] . "'" . $preselected_operator . ">" . $operator->[1] . "</option>";
379 :     }
380 :     $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>";
381 :     } else {
382 :     $table .= "<td></td>";
383 :     }
384 :     $i++;
385 :     }
386 :    
387 :     $table .= "</tr>";
388 :     }
389 :     }
390 :    
391 : paczian 1.1 # write table header
392 :     $table .= "<tr>";
393 :     my $i = 1;
394 :    
395 :     # check column widths
396 :     my $colwidths;
397 :    
398 :     # iterate through columns
399 :     foreach my $col (@columns) {
400 :     if ($column_widths->[$i - 1] ne -1) {
401 :     push(@$colwidths, "width: " . $column_widths->[$i - 1] . "px;");
402 :     } else {
403 :     push(@$colwidths, "");
404 :     }
405 :    
406 :     # count up column name
407 :     my $name = $id . "_col_" . $i;
408 :    
409 :     # prepare html for sorting according to column
410 :     my $order_img = "<a href='javascript: table_sort(\"" . $id . "\", \"" . $i . "\", \"ASC\");' class='table_first_row' title='Click to sort'>";
411 :    
412 : paczian 1.3 # create config button
413 :     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>~;
414 : paczian 1.1
415 :     # check for simple filter
416 :     my $filter = "";
417 :     if (defined($operands{$col})) {
418 : paczian 1.3 $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'>";
419 : paczian 1.1 }
420 :    
421 :     # check if colum header click should sort
422 : paczian 1.3 if (($sortable) && ($sortcols->{$i} || $sortcols->{'all'})) {
423 :     $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>";
424 : paczian 1.1 } else {
425 : paczian 1.3 $table .= "<td name='" . $name . "' class='table_first_row' style='" . $colwidths->[$i - 1] . "'>" . $conf_button . $col . $filter . "</td>";
426 : paczian 1.1 }
427 :    
428 :     # increase column counter
429 :     $i ++;
430 :     }
431 :     $table .= "</tr>";
432 :    
433 :     # end data table
434 :     $table .= "</table></td></tr>";
435 :    
436 :     # check for display options - display browse element at the bottom
437 :     if ($show_bottombrowse) {
438 :     $table .= get_browse($params);
439 :     }
440 :    
441 :     # end surrounding table
442 :     $table .= "</table>";
443 :    
444 :     # include image for table initialization
445 :     $table .= "<img src='" . $img_path . "clear.gif' onload='initialize_table(\"" . $id . "\")'>";
446 :    
447 :     return $table;
448 :     }
449 :    
450 :     # get the browsing html
451 :     sub get_browse {
452 :     my ($params) = @_;
453 :    
454 :     my $browse = "";
455 :     my $left = "";
456 :     my $right = "";
457 :     my $offset = $params->{offset} || 1;
458 :     my $id = $params->{id} || "table";
459 :     my $perpage = $params->{perpage} || 0;
460 :     my $total = $params->{total} || 2;
461 :    
462 :     if ($offset > 0) {
463 :     $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>";
464 :     }
465 :    
466 :     if (($offset + $perpage) < $total) {
467 :     $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>";
468 :     }
469 :    
470 :     my $to = $offset + $perpage;
471 :     if (($offset + $perpage) > $total) {
472 :     $to = $total;
473 :     }
474 :    
475 : paczian 1.3 $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>";
476 : paczian 1.1
477 :     return $browse;
478 :     }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3