2 package SGN
::Controller
::AJAX
::BreedersToolbox
::Folder
;
5 use List
::MoreUtils qw
| any
|;
8 BEGIN { extends
'Catalyst::Controller::REST' }
11 default => 'application/json',
13 map => { 'application/json' => 'JSON' },
16 sub get_folder
: Chained
('/') PathPart
('ajax/folder') CaptureArgs
(1) {
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) {
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)) {
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 });
64 $c->stash->{rest
} = { error
=> "A folder or trial with that name already exists in the database. Please select another name." };
67 my $folder = CXGN
::Trial
::Folder
->create({
68 bcs_schema
=> $schema,
69 parent_folder_id
=> $parent_folder_id,
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,
82 folder_id
=> $folder->folder_id()
86 sub delete_folder
: Chained
('get_folder') PathPart
('delete') Args
(0) {
90 if (! $self->check_privileges($c)) {
94 my $folder = CXGN
::Trial
::Folder
->new({
95 bcs_schema
=> $c->stash->{schema
},
96 folder_id
=> $c->stash->{folder_id
}
99 my $delete_folder = $folder->delete_folder();
100 if ($delete_folder) {
101 $c->stash->{rest
} = { success
=> 1 };
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) {
111 my $new_folder_name = $c->req->param("new_name") ;
112 if (! $self->check_privileges($c)) {
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 };
124 $c->stash->{rest
} = { error
=> 'Folder could not be renamed!' };
130 sub associate_parent_folder
: Chained
('get_folder') PathPart
('associate/parent') Args
(1) {
133 my $parent_id = shift;
135 if (! $self->check_privileges($c)) {
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) {
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)) {
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
{
179 print STDERR
"User not logged in... not uploading coordinates.\n";
180 $c->stash->{rest
} = {error
=> "You need to be logged in." };
184 if (!any
{ $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles) ) {
185 $c->stash->{rest
} = {error
=> "You have insufficient privileges." };