clean
[sgn.git] / lib / SGN / Controller / AJAX / Trials.pm
blob21f1f390dc6cc2ffa6b7e274120ce33c41a7f912
2 package SGN::Controller::AJAX::Trials;
4 use Moose;
6 use CXGN::BreedersToolbox::Projects;
7 use CXGN::Trial::Folder;
8 use Data::Dumper;
9 use Carp;
10 use File::Path qw(make_path);
11 use File::Spec::Functions qw / catfile catdir/;
12 use SGN::Model::Cvterm;
14 BEGIN { extends 'Catalyst::Controller::REST'; }
16 __PACKAGE__->config(
17 default => 'application/json',
18 stash_key => 'rest',
19 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
23 sub get_trials : Path('/ajax/breeders/get_trials') Args(0) {
24 my $self = shift;
25 my $c = shift;
27 my $p = CXGN::BreedersToolbox::Projects->new( { schema => $c->dbic_schema("Bio::Chado::Schema") } );
29 my $projects = $p->get_breeding_programs();
31 my %data = ();
32 foreach my $project (@$projects) {
33 my $trials = $p->get_trials_by_breeding_program($project->[0]);
34 $data{$project->[1]} = $trials;
38 $c->stash->{rest} = \%data;
41 sub get_trials_with_folders : Path('/ajax/breeders/get_trials_with_folders') Args(0) {
42 my $self = shift;
43 my $c = shift;
45 my $schema = $c->dbic_schema("Bio::Chado::Schema");
46 my $p = CXGN::BreedersToolbox::Projects->new( { schema => $schema } );
48 my $projects = $p->get_breeding_programs();
50 my $html = "";
51 foreach my $project (@$projects) {
52 my $folder = CXGN::Trial::Folder->new( { bcs_schema => $schema, folder_id => $project->[0] });
53 $html .= $folder->get_jstree_html('breeding_program');
56 my $dir = catdir($c->site_cluster_shared_dir, "folder");
57 eval { make_path($dir) };
58 if ($@) {
59 print "Couldn't create $dir: $@";
61 my $filename = $dir."/entire_jstree_html.txt";
63 my $OUTFILE;
64 open $OUTFILE, '>', $filename or die "Error opening $filename: $!";
65 print { $OUTFILE } $html or croak "Cannot write to $filename: $!";
66 close $OUTFILE or croak "Cannot close $filename: $!";
68 $c->stash->{rest} = { status => 1 };
71 sub get_trials_with_folders_cached : Path('/ajax/breeders/get_trials_with_folders_cached') Args(0) {
72 my $self = shift;
73 my $c = shift;
75 my $dir = catdir($c->site_cluster_shared_dir, "folder");
76 my $filename = $dir."/entire_jstree_html.txt";
77 my $html = '';
78 open(my $fh, '<', $filename) or die "cannot open file $filename";
80 local $/;
81 $html = <$fh>;
83 close($fh);
85 #print STDERR $html;
86 $c->stash->{rest} = { html => $html };
89 sub trial_autocomplete : Local : ActionClass('REST') { }
91 sub trial_autocomplete_GET :Args(0) {
92 my ($self, $c) = @_;
94 my $term = $c->req->param('term');
96 $term =~ s/(^\s+|\s+)$//g;
97 $term =~ s/\s+/ /g;
99 my $trial_design_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($c->dbic_schema("Bio::Chado::Schema"), "design", "project_property")->cvterm_id();
100 my @response_list;
101 my $q = "select distinct(name) from project join projectprop using(project_id) where project.name ilike ? and projectprop.type_id = ? ORDER BY name";
102 my $sth = $c->dbc->dbh->prepare($q);
103 $sth->execute('%'.$term.'%', $trial_design_cvterm_id);
104 while (my ($project_name) = $sth->fetchrow_array) {
105 push @response_list, $project_name;
107 print STDERR Dumper \@response_list;
109 $c->{stash}->{rest} = \@response_list;