AcePerl

 view release on metacpan or  search on metacpan

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

  DoRedirect
  GetAceObject
  Object2URL
  ObjectLink
  OpenDatabase
  PrintTop
  PrintBottom
  Url

The following subroutines are exported if explicitly requested:

  AceAddCookie
  AceInit
  AceHeader
  AceMultipleChoices
  AceRedirect
  DB_Name
  Footer
  Header
  ResolveUrl
  Style
  Toggle
  TypeSelector

To load the default subroutines load the module with:

   use Ace::Browser::AceSubs;

To bring in a set of optionally routines, load the module with:

   use Ace::Browser::AceSubs qw(AceInit AceRedirect);

To bring in all the default subroutines, plus some of the optional
ones:

   use Ace::Browser::AceSubs qw(:DEFAULT AceInit AceRedirect);

There are two main types of AceBrowser scripts:

=over 4

=item display scripts

These are called with the CGI parameters b<name> and b<class>,
corresponding to the name and class of an AceDB object to display.
The subroutine GetAceObject() will return the requested object, or
undef if the object does not exist.

To retrieve the parameters, use the CGI.pm param() method:

  $name  = param('name');
  $class = param('class');


=item search scripts

These are not called with any CGI parameters on their first
invocation, but can define their own parameter lists by creating
fill-out forms.  The AceBrowser system remembers the last search
performed by a search script in a cookie and regenerates the CGI
parameters the next time the user selects that search script.

=back

=head1 SUBROUTINES

The following sections describe the exported subroutines.

=over 4

=cut

use strict;
use Ace::Browser::SiteDefs;
use Ace 1.76;
use CGI qw(:standard escape);
use CGI::Cookie;
use File::Path 'mkpath';

use vars qw/@ISA @EXPORT @EXPORT_OK $VERSION %EXPORT_TAGS 
  %DB %OPEN $HEADER $TOP @COOKIES
  $APACHE_CONF/;

require Exporter;
@ISA = qw(Exporter);
$VERSION = 1.21;

######################### This is the list of exported subroutines #######################
@EXPORT = qw(
	     GetAceObject AceError AceNotFound AceMissing DoRedirect
	     OpenDatabase Object2URL Url
	     ObjectLink Configuration PrintTop PrintBottom);
@EXPORT_OK = qw(AceRedirect Toggle ResolveUrl AceInit AceAddCookie
		AceHeader TypeSelector Style AcePicRoot
		Header Footer DB_Name AceMultipleChoices);
%EXPORT_TAGS = ( );

use constant DEFAULT_DATABASE  => 'default';
use constant PRIVACY           => 'misc/privacy';  # privacy/cookie statement
use constant SEARCH_BROWSE     => 'search';   # a fallback search script
my %VALID;  # cache for get_symbolic() lookups

=item AceError($message)

This subroutine will print out an error message and exit the script.
The text of the message is taken from $message.

=cut

sub AceError {
    my $msg = shift;
    PrintTop(undef,undef,'Error');
    print CGI::font({-color=>'red'},$msg);
    PrintBottom();
    Apache->exit(0) if defined &Apache::exit;
    exit(0);
}

=item AceHeader()

This function prints the HTTP header and issues a number of cookies

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

                                       : qq(<A HREF="http://stein.cshl.org/AcePerl">AcePerl Home Page</A>);


  return <<END;
<TABLE WIDTH="100%" BORDER=0 CELLPADDING=0 CELLSPACING=0>
<TR CLASS="technicalinfo">
    <TD  CLASS="small" VALIGN="TOP">
    $hlink<br>$clink
    </TD>
    <TD  CLASS="small" ALIGN=RIGHT VALIGN=TOP><p><strong>$feedback_link</strong><br>
    $privacy_link<br>
    <A HREF="mailto:$webmaster"><address>$webmaster</address></A><br>
    </TD>
</TR>
</TABLE>
END
}

=item $object = GetAceObject()

This function is called by display scripts to return the
Ace::Object.that the user wishes to view.  It automatically opens or
refreshes the database, and performs the request using the values of the
"name" and "class" CGI variables.

If a single object is found, the function returns it as the function
result.  If no objects are found, it returns undef.  If more than one
object is found, the function invokes AceMultipleChoices() and exits
the script.

=cut

