Merge branch 'master' into topic/simple_image_upload
[sgn.git] / cgi-bin / jsforms / pub_ajax_form.pl
blobb9495d6ab4898a0b3a4255fcc080d9f82d01a596
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 $schema = $c->dbic_schema( 'Bio::Chado::Schema', 'sgn_chado' );
39 my $dbh = $schema->storage->dbh;
40 $self->set_object_id($pub_id);
41 $self->set_object_name('publication'); #this is useful for email messages
42 $self->set_object( CXGN::Chado::Publication->new($dbh, $pub_id) );
45 unless ( ( $pub_id =~ m /^\d+$/ || !$pub_id ) ) {
46 $json_hash{error}="No publication exists for identifier $pub_id";
48 $self->set_json_hash(%json_hash);
49 $self->set_primary_key("pub_id");
52 $self->print_json() if $json_hash{error};
56 sub store {
57 my $self=shift;
59 my $pub = $self->get_object();
60 my $pub_id = $self->get_object_id();
62 my %args = $self->get_args();
63 my %json_hash = $self->get_json_hash();
64 my $initial_pub_id = $pub_id;
66 my $error;
69 $pub->set_title($args{title});
70 $pub->set_series_name($args{series});
71 $pub->set_volume($args{volume});
72 $pub->set_issue($args{issue});
73 $pub->set_pyear($args{pyear});
74 $pub->set_pages($args{pages});
75 $pub->set_abstract($args{abstract});
76 $pub->set_author_string($args{authors});
77 $pub->set_cvterm_name($args{cvterm_name});
79 #########
81 my $validate;
82 try{
83 $self->SUPER::store(); #this sets $json_hash{validate} if the form validation failed.
84 $pub_id = $pub->get_pub_id() ;
85 } catch {
86 $error = " An error occurred. Cannot store to the database\n An email message has been sent to the SGN development team";
87 CXGN::Contact::send_email('pub_ajax_form.pl died', $error . "\n" . $_ , 'sgn-bugs@sgn.cornell.edu');
90 #the validate field is false is validation passed for all fields, true if did not pass and the form is re-printed
92 %json_hash= $self->get_json_hash();
93 $validate= $json_hash{validate};
94 $json_hash{error} = $error if $error;
96 my $refering_page="/publication/$pub_id/view";
97 $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});
98 $json_hash{refering_page}=$refering_page if !$initial_pub_id && !$validate && !$error;
100 $self->set_json_hash(%json_hash);
101 $self->print_json();
105 ####################################
106 sub delete {
107 my $self = shift;
108 my $check = $self->check_modify_privileges();
109 $self->print_json() if $check ; #error or no user privileges
111 my $pub = $self->get_object();
112 my $pub_id = $pub->get_pub_id;
113 my $pub_title;
115 my %json_hash= $self->get_json_hash();
116 my $refering_page="/publication/$pub_id/view";
117 my $message;
119 if ($pub_id && !$json_hash{error} ) {
120 $pub_title = $pub->get_title;
122 try {
123 $message = $pub->delete ;
124 $json_hash{error} = $message;
125 }catch {
126 $json_hash{error} = " An error occurred. Cannot delete publication\n An email message has been sent to the SGN development team";
127 $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'});
129 $json_hash{reload} = 1;
132 $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});
133 $self->set_json_hash(%json_hash);
134 $self->print_json();
140 sub generate_form {
141 my $self = shift;
142 my $form_id = 'edit_pub'; # a form_id is required for ajax forms
144 $self->init_form($form_id) ; ## instantiate static/editable/confirmStore form
145 my $pub = $self->get_object();
146 my %args = $self->get_args();
147 my $form = $self->get_form();
148 my $type = $args{type};
149 my $type_id = $args{type_id};
150 my $refering_page= $args{refering_page};
152 my @types = qw |journal book curator |;
154 #########
155 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');
157 if ($self->get_action =~ /new|edit/ ) {
158 $form->add_select(
159 display_name => "Publication type",
160 field_name => "cvterm_name",
161 contents => $pub->get_cvterm_name(),
162 length => 20,
163 object => $pub,
164 getter => "get_cvterm_name",
165 setter => "set_cvterm_name",
166 select_list_ref => \@types,
167 select_id_list_ref => \@types,
170 $form->add_textarea(
171 display_name => "Title",
172 field_name => "title",
173 object => $pub,
174 getter => "get_title",
175 setter => "set_title",
176 validate => 'string',
177 columns => 80,
178 rows => 1,
180 $form->add_field(
181 display_name => "Series name",
182 field_name => "series_name",
183 object => $pub,
184 getter => "get_series_name",
185 setter => "set_series_name",
186 validate => 'string',
189 $form->add_field(
190 display_name => "Volume",
191 field_name => "volume",
192 object => $pub,
193 getter => "get_volume",
194 setter => "set_volume",
196 $form->add_field(
197 display_name => "Issue",
198 field_name => "issue",
199 object => $pub,
200 getter => "get_issue",
201 setter => "set_issue",
203 $form->add_field(
204 display_name => "Year",
205 field_name => "year",
206 object => $pub,
207 getter => "get_pyear",
208 setter => "set_pyear",
209 validate => 'integer',
211 $form->add_field(
212 display_name => "Pages",
213 field_name => "pages",
214 object => $pub,
215 getter => "get_pages",
216 setter => "set_pages",
217 validate => 'string',
219 $form->add_field(
220 display_name => $author_example,
221 field_name => "author",
222 object => $pub,
223 getter => "get_authors_as_string",
224 setter => "set_author_string",
225 columns => 80,
226 rows => 1,
229 $form->add_textarea(
230 display_name => "Abstract",
231 field_name => "abstract",
232 object => $pub,
233 getter => "get_abstract",
234 setter => "set_abstract",
235 columns => 80,
236 rows => 12,
239 $form->add_hidden(
240 field_name => "pub_id",
241 contents => $pub->get_pub_id(),
242 object => $pub,
243 getter => "get_pub_id",
244 setter => "set_pub_id",
247 $form->add_hidden(
248 field_name => "type",
249 contents => $type,
251 $form->add_hidden(
252 field_name => "type_id",
253 contents => $type_id,
255 $form->add_hidden(
256 field_name => "refering_page",
257 contents => $refering_page,
259 $form->add_hidden(
260 field_name => "action",
261 contents => "store",
265 if ( $self->get_action() =~ /view|edit/ ) {
266 $form->from_database();
268 elsif ( $self->get_action() =~ /store/ ) {
269 my %json_hash = $self->get_json_hash() ;
270 print $json_hash{html} ;
271 $form->from_request( %args );