AcePerl

 view release on metacpan or  search on metacpan

Ace/Browser/SearchSubs.pm  view on Meta::CPAN

@EXPORT = qw(
	     MAXOBJECTS
	     SEARCH_ICON
	     AceSearchTable AceResultsTable AceSearchOffset
	     DisplayInstructions
	    );

# ----- constants used by the pattern search script ------
use constant ROWS           => 10;    # how many rows to allocate for search results
use constant COLS           =>  5;    #  "   "   columns   "       "    "      "
use constant MAXOBJECTS     => ROWS * COLS;  # total objects per screen
use constant ICONS          => '/ico';
use constant SEARCH_ICON    => '/ico/search.gif';
use constant SPACER_ICON    => 'spacer.gif';
use constant LEFT_ICON      => 'cylarrw.gif';
use constant RIGHT_ICON     => 'cyrarrw.gif';

=item $offset = AceSearchOffset()

When the user is paging back and forth among a multi-page list of
results, this function returns the index of the first item to display.

=cut

sub AceSearchOffset {
  my $offset = param('offset') || 0;
  $offset += param('scroll') if param('scroll');
  $offset;
}

=item AceSearchTable([{hash}],$title,@contents)

Given a title and the HTML contents, this formats the search into a
table and gives it the background and foreground colors used elsewhere
for searches.  The formatted search is then printed.

The HTML contents are usually a fill-out form.  For convenience, you
can provide the contents in multiple parts (lines or elements) and
they will be concatenated together.

If the first argument is a hashref, then its contents will be passed
to start_form() to override the form arguments.

=cut

sub AceSearchTable {
  my %attributes = %{shift()} if ref($_[0]) eq 'HASH';
  my ($title,@body) = @_;
  print
    start_form(-action=>url(-absolute=>1,-path_info=>1).'#results',%attributes),
    a({-name=>'search'},''),
    table({-border=>0,-width=>'100%'},
	  TR({-valign=>'MIDDLE'},
	     td({-class=>'searchbody'},@body))),
    end_form;
}

=item AceResultsTable($objects,$count,$offset,$title)

This subroutine formats the results of a search into a pageable list
and prints out the resulting HTML.  The following arguments are required:

 $objects   An array reference containing the objects to place in the
            table.

 $count     The total number of objects.

 $offset    The offset into the array, as returned by AceSearchOffset()

 $title     A title for the table.

The array reference should contain no more than MAXOBJECTS objects.
The AceDB query should be arranged in such a way that this is the
case.  A typical idiom is the following:

  my $offset = AceSearchOffset();
  my $query  = param('query');
  my $count;
  my @objs = $db->fetch(-query=> $query,
			-count  => MAXOBJECTS,
			-offset => $offset,
			-total => \$count
		       );
  AceResultsTable(\@objs,$count,$offset,'Here are the results');

=cut

sub AceResultsTable {
  my ($objects,$count,$offset,$title) = @_;
  Delete('scroll');
  param(-name=>'offset',-value=>$offset);
  my @cheaders = map { $offset + ROWS * $_ } (0..(@$objects-1)/ROWS) if @$objects;
  my @rheaders = (1..min(ROWS,$count));

  $title ||= 'Search Results';

  print 
    a({-name=>'results'},''),
    start_table({-border=>0,-cellspacing=>2,-cellpadding=>2,-width=>'100%',-align=>'CENTER',-class=>'resultsbody'}),
    TR(th({-class=>'resultstitle'},$title));
  unless (@$objects) {
    print end_table,p();
    return;
  }

  print start_Tr,start_td;

  my $need_navbar = $offset > 0 || $count >= MAXOBJECTS;
  my @buttons = make_navigation_bar($offset,$count) if $need_navbar;

  print table({-width=>'50%',-align=>'CENTER'},Tr(@buttons)) if $need_navbar;
  print table({-width=>'100%'},tableize(ROWS,COLS,\@rheaders,\@cheaders,@$objects));

  print end_td,end_Tr,end_table,p();
}

# ------ ugly internal routines for scrolling along the search results list -----
sub make_navigation_bar {
  my($offset,$count) = @_;
  my (@buttons);
  my ($page,$pages) =  (1+int($offset/MAXOBJECTS),1+int($count/MAXOBJECTS));



( run in 0.569 second using v1.01-cache-2.11-cpan-d8267643d1d )