AcePerl

 view release on metacpan or  search on metacpan

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


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));
  my $c = Configuration();
  my $icons  = $c->Icons || '/ico';
  my $spacer = "$icons/". SPACER_ICON;
  my $left   = "$icons/". LEFT_ICON;
  my $right  = "$icons/". RIGHT_ICON;
  my $url    = url(-absolute=>1,-query=>1);
  #  my $url    = self_url();
  push(@buttons,td({-align=>'RIGHT',-valign=>'MIDDLE'},
		   $offset > 0 
		               ? a({-href=>$url
                                  . '&scroll=-' . MAXOBJECTS},
				      img({-src=>$left,-alt=>'< PREVIOUS',-border=>0}))
                               : img({-src=>$spacer,-alt=>''})
		   )
      );

  my $p = 1;
  while ($pages/$p > 25) { $p++; }
  my (@v,%v);
  for (my $i=1;$i<=$pages;$i++) {
    next unless ($i == $page) or (($i-1) % $p == 0);
    my $s = ($i - $page) * MAXOBJECTS;
    push(@v,$s);
    $v{$s}=$i;
  }
  my @hidden;
  Delete('scroll');
  Delete('Go');
  foreach (param()) {
    push(@hidden,hidden(-name=>$_,-value=>[param($_)]));
  }



( run in 0.856 second using v1.01-cache-2.11-cpan-140bd7fdf52 )