#!/usr/bin/perl

our $VERSION = '0.02';

# $Id: germplasm_search,v 1.7.2.2 2007/06/14 19:04:09 kclark Exp $

use warnings;
use strict;

use FindBin::Real qw(Bin);
use HTML::SearchPage;
use HTML::SearchPage::Param;
use Panzea::FormModifier;
use Panzea::FormConfig;
use Panzea::WebFormUtils;

my $config_file = '/usr/local/gramene-25/conf//html-searchpage.conf'; # MKFILE:Q:CONF_FILE

my $config = Panzea::FormConfig->new($config_file);

my $code2sampstat = Panzea::WebFormUtils->code2sampstat();

# Search page object
my $sp = HTML::SearchPage->new(
    page_title     => 'Germplasm Search',
    header         => $config->cfg('form_header'),
    css            => $config->cfg('form_css'),
    temp_dir       => $config->cfg('temp_dir'),
    temp_dir_eq    => $config->cfg('temp_dir_eq'),
    instructions   => $config->cfg('instructions_germplasm_search'),
    footer         => $config->cfg('form_footer'),
    base_sql_table => 'div_taxonomy dt
        RIGHT JOIN div_passport dp USING (div_taxonomy_id)
        LEFT JOIN div_accession_collecting dac USING (div_accession_collecting_id)
        LEFT JOIN div_locality dl USING (div_locality_id)',
    base_sql_fields => [
        'dp.accename',
        'dp.source',
        'dp.sampstat',
        'dt.genus',
        'dt.species',
        'dt.subspecies',
        'dt.race',
        'dt.population',    # in txt only
        'dl.country',
        'dl.state_province',
        'dl.locality_name',    # in txt only
        'dl.elevation',        # in txt only
        'dl.latitude',         # in txt only
        'dl.longitude',        # in txt only
        'dac.collector',
        'dac.collnumb',
        'dac.collsrc',         # in txt only
        'dac.collcode',        # in txt only
    ],
    base_output_headers => [
        'Accession:dp.accename',
        'Source:dp.source',
        'Germplasm Type',
        'Genus:dt.genus',
        'Species:dt.species',
        'Subspecies:dt.subspecies',
        'Racename:dt.race',
        'Population',          # in txt only
        'Country:dl.country',
        'State/Province:dl.state_province',
        'Locality',            # in txt only
        'Elevation',           # in txt only
        'Latitude',            # in txt only
        'Longitude',           # in txt only
        'Collector:dac.collector',
        'Collnumb:dac.collnumb',
        'Collsrc',             # in txt only
        'Collcode',            # in txt only
    ],
    sort_fields      => 3,
    sort_defaults    => [qw(asc dp.accename asc dp.source)],
    method           => $config->cfg('form_method'),
    page_size        => $config->cfg('form_page_size'),
    db_access_params => $config->cfg('db_access_params'),
    debug_level      => $config->cfg('form_debug_level'),
    go_to_results    => $config->cfg('form_go_to_results'),
    show_search_url  => $config->cfg('form_show_search_url'),
    modifier         => Panzea::FormModifier->new(),
);                             # Displays error page if fails

# Determine sampstats and format for display
my @sampstats;
eval {
    # *** Temporary ***
    # Wheat database contains non-null empty sampstat values
    # Search for non-empty values as well until these values are made null
    my $statement =
      qq[SELECT DISTINCT sampstat FROM div_passport 
         WHERE sampstat IS NOT NULL AND sampstat != ""];

    @sampstats = $sp->run_distinct_statement($statement);
};
$sp->display_error_page($@) if $@;

my @formatted_sampstats = map {
        $code2sampstat->{$_}
      ? $_ . ":" . $sp->url_encode($code2sampstat->{$_})
      : $_ . ":"
      . $sp->url_encode($_)
} @sampstats;

# Param fields
my $pf;

$pf = HTML::SearchPage::Param->new(
    -label            => 'Accession:',
    -sql_column       => 'dp.accename',
    -form_name        => 'germplasm',
    -operator_list    => ['=:equals', 'like_c:contains', 'like_m:matches'],
    -operator_display => 1,
    -param_type       => 'text:12',
    -auto_all         => 1,
    -auto_null        => 1,
) or $sp->display_error_page($@);

$sp->param_field('germplasm', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Source:',
    -sql_column       => 'dp.source',
    -form_name        => 'source',
    -operator_list    => ['=:equals', 'like_c:contains', 'like_m:matches'],
    -operator_display => 1,
    -param_type       => 'text:12',
    -auto_all         => 1,
    -auto_null        => 1,
) or $sp->display_error_page($@);

