clean
[sgn.git] / lib / SGN / Controller / AJAX / HTMLSelect.pm
blob2e54b40f52d8167e0da6542443cd9a94d4470c28
2 =head1 SGN::Controller::AJAX::HTMLSelect - a resource to dynamically obtain html selects for a number of widely used data types
4 =head1 SYNOPSYS
6 get_location_select()
8 get_breeding_program_select()
10 get_year_select()
14 =head1 AUTHOR
16 Lukas Mueller <lam87@cornell.edu>
18 =cut
20 package SGN::Controller::AJAX::HTMLSelect;
22 use Moose;
24 use Data::Dumper;
25 use CXGN::BreedersToolbox::Projects;
26 use CXGN::Page::FormattingHelpers qw | simple_selectbox_html |;
27 use CXGN::Trial;
28 use CXGN::Trial::Folder;
29 use SGN::Model::Cvterm;
31 BEGIN { extends 'Catalyst::Controller::REST' };
33 __PACKAGE__->config(
34 default => 'application/json',
35 stash_key => 'rest',
36 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
40 sub get_location_select : Path('/ajax/html/select/locations') Args(0) {
41 my $self = shift;
42 my $c = shift;
44 my $id = $c->req->param("id") || "location_select";
45 my $name = $c->req->param("name") || "location_select";
46 my $empty = $c->req->param("empty") || "";
48 my $locations = CXGN::BreedersToolbox::Projects->new( { schema => $c->dbic_schema("Bio::Chado::Schema") } )->get_all_locations();
50 if ($empty) { unshift @$locations, [ "", "please select" ] }
52 my $default = $c->req->param("default") || @$locations[0]->[0];
54 my $html = simple_selectbox_html(
55 name => $name,
56 id => $id,
57 choices => $locations,
58 selected => $default
60 $c->stash->{rest} = { select => $html };
63 sub get_breeding_program_select : Path('/ajax/html/select/breeding_programs') Args(0) {
64 my $self = shift;
65 my $c = shift;
67 my $id = $c->req->param("id") || "breeding_program_select";
68 my $name = $c->req->param("name") || "breeding_program_select";
69 my $empty = $c->req->param("empty") || "";
71 my $breeding_programs = CXGN::BreedersToolbox::Projects->new( { schema => $c->dbic_schema("Bio::Chado::Schema") } )->get_breeding_programs();
73 my $default = $c->req->param("default") || @$breeding_programs[0]->[0];
74 if ($empty) { unshift @$breeding_programs, [ "", "please select" ]; }
76 my $html = simple_selectbox_html(
77 name => $name,
78 id => $id,
79 choices => $breeding_programs,
80 selected => $default
82 $c->stash->{rest} = { select => $html };
85 sub get_year_select : Path('/ajax/html/select/years') Args(0) {
86 my $self = shift;
87 my $c = shift;
89 my $id = $c->req->param("id") || "year_select";
90 my $name = $c->req->param("name") || "year_select";
91 my $empty = $c->req->param("empty") || "";
92 my $auto_generate = $c->req->param("auto_generate") || "";
94 my @years;
95 if ($auto_generate) {
96 my $next_year = 1901 + (localtime)[5];
97 my $oldest_year = $next_year - 30;
98 @years = sort { $b <=> $a } ($oldest_year..$next_year);
100 else {
101 @years = sort { $b <=> $a } CXGN::BreedersToolbox::Projects->new( { schema => $c->dbic_schema("Bio::Chado::Schema") } )->get_all_years();
104 my $default = $c->req->param("default") || @years[1];
106 my $html = simple_selectbox_html(
107 name => $name,
108 id => $id,
109 choices => \@years,
110 selected => $default
112 $c->stash->{rest} = { select => $html };
115 sub get_trial_folder_select : Path('/ajax/html/select/folders') Args(0) {
116 my $self = shift;
117 my $c = shift;
119 my $breeding_program_id = $c->req->param("breeding_program_id");
121 my $id = $c->req->param("id") || "folder_select";
122 my $name = $c->req->param("name") || "folder_select";
123 my $empty = $c->req->param("empty") || ""; # set if an empty selection should be present
126 my @folders = CXGN::Trial::Folder->list({
127 bcs_schema => $c->dbic_schema("Bio::Chado::Schema"),
128 breeding_program_id => $breeding_program_id
131 if ($empty) {
132 unshift @folders, [ 0, "None" ];
135 my $html = simple_selectbox_html(
136 name => $name,
137 id => $id,
138 choices => \@folders,
140 $c->stash->{rest} = { select => $html };
143 sub get_trial_type_select : Path('/ajax/html/select/trial_types') Args(0) {
144 my $self = shift;
145 my $c = shift;
147 my $id = $c->req->param("id") || "trial_type_select";
148 my $name = $c->req->param("name") || "trial_type_select";
150 my @types = CXGN::Trial::get_all_project_types($c->dbic_schema("Bio::Chado::Schema"));
152 my $default = $c->req->param("default") || @types[0]->[0];
154 my $html = simple_selectbox_html(
155 name => $name,
156 id => $id,
157 choices => \@types,
158 selected => $default
160 $c->stash->{rest} = { select => $html };
163 sub get_trials_select : Path('/ajax/html/select/trials') Args(0) {
164 my $self = shift;
165 my $c = shift;
167 my $p = CXGN::BreedersToolbox::Projects->new( { schema => $c->dbic_schema("Bio::Chado::Schema") } );
169 my $breeding_program_id = $c->req->param("breeding_program_id");
170 my $projects;
171 if (!$breeding_program_id) {
172 $projects = $p->get_breeding_programs();
173 } else {
174 push @$projects, [$breeding_program_id];
177 my $id = $c->req->param("id") || "html_trial_select";
178 my $name = $c->req->param("name") || "html_trial_select";
179 my @trials;
180 foreach my $project (@$projects) {
181 my ($field_trials, $cross_trials, $genotyping_trials) = $p->get_trials_by_breeding_program($project->[0]);
182 foreach (@$field_trials) {
183 push @trials, $_;
187 my $html = simple_selectbox_html(
188 multiple => 1,
189 name => $name,
190 id => $id,
191 choices => \@trials,
193 $c->stash->{rest} = { select => $html };
196 sub get_genotyping_protocols_select : Path('/ajax/html/select/genotyping_protocols') Args(0) {
197 my $self = shift;
198 my $c = shift;
200 my $id = $c->req->param("id") || "gtp_select";
201 my $name = $c->req->param("name") || "genotyping_protocol_select";
202 my $empty = $c->req->param("empty") || "";
203 my $default_gtp;
204 my %gtps;
206 my $gt_protocols = CXGN::BreedersToolbox::Projects->new( { schema => $c->dbic_schema("Bio::Chado::Schema") } )->get_gt_protocols();
208 if (@$gt_protocols) {
209 $default_gtp = $c->config->{default_genotyping_protocol};
210 %gtps = map { @$_[1] => @$_[0] } @$gt_protocols;
212 if(!exists($gtps{$default_gtp}) && !($default_gtp =~ /^none$/)) {
213 die "The conf variable default_genotyping_protocol: \"$default_gtp\" does not match any protocols in the database. Set it in sgn_local.conf using a protocol name from the nd_protocol table, or set it to 'none' to silence this error.";
215 } else {
216 $gt_protocols = ["No genotyping protocols found"];
218 my $html = simple_selectbox_html(
219 name => $name,
220 id => $id,
221 choices => $gt_protocols,
222 selected => $gtps{$default_gtp}
224 $c->stash->{rest} = { select => $html };
228 sub ontology_children_select : Path('/ajax/html/select/ontology_children') Args(0) {
229 my ($self, $c) = @_;
230 my $parent_node_cvterm = $c->request->param("parent_node_cvterm");
231 my $rel_cvterm = $c->request->param("rel_cvterm");
232 my $rel_cv = $c->request->param("rel_cv");
234 my $select_name = $c->request->param("selectbox_name");
235 my $select_id = $c->request->param("selectbox_id");
237 my $empty = $c->request->param("empty") || '';
239 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
240 my $parent_node_cvterm_id = SGN::Model::Cvterm->get_cvterm_row_from_trait_name($schema, $parent_node_cvterm)->cvterm_id();
241 my $rel_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, $rel_cvterm, $rel_cv)->cvterm_id();
243 my $ontology_children_ref = $schema->resultset("Cv::CvtermRelationship")->search({type_id => $rel_cvterm_id, object_id => $parent_node_cvterm_id})->search_related('subject');
244 my @ontology_children;
245 while (my $child = $ontology_children_ref->next() ) {
246 my $dbxref_info = $child->search_related('dbxref');
247 my $accession = $dbxref_info->first()->accession();
248 my $db_info = $dbxref_info->search_related('db');
249 my $db_name = $db_info->first()->name();
250 push @ontology_children, [$child->name."|".$db_name.":".$accession, $child->name."|".$db_name.":".$accession];
253 if ($empty) {
254 unshift @ontology_children, [ 0, "None" ];
257 my $html = simple_selectbox_html(
258 name => $select_name,
259 id => $select_id,
260 choices => \@ontology_children,
262 $c->stash->{rest} = { select => $html };