Merge pull request #5205 from solgenomics/topic/generic_trial_upload
[sgn.git] / cgi-bin / image / ajax / image_ajax_form.pl
blob0d76f5ae54a6fd5d02d939949d3e2f28d88e4fba
2 use strict;
3 use warnings;
5 my $image_ajax_form = CXGN::ImageAjaxForm->new();
7 package CXGN::ImageAjaxForm;
9 use CatalystX::GlobalContext '$c';
11 use base "CXGN::Page::Form::AjaxFormPage";
13 use JSON;
14 use SGN::Image;
15 use Try::Tiny;
17 sub define_object {
18 my $self = shift;
20 my %json_hash = $self->get_json_hash();
22 # call set_object_id, set_object and set_primary_key here
23 # with the appropriate parameters.
25 my %args = $self->get_args();
27 if ( !exists( $args{object_id} ) ) {
28 $json_hash{error} = "No object_id provided";
31 $self->set_object_name('image');
32 $self->set_object_id( $args{object_id} );
33 $self->set_object(
34 SGN::Image->new( $self->get_dbh(), $self->get_object_id(), $c ) );
35 $self->set_primary_key("object_id");
36 $self->set_owners( $self->get_object()->get_sp_person_id() );
37 $self->set_json_hash(%json_hash);
40 sub generate_form {
41 my $self = shift;
43 my $form_id = 'image_form';
44 my %args = $self->get_args();
46 $self->init_form($form_id);
47 my $form = $self->get_form();
49 my $image = $self->get_object();
50 my $object_id = $self->get_object_id();
51 my $submitter = CXGN::People::Person->new( $self->get_dbh(), $image->get_sp_person_id() );
52 my $sp_person_id = $submitter->get_sp_person_id();
53 my $submitter_name = ($submitter->get_first_name() || '') . " " . ($submitter->get_last_name() || '');
54 my $submitter_link = qq |<a href="/solpeople/personal-info.pl?sp_person_id=$sp_person_id">$submitter_name </a> |;
56 my $name = "";
57 my $description = "";
59 if ( $self->get_action() =~ /view|edit/i ) {
60 $name = $image->get_name();
61 $description = $image->get_description();
63 if ( $self->get_action() =~ /edit/i ) {
64 $name ||= $args{name};
65 $description ||= $args{description};
67 if ( $self->get_action() =~ /store/i ) {
68 $name = $args{name};
69 $description = $args{description};
72 $form->add_field(
73 display_name => "Image Name:",
74 field_name => "name",
75 contents => $name,
76 length => 15,
77 object => $image,
78 getter => "get_name",
79 setter => "set_name"
81 $form->add_field(
82 display_name => "Image Description: ",
83 field_name => "description",
84 contents => $description,
85 length => 40,
86 object => $image,
87 getter => "get_description",
88 setter => "set_description"
90 $form->add_hidden(
91 display_name => "Image ID",
92 field_name => "object_id",
93 contents => $object_id
95 $form->add_hidden(
96 display_name => "Action",
97 field_name => "action",
98 contents => "store"
100 $form->add_label(
101 display_name => "Uploaded by: ",
102 field_name => "submitter",
103 contents => $submitter_link,
105 $self->set_form($form);
109 sub delete {
110 my ( $self ) = @_;
112 $self->check_modify_privileges
113 or $self->print_json;
116 my %json = $self->get_json_hash;
118 try {
119 $self->get_object->delete;
120 $json{success} = 1;
121 } catch {
122 $json{error} = "Deletion failed ($_)";
125 #$self->set_json_hash( %json );
126 #$self->print_json;
131 sub store {
132 my $self=shift;
133 my $image = $self->get_object();
134 my $image_id = $self->get_object_id();
135 my %args = $self->get_args();
136 my %json_hash = $self->get_json_hash();
137 my $error;
138 try{
139 $self->SUPER::store(); #this sets $json_hash{validate} if the form validation failed.
140 $image_id = $image->get_image_id;
141 } catch {
142 $error = " An error occurred. Cannot store to the database\n An email message has been sent to the SGN development team";
143 CXGN::Contact::send_email('image_ajax_form.pl died', $error . "\n" . $_ , 'sgn-bugs@sgn.cornell.edu');
145 #the validate field is false is validation passed for all fields, true if did not pass and the form is re-printed
147 %json_hash= $self->get_json_hash();
148 my $validate= $json_hash{validate};
149 $json_hash{error} = $error if $error;
151 my $refering_page="/image/index.pl?image_id=$image_id";
152 $self->send_form_email({subject=>"[New image details stored] image $image_id", mailing_list=>'sgn-db-curation@sgn.cornell.edu', refering_page=>"www.solgenomics.net".$refering_page}) if (!$validate && !$json_hash{error});
153 $json_hash{refering_page}=$refering_page if !$validate && !$error;
155 $self->set_json_hash(%json_hash);
156 $self->print_json();