# $Id: person_adm.pm,v 1.2 2004/04/30 19:43:22 gwolf Exp $
######################################
# Comas - Conference Management System
######################################
# Copyright 2003 CONSOL
# Congreso Nacional de Software Libre (http://www.consol.org.mx/)
#   Gunnar Wolf <gwolf@gwolf.cx>
#   Manuel Rabade <mig@mig-29.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
######################################

######################################
# Module: Comas::Admin::person_adm
# Person's information administration
######################################
# Depends on:
#
# Comas::Admin - Manage administrative tasks for a Comas database
# Comas::Payment - Managing payments for Comas
# Comas::Person - Handles the interaction with a person for Comas
# Comas::Proposal - Handles the interaction with a proposal for Comas
# This module is not meant to be called on its own, it should be called from
# Comas::Admin.
package Comas::Admin::person_adm;

use strict;
use warnings;
use Carp;
use Comas::Admin;
use Comas::Payment;
use Comas::Person;
use Comas::Proposal;
use Exporter;

our @ISA = qw(Exporter);
our @EXPORT_OK = qw(search get_person_data set_person_firstname 
		    set_person_famname set_person_nickname set_person_passwd 
		    set_person_org set_person_dept set_person_title 
		    set_person_studies_id set_person_email 
		    set_person_person_type_id set_person_postal_addr 
		    set_person_zip set_person_city set_person_state_id
		    set_person_country set_person_phone set_person_fax 
		    set_person_birth set_person_resume 
		    set_person_pers_status_id delete_person);
our %EXPORT_TAGS = (lookup => [qw(search get_person_data)],
		    modif => [qw(set_person_firstname set_person_famname
				 set_person_nickname set_person_passwd 
				 set_person_org set_person_dept
				 set_person_title set_person_studies_id
				 set_person_email set_person_person_type_id
				 set_person_postal_addr set_person_zip
				 set_person_city set_person_state_id
				 set_person_country set_person_phone 
				 set_person_fax set_person_birth
				 set_person_resume
				 set_person_pers_status_id)],
		    delete => [qw(delete_person)]);

=head1 NAME

Comas::Admin::person_adm - Person's information administration

=head1 SYNOPSIS

This module is not meant to be used by itself, but as an auxiliary to
L<Comas::Admin|Comas::Admin>. Please check L<Comas::Admin|Comas::Admin>'s
documentation, in the B<Person-related catalog administration> section, for
further details on the usage for this functions.

=head1 SEE ALSO

L<Comas::Admin|Comas::Admin> module documentation

=head1 AUTHOR

Gunnar Wolf, gwolf@gwolf.cx

Manuel Rabade, mig@mig-29.net

Comas has been developed for CONSOL, Congreso Nacional de Software Libre,
http://www.consol.org.mx/

=head1 COPYRIGHT

Copyright 2003 Gunnar Wolf and Manuel Rabade

This library is free software, you can redistribute it and/or modify it
under the terms of the GPL version 2 or later.

=cut

sub search  { 
    my $adm = shift; 

    unless ($adm->ck_admin_task(-task=>'person_adm')) {
	carp 'Access denied.';
	return undef;
    }

    return Comas::Person->search(-db=>$adm->{-db}, @_); 
}

sub get_person_data { 
    my ($adm, $id, $person);
    $adm = shift; 
    $id = shift;

    unless ($adm->ck_admin_task(-task=>'person_adm')) {
	carp 'Access denied.';
	return undef;
    }

    $person = Comas::Person->new(-db=>$adm->{-db}, -id=>$id) or return undef;
    return $person->get_data;
}

sub set_person_firstname  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'firstname', $newval);
}

sub set_person_famname {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'famname', $newval);
}

sub set_person_nickname  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'nickname', $newval);
}

sub set_person_passwd  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'passwd', $newval);
}

sub set_person_org {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'org', $newval);
}

sub set_person_dept  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'dept', $newval);
}

sub set_person_title  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'title', $newval);
}

sub set_person_studies_id  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'studies_id', $newval);
}

