Merge branch 'master' of https://github.com/solgenomics/sgn
[sgn.git] / cgi-bin / jsforms / locus_ajax_form.pl
blob74f3b5061f887383e4ebf29dfdd3505f10d2adbf
2 use strict;
3 use warnings;
5 my $locus_form = CXGN::Phenome::LocusForm->new();
7 package CXGN::Phenome::LocusForm;
9 use base qw/CXGN::Page::Form::AjaxFormPage /;
11 use CXGN::Phenome::Locus;
12 use CXGN::Phenome::Locus::LinkageGroup;
13 use CXGN::Tools::Organism;
15 use CXGN::People::Person;
16 use CXGN::Contact;
17 use CXGN::Feed;
19 use Try::Tiny;
21 use JSON;
24 sub new {
25 my $class = shift;
26 my $self = $class->SUPER::new(@_);
27 return $self;
30 sub define_object {
31 my $self = shift;
32 my %args = $self->get_args();
33 my $locus_id = $args{locus_id} || $args{object_id};
34 my $user_type = $self->get_user()->get_user_type();
35 my %json_hash= $self->get_json_hash();
37 $self->set_object_id($locus_id);
38 $self->set_object_name('Locus'); #this is useful for email messages
39 $self->set_object(
40 CXGN::Phenome::Locus->new( $self->get_dbh(), $self->get_object_id() ) );
41 if ( $self->get_object()->get_obsolete() eq 't' && $user_type ne 'curator' )
43 ##print STDERR "ERROR:: Locus $locus_id is obsolete!";
44 $json_hash{error}="Locus $locus_id is obsolete!";
46 unless ( ( $locus_id =~ m /^\d+$/ || !$locus_id ) ) {
47 #print STDERR "ERROR:: 'No locus exists for identifier $locus_id'\n\n";
48 $json_hash{error}="No locus exists for identifier $locus_id";
50 $self->set_json_hash(%json_hash);
51 $self->set_primary_key("locus_id");
52 $self->set_owners( $self->get_object()->get_owners() );
54 $self->print_json() if $json_hash{error};
58 sub store {
59 my $self=shift;
60 my $locus = $self->get_object();
61 my $locus_id = $self->get_object_id();
62 my %args = $self->get_args();
63 my %json_hash = $self->get_json_hash();
64 my $initial_locus_id = $locus_id;
66 my $error;
67 $locus->set_common_name_id($args{common_name_id});
69 my ($message) =
70 $locus->exists_in_database( $args{locus_name}, $args{locus_symbol} );
71 my $validate;
72 if ($message) {
73 $error = " Locus $args{locus_name} (symbol= $args{locus_symbol} ) already exists in the database ";
74 }else {
75 try{
76 $self->SUPER::store(); #this sets $json_hash{validate} if the form validation failed.
77 $locus_id = $locus->get_locus_id() ;
78 } catch {
79 $error = " An error occurred. Cannot store to the database\n An email message has been sent to the SGN development team";
80 CXGN::Contact::send_email('locus_ajax_form.pl died', $error . "\n" . $_ , 'sgn-bugs@sgn.cornell.edu');
83 #the validate field is false is validation passed for all fields, true if did not pass and the form is re-printed
84 #$json_hash{validate}= $validate;
85 %json_hash= $self->get_json_hash();
86 $validate= $json_hash{validate};
87 $json_hash{error} = $error if $error;
89 my $refering_page="/phenome/locus_display.pl?locus_id=$locus_id";
90 $self->send_form_email({subject=>"[New locus details stored] locus $locus_id", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page}) if (!$validate && !$json_hash{error});
91 $json_hash{refering_page}=$refering_page if !$initial_locus_id && !$validate && !$error;
93 $self->set_json_hash(%json_hash);
95 $self->print_json();
99 ####################################
100 sub delete {
101 ##Delete the locus (actually set obsolete = 't')
102 my $self = shift;
103 my $check = $self->check_modify_privileges();
104 $self->print_json() if $check ; #error or no user privileges
106 my $locus = $self->get_object();
107 my $locus_name = $locus->get_locus_name();
108 my $locus_id = $locus->get_locus_id();
109 my %json_hash= $self->get_json_hash();
110 my $refering_page="/phenome/locus_display.pl?locus_id=$locus_id";
112 if (!$json_hash{error} ) {
113 try {
114 $locus->delete();
115 }catch {
116 $json_hash{error} = " An error occurred. Cannot delete locus\n An email message has been sent to the SGN development team";
118 $json_hash{reload} = 1;
120 $self->send_form_email({subject=>"Locus obsoleted ($locus_name)", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page, action=>'delete'}) if (!$json_hash{error});
121 $self->set_json_hash(%json_hash);
122 $self->print_json();
127 sub generate_form {
128 my $self = shift;
129 my $form_id = 'edit_locus'; # a form_id is required for ajax forms
130 $self->init_form($form_id) ; ## instantiate static/editable/confirmStore form
132 my $locus = $self->get_object();
133 my %args = $self->get_args();
134 my $form = $self->get_form();
135 my $dbh = $self->get_dbh();
137 my ( $organism_names_ref, $organism_ids_ref ) =
138 CXGN::Tools::Organism::get_all_organisms( $self->get_dbh() );
139 my ($lg_names_ref) =
140 CXGN::Phenome::Locus::LinkageGroup::get_all_lgs( $self->get_dbh() );
141 my ($lg_arms_ref) =
142 CXGN::Phenome::Locus::LinkageGroup::get_lg_arms( $self->get_dbh() );
144 if ( $self->get_action =~ /new|store/ ) {
145 $self->get_form->add_select(
146 display_name => "Organism ",
147 field_name => "common_name_id",
148 contents => $locus->get_common_name_id(),
149 length => 20,
150 object => $locus,
151 getter => "get_common_name_id",
152 setter => "set_common_name_id",
153 select_list_ref => $organism_names_ref,
154 select_id_list_ref => $organism_ids_ref,
158 if ( $locus->get_obsolete() eq 't' ) {
159 $form->add_label(
160 display_name => "Status",
161 field_name => "obsolete_stat",
162 contents => 'OBSOLETE',
165 $form->add_field(
166 display_name => "Locus",
167 field_name => "Locus",
168 id => "genome_locus",
169 object => $locus,
170 getter => "get_genome_locus",
171 setter => "set_genome_locus",
172 autocomplete => '/ajax/locus/genome_autocomplete',
174 $form->add_field(
175 display_name => "Locus name ",
176 field_name => "locus_name",
177 object => $locus,
178 getter => "get_locus_name",
179 setter => "set_locus_name",
180 validate => 'string',
183 $form->add_field(
184 display_name => "Symbol ",
185 field_name => "locus_symbol",
186 object => $locus,
187 getter => "get_locus_symbol",
188 setter => "set_locus_symbol",
189 validate => 'token',
190 formatting => '<i>*</i>',
193 $form->add_field(
194 display_name => "Gene activity ",
195 field_name => "gene_activity",
196 object => $locus,
197 getter => "get_gene_activity",
198 setter => "set_gene_activity",
199 length => '50',
202 $form->add_textarea(
203 display_name => "Description ",
204 field_name => "description",
205 object => $locus,
206 getter => "get_description",
207 setter => "set_description",
208 columns => 40,
209 rows => => 4,
212 $form->add_select(
213 display_name => "Chromosome ",
214 field_name => "lg_name",
215 contents => $locus->get_linkage_group(),
216 length => 10,
217 object => $locus,
218 getter => "get_linkage_group",
219 setter => "set_linkage_group",
220 select_list_ref => $lg_names_ref,
221 select_id_list_ref => $lg_names_ref,
224 $form->add_select(
225 display_name => "Arm",
226 field_name => "lg_arm",
227 contents => $locus->get_lg_arm(),
228 length => 10,
229 object => $locus,
230 getter => "get_lg_arm",
231 setter => "set_lg_arm",
232 select_list_ref => $lg_arms_ref,
233 select_id_list_ref => $lg_arms_ref,
236 $form->add_hidden(
237 field_name => "locus_id",
238 contents => $locus->get_locus_id(),
241 $form->add_hidden(
242 field_name => "action",
243 contents => "store",
246 $form->add_hidden(
247 field_name => "sp_person_id",
248 contents => $self->get_user()->get_sp_person_id(),
249 object => $locus,
250 setter => "set_sp_person_id",
253 $form->add_hidden(
254 field_name => "updated_by",
255 contents => $self->get_user()->get_sp_person_id(),
256 object => $locus,
257 setter => "set_updated_by",
260 if ( $self->get_action() =~ /view|edit/ ) {
261 $form->from_database();
262 $form->add_hidden(
263 field_name => "common_name_id",
264 contents => $locus->get_common_name_id(),
268 elsif ( $self->get_action() =~ /store/ ) {
269 $form->from_request( %args );