Merge pull request #5205 from solgenomics/topic/generic_trial_upload
[sgn.git] / lib / SGN / Controller / AJAX / BreedersToolbox / Folder.pm
blob87c18722d3ebadb8e0ac209015f16068c40a355a
2 package SGN::Controller::AJAX::BreedersToolbox::Folder;
4 use Moose;
5 use List::MoreUtils qw | any |;
6 use Data::Dumper;
8 BEGIN { extends 'Catalyst::Controller::REST' }
10 __PACKAGE__->config(
11 default => 'application/json',
12 stash_key => 'rest',
13 map => { 'application/json' => 'JSON' },
16 sub get_folder : Chained('/') PathPart('ajax/folder') CaptureArgs(1) {
17 my $self = shift;
18 my $c = shift;
20 my $folder_id = shift;
21 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
22 $c->stash->{schema} = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
23 $c->stash->{folder_id} = $folder_id;
27 sub create_folder :Path('/ajax/folder/new') Args(0) {
28 my $self = shift;
29 my $c = shift;
30 my $parent_folder_id = $c->req->param("parent_folder_id");
31 my $folder_name = $c->req->param("folder_name");
32 my $breeding_program_id = $c->req->param("breeding_program_id");
34 my $folder_for_trials;
35 my $folder_for_crosses;
36 my $folder_for_genotyping_trials;
37 my $folder_for_genotyping_projects;
38 my $folder_for_tracking_activities;
39 my $folder_for_transformations;
41 my $project_type = $c->req->param("project_type");
42 if ($project_type eq 'field_trial') {
43 $folder_for_trials = 1;
44 } elsif ($project_type eq 'crossing_experiment') {
45 $folder_for_crosses = 1;
46 } elsif ($project_type eq 'genotyping_plate') {
47 $folder_for_genotyping_trials = 1;
48 } elsif ($project_type eq 'genotyping_project') {
49 $folder_for_genotyping_projects = 1;
50 } elsif ($project_type eq 'activity_record') {
51 $folder_for_tracking_activities = 1;
52 } elsif ($project_type eq 'transformation_project') {
53 $folder_for_transformations = 1;
56 if (! $self->check_privileges($c)) {
57 return;
59 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
60 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
61 my $existing = $schema->resultset("Project::Project")->find( { name => $folder_name });
63 if ($existing) {
64 $c->stash->{rest} = { error => "A folder or trial with that name already exists in the database. Please select another name." };
65 $c->detach;
67 my $folder = CXGN::Trial::Folder->create({
68 bcs_schema => $schema,
69 parent_folder_id => $parent_folder_id,
70 name => $folder_name,
71 breeding_program_id => $breeding_program_id,
72 folder_for_trials => $folder_for_trials,
73 folder_for_crosses => $folder_for_crosses,
74 folder_for_genotyping_trials => $folder_for_genotyping_trials,
75 folder_for_genotyping_projects => $folder_for_genotyping_projects,
76 folder_for_tracking_activities => $folder_for_tracking_activities,
77 folder_for_transformations => $folder_for_transformations,
78 });
80 $c->stash->{rest} = {
81 success => 1,
82 folder_id => $folder->folder_id()
86 sub delete_folder : Chained('get_folder') PathPart('delete') Args(0) {
87 my $self = shift;
88 my $c = shift;
90 if (! $self->check_privileges($c)) {
91 return;
94 my $folder = CXGN::Trial::Folder->new({
95 bcs_schema => $c->stash->{schema},
96 folder_id => $c->stash->{folder_id}
97 });
99 my $delete_folder = $folder->delete_folder();
100 if ($delete_folder) {
101 $c->stash->{rest} = { success => 1 };
102 } else {
103 $c->stash->{rest} = { error => 'Folder Not Deleted! To delete a folder first move all trials and sub-folders out of it.' };
108 sub rename_folder : Chained('get_folder') PathPart('name') Args(0) {
109 my $self = shift;
110 my $c = shift;
111 my $new_folder_name = $c->req->param("new_name") ;
112 if (! $self->check_privileges($c)) {
113 return;
116 my $folder = CXGN::Trial::Folder->new({
117 bcs_schema => $c->stash->{schema},
118 folder_id => $c->stash->{folder_id}
120 my $rename_folder = $folder->rename_folder($new_folder_name);
121 if ($rename_folder) {
122 $c->stash->{rest} = { success => 1 };
123 } else {
124 $c->stash->{rest} = { error => 'Folder could not be renamed!' };
130 sub associate_parent_folder : Chained('get_folder') PathPart('associate/parent') Args(1) {
131 my $self = shift;
132 my $c = shift;
133 my $parent_id = shift;
135 if (! $self->check_privileges($c)) {
136 return;
139 my $folder = CXGN::Trial::Folder->new(
141 bcs_schema => $c->stash->{schema},
142 folder_id => $c->stash->{folder_id}
145 $folder->associate_parent($parent_id);
147 $c->stash->{rest} = { success => 1 };
151 sub set_folder_categories : Chained('get_folder') PathPart('categories') Args(0) {
152 my $self = shift;
153 my $c = shift;
154 my $folder_for_trials = $c->req->param("folder_for_trials") eq 'true' ? 1 : 0;
155 my $folder_for_crosses = $c->req->param("folder_for_crosses") eq 'true' ? 1 : 0;
156 my $folder_for_genotyping_trials = $c->req->param("folder_for_genotyping_trials") eq 'true' ? 1 : 0;
158 if (! $self->check_privileges($c)) {
159 return;
162 my $folder = CXGN::Trial::Folder->new({
163 bcs_schema => $c->stash->{schema},
164 folder_id => $c->stash->{folder_id}
167 $folder->set_folder_content_type('folder_for_trials', $folder_for_trials);
168 $folder->set_folder_content_type('folder_for_crosses', $folder_for_crosses);
169 $folder->set_folder_content_type('folder_for_genotyping_trials', $folder_for_genotyping_trials);
171 $c->stash->{rest} = { success => 1 };
174 sub check_privileges {
175 my $self = shift;
176 my $c = shift;
178 if (!$c->user()) {
179 print STDERR "User not logged in... not uploading coordinates.\n";
180 $c->stash->{rest} = {error => "You need to be logged in." };
181 $c->detach;
184 if (!any { $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles) ) {
185 $c->stash->{rest} = {error => "You have insufficient privileges." };
186 $c->detach;
188 return 1;