sub set_person_email  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'email', $newval);
}

sub set_person_person_type_id  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'person_type_id', $newval);
}

sub set_person_postal_addr  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'postal_addr', $newval);
}

sub set_person_zip  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'zip', $newval);
}

sub set_person_city  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'city', $newval);
}

sub set_person_state_id {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'state_id', $newval);
}

sub set_person_country  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'country', $newval);
}

sub set_person_phone  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'phone', $newval);
}

sub set_person_fax  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'fax', $newval);
}

sub set_person_birth  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'birth', $newval);
}

sub set_person_resume  {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'resume', $newval);
}

sub set_person_pers_status_id {
    my ($adm, $id, $newval);
    $adm = shift;
    $id = shift;
    $newval = shift;

    return $adm->_set_person_attr($id, 'status_id', $newval);
}

sub set_person_data {
    my ($adm, %data, $person);
    $adm = shift;
    %data = @_;

    return undef unless $data{-id};
    $person = Comas::Person->new(-id=>$data{-id}) or return undef;

    return $person->set_data(%data);
}

sub delete_person {
    my ($adm, $pers_id, $person, $sth);
    $adm = shift;
    $pers_id = shift;

    unless ($adm->ck_admin_task(-task=>'person_adm')) {
	carp 'Access denied.';
	return undef;
    }

    # First create the Person object, in order to check for participation
    # as an author in any proposal
    unless ($person = Comas::Person->new(-db=>$adm->{-db}, -id=>$pers_id)) {
	carp "Person $pers_id does not exist";
	return undef;
    }

    # Are there payments related to this person? If so, we cannot remove it.
    ###
    ### Note that once they are implemented, expenses should also be checked
    ### here.
    ###
    if (Comas::Payment->get_payments(-db=>$adm->{-db}, -person=>$pers_id)) {
	carp "Person $pers_id has registered payments, cannot delete it.";
	return undef;
    }

    for my $prop_id ($person->get_props) {
	my $proposal;
	unless ($proposal = Comas::Proposal->new(-db=>$adm->{-db}, 
						    -id=>$prop_id)) {
	    carp "Could not query for information on proposal $prop_id";
	    return undef;
	}

	# Is there any other author for this proposal? If so, keep the 
	# proposal, just remove this author. Otherwise, remove the proposal.
	if (grep {$_ != $pers_id} $proposal->get_authors) {
	    unless ($proposal->del_author($pers_id)) {
		carp "Could not remove $pers_id as an author from $prop_id";
		return undef;
	    }
	} else {
	    unless ($proposal->delete) {
		# $proposal->delete takes care of failing if the proposal has
		# been accepted. 
		carp "Could not remove proposal $prop_id";
		return undef;
	    }
	}
    }

    # Ok, now on to remove the person. 
    unless ($sth = $adm->{-db}->prepare('DELETE FROM person WHERE id = ?')
	    and $sth->execute($pers_id)) {
	carp "Could not remove person $pers_id";
	return undef;
    }

    return 1;
}

#################################################################
# Private methods - not for human consumption

sub _set_person_attr {
    my ($adm, $id, $attr, $newval, $sth);
    $adm = shift;
    $id = shift;
    $attr = shift;
    $attr =~ s/^-//;
    $newval = shift;

    unless ($adm->ck_admin_task(-task=>'person_adm')) {
	carp 'Access denied.';
	return undef;
    }

    $sth = $adm->{-db}->prepare("UPDATE person SET $attr = ? WHERE id = ?");
    return $sth->execute($newval, $id);
}

1;

# $Log: person_adm.pm,v $
# Revision 1.2  2004/04/30 19:43:22  gwolf
# Implemented person deletion in Comas::Admin::person_adm,
# Comas::HTML::committee::person_adm. Modified other files for the
# inclusion of this functionality.
#
# Revision 1.1  2004/03/29 07:22:33  gwolf
# Added a new administration module, for managing information regarding persons. Basically reimplemented what is already available in Person, but via the Admin interface and with the Admin checks.
#
