#!@@_perl_root_@@/bin/perl.exe 
package UpdateColleague;

##########################################################
#                                                        #
# dictyBase Extension of UpdateColleague                 #
#                                                        #
##########################################################

use UpdateColleague_base;

BEGIN { %UpdateColleague:: = %UpdateColleague_base:: }



use Mail::Sendmail;
use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/Objects";
use ConfigPathdictyBase;

use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase";
use dictyBaseColleague;

my $configPath = ConfigPathdictyBase->new;

#
#  changed /bin/mv to rename (portability)
#
# also added calls to new functions listserve_utils (subscribe/unsubscribe to listserve)
#  and notify_colleague (sends them an email)
#
#######################################################################
sub commitInfo {
#######################################################################
    my ($self) = shift;

    &printStartPage($self->database, $self->title, $self->help);
    $self->processData;

    print b("Colleague_no = ".param('colleague_no')),p;

    print b("Please wait"), ", committing data into the database...", p;


    my $collObject = dictyBaseColleague->new(dbh=>$dbh,
					colleagueNo=>param('colleague_no'));

   #
   # since we only allow one email, take first item in array as the old email
   #
    my $emailRef = $collObject->emailRef;
    my $oldEmail = $emailRef->[0]->[0];
    my $newEmail = param('email');
    my $oldContact  = $collObject->{'_dictyBasecontact'};
    my $newContact  = param('dictyBasecontact');

    $self->listserve_utils($oldEmail, $newEmail, $oldContact, $newContact);

    $self->updateColleague($collObject);
    $self->updatePhone($collObject);
    $self->updatePi($collObject);
    $self->updateEmail($collObject);
    $self->updateUrl($collObject);
    $self->updateAssociate4pi($collObject);
    $self->updateAssociate($collObject);
    $self->updateResearchTopics($collObject);
    $self->updateKeyword($collObject);
    $self->updateAssociatedLoci($collObject);
    $self->updateComment($collObject);
    $self->updateInterest($collObject);
    $self->insertNote;

    $self->notify_colleague($newEmail,param('colleague_no'));

    print p, b("All updates completed, and the colleague has been notified");
    print br, "The colleague entry has been successfully commited into database!", p;
    print b("View current colleague ".a({-href=>"http://dictybase.org/db/cgi-bin/$dblink/colleague/colleagueSearch?id=".param('colleague_no')."&win=1", -target=>"infowin", -onClick=>"open_win()"}, param('lname').", ".param('fname'))),p;

    my $id = param('id');
    if ($id) {
	   rename "$dir/$id.MajorUpdate", "$dir/archive/$id.MajorUpdate";
    }
    &returnForm($self->user, $dblink);
    &printEndPage;
}

#
# new subroutine, emails colleague
#
sub notify_colleague {
   my ($self, $email, $colleague_no) = @_;


   $message =  "Thank you for using dictyBase to update your information in our database.\n";
   $message .= "Your information has been processed and is now updated on the site.\n";
   $message .= "You can view your profile here: http://dictybase.org/db/cgi-bin/dictyBase/colleague/colleagueSearch?id=".$colleague_no;
   $message .= "\n\nThe dictyBase Team.\n";
   $message .= "http://dictyBase.org";

   my %mail = (To      => $email,
	       smtp => 'smtprelay.northwestern.edu',
               From    => 'dictyBase@northwestern.edu',
               Subject => 'Colleague Entry in dictyBase',
               Message => "$message",
              );
   sendmail(%mail) or die $Mail::Sendmail::error;

}

#
# new subroutine, emails listserve for subscribe/unsubscribe
#
sub listserve_utils {
   my ($self, $OLDemail, $NEWemail, $OLDsubscribeYN, $NEWsubscribeYN) = @_;


  #
  #  want to do something if they are subscribed and their email changed
  #  or their subscription status changes
  #

   my $message;

   if (   $OLDsubscribeYN eq 'Y' and ( 
             ($NEWsubscribeYN eq 'N' and $OLDemail) or  # unsubscribing
             ($NEWemail ne $OLDemail and $OLDemail)     # changing email
          ) 
   ) {
      $message  .= "DELETE DICTY $OLDemail\n" ;
   }
   if (    $NEWsubscribeYN eq 'Y' and ( 
              $OLDsubscribeYN eq 'N' or                 #subscribing
              ($NEWemail ne $OLDemail)                  #changing email
           )
   ) {
     $message  .= "ADD DICTY $NEWemail ".$self->{'_fname'}." ".$self->{'_lname'}."\n" if $NEWemail;
   }
   my %mail = (To      => 'siddhartha-basu@northwestern.edu',
	       smtp => 'smtprelay.northwestern.edu',
               From    => 'dicty@northwestern.edu',
               Subject => 'Listserve add/delete user',
               Message => "$message",
              );
   sendmail(%mail) or die $Mail::Sendmail::error;
   $message =~ s/\n/<br>/ig;
   print "Message sent to listserve<br><br> $message<br><br>" if $message;
}
###################################################################
1;
###################################################################