# open database, return object requested by CGI parameters
sub GetAceObject {
  my $db = OpenDatabase() ||  AceError("Couldn't open database."); # exits
  my $name  = param('name') or return;
  my $class = param('class') or return;
  my @objs = $db->fetch($class => $name);
  if (@objs > 1) {
    AceMultipleChoices($name,'',\@objs);
    Apache->exit(0) if defined &Apache::exit;
    exit(0);
  }
  return $objs[0];
}

=item $html = Header()

This subroutine returns the boilerplate at the top of the HTML page as 
a string, but does not print it out.  It is not exported by default.

=cut

sub Header {
  my $config = Configuration();
  my $dbname = get_symbolic();

  return unless my $searches = $config->Searches;
  my $banner                 = $config->Banner;

  # next select the correct search script
  my @searches = @{$searches};
  my $self = url(-relative=>1);
  my $modperl = $ENV{MOD_PERL} && Apache->can('request') && eval {Apache->request->dir_config('AceBrowserConf')};
  my @row;
  foreach (@searches) {
    my ($name,$url,$on,$off,$size) = @{$config->searches($_)}{qw/name url onimage
								offimage size/};
    my $active = $url =~ /\b$self\b/;
    my $image = $active ? $on : $off;

    # replace the url with a cookie, if one is defined
    my $cookie_name = "SEARCH_${dbname}_${_}";
    my $query_string = cookie($cookie_name) unless /blast/;
    $url .= "/$dbname" unless $url =~ /\b$dbname\b/ or $modperl;
    $url .= "?$query_string" if $query_string;

    if ($image) {
    push @row,a({-href=>$url},img({-src=>$image,-border=>0,
				   -width=>$size->[0],-height=>$size->[1],
				   -alt=>$name}));

  } else {
    push @row,$active ? font({-color=>'black'},$name) : a({-href=>$url,-class=>'searchbanner'},$name);
  }
  }

  my ($home,$label) = @{$config->Home} if $config->Home;

  return table({-border=>0,-cellspacing=>1,-width=>'100%'},
	       Tr(td({-align=>'CENTER',-class=>'searchbanner'},\@row)),
	       Tr(td({-align=>'CENTER',-valign=>'BOTTOM',colspan=>scalar(@row)},
		     a({-href=>$home},$banner))
		 )
	      );
}

=item $url = Object2URL($object)

=item $url = Object2URL($name,$class)

In its single-argument form, this function takes an AceDB Object and
returns an AceBrowser URL.  The URL chosen is determined by the
configuration settings.

It is also possible to pass Object2URL an object name and class, in
the case that an AceDB object isn't available.

The return value is a URL.

=cut

