###############################################################################
# $Id$
#
# Description : Generic Table building mechanism designed for use with cgi
# scripts. Default export mode is HTML; can also export as
# TSV.
#
# Copyright (C) 2005, Institute for Systems Biology
# This program is governed by the terms of the GNU General Public License (GPL)
# version 2 as published by the Free Software Foundation. It is provided
# WITHOUT ANY WARRANTY. See the full description of GPL terms in the
# LICENSE file distributed with this software.
#
###############################################################################
package SBEAMS::Connection::SBPage;
use strict;
use overload ( '""', \&asHTML );
use SBEAMS::Connection qw( $q );
use SBEAMS::Connection::DataTable;
use SBEAMS::Connection::Log;
use SBEAMS::Connection::Tables;
use SBEAMS::Connection::Settings qw( :default );
use POSIX;
my $log = SBEAMS::Connection::Log->new();
##### Public Methods ###########################################################
#
# Method to produce standard SBEAMS server web page
#+
# Constructor method. Any name => value parameters passed will be appended
# as table attributes
#
sub new {
my $class = shift;
my $this = { user_context => 1,
@_,
};
bless $this, $class;
return $this;
}
sub setSBEAMS {
my $this = shift;
my %args = @_;
die "Missing sbeams object" unless $args{sbeams};
$this->{sbeams} = $args{sbeams};
return 1;
}
sub setSBEAMSMod {
$log->debug( "Setting sbeamsmod" );
my $this = shift;
my %args = @_;
die "Missing sbeamsMOD object" unless $args{sbeamsMOD};
$this->{sbeamsMOD} = $args{sbeamsMOD};
return 1;
}
sub getSBEAMS {
my $this = shift;
return $this->{sbeams};
}
sub getSBEAMSMod {
my $this = shift;
return $this->{sbeamsMOD};
}
sub addContent {
my $this = shift;
my $content = shift;
$this->{_content} = $content;
}
sub printPage {
my $this = shift;
my $sbeams = $this->getSBEAMS();
print $this->asHTML();
}
sub asHTML {
my $this = shift;
my %args = @_;
my $nav_bar = $args{'nav_bar'} || 1;
my $sbeams = $this->getSBEAMS() || die "Must supply sbeams object";
my $sbeamsMOD = $this->getSBEAMSMod();
my $header = $this->{header} || $sbeams->get_http_header();
my $jscript = $this->_getJavascriptFunctions();
my $style = $this->_getStyleSheet();
my $navbar = $this->_getNavBar( $sbeams );
my $footer = $this->_getFooter();
#### Determine the Title bar background decoration
my $head_bkg = ( $DBVERSION =~ /Primary/ ) ? "$HTML_BASE_DIR/images/plaintop.jpg" : $BGCOLOR;
my $head_tag = ($DBVERSION =~ /Primary/) ? 'BACKGROUND' : 'BGCOLOR';
my $padding = ' ' x 300;
my $mpad = ' ' x 5;
my $maintab = SBEAMS::Connection::DataTable->new( BORDER => 0, WIDTH => '60%', CELLPADDING => 0, CELLSPACING => 0 );
my $isblink =<<" END_LINK";
END_LINK
my $banner = $this->_getBanner( $sbeams );
my $context = ( $this->{user_context} ) ? $this->_getUserContext( $sbeams ) : '';
my $mainpage =<<" END_MAIN";
$context
$padding
$mpad
END
}
sub _getNavBar {
my $this = shift;
my $sbeams = shift;
if ( $this->getSBEAMSMod() ) {
my $sbeamsMOD = $this->getSBEAMSMod();
my $menu;
# Try to call getMenu method on sbeamMOD object
eval { $menu = $sbeamsMOD->getMenu( sbeams => $sbeams ) };
$log->debug( $@ );
# Return menu if we got one
return $menu if $menu;
}
my $ntable = SBEAMS::Connection::DataTable->new( CELLPADDING => 2 );
$ntable->addRow( [ "$DBTITLE Home" ] );
$ntable->addRow( [ "Change Password" ] );
$ntable->addRow( [ "Logout" ] );
$ntable->addRow( [ ' ' ] );
$ntable->addRow( [ 'Available Modules' ] );
my $mpad = ' ' x 4;
# Get the list of Modules
my @modules = $sbeams->getModules();
foreach my $mod ( @modules ) {
$ntable->addRow( [ "$mpad $mod" ] );
}
$ntable->addRow( [ ' ' ] );
if ( $sbeams->isAdminUser ) {
$ntable->addRow( [ ' ' ] );
$ntable->addRow( [ "Admin" ] );
}
$ntable->addRow( [ ' ' ] );
$ntable->addRow( [ "Documentation" ] );
$ntable->setColAttr( COLS => [1], ROWS => [ 1..$ntable->getRowNum()], NOWRAP => 1 );
return( <<" END_NAV" );
$ntable
END_NAV
}
sub _getBanner {
my $this = shift;
my $sbeams = shift;
if ( $this->getSBEAMSMod() ) {
my $sbeamsMOD = $this->getSBEAMSMod();
my $banner;
# Try to call getMenu method on sbeamMOD object
eval { $banner = $sbeamsMOD->getBanner( sbeams => $sbeams ) };
$log->debug( $@ );
# Return Banner if we got one
return $banner if $banner;
}
return <<" END_BAN";
$DBTITLE - Systems Biology Experiment Analysis Management System $DBVERSION
END_BAN
}
###############################################################################
#
# Return the standard style sheet for pages. Use a font size of 10pt if
# remote client is on Windows, else use 12pt. This ends up making fonts
# appear the same size on Windows+IE and Linux+Netscape. Other tweaks for
# different browsers might be appropriate.
###############################################################################
sub _getStyleSheet {
my $this = shift;
my $font_size=9;
my $font_size_sm=8;
my $font_size_lg=12;
my $font_size_hg=14;
if ( $ENV{HTTP_USER_AGENT} =~ /Mozilla\/4.+X11/ ) {
$font_size=12;
$font_size_sm=11;
$font_size_lg=14;
$font_size_hg=19;
}
return <<" END_STYLE";
END_STYLE
}
###############################################################################
# getJavascriptFunctions
#
# Return the standard Javascript functions that should appear at the top of
# most pages. There probably should be some customization allowance here.
# Not sure how to design that yet.
###############################################################################
sub _getJavascriptFunctions {
my $this = shift;
my $javascript_includes = shift;
return <<" END";
END
}
sub _getUserContext {
my $this = shift;
my $sbeams = shift;
my %args = @_;
#### Define standard variables
my $style = 'HTML';
my ($work_group_sql, $project_sql, @rows);
my ($work_group_chooser, $project_chooser);
#### Find sub directory
my $subdir = $sbeams->getSBEAMS_SUBDIR();
$subdir .= "/" if ($subdir);
#### Get all relevant user information
my $current_username = $sbeams->getCurrent_username;
my $current_contact_id = $sbeams->getCurrent_contact_id;
my $current_work_group_id = $sbeams->getCurrent_work_group_id;
my $current_work_group_name = $sbeams->getCurrent_work_group_name;
my $current_project_id = $sbeams->getCurrent_project_id;
my $current_project_name = $sbeams->getCurrent_project_name;
my $current_user_context_id = $sbeams->getCurrent_user_context_id;
#### The guest user should never be presented with sbeams
if ($current_username eq 'guest') {
return;
}
#### Find out the current URI
my $submit_string = $ENV{'SCRIPT_URI?'} || '';
my $context = '';
# Bail if not in HTML mode
return unless ($style eq "HTML");
$context =<<" END";
END
$work_group_sql = qq~
SELECT WG.work_group_id,WG.work_group_name
FROM $TB_WORK_GROUP WG
INNER JOIN $TB_USER_WORK_GROUP UWG ON ( WG.work_group_id=UWG.work_group_id )
WHERE contact_id = '$current_contact_id'
AND WG.record_status != 'D'
AND UWG.record_status != 'D'
ORDER BY WG.work_group_name
~;
@rows = $sbeams->selectSeveralColumns($work_group_sql);
$work_group_chooser = "';
#### Get accessible projects and make