Merge pull request #2660 from solgenomics/isaak-selection-prop
[sgn.git] / cgi-bin / jsforms / locusgroup_ajax_form.pl
blobdca210c01af3591cc1aee5493b66f7e88c61251e
2 use strict;
3 use warnings;
5 my $form = CXGN::Phenome::LocusGroupForm->new();
7 package CXGN::Phenome::LocusGroupForm;
9 use base qw/CXGN::Page::Form::AjaxFormPage /;
11 use CXGN::Phenome::LocusGroup;
12 use CXGN::Phenome::Schema;
14 use CXGN::Tools::Organism;
16 use CXGN::People::Person;
17 use CXGN::Contact;
18 use CXGN::Feed;
20 use Try::Tiny;
22 use JSON;
25 sub new {
26 my $class = shift;
27 my $self = $class->SUPER::new(@_);
28 return $self;
31 sub define_object {
32 my $self = shift;
33 my %args = $self->get_args();
34 my $lg_id = $args{locusgroup_id} || $args{object_id};
35 my $user_type = $self->get_user()->get_user_type();
36 my %json_hash= $self->get_json_hash();
37 my $schema= CXGN::Phenome::Schema->connect( sub { $self->get_dbh->get_actual_dbh } , { on_connect_do => ['set search_path to public,phenome;'] } );
39 $self->set_object_id($lg_id);
40 $self->set_object_name('LocusGroup'); #this is useful for email messages
41 $self->set_object(
42 CXGN::Phenome::LocusGroup->new( $schema, $self->get_object_id ) );
43 if ( $self->get_object()->get_obsolete() eq 't' && $user_type ne 'curator' )
45 $json_hash{error}="Locus group $lg_id is obsolete!";
47 unless ( ( $lg_id =~ m /^\d+$/ || !$lg_id ) ) {
48 $json_hash{error}="No locus group exists for identifier $lg_id";
50 $self->set_json_hash(%json_hash);
51 $self->set_primary_key("locusgroup_id");
52 $self->set_owners( $self->get_object->get_sp_person_id );
53 $self->print_json() if $json_hash{error};
57 sub store {
58 my $self=shift;
59 my $locusgroup = $self->get_object();
60 my $lg_id = $self->get_object_id();
61 my %args = $self->get_args();
62 my %json_hash = $self->get_json_hash();
63 my $initial_lg_id = $lg_id;
65 my $error;
66 print STDERR "****locusgroup name arg = " . $args{locusgroup_name} ;
67 $locusgroup->set_locusgroup_name($args{locusgroup_name});
68 my ($message) = $locusgroup->exists_in_database($args{locusgroup_name});
69 my $validate;
70 if ($message) {
71 $error = " Locus group $args{locusgroup_name} already exists in the database ";
72 }else {
73 try{
74 $self->SUPER::store(); #this sets $json_hash{validate} if the form validation failed.
75 $lg_id = $locusgroup->get_locusgroup_id() ;
76 } catch {
77 $error = " An error occurred. Cannot store to the database\n An email message has been sent to the SGN development team";
78 CXGN::Contact::send_email('locus_ajax_form.pl died', $error . "\n" . $_ , 'sgn-bugs@sgn.cornell.edu');
81 #the validate field is false is validation passed for all fields, true if did not pass and the form is re-printed
82 #$json_hash{validate}= $validate;
83 %json_hash= $self->get_json_hash();
84 $validate= $json_hash{validate};
85 $json_hash{error} = $error if $error;
87 my $refering_page="/genefamily/manual/$lg_id/view";
88 $self->send_form_email({subject=>"[New locusgroup details stored] locusgroup $lg_id", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page}) if (!$validate && !$json_hash{error});
89 $json_hash{refering_page}=$refering_page if !$initial_lg_id && !$validate && !$error;
91 $self->set_json_hash(%json_hash);
92 $self->print_json();
96 ####################################
97 sub delete {
98 ##Delete the locusgroup (actually set obsolete = 't')
99 my $self = shift;
100 my $check = $self->check_modify_privileges();
101 $self->print_json() if $check ; #error or no user privileges
103 my $locusgroup = $self->get_object();
104 my $lg_name = $locusgroup->get_locusgroup_name();
105 my $lg_id = $locusgroup->get_locusgroup_id();
106 my %json_hash= $self->get_json_hash();
107 my $refering_page="/genefamily/manual/$lg_id/view";
108 if (!$json_hash{error} ) {
109 try {
110 $locusgroup->delete();
111 }catch {
112 $json_hash{error} = " An error occurred. Cannot delete locusgroup\n An email message has been sent to the SGN development team";
114 $json_hash{reload} = 1;
116 $self->send_form_email({subject=>"LocusGroup obsoleted ($lg_name)", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page, action=>'delete'}) if (!$json_hash{error});
117 $self->set_json_hash(%json_hash);
118 $self->print_json();
123 sub generate_form {
124 my $self = shift;
125 my $form_id = 'edit_locusgroup'; # a form_id is required for ajax forms
126 $self->init_form($form_id) ; ## instantiate static/editable/confirmStore form
128 my $locusgroup = $self->get_object();
129 my %args = $self->get_args();
130 my $form = $self->get_form();
131 my $relationship = $locusgroup->get_relationship_name;
133 if ( $locusgroup->get_obsolete() eq 't' ) {
134 $form->add_label(
135 display_name => "Status",
136 field_name => "obsolete_stat",
137 contents => 'OBSOLETE',
140 $form->add_field(
141 display_name => "Gene family name",
142 field_name => "locusgroup_name",
143 id => "locusgroup_name",
144 object => $locusgroup,
145 getter => "get_locusgroup_name",
146 setter => "set_locusgroup_name",
147 validate => 'string',
149 $form->add_label(
150 display_name => "Relationship",
151 field_name => "relationship",
152 contents => $relationship,
154 #$form->add_select(
155 # display_name => "Chromosome ",
156 # field_name => "lg_name",
157 # contents => $locus->get_linkage_group(),
158 # length => 10,
159 # object => $locus,
160 # getter => "get_linkage_group",
161 # setter => "set_linkage_group",
162 #select_list_ref => $lg_names_ref,
163 #select_id_list_ref => $lg_names_ref,
164 # );
166 $form->add_hidden(
167 field_name => "locusgroup_id",
168 contents => $locusgroup->get_locusgroup_id(),
171 $form->add_hidden(
172 field_name => "action",
173 contents => "store",
176 $form->add_hidden(
177 field_name => "sp_person_id",
178 contents => $self->get_user()->get_sp_person_id(),
179 object => $locusgroup,
180 setter => "set_sp_person_id",
183 # $form->add_hidden(
184 # field_name => "updated_by",
185 # contents => $self->get_user()->get_sp_person_id(),
186 # object => $locusgroup,
187 # setter => "set_updated_by",
188 # );
190 if ( $self->get_action() =~ /view|edit/ ) {
191 $form->from_database();
192 #$form->add_hidden(
193 # field_name => "common_name_id",
194 # contents => $locus->get_common_name_id(),
195 # );
197 elsif ( $self->get_action() =~ /store/ ) {
198 $form->from_request( %args );