fixed typo in arg name refering_page
[sgn.git] / cgi-bin / jsforms / locus_ajax_form.pl
blob65cc485462cbe659e42e3bbcc79808682190d0bb
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 ### $self->get_ajax_page()->send_http_header();
29 return $self;
32 sub define_object {
33 my $self = shift;
35 my %args = $self->get_args();
36 my $locus_id = $args{locus_id} || $args{object_id};
37 my $user_type = $self->get_user()->get_user_type();
38 my %json_hash= $self->get_json_hash();
40 $self->set_object_id($locus_id);
41 $self->set_object_name('Locus'); #this is useful for email messages
42 $self->set_object(
43 CXGN::Phenome::Locus->new( $self->get_dbh(), $self->get_object_id() ) );
44 if ( $self->get_object()->get_obsolete() eq 't' && $user_type ne 'curator' )
46 ##print STDERR "ERROR:: Locus $locus_id is obsolete!";
47 $json_hash{error}="Locus $locus_id is obsolete!";
49 unless ( ( $locus_id =~ m /^\d+$/ || !$locus_id ) ) {
50 #print STDERR "ERROR:: 'No locus exists for identifier $locus_id'\n\n";
51 $json_hash{error}="No locus exists for identifier $locus_id";
53 $self->set_json_hash(%json_hash);
54 $self->set_primary_key("locus_id");
55 $self->set_owners( $self->get_object()->get_owners() );
57 $self->return_json() if $json_hash{error};
60 sub display_form {
61 my $self = shift;
62 my %json_hash = $self->get_json_hash();
64 if (!($json_hash{html}) ) { $json_hash{html} = $self->get_form()->as_table_string() ; }
65 $self->check_modify_privileges();
67 $json_hash{"user_type"} = $self->get_user()->get_user_type();
68 $json_hash{"is_owner"} = $self->get_is_owner();
70 $json_hash{"editable_form_id"} = $self->get_form()->get_form_id();
73 $self->set_json_hash(%json_hash);
74 $self->return_json();
77 sub store {
78 my $self=shift;
80 my $locus = $self->get_object();
81 my $locus_id = $self->get_object_id();
82 my %args = $self->get_args();
83 my %json_hash = $self->get_json_hash();
84 my $initial_locus_id = $locus_id;
86 my $error;
87 $locus->set_common_name_id($args{common_name_id});
89 my ($message) =
90 $locus->exists_in_database( $args{locus_name}, $args{locus_symbol} );
91 my $validate;
92 if ($message) {
93 $error = " Locus $args{locus_name} (symbol= $args{locus_symbol} ) already exists in the database ";
94 }else {
95 try{
96 $self->SUPER::store(); #this sets $json_hash{validate} if the form validation failed.
97 $locus_id = $locus->get_locus_id() ;
98 } catch {
99 $error = " An error occurred. Cannot store to the database\n An email message has been sent to the SGN development team";
100 CXGN::Contact::send_email('locus_ajax_form.pl died', $error . "\n" . $_ , 'sgn-bugs@sgn.cornell.edu');
103 #the validate field is false is validation passed for all fields, true if did not pass and the form is re-printed
104 #$json_hash{validate}= $validate;
105 %json_hash= $self->get_json_hash();
106 $validate= $json_hash{validate};
107 $json_hash{error} = $error if $error;
109 my $refering_page="/phenome/locus_display.pl?locus_id=$locus_id";
110 $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});
111 $json_hash{refering_page}=$refering_page if !$initial_locus_id && !$validate && !$error;
113 $self->set_json_hash(%json_hash);
115 $self->return_json();
119 ####################################
120 sub delete {
121 ##Delete the locus (actually set obsolete = 't')
122 my $self = shift;
123 my $check = $self->check_modify_privileges();
124 $self->return_json() if $check ; #error or no user privileges
126 my $locus = $self->get_object();
127 my $locus_name = $locus->get_locus_name();
128 my $locus_id = $locus->get_locus_id();
129 my %json_hash= $self->get_json_hash();
130 my $refering_page="/phenome/locus_display.pl?locus_id=$locus_id";
132 if (!$json_hash{error} ) {
133 try {
134 $locus->delete();
135 }catch {
136 $json_hash{error} = " An error occurred. Cannot delete locus\n An email message has been sent to the SGN development team";
138 $json_hash{reload} = 1;
140 $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});
141 $self->set_json_hash(%json_hash);
142 $self->return_json();
147 sub generate_form {
148 my $self = shift;
149 my $form_id = 'edit_locus'; # a form_id is required for ajax forms
151 $self->init_form($form_id) ; ## instantiate static/editable/confirmStore form
153 my $locus = $self->get_object();
154 my %args = $self->get_args();
155 my $form = $self->get_form();
156 my $dbh = $self->get_dbh();
158 my ( $organism_names_ref, $organism_ids_ref ) =
159 CXGN::Tools::Organism::get_all_organisms( $self->get_dbh() );
160 my ($lg_names_ref) =
161 CXGN::Phenome::Locus::LinkageGroup::get_all_lgs( $self->get_dbh() );
162 my ($lg_arms_ref) =
163 CXGN::Phenome::Locus::LinkageGroup::get_lg_arms( $self->get_dbh() );
165 if ( $self->get_action =~ /new|store/ ) {
166 $self->get_form->add_select(
167 display_name => "Organism ",
168 field_name => "common_name_id",
169 contents => $locus->get_common_name_id(),
170 length => 20,
171 object => $locus,
172 getter => "get_common_name_id",
173 setter => "set_common_name_id",
174 select_list_ref => $organism_names_ref,
175 select_id_list_ref => $organism_ids_ref,
179 if ( $locus->get_obsolete() eq 't' ) {
180 $form->add_label(
181 display_name => "Status",
182 field_name => "obsolete_stat",
183 contents => 'OBSOLETE',
186 $form->add_field(
187 display_name => "Locus name ",
188 field_name => "locus_name",
189 object => $locus,
190 getter => "get_locus_name",
191 setter => "set_locus_name",
192 validate => 'string',
195 $form->add_field(
196 display_name => "Symbol ",
197 field_name => "locus_symbol",
198 object => $locus,
199 getter => "get_locus_symbol",
200 setter => "set_locus_symbol",
201 validate => 'token',
202 formatting => '<i>*</i>',
205 $form->add_field(
206 display_name => "Gene activity ",
207 field_name => "gene_activity",
208 object => $locus,
209 getter => "get_gene_activity",
210 setter => "set_gene_activity",
211 length => '50',
214 $form->add_textarea(
215 display_name => "Description ",
216 field_name => "description",
217 object => $locus,
218 getter => "get_description",
219 setter => "set_description",
220 columns => 40,
221 rows => => 4,
224 $form->add_select(
225 display_name => "Chromosome ",
226 field_name => "lg_name",
227 contents => $locus->get_linkage_group(),
228 length => 10,
229 object => $locus,
230 getter => "get_linkage_group",
231 setter => "set_linkage_group",
232 select_list_ref => $lg_names_ref,
233 select_id_list_ref => $lg_names_ref,
236 $form->add_select(
237 display_name => "Arm",
238 field_name => "lg_arm",
239 contents => $locus->get_lg_arm(),
240 length => 10,
241 object => $locus,
242 getter => "get_lg_arm",
243 setter => "set_lg_arm",
244 select_list_ref => $lg_arms_ref,
245 select_id_list_ref => $lg_arms_ref,
248 $form->add_hidden(
249 field_name => "locus_id",
250 contents => $locus->get_locus_id(),
253 $form->add_hidden(
254 field_name => "action",
255 contents => "store",
258 $form->add_hidden(
259 field_name => "sp_person_id",
260 contents => $self->get_user()->get_sp_person_id(),
261 object => $locus,
262 setter => "set_sp_person_id",
265 $form->add_hidden(
266 field_name => "updated_by",
267 contents => $self->get_user()->get_sp_person_id(),
268 object => $locus,
269 setter => "set_updated_by",
272 if ( $self->get_action() =~ /view|edit/ ) {
273 $form->from_database();
274 $form->add_hidden(
275 field_name => "common_name_id",
276 contents => $locus->get_common_name_id(),
280 elsif ( $self->get_action() =~ /store/ ) {
281 $form->from_request( %args );