Merge pull request #5230 from solgenomics/topic/open_pollinated
[sgn.git] / lib / SGN / Controller / AJAX / Search / Trial.pm
blob9dfdd386056555b134c040762d2f111e05d6dbab
2 package SGN::Controller::AJAX::Search::Trial;
4 use Moose;
5 use Data::Dumper;
6 use CXGN::Trial;
7 use CXGN::Trial::Search;
8 use JSON;
10 BEGIN { extends 'Catalyst::Controller::REST'; }
12 __PACKAGE__->config(
13 default => 'application/json',
14 stash_key => 'rest',
15 map => { 'application/json' => 'JSON' },
18 sub search : Path('/ajax/search/trials') Args(0) {
19 my $self = shift;
20 my $c = shift;
22 my @location_ids;
23 my $location_id = $c->req->param('location_id');
24 if ($location_id && $location_id ne 'not_provided'){
25 #print STDERR "location id: " . $location_id . "\n";
26 push @location_ids, $location_id;
29 my $checkbox_select_name = $c->req->param('select_checkbox_name');
30 my $field_trials_only = $c->req->param('field_trials_only') || 1;
31 my $trial_design_list = $c->req->param('trial_design') ? [$c->req->param('trial_design')] : [];
33 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
34 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
36 my $trial_search = CXGN::Trial::Search->new({
37 bcs_schema=>$schema,
38 location_id_list=>\@location_ids,
39 field_trials_only=>$field_trials_only,
40 trial_design_list=>$trial_design_list
41 });
42 my ($data, $total_count) = $trial_search->search();
43 my @result;
44 my %selected_columns = ('plot_name'=>1, 'plot_id'=>1, 'block_number'=>1, 'plot_number'=>1, 'rep_number'=>1, 'row_number'=>1, 'col_number'=>1, 'accession_name'=>1, 'is_a_control'=>1);
45 my $selected_columns_json = encode_json \%selected_columns;
46 foreach (@$data){
47 my $folder_string = '';
48 if ($_->{folder_name}){
49 $folder_string = "<a href=\"/folder/$_->{folder_id}\">$_->{folder_name}</a>";
51 my @res;
52 if ($checkbox_select_name){
53 push @res, "<input type='checkbox' name='$checkbox_select_name' value='$_->{trial_id}'>";
55 push @res, (
56 "<a href=\"/breeders_toolbox/trial/$_->{trial_id}\">$_->{trial_name}</a>",
57 $_->{description},
58 "<a href=\"/breeders/program/$_->{breeding_program_id}\">$_->{breeding_program_name}</a>",
59 $folder_string,
60 $_->{year},
61 $_->{location_name},
62 $_->{trial_type},
63 $_->{design},
64 $_->{project_planting_date},
65 $_->{project_harvest_date},
66 "<a class='btn btn-sm btn-default' href='/breeders/trial/$_->{trial_id}/download/layout?format=csv&dataLevel=plots&selected_columns=$selected_columns_json'>Download Plot Layout</a>"
68 push @result, \@res;
70 #print STDERR Dumper \@result;
72 $c->stash->{rest} = { data => \@result };