minor tweak of announcement.
[sgn.git] / cgi-bin / phenome / stock / stock_ajax_form.pl
blobd3a80d0592207342e17d8ed47822dd82f667fca4
1 use strict;
2 use warnings;
4 package CXGN::Phenome::Stock::StockForm;
5 my $stock_form = CXGN::Phenome::Stock::StockForm->new();
7 use base qw/CXGN::Page::Form::AjaxFormPage /;
10 use CXGN::Tools::Organism;
11 use Bio::Chado::Schema;
12 use CXGN::Chado::Stock;
14 use CXGN::People::Person;
15 use CXGN::Contact;
18 use Try::Tiny;
20 use JSON;
21 use CatalystX::GlobalContext qw( $c );
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 $stock_id = $args{stock_id} || $args{object_id};
34 my $user_type = $self->get_user()->get_user_type();
35 my %json_hash= $self->get_json_hash();
37 my $schema = $c->dbic_schema( 'Bio::Chado::Schema', 'sgn_chado' );
39 $self->set_object_id($stock_id);
40 $self->set_object_name('Stock'); #this is useful for email messages
41 $self->set_object( CXGN::Chado::Stock->new($schema, $stock_id) );
43 if ( $self->get_object()->get_is_obsolete() == 1 && $user_type ne 'curator' )
45 $json_hash{error}="Stock $stock_id is obsolete!";
47 unless ( ( $stock_id =~ m /^\d+$/ || !$stock_id ) ) {
48 $json_hash{error}="No stock exists for identifier $stock_id";
50 $self->set_json_hash(%json_hash);
51 $self->set_primary_key("stock_id");
52 my @owners;
53 #my @owners = $self->get_object()->search_related("stockprops", {
54 #type_id => $sp_person_cvterm_id } );
56 $self->set_owners( @owners );
58 $self->print_json() if $json_hash{error};
62 sub store {
63 my $self=shift;
65 my $stock = $self->get_object();
66 my $stock_id = $self->get_object_id();
67 my $bcs_stock = $stock->get_object_row();
68 my %args = $self->get_args();
69 my %json_hash = $self->get_json_hash();
70 my $initial_stock_id = $stock_id;
72 my $error;
73 $stock->set_species($args{organism});
74 $stock->set_type_id($args{type_id});
75 $stock->set_name($args{name});
76 $stock->set_uniquename($args{uniquename});
77 $stock->set_description($args{description});
80 my $message = $stock->exists_in_database();
81 my $validate;
82 if ($message) {
83 $error = " Stock $args{uniquename} already exists in the database ";
84 }else {
85 try{
86 $self->SUPER::store(); #this sets $json_hash{validate} if the form validation failed.
87 $stock_id = $stock->get_stock_id() ;
88 } catch {
89 $error = " An error occurred. Cannot store to the database\n An email message has been sent to the SGN development team";
90 CXGN::Contact::send_email('stock_ajax_form.pl died', $error . "\n" . $_ , 'sgn-bugs@sgn.cornell.edu');
93 #the validate field is false is validation passed for all fields, true if did not pass and the form is re-printed
95 %json_hash= $self->get_json_hash();
96 $validate= $json_hash{validate};
97 $json_hash{error} = $error if $error;
99 my $refering_page="/stock/$stock_id/view";
100 $self->send_form_email({subject=>"[New stock details stored] stock $stock_id", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page}) if (!$validate && !$json_hash{error});
101 $json_hash{refering_page}=$refering_page if !$initial_stock_id && !$validate && !$error;
103 $self->set_json_hash(%json_hash);
105 $self->print_json();
109 ####################################
110 sub delete {
111 ##Delete the stock (actually set obsolete = 't')
112 my $self = shift;
113 my $check = $self->check_modify_privileges();
114 $self->print_json() if $check ; #error or no user privileges
116 my $stock = $self->get_object();
117 my $stock_name = $stock->get_name();
118 my $stock_id = $stock->get_stock_id();
119 my %json_hash= $self->get_json_hash();
120 my $refering_page="/phenome/stock/view/id/$stock_id";
122 if (!$json_hash{error} ) {
123 try {
124 $stock->set_is_obsolete(1) ;
125 $stock->store();
126 }catch {
127 $json_hash{error} = " An error occurred. Cannot delete stock\n An email message has been sent to the SGN development team";
128 $self->send_form_email({subject=>"Stock delete failed! ($stock_name) $_", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page, action=>'delete'});
130 $json_hash{reload} = 1;
132 $self->send_form_email({subject=>"Stock obsoleted ($stock_name)", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page, action=>'delete'}) if (!$json_hash{error});
133 $self->set_json_hash(%json_hash);
134 $self->print_json();
139 sub generate_form {
140 my $self = shift;
141 my $form_id = 'edit_stock'; # a form_id is required for ajax forms
143 $self->init_form($form_id) ; ## instantiate static/editable/confirmStore form
144 my $stock = $self->get_object();
145 my $bcs_stock = $stock->get_object_row();
146 my %args = $self->get_args();
147 my $form = $self->get_form();
149 #########
150 my $organism_obj = $bcs_stock->organism if $bcs_stock;
151 my $species = $organism_obj ? $organism_obj->species : undef;
152 my $organism_id = $stock->get_organism_id;
153 my ($stock_type) = $stock->get_schema->resultset("Cv::Cv")->search(
154 { name => 'stock type' , }
156 my @types;
157 my @type_ids;
158 if ($stock_type) {
159 my $cvterm_res = $stock_type->
160 search_related('cvterms');
161 while (my $cvterm = $cvterm_res->next() ) {
162 push @types, $cvterm->name() ;
163 push @type_ids, $cvterm->cvterm_id();
166 ##########
168 if ( $self->get_action =~ /new|store|edit/ ) {
169 $form->add_field(
170 display_name => "Organism",
171 field_name => "Organism",
172 id => "species_name",
173 object => $stock,
174 getter => "get_species",
175 setter => "set_species",
176 autocomplete => '/ajax/organism/autocomplete',
178 $self->get_form->add_select(
179 display_name => "Stock type",
180 field_name => "type_id",
181 contents => $stock->get_type_id(),
182 length => 20,
183 object => $stock,
184 getter => "get_type_id",
185 setter => "set_type_id",
186 select_list_ref => \@types,
187 select_id_list_ref => \@type_ids,
190 if ( $stock->get_is_obsolete() ) {
191 $form->add_label(
192 display_name => "Status",
193 field_name => "obsolete_stat",
194 contents => 'OBSOLETE',
197 if ( $self->get_action =~ /view/ ) {
198 $form->add_label(
199 display_name => "Organism",
200 field_name => "stock_organism",
201 contents => $species ? qq|<a href="/organism/$organism_id/view"> | . $species . "</a>" : '',
203 $form->add_label(
204 display_name => "Stock type",
205 field_name => "stock_type",
206 contents => $stock->get_type()->name() ,
210 $form->add_field(
211 display_name => "Stock name ",
212 field_name => "name",
213 object => $stock,
214 getter => "get_name",
215 setter => "set_name",
216 validate => 'string',
218 $form->add_field(
219 display_name => "Uniquename ",
220 field_name => "uniquename",
221 object => $stock,
222 getter => "get_uniquename",
223 setter => "set_uniquename",
224 validate => 'string',
227 $form->add_textarea(
228 display_name => "Description",
229 field_name => "description",
230 object => $stock,
231 getter => "get_description",
232 setter => "set_description",
233 columns => 40,
234 rows => 4,
237 $form->add_hidden(
238 field_name => "stock_id",
239 contents => $stock->get_stock_id(),
242 $form->add_hidden(
243 field_name => "action",
244 contents => "store",
246 if ($self->get_action =~ /view/ ) {
247 $form->add_hidden(
248 field_name => "type_id",
249 contents => $stock->get_type_id,
252 if ( $self->get_action() =~ /view|edit/ ) {
253 $form->from_database();
255 elsif ( $self->get_action() =~ /store/ ) {
256 my %json_hash = $self->get_json_hash() ;
257 print $json_hash{html} ;
258 $form->from_request( %args );