fix test with new description field.
[sgn.git] / cgi-bin / insitu / detail / add_image.pl
blobaae5e1ac93c81228dbdc9bb1c34f10fdb2e4d7c4
2 use strict;
3 use CGI ();
5 use CXGN::Page;
6 use CXGN::Page::FormattingHelpers qw / blue_section_html page_title_html /;
7 use CXGN::Insitu::Image;
8 use CXGN::Insitu::Experiment;
10 # get the parameters.
11 # legal parameters are:
12 # action: new upload
13 # image_id: a valid image_id
14 # experiment_id: a valid experiment_id
15 # upload_file: the file to be uploaded, if action=upload.
17 my %args =();
19 # here we need to use the Apache2::Request methods for getting
20 # the parameters because uploading a file will cause the
21 # page object not to receive any of the parameters.
23 my $cgi = CGI->new;
24 $args{image_id} = $cgi->param("image_id");
25 $args{experiment_id} = $cgi->param("experiment_id");
26 $args{action}=$cgi->param("action");
27 $args{upload_file}=$cgi->param("upload_file");
29 # now that we got everthing safely, let's initiate the page object
31 my $page = CXGN::Page->new();
33 # get db connection
35 my $dbh = CXGN::DB::Connection->new("insitu");
37 # set the default action if none was supplied
39 if (!exists($args{action}) || !defined($args{action})) {
40 $args{action}="new";
43 # get the user_id if user is logged in for actions add and upload.
45 my $user = undef;
46 my $user_id=undef;
47 if ($args{action} =~/new|upload/i ) {
48 $user = CXGN::People::Person->new(CXGN::Login->new()->verify_session());
49 $user_id= $user->get_sp_person_id();
50 if ($user->get_user_type() !~ /curator|submitter/) {
51 $page->message_page("You do not have sufficient privileges to upload images. You need an account of type 'submitter'. Please contact SGN to change your account type. Sorry.");
55 if (!exists($args{experiment_id}) || !defined($args{experiment_id})) {
56 my $page = CXGN::Page->new();
58 $page->error_page("Need an experiment_id to add image.");
61 # create an new image object and go through the different
62 # possible actions. Emit error pages if needed.
64 my $image = CXGN::Insitu::Image->new($dbh);
65 $image -> set_experiment_id($args{experiment_id});
66 $image -> set_user_id($user_id);
68 if ($args{action} eq "new") {
69 add_dialog($image, %args);
70 exit();
72 elsif ($args{action} eq "upload") {
73 upload($dbh, $cgi, $image, %args);
75 else {
76 my $page = CXGN::Page->new();
77 $page->error_page("No recognized action defined.");
80 sub upload {
81 my $dbh = shift;
82 my $cgi = shift;
83 my $image = shift;
84 my %args = @_;
86 # deal with the upload options
88 # TODO fix this upload
89 my $upload = $cgi->upload();
90 my $upload_fh;
92 my $experiment = CXGN::Insitu::Experiment->new($dbh, $image->get_experiment_id());
93 #print STDERR "Uploading file $args{upload_file}...\n";
94 if (defined $upload) {
95 $upload_fh = $upload->fh();
96 $image->upload_image($experiment, $args{upload_file}, $upload_fh);
97 $image->process_image($args{upload_file}, $experiment);
98 my $experiment_id = $image->get_experiment_id();
99 my $page = CXGN::Page->new();
100 $page->header();
101 print "The following file was uploaded: <br />";
103 print $image->get_img_src_tag();
105 print qq { <br /><br />Return to <a href="experiment.pl?experiment_id=$experiment_id">experiment</a> };
108 $page->footer();
110 # we're done!
112 exit();
114 else {
115 my $page = CXGN::Page->new();
116 $page->error_page("A freakin error occurred!");
122 sub add_dialog {
123 my $image = shift;
126 my $page = CXGN::Page->new();
127 my $experiment_id = $image->get_experiment_id();
128 my $experiment = CXGN::Insitu::Experiment->new($image->get_dbh(),$experiment_id);
129 my $experiment_name = $experiment->get_name();
131 $page->header();
133 page_title_html("<a href=\"/insitu\">Insitu</a> Add Image" );
135 foreach my $k (keys %args) {
136 #print "$k, $args{$k}<br />\n";
139 print qq {
140 <form action="add_image.pl" method="post" enctype="multipart/form-data" >
141 Upload an image for experiment "$experiment_name":
142 [<a href="experiment.pl?experiment_id=$experiment_id&amp;action=edit">Cancel</a>]<br /><br />
144 <input type="file" name="upload_file" value="Choose image file" />
145 <input type="hidden" name="action" value="upload" />
146 <input type="hidden" name="experiment_id" value="$experiment_id" />
147 <input type="submit" value="Upload" />
148 </form>
150 $page->footer();
151 exit();