debugging
[sgn.git] / cgi-bin / tag / index.pl
blobd019a572a4057d1e4dc71d088badb6642c322de3
2 use strict;
4 my $tag_detail_page = CXGN::Tag_detail_page->new();
7 package CXGN::Tag_detail_page;
9 use CXGN::Page;
10 use CXGN::Page::FormattingHelpers qw / blue_section_html page_title_html /;
11 use CXGN::Tag;
12 use CXGN::Insitu::Experiment;
13 use CXGN::Image;
14 use CXGN::Login;
15 use CXGN::Contact;
16 use CatalystX::GlobalContext qw( $c );
18 use base qw / CXGN::Page::Form::SimpleFormPage /;
20 sub new {
21 my $class = shift;
22 my $self = $class->SUPER::new(@_);
23 $self->set_dbh(CXGN::DB::Connection->new());
24 $self->set_script_name("index.pl");
25 return $self;
28 sub define_object {
29 my $self = shift;
31 # call set_object_id, set_object and set_primary_key here
32 # with the appropriate parameters.
34 my %args = $self->get_args();
35 $self->set_object_id($args{tag_id});
36 $self->set_object(CXGN::Tag->new($self->get_dbh(),
37 $self->get_object_id())
39 $self->set_primary_key("tag_id");
40 $self->set_owners(); # nobody owns tags...
43 # override store to check if a tag with the submitted name already exists
45 sub store {
46 my $self = shift;
48 my $tag = $self->get_object();
50 my $tag_id = $self->get_object_id();
51 my %args = $self->get_args();
52 my $sp_person_id= CXGN::Login->new($self->get_dbh())->has_session();
54 my $not_new_tag = "";
55 print STDERR "*** STORING TAG ***\n";
56 my $existing_id = CXGN::Tag::exists_tag_named($self->get_dbh(), $args{tag});
57 if ($existing_id) {
58 print STDERR "Tag already exists...(id = $existing_id)\n";
59 $tag = CXGN::Tag->new($self->get_dbh(), $existing_id);
60 $self->set_object($tag);
61 $self->set_object_id($existing_id);
62 $not_new_tag = "Associated already existing tag.";
64 else {
65 $self->SUPER::store(1);
68 $tag_id = $self->get_object_id();
69 $tag = $self->get_object();
71 if (exists($args{experiment_id}) && $args{experiment_id}) {
72 print STDERR "/tag/: associating exp $args{experiment_id} to tag ".$tag->get_tag_id()."\n";
73 my $existing_id = CXGN::Insitu::Experiment::exists_tag_experiment($self->get_dbh(), $tag_id, $args{experiment_id});
74 if (!$existing_id) {
75 $tag -> associate_experiment($args{experiment_id}, $sp_person_id);
76 $self->send_tag_email('experiment', $args{experiment_id});
77 }else { $self->get_page()->message_page("tag '" . $tag->get_name() ."' already associated with experiment " . $args{experiment_id}) };
79 if (exists($args{image_id}) && $args{image_id}) {
80 my $image = CXGN::Image->new(dbh=>$self->get_dbh(), image_id=>$args{image_id}, image_dir => $c->get_conf('static_datasets_path')."/".$c->get_conf('image_dir'));
82 my ($existing_id, $obsolete) = CXGN::Image::exists_tag_image_named($self->get_dbh(), $tag_id, $args{image_id});
83 if ($existing_id && $obsolete == 0) {
85 $self->get_page()->header();
86 print "Tag '".$tag->get_name(). "' already associated with image " . $args{image_id} . " <br />";
87 print qq { <a href="javascript:history.back(1)">back to image page</a> };
88 $self->get_page()->footer();
89 exit();
90 }else {
91 $tag -> associate_image($args{image_id}, $sp_person_id);
92 $self->send_tag_email('image', $args{image_id});
95 $self->get_page()->header();
97 print "Adding tag ".$tag->get_name();
98 if ($args{experiment_id}) {
99 my $experiment = CXGN::Insitu::Experiment->new($self->get_dbh(), $args{experiment_id});
100 print " to experiment ".$experiment->get_name()."<br /><br />";
101 print qq { view <a href="/insitu/detail/experiment.pl?experiment_id=$args{experiment_id}&amp;action=view">experiment</a> };
103 if ($args{image_id}) {
104 my $image = CXGN::Image->new(dbh=>$self->get_dbh(), image_id=>$args{image_id}, image_dir => $c->get_conf('static_datasets_path')."/". $c->get_conf('image_dir'));
105 print " to image ".$image->get_name()."<br /><br />";
106 print qq { view <a href="/image/?image_id=$args{image_id}&amp;action=view">image</a> };
109 $self->get_page()->footer();
111 exit();
115 sub delete_dialog {
116 my $self = shift;
117 $self->delete();
120 sub delete {
121 my $self = shift;
122 my %args = $self->get_args();
124 my $experiment;
125 my $experiment_name;
126 my $image;
127 my $image_name;
129 my $tag_name = $self->get_object()->get_name();
131 if ($args{experiment_id}) {
132 $experiment = CXGN::Insitu::Experiment->new($self->get_dbh(), $args{experiment_id});
133 $experiment_name = $experiment->get_name();
134 $experiment->remove_tag($args{tag_id});
137 if ($args{image_id}) {
138 $image = CXGN::Image->new(dbh=>$self->get_dbh(), image_id=>$args{image_id}, image_dir=>$c->get_conf('static_datasets_path')."/".$c->get_conf('image_dir'));
139 $image_name = $image->get_name();
140 $image ->remove_tag($self->get_object());
143 $self->get_page()->header();
145 if ($experiment) {
146 print qq { Removed tag "$tag_name" association from experiment "$experiment_name". };
147 print qq { <a href="/insitu/detail/experiment.pl?experiment_id=$args{experiment_id}&amp;action=view">back to experiment</a> };
149 elsif ($image) {
150 print qq { Removed tag "$tag_name" association from image "$image_name". };
151 print qq { <a href="/image/index.pl?image_id=$args{image_id}&amp;action=view">back to image detail page</a> };
153 if (!$args{image_id} && !$args{experiment_id}) {
154 print qq { No associations were deleted because no association information was provided. };
157 $self->get_page()->footer();
161 sub generate_form {
162 my $self = shift;
164 my %args = $self->get_args();
165 my $tag = $self->get_object();
166 my $tag_id = $self->get_object_id();
168 $self->init_form();
170 # generate the form with the appropriate values filled in.
171 # if we view, then take the data straight out of the database
172 # if we edit, take data from database and override with what's
173 # in the submitted form parameters.
174 # if we store, only take the form parameters into account.
175 # for new, we don't do anything - we present an empty form.
178 # add form elements
180 $self->get_form()->add_field(display_name=>"Tag name: ",
181 field_name=>"tag",
182 length=>20,
183 object=>$tag,
184 getter=>"get_name",
185 setter=>"set_name",
186 validate=>"string"
189 #$self->get_form()->add_field( display_name=>"Description: ",
190 # field_name=>"description",
191 # length=>50,
192 # object=>$tag,
193 # getter=>"get_description",
194 # setter=>"set_description");
196 $self->get_form()->add_hidden( field_name=>"action", contents=>"store" );
197 $self->get_form()->add_hidden( field_name=>"tag_id", contents=>$tag_id );
198 $self->get_form()->add_hidden( field_name=>"experiment_id", contents=>$args{experiment_id} );
199 $self->get_form()->add_hidden( field_name=>"image_id", contents=>$args{image_id} );
200 $self->get_form()->add_hidden (
201 field_name => "sp_person_id",
202 contents =>$self->get_user()->get_sp_person_id(),
203 object => $tag,
204 setter =>"set_sp_person_id",
207 # populate the form
208 # (do nothing here because tags cannot be edited).
209 #if ($self->get_action()=~/view|edit/i) {
210 # $self->get_form()->from_database();
212 if ($self->get_action()=~/store/i) {
213 $args{tag}=lc($args{tag}); # somehow this doesn't work -- would like to lowercase all tags...
214 $self->get_form()->from_request(%args);
220 sub display_page {
221 my $self = shift;
222 my %args = $self->get_args();
224 # generate an appropriate edit link
226 my $script_name = $self->get_script_name();
228 # generate some experiment and/or image information
230 my $experiment;
231 my $experiment_name;
232 my $image;
233 my $image_name;
235 my @tags = ();
236 my @image_tags = ();
237 my @experiment_tags = ();
239 # render the form
241 $self->get_page()->header();
243 print qq { <b>Associated tags</b> };
245 if ($args{experiment_id}) {
246 $experiment = CXGN::Insitu::Experiment->new($self->get_dbh(), $args{experiment_id});
247 @experiment_tags = $experiment->get_tags();
248 my $experiment_id = $experiment->get_experiment_id();
249 print "for experiment ".$experiment->get_name()."<br /><br />\n";
250 foreach my $t (@experiment_tags) {
251 my $tag_id = $t->get_tag_id();
252 print $t->get_name(). qq { \n <a href="/tag/?experiment_id=$experiment_id&amp;tag_id=$tag_id&action=delete">[Remove]</a> <br />\n };
256 if ($args{image_id}) {
257 $image = CXGN::Image->new(dbh=>$self->get_dbh(), image_id=>$args{image_id}, image_dir=>$c->get_conf('static_datasets_path')."/".$c->get_conf('image_dir'));
258 @image_tags = (@tags, $image->get_tags());
259 my $image_id = $image->get_image_id();
260 my $image_name = $image->get_name() || "Untitled";
261 print qq { for image "$image_name"<br /><br />\n };
263 foreach my $t (@image_tags) {
264 my $tag_id = $t->get_tag_id();
265 print $t->get_name(). qq { <a href="/tag/?image_id=$args{image_id}&amp;tag_id=$tag_id&action=delete">[Remove]</a> <br /> \n };
268 if (!@experiment_tags && !@image_tags) { print "<b>None found</b><br /><br />\n"; }
270 print qq { <br /><br /><b>Associate another tag</b>: };
272 print qq { <center> };
274 $self->get_form()->as_table();
276 print qq { </center> };
278 if ($args{experiment_id}) {
279 print qq { <a href="/insitu/detail/experiment.pl?experiment_id=$args{experiment_id}&amp;action=view">back to experiment page</a> };
280 }elsif ($args{image_id}) {
281 print qq { <a href="/image/index.pl?image_id=$args{image_id}&amp;action=view">back to image page</a> };
283 $self->get_page()->footer();
290 sub send_tag_email {
291 my $self=shift;
292 my $object_name=shift;
293 my $object_id= shift;
294 my $action=shift;
295 my %args = $self->get_args();
297 my $tag_id=$self->get_object_id();
299 my $subject="[New tag stored] tag $tag_id";
300 my $username= $self->get_user()->get_first_name()." ".$self->get_user()->get_last_name();
301 my $sp_person_id=$self->get_user()->get_sp_person_id();
302 my $user_link= qq |<a href= "http://www.sgn.cornell.edu/solpeople/personal-info.pl?sp_person_id=$sp_person_id">$username</a>|;
304 my $fdbk_body;
305 if ($action eq 'delete') {
306 $fdbk_body="$user_link has deleted tag $tag_id for $object_name $object_id\n "; }
307 else {
308 $fdbk_body="$user_link has associated tag " . $self->get_object()->get_name() . " with $object_name $object_id \n"; }
311 CXGN::Contact::send_email($subject,$fdbk_body, 'sgn-db-curation@sgn.cornell.edu');