$sp->param_field('source', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Germplasm Type:',
    -sql_column       => 'dp.sampstat',
    -form_name        => 'sampstat',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => \@formatted_sampstats,
    -auto_all         => 1,
    -auto_null        => 1,
    -param_default    => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('sampstat', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Genus:',
    -sql_column       => 'dt.genus',
    -form_name        => 'genus',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => [
        'DISTINCT:SELECT DISTINCT genus from div_taxonomy
         WHERE genus IS NOT NULL AND genus != ""
         ORDER BY genus'
    ],
    -auto_all      => 1,
    -auto_null     => 1,
    -param_default => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('genus', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Species:',
    -sql_column       => 'dt.species',
    -form_name        => 'species',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => [
        'DISTINCT:SELECT DISTINCT species from div_taxonomy
         WHERE species IS NOT NULL AND species != ""
         ORDER BY species'
    ],
    -auto_all      => 1,
    -auto_null     => 1,
    -param_default => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('species', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Subspecies:',
    -sql_column       => 'dt.subspecies',
    -form_name        => 'subspecies',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => [
        'DISTINCT:SELECT DISTINCT subspecies from div_taxonomy
         WHERE subspecies IS NOT NULL AND subspecies != ""
         ORDER BY subspecies'
    ],
    -auto_all      => 1,
    -auto_null     => 1,
    -param_default => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('subspecies', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Racename:',
    -sql_column       => 'dt.race',
    -form_name        => 'racename',
    -operator_list    => ['=:equals', 'like_c:contains', 'like_m:matches'],
    -operator_display => 1,
    -param_type       => 'text:12',
    -auto_all         => 1,
    -auto_null        => 1,
) or $sp->display_error_page($@);

$sp->param_field('racename', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Country:',
    -sql_column       => 'dl.country',
    -form_name        => 'country',
    -operator_list    => ['=:equals'],
    -operator_display => 0,
    -operator_default => '=',
    -param_type       => 'drop_down',
    -param_list       => [
        'DISTINCT:SELECT DISTINCT country from div_locality
         WHERE country IS NOT NULL AND country != ""
         ORDER BY country'
    ],
    -auto_all      => 1,
    -auto_null     => 1,
    -param_default => ['all'],
) or $sp->display_error_page($@);
$sp->param_field('country', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'State/Province:',
    -sql_column       => 'dl.state_province',
    -form_name        => 'state_province',
    -operator_list    => ['=:equals', 'like_c:contains', 'like_m:matches'],
    -operator_display => 1,
    -param_type       => 'text:12',
    -auto_all         => 1,
    -auto_null        => 1,
) or $sp->display_error_page($@);

$sp->param_field('state_province', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Collector:',
    -sql_column       => 'dac.collector',
    -form_name        => 'collector',
    -operator_list    => ['=:equals', 'like_c:contains', 'like_m:matches'],
    -operator_display => 1,
    -param_type       => 'text:12',
    -auto_all         => 1,
    -auto_null        => 1,
) or $sp->display_error_page($@);

$sp->param_field('collector', $pf);

$pf = HTML::SearchPage::Param->new(
    -label            => 'Collection:',
    -sql_column       => 'dac.collnumb',
    -form_name        => 'collnumb',
    -operator_list    => ['=:equals', 'like_c:contains', 'like_m:matches'],
    -operator_display => 1,
    -param_type       => 'text:12',
    -auto_all         => 1,
    -auto_null        => 1,
) or $sp->display_error_page($@);

$sp->param_field('collection', $pf);

# Modifications
$sp->add_modification(
    -action => 'add_link',
    -column => 1,
    -type   => 'source'
);

$sp->add_modification(
    -action => 'translate',
    -column => 2,
    -type   => 'code2sampstat'
);

$sp->add_modification(
    -action          => 'remove_columns',
    -column          => [qw(7 10 11 12 13 16 17)],
    -affected_format => 'html',
);

# Call display method
$sp->display_page;

=head1 NAME

germplasm_search

=head1 DESCRIPTION

Panzea web display script.

Please refer to documentation of HTML::SearchPage and
HTML::SearchPage::Param for information on script structure.

=head1 AUTHOR

Payan Canaran <canaran@cshl.edu>

=head1 BUGS

=head1 VERSION

Version 0.01

=head1 ACKNOWLEDGEMENTS

=head1 COPYRIGHT & LICENSE

Copyright (c) 2005-2007 Cold Spring Harbor Laboratory

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself. See DISCLAIMER.txt for
disclaimers of warranty.

=cut