# general mapping from a display to a url
sub Object2URL {
    my ($object,$extra) = @_;
    my ($name,$class);
    if (ref($object)) {
	($name,$class) = ($object,$object->class);
    } else {
	($name,$class) = ($object,$extra);
    }

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


Example:

  my $author = $db->fetch(Author => 'Sulston JE');
  print ObjectLink($author,$author->Full_name);

This will print out a link to a page that will display details on the
author page.  The text of the link will be the value of the Full_name
tag.

=cut

sub ObjectLink {
  my $object     = shift;
  my $link_text  = shift;
  my $target     = shift;
  my $url = Object2URL($object,@_) or return ($link_text || "$object");
  my @targ = $target ? (-target=>$target) : ();
  return a({-href=>Object2URL($object,@_),-name=>"$object",@targ},($link_text || "$object"));
}

=item $db = OpenDatabase()

This function opens the Acedb database designated by the configuration
file.  In modperl environments, this function caches database handles
and reuses them, pinging and reopening them in the case of timeouts.

This function is not exported by default.

=cut

use Carp 'cluck';

################ open a database #################
sub OpenDatabase {
  my $name = shift || get_symbolic();
  AceInit();
  $name =~ s!/$!!;
  my $db = $DB{$name};
  return $db if $db && $db->ping;

  my ($host,$port,$user,$password,
      $cache_root,$cache_size,$cache_expires,$auto_purge_interval)
    = getDatabasePorts($name);
  my @auth  = (-user=>$user,-pass=>$password) if $user && $password;
  my @cache = (-cache => { cache_root=>$cache_root,
			   max_size            => $cache_size || $Cache::SizeAwareCache::NO_MAX_SIZE || -1,  # hardcoded $NO_MAX_SIZE constant
			   default_expires_in  => $cache_expires       || '1 day',
			   auto_purge_interval => $auto_purge_interval || '6 hours',
			 } 
	      ) if $cache_root;
  $DB{$name} = Ace->connect(-host=>$host,-port=>$port,-timeout=>50,@auth,@cache);
  return $DB{$name};
}

=item PrintTop($object,$class,$title,@html_headers)

The PrintTop() function generates all the boilerplate at the top of a
typical AceBrowser page, including the HTTP header information, the
page title, the navigation bar for searches, the web site banner, the
type selector for choosing alternative displays, and a level-one
header.

Call it with one or more arguments.  The arguments are:

  $object    An AceDB object.  The navigation bar and title will be
	     customized for the object.

  $class     If no AceDB object is available, then you can pass 
	     a string containing the AceDB class that this page is
	     designed to display.

  $title     A title to use for the HTML page and the first level-one
	     header.  If not provided, a generic title "Report for
	     Object" is generated.

  @html_headers  Additional HTML headers to pass to the the CGI.pm
             start_html. 
	

=cut

# boilerplate for the top of the page
sub PrintTop {
  my ($object,$class,$title,@additional_header_stuff) = @_;
  return if $TOP++;
  $class = $object->class if defined $object && ref($object);
  $class ||= param('class') unless defined($title);
  AceHeader();
  $title ||= defined($object) ? "$class Report for: $object" : $class ? "$class Report" : ''
    unless defined($title);
  print start_html (
                    '-Title'   => $title,
                    '-Style'   => Style(),
                    @additional_header_stuff,
                    );
  print Header();
  print TypeSelector($object,$class) if defined $object;
  print h1($title) if $title;
}

=item PrintBottom()

The PrintBottom() function outputs all the boilerplate at the bottom
of a typical AceBrowser page.  If a user-defined footer is present in
the configuration file, that is printed.  Otherwise, the method prints 
a horizontal rule followed by links to the site home page, the AcePerl 
home page, the privacy policy, and the feedback page.

=cut

sub PrintBottom {
  print hr,Footer(),end_html();
}


=item $hashref = Style()

This subroutine returns a hashref containing a reference to the
configured stylesheet, in the following format:

  { -src => '/ace/stylesheets/current_stylesheet.css' }

This hash is suitable for passing to the -style argument of CGI.pm's
start_html() function, or for use as an additional header in
PrintTop().  You may add locally-defined stylesheet elements to the
hash before calling start_html().  See the pic script for an example
of how this is done this.

This function is not exported by default.

=cut

=item $url = ResolveUrl($url,$param)

Given a URL and a set of parameters, this function does the necessary
magic to add the symbolic database name to the end of the URL (if
needed) and then tack the parameters onto the end.

A typical call is:

  $url = ResolveUrl('/cgi-bin/ace/generic/tree','name=fred;class=Author');

This function is not exported by default.

=cut

sub ResolveUrl {
    my ($url,$param) = @_;
    my ($main,$query,$frag) = $url =~ /^([^?\#]+)\??([^\#]*)\#?(.*)$/ if defined $url;
    $main ||= '';
    
    if (!defined $APACHE_CONF) {
      $APACHE_CONF = eval { Apache->request->dir_config('AceBrowserConf') } ? 1 : 0;
    }

    $main = Configuration()->resolvePath($main) unless $main =~ m!^/!;
    if (my $id = get_symbolic()) {
      $main .= "/$id" unless $main =~ /$id/ or $APACHE_CONF;
    }

    $main .= "?$query" if $query; # put the query string back
    $main .= "?$param" if $param and !$query;
    $main .= ";$param" if $param and  $query;
    $main .= "#$frag" if $frag;
    return $main;
}

# A consistent stylesheet across pages
sub Style {
    my $stylesheet = Configuration()->Stylesheet;
    return { -src => $stylesheet };
}

=item $boolean = Toggle($section,[$label,$object_count,$add_plural,$add_count])

=item ($link,$bool) = Toggle($section,$label,$object_count,$add_plural,$add_count)

The Toggle() subroutine makes it easy to create HTML sections that
open and close when the user selects a toggle icon (a yellow
triangle).

Toggle() can be used to manage multiple collapsible HTML sections, but
each section must have a unique name.  The required first argument is
the section name.  Optional arguments are:

  $label         The text of the generated link, for example "sequence"

  $object_count  The number of objects that opening the section will reveal

  $add_plural    If true, the label will be pluralized when
		 appropriate

  $add_count	 If true, the label will have the object count added
		 when appropriate

In a scalar context, Toggle() prints the link HTML and returns a
boolean flag.  A true result indicates that the section is expanded
and should be generated.  A false result indicates that the section is 
collapsed.

In a list context, Toggle() returns a two-element list.  The first
element is the HTML link that expands and contracts the section.  The
second element is a boolean that indicates whether the section is
currently open or closed.

This example indicates typical usage:

  my $sequence = GetAceObject();
  print "sequence name = ",$sequence,"\n";
  print "sequence clone = ",$sequence->Clone,"\n";
  if (Toggle('dna','Sequence DNA')) {
      print $sequence->asDNA;
  }

An alternative way to do the same thing:

  my $sequence = GetAceObject();
  print "sequence name = ",$sequence,"\n";
  print "sequence clone = ",$sequence->Clone,"\n";
  my ($link,$open) = Toggle('dna','Sequence DNA');
  print $link;
  print $sequence->asDNA if $open;

=cut

# Toggle a subsection open and close
sub Toggle {
    my ($section,$label,$count,$addplural,$addcount,$max_open) = @_;
    $OPEN{$section}++ if defined($max_open) && $count <= $max_open;

    my %open = %OPEN;
    $label ||= $section;
    my $img;
    if (exists $open{$section}) {
	delete $open{$section};
	$img =  img({-src=>'/ico/triangle_down.gif',-alt=>'^',
			-height=>6,-width=>11,-border=>0}),
    } else {
	$open{$section}++;
	$img =  img({-src=>'/ico/triangle_right.gif',-alt=>'&gt;',
			-height=>11,-width=>6,-border=>0}),
	my $plural = ($addplural and $label !~ /s$/) ? "${label}s" : "$label";
	$label = font({-class=>'toggle'},!$addcount ? $plural : "$count $plural");
    }
    param(-name=>'open',-value=>join(' ',keys %open));
    my $url = url(-absolute=>1,-path_info=>1,-query=>1);

    my $link = a({-href=>"$url#$section",-name=>$section},$img.'&nbsp;'.$label);
    if (wantarray ){
      return ($link,$OPEN{$section})
    } else {
      print $link,br;
      return $OPEN{$section};
    }
}

=item $html = TypeSelector($name,$class)

This subroutine generates the HTML for the type selector navigation
bar.  The links in the bar are dynamically generated based on the
values of $name and $class.  This function is called by PrintTop().
It is not exported by default.

=cut

# Choose a set of displayers based on the type.
sub TypeSelector {
    my ($name,$class) = @_;
    return unless $class;

    my ($n,$c) = (escape("$name"),escape($class));
    my @rows;

    # add the special displays
    my @displays       = Configuration()->class2displays($class,$name);
    my @basic_displays = Configuration()->class2displays('default');
    @basic_displays    = Ace::Browser::SiteDefs->getConfig(DEFAULT_DATABASE)->class2displays('default') 
      unless @basic_displays;

    my $display = url(-absolute=>1,-path=>1);

    foreach (@displays,@basic_displays) {
 	my ($url,$icon,$label) = @{$_}{qw/url icon label/};
	next unless $url;
	my $u = ResolveUrl($url,"name=$n;class=$c");
	($url = $u) =~ s/[?\#].*$//;

	my $active = $url =~ /^$display/;
	my $cell;
	unless ($active) {
	  $cell = defined $icon ? a({-href=>$u,-target=>'_top'},
				    img({-src=>$icon,-border=>0}).br().$label)
				: a({-href=>$u,-target=>'_top'},$label);
	} else {
	  $cell = defined $icon ? img({-src=>$icon,-border=>0}).br().font({-color=>'red'},$label)
				: font({-color=>'red'},$label);
	}
	  push (@rows,td({-align=>'CENTER',-class=>'small'},$cell));
	}
    return table({-width=>'100%',-border=>0,-class=>'searchtitle'},
		 TR({-valign=>'bottom'},@rows));
}

=item $url = Url($display,$params)

Given a symbolic display name, such as "tree" and a set of parameters, 
this function looks up its URL and then calls ResolveUrl() to create a 
single Url.

When hard-coding relative URLs into AceBrowser scripts, it is
important to pass them through Url().  The reason for this is that
AceBrowser may need to attach the database name to the URL in order to
identify it.

Example:

  my $url = Url('../sequence_dump',"name=$name;long_dump=yes");
  print a({-href=>$url},'Dump this sequence');



( run in 2.745 seconds using v1.01-cache-2.11-cpan-75ffa21a3d4 )