#!/usr/bin/perl

our $VERSION = '0.02';

# $Id: phenotype_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;

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

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

# Search page object
my $sp = HTML::SearchPage->new(
    page_title     => 'Phenotype 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_phenotype_search'),
    footer         => $config->cfg('form_footer'),
    base_sql_table => 'aux_uom_to_passport autp
                                                            LEFT JOIN div_trait_uom dtu ON (dtu.div_trait_uom_id = autp.div_trait_uom_id)
                                                            LEFT JOIN div_trait dt      ON (dt.div_trait_id = autp.div_trait_id)
                                                            LEFT JOIN div_obs_unit dou  ON (dou.div_obs_unit_id = autp.div_obs_unit_id)
                                                            LEFT JOIN div_locality dl   ON (dl.div_locality_id = autp.div_locality_id)
                                                            LEFT JOIN div_stock ds      ON (ds.div_stock_id = autp.div_stock_id)
                                                            LEFT JOIN div_passport dp   ON (dp.div_passport_id = autp.div_passport_id)',
    base_sql_fields => [
        'dtu.local_trait_name',
        'dl.locality_name',
        qq[DATE_FORMAT(dou.planting_date, '%d-%b-%y')],
        'dou.rep',
        'dp.accename',
        'dp.source',
        qq[CONCAT(dt.div_obs_unit_id, ':', dt.div_trait_uom_id)],
    ],
    base_output_headers => [
        'Phenotype Name:dtu.local_trait_name',
        'Evaluation Locality:dl.locality_name',
        qq[Planting Date:dou.planting_date],
        'Rep:dou.rep',
        'Accession:dp.accename',
        'Source:dp.source',
        'Phenotype Value',
    ],
    sort_fields   => 6,
    sort_defaults => [
        qw(asc dtu.local_trait_name asc dl.locality_name asc dou.planting_date
          asc dou.rep 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

# Param fields
my $pf;

$pf = HTML::SearchPage::Param->new(
    -label            => 'Phenotype Name:',
    -sql_column       => 'dtu.local_trait_name',
    -form_name        => 'phenotype_name',
    -operator_list    => ['=:in', '<>:not in'],
    -operator_display => 1,
    -param_type       => 'scrolling_list:3',
    -param_list       => [
        'DISTINCT:SELECT distinct local_trait_name from div_trait_uom
                                                                  WHERE local_trait_name IS NOT NULL AND local_trait_name != ""
                                                                  ORDER BY local_trait_name'
    ],
    -auto_all      => 1,
    -auto_null     => 0,
    -param_default => ['all'],
) or $sp->display_error_page($@);

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

$pf = HTML::SearchPage::Param->new(
    -label            => 'Evaluation Locality:',
    -sql_column       => 'dl.locality_name',
    -form_name        => 'locality_name',
    -operator_list    => ['=:in', '<>:not in'],
    -operator_display => 1,
    -param_type       => 'scrolling_list:3',
    -param_list       => [
        'DISTINCT:SELECT distinct dl.locality_name
                                                                   FROM aux_uom_to_passport autp
                                                                   LEFT JOIN div_locality dl ON (dl.div_locality_id = autp.div_locality_id)
				                                                   WHERE dl.locality_name IS NOT NULL AND dl.locality_name != ""
                                                                   ORDER BY locality_name'
    ],
    -auto_all      => 1,
    -auto_null     => 0,
    -param_default => ['all'],
) or $sp->display_error_page($@);

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

$pf = HTML::SearchPage::Param->new(
    -label            => 'Planting year:',
    -sql_column       => qq[YEAR(planting_date)],
    -form_name        => 'planting_year',
    -operator_list    => ['=:in', '<>:not in'],
    -operator_display => 1,
    -param_type       => 'scrolling_list:3',
    -param_list       => [
        'DISTINCT:SELECT distinct YEAR(dou.planting_date)
				                                                   FROM aux_uom_to_passport autp
                                                                   LEFT JOIN div_obs_unit dou ON (dou.div_obs_unit_id = autp.div_obs_unit_id)
				                                                   WHERE dou.planting_date IS NOT NULL AND dou.planting_date != ""
                                                                   ORDER BY YEAR(dou.planting_date)'
    ],
    -auto_all      => 1,
    -auto_null     => 0,
    -param_default => ['all'],
) or $sp->display_error_page($@);

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

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

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

$sp->add_modification(
    -action => 'add_link',
    -column => 4,
    -type   => 'accession'
);

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

$sp->add_modification(
    -action => 'get_phenotype_values',
    -column => 6,
);

# Call display method
$sp->display_page;

=head1 NAME

phenotype_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.02

=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

