debugging
[sgn.git] / cgi-bin / jsforms / pub_ajax_form.pl
blob4191293295aadda09c9ff68f45b3107b47cc8a3b
1 use strict;
2 use warnings;
4 package CXGN::Chado::Pub::PubForm;
5 my $pub_form = CXGN::Chado::Pub::PubForm->new();
7 use base qw/CXGN::Page::Form::AjaxFormPage /;
10 use Bio::Chado::Schema;
11 use CXGN::Chado::Publication;
13 use CXGN::People::Person;
14 use CXGN::Contact;
15 use CXGN::Page::FormattingHelpers qw/
16 tooltipped_text
19 use Try::Tiny;
21 use JSON;
22 use CatalystX::GlobalContext qw( $c );
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 $pub_id = $args{pub_id} || $args{object_id};
35 my $user_type = $self->get_user()->get_user_type();
36 my %json_hash= $self->get_json_hash();
38 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
39 my $schema = $c->dbic_schema( 'Bio::Chado::Schema', 'sgn_chado', $sp_person_id );
40 my $dbh = $schema->storage->dbh;
41 $self->set_object_id($pub_id);
42 $self->set_object_name('publication'); #this is useful for email messages
43 $self->set_object( CXGN::Chado::Publication->new($dbh, $pub_id) );
46 unless ( ( $pub_id =~ m /^\d+$/ || !$pub_id ) ) {
47 $json_hash{error}="No publication exists for identifier $pub_id";
49 $self->set_json_hash(%json_hash);
50 $self->set_primary_key("pub_id");
53 $self->print_json() if $json_hash{error};
57 sub store {
58 my $self=shift;
60 my $pub = $self->get_object();
61 my $pub_id = $self->get_object_id();
63 my %args = $self->get_args();
64 my %json_hash = $self->get_json_hash();
65 my $initial_pub_id = $pub_id;
67 my $error;
70 $pub->set_title($args{title});
71 $pub->set_series_name($args{series});
72 $pub->set_volume($args{volume});
73 $pub->set_issue($args{issue});
74 $pub->set_pyear($args{pyear});
75 $pub->set_pages($args{pages});
76 $pub->set_abstract($args{abstract});
77 $pub->set_author_string($args{authors});
78 $pub->set_cvterm_name($args{cvterm_name});
80 #########
82 my $validate;
83 try{
84 $self->SUPER::store(); #this sets $json_hash{validate} if the form validation failed.
85 $pub_id = $pub->get_pub_id() ;
86 } catch {
87 $error = " An error occurred. Cannot store to the database\n An email message has been sent to the SGN development team";
88 CXGN::Contact::send_email('pub_ajax_form.pl died', $error . "\n" . $_ , 'sgn-bugs@sgn.cornell.edu');
91 #the validate field is false is validation passed for all fields, true if did not pass and the form is re-printed
93 %json_hash= $self->get_json_hash();
94 $validate= $json_hash{validate};
95 $json_hash{error} = $error if $error;
97 my $refering_page="/publication/$pub_id/view";
98 $self->send_form_email({subject=>"[New publication details stored] publication $pub_id", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page}) if (!$validate && !$json_hash{error});
99 $json_hash{refering_page}=$refering_page if !$initial_pub_id && !$validate && !$error;
101 $self->set_json_hash(%json_hash);
102 $self->print_json();
106 ####################################
107 sub delete {
108 my $self = shift;
109 my $check = $self->check_modify_privileges();
110 $self->print_json() if $check ; #error or no user privileges
112 my $pub = $self->get_object();
113 my $pub_id = $pub->get_pub_id;
114 my $pub_title;
116 my %json_hash= $self->get_json_hash();
117 my $refering_page="/publication/$pub_id/view";
118 my $message;
120 if ($pub_id && !$json_hash{error} ) {
121 $pub_title = $pub->get_title;
123 try {
124 $message = $pub->delete ;
125 $json_hash{error} = $message;
126 }catch {
127 $json_hash{error} = " An error occurred. Cannot delete publication\n An email message has been sent to the SGN development team";
128 $self->send_form_email({subject=>"Publication delete failed! ($pub_id) $_", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page, action=>'delete'});
130 $json_hash{reload} = 1;
133 $self->send_form_email({subject=>"Publication deleted ($pub_id)", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page, action=>'delete'}) if (!$json_hash{error});
134 $self->set_json_hash(%json_hash);
135 $self->print_json();
141 sub generate_form {
142 my $self = shift;
143 my $form_id = 'edit_pub'; # a form_id is required for ajax forms
145 $self->init_form($form_id) ; ## instantiate static/editable/confirmStore form
146 my $pub = $self->get_object();
147 my %args = $self->get_args();
148 my $form = $self->get_form();
149 my $type = $args{type};
150 my $type_id = $args{type_id};
151 my $refering_page= $args{refering_page};
153 my @types = qw |journal book curator |;
155 #########
156 my $author_example = tooltipped_text('Authors', 'Author names should be entered in the order of last name, followed by "," then first name followed by ".". e.g Darwin, Charles. van Rijn, Henk. Giorgio,AB');
158 if ($self->get_action =~ /new|edit/ ) {
159 $form->add_select(
160 display_name => "Publication type",
161 field_name => "cvterm_name",
162 contents => $pub->get_cvterm_name(),
163 length => 20,
164 object => $pub,
165 getter => "get_cvterm_name",
166 setter => "set_cvterm_name",
167 select_list_ref => \@types,
168 select_id_list_ref => \@types,
171 $form->add_textarea(
172 display_name => "Title",
173 field_name => "title",
174 object => $pub,
175 getter => "get_title",
176 setter => "set_title",
177 validate => 'string',
178 columns => 80,
179 rows => 1,
181 $form->add_field(
182 display_name => "Series name",
183 field_name => "series_name",
184 object => $pub,
185 getter => "get_series_name",
186 setter => "set_series_name",
187 validate => 'string',
190 $form->add_field(
191 display_name => "Volume",
192 field_name => "volume",
193 object => $pub,
194 getter => "get_volume",
195 setter => "set_volume",
197 $form->add_field(
198 display_name => "Issue",
199 field_name => "issue",
200 object => $pub,
201 getter => "get_issue",
202 setter => "set_issue",
204 $form->add_field(
205 display_name => "Year",
206 field_name => "year",
207 object => $pub,
208 getter => "get_pyear",
209 setter => "set_pyear",
210 validate => 'integer',
212 $form->add_field(
213 display_name => "Pages",
214 field_name => "pages",
215 object => $pub,
216 getter => "get_pages",
217 setter => "set_pages",
218 validate => 'string',
220 $form->add_field(
221 display_name => $author_example,
222 field_name => "author",
223 object => $pub,
224 getter => "get_authors_as_string",
225 setter => "set_author_string",
226 columns => 80,
227 rows => 1,
230 $form->add_textarea(
231 display_name => "Abstract",
232 field_name => "abstract",
233 object => $pub,
234 getter => "get_abstract",
235 setter => "set_abstract",
236 columns => 80,
237 rows => 12,
240 $form->add_hidden(
241 field_name => "pub_id",
242 contents => $pub->get_pub_id(),
243 object => $pub,
244 getter => "get_pub_id",
245 setter => "set_pub_id",
248 $form->add_hidden(
249 field_name => "type",
250 contents => $type,
252 $form->add_hidden(
253 field_name => "type_id",
254 contents => $type_id,
256 $form->add_hidden(
257 field_name => "refering_page",
258 contents => $refering_page,
260 $form->add_hidden(
261 field_name => "action",
262 contents => "store",
266 if ( $self->get_action() =~ /view|edit/ ) {
267 $form->from_database();
269 elsif ( $self->get_action() =~ /store/ ) {
270 my %json_hash = $self->get_json_hash() ;
271 print $json_hash{html} ;
272 $form->from_request( %args );