# $Id: person_adm.pm,v 1.8 2004/11/20 19:33:48 mig 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::HTML::committee::person_adm
# Attendee lookup/administration
######################################
# Depends on:
#
# Comas::Person - Handles the interaction with a person for Comas
# Comas::Proposal - Handles the interaction with a proposal for Comas
# Comas::Admin - Manage administrative tasks for a Comas database
# Comas::HTML::common - Common functions for the HTML FrontEnd
# Data::FormValidator - Validates user input based on input profile
# HTML::FromText - Marks up plain text as HTML

# This module is not meant to be called on its own, it should be called from
# Comas::HTML.

package Comas::HTML::committee::person_adm;

use strict;
use warnings;
use Carp;

use Comas::Admin;
use Comas::Proposal;
use Comas::Person;
use Comas::HTML::common qw(:all);

use Data::FormValidator;
use HTML::FromText;

use Exporter;

our @ISA = qw(Exporter);
our @EXPORT_OK = qw( person_adm_person_search person_adm_show_person 
		     person_adm_modif_person person_adm_delete_person);
our %EXPORT_TAGS = (all => [@EXPORT_OK]);

=head1 NAME
    
Comas::HTML::committee::admin_mgr - Manage Admin

=head1 SYNOPSIS

This module is not meant to be used by itself, but as an auxiliary to
L<Comas::HTML|Comas::HTML>. Please check L<Comas::HTML|Comas::HTML>'s
documentation, in the B<Front End Tasks> section, for further details on the
usage for this functions.

=head1 INTERNALS

(To be done)

=cut

sub person_adm_person_search {
    my ($h, $errmsg, $template, %criteria, @persons);
    $h = shift;
    $errmsg = shift;

    unless($h->_ck_committee_task) {
        return $h->committee_menu('NOT_AUTHORIZED');
    }
    
    $template = $h->new_template('committee/person_adm/person_list.tmpl');

    if (defined $errmsg) {
	$template->param($errmsg => 1);
    }

    # Build the selectable items
    $template->param(-country_list => 
		         _catalog2html_opts($h->{-db}, 'country', 
					    $h->{-params}{-srch_country_id}),
		     -person_type_list =>
		         _catalog2html_opts($h->{-db}, 'person_type',
					$h->{-params}{-srch_person_type_id}));

    # What is the user asking us to search on?
    # (no search criteria means list everything)
    for my $key qw(firstname famname country_id person_type_id email){
 	if ($h->{-params}->{"-srch_$key"}) {
	    $criteria{"-$key"} = $h->{-params}->{"-srch_$key"};
	}
    }

    # Perform the search, store the users' information in @person to be sent
    # to the template
    for my $p_id (Comas::Person->search(-db=>$h->{-db}, %criteria)) {
	my ($person, $num_props);

	unless ($person = Comas::Person->new(-db=>$h->{-db}, -id=>$p_id)) {
	    warn "Couldn't instantiate person - ID: $p_id";
	    next;
	}
	$num_props = scalar $person->get_props;
	push(@persons, {-id => $p_id,
			-firstname => $person->get_firstname,
			-famname => $person->get_famname,
			-email => $person->get_email,
			-country => 
			   $person->{-db}->getCatDescr('country',
				        $person->get_country_id),
			-person_type => 
			   $person->{-db}->getCatDescr('person_type',
				    $person->get_person_type_id),
			-num_props => $num_props });
    }

    # Reorder @persons, fill it in the template.
    ### PENDING: Allow for more than one search criteria
    @persons = sort {lc($a->{-famname}) cmp lc($b->{-famname})} @persons;

    $template->param(person=>\@persons, -tot_person=>scalar @persons);

    return $template->output;
}

sub person_adm_show_person {
    my ($h, $p_id, $person, @props, %p_data, $template);
    $h = shift;
    $p_id = shift;

    unless ($h->_ck_committee_task) {
	return $h->committee_menu('NOT_AUTHORIZED');
    }

    unless ($person = Comas::Person->new(-db=>$h->{-db}, -id=>$p_id)) {
	return $h->person_adm_person_search('PERSON_DOES_NOT_EXIST');
    }

    $template = $h->new_template('committee/person_adm/person_data.tmpl');

    # Get all the relevant person's data at once
    %p_data = $person->get_data;

    # Instantiate each of the person's proposals and get its data
    for my $prop_id ($person->get_props) {
	my $prop = Comas::Proposal->new(-id=>$prop_id, -db=>$h->{-db});

	my %prop_data = $prop->get_data;

	# Put the data we need ready to be passed to the template
	push @props, {-pr_id => $prop_id,
		      -pr_title => $prop_data{-title},
		      -pr_track_id => $prop_data{-track_id},
		      -pr_prop_status_id => $prop_data{-prop_status_id},
		      -pr_prop_type_id => $prop_data{-prop_type_id}
		  }
    }

    $template->param(-id => $p_id,
		     -login => $p_data{-login},
		     -firstname => $p_data{-firstname},
		     -famname => $p_data{-famname},
		     -org => $p_data{-org},
		     -dept => $p_data{-dept},
		     -title => $p_data{-title},
		     -studies_list =>
 		         _catalog2html_opts($h->{-db}, 'studies',
					    $p_data{-studies_id}),
		     -email => $p_data{-email},
		     -person_type_list =>
		         _catalog2html_opts($h->{-db}, 'person_type',
					    $p_data{-person_type_id}),
		     -postal_addr => $p_data{-postal_addr},
		     -zip => $p_data{-zip},
		     -city => $p_data{-city},
		     -state_list =>
		         _catalog2html_opts($h->{-db}, 'state',
					    $p_data{-state_id}),
		     -country_list => 
		         _catalog2html_opts($h->{-db}, 'country', 
					    $p_data{-country_id}),
		     -phone => $p_data{-phone},
		     -fax => $p_data{-fax},
		     -birth => $p_data{-birth},
		     -resume => $p_data{-resume},
		     -pers_status_id => 
		         _catalog2html_opts($h->{-db}, 'pers_status',
					    $p_data{-pers_status_id}),
		     proposals => \@props
		     );

    return $template->output;
}

sub person_adm_modif_person {
    my ($h, $p_id, $person, %new_data);
    $h = shift;
    $p_id = shift;

    unless ($h->_ck_committee_task) {
	return $h->committee_menu('NOT_AUTHORIZED');
    }

    unless ($person = Comas::Person->new(-db=>$h->{-db}, -id=>$p_id)) {
	return $h->person_adm_person_search('PERSON_DOES_NOT_EXIST');
    }

    for my $key qw(-firstname -famname -org -dept -title -studies_id -email
		   -person_type_id -postal_addr -zip -city -state_id 
		   -country_id -phone -fax -birth -resume -pers_status_id){
	$new_data{$key} = $h->{-params}->{$key} if
	    defined $h->{-params}->{$key};
    }

    unless ($person->set_data(%new_data)) {
	return $h->person_adm_person_search('ERROR_MODIFYING');
    }

    return $h->person_adm_person_search('MODIF_PERSON');
}

sub person_adm_delete_person {
    my ($h, $p_id, $admin, $template);
    $h = shift;
    $p_id = shift;

    unless ($h->_ck_committee_task) {
	return $h->committee_menu('NOT_AUTHORIZED');
    }

    $admin = Comas::Admin->new(-db => $h->{-db_adm},
                               %{$h->{-session}->{-admin}});

    $template = $h->new_template('committee/person_adm/person_delete.tmpl');

    if ($h->{-params}->{confirm}) {
	# User deletion has been confirmed - Proceed.
	if ($admin->delete_person($p_id)) {
	    return $h->committee_menu('PERSON_DELETED');
	} else {
	    return $h->committee_menu('ERROR_DELETING_PERSON')
	}
    } else {
	# Ask for confirmation.
	my ($person, %p_data, @prop_data);
	unless ($person = Comas::Person->new(-db=>$h->{-db}, -id=>$p_id)) {
	    return $h->committee_menu('ERROR_GETTING_DATA');
	}

	%p_data = $person->get_data;

	# Some keys in $person->get_data are <attribute>_id - We get the
	# corresponding value from the catalog. Besides that change, the whole
	# %p_data can be fed to the template.
	for my $key (keys %p_data) {
	    next unless $key =~ /^-(.+)_id$/;
	    $p_data{"-$1"} = $h->{-db}->getCatDescr($1, $p_data{$key});
	}

	$template->param(ask_confirm=>1, -id => $p_id, %p_data);

	# Does the person have proposals registered? If so, show the relevant
	# titles/links
	for my $prop_id ($person->get_props) {
	    # This bit of data is not _that_ important - If we are not able to
	    # instantiate it, instead of just failing, we go to the next 
	    # element.
	    my $proposal = Comas::Proposal->new(-db=>$h->{-db}, -id=>$prop_id)
		or next;

	    push(@prop_data, {-pr_id => $prop_id,
			      -pr_title => $proposal->get_title});
	}
	if (@prop_data) {
	    $template->param(proposals => \@prop_data);
	}
    }

    return $template->output;
}

1;

=pod

=head1 SEE ALSO

L<Comas::HTML|Comas::HTML> 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

# $Log: person_adm.pm,v $
# Revision 1.8  2004/11/20 19:33:48  mig
# - PODs are uniformed with the other PODs of Comas::HTML::*, ready to be written.
#
# Revision 1.7  2004/07/08 00:15:40  gwolf
# Handling Comas' and the conference's base URL in the configuration,
# not hard-coded in the HTML templates themselves
#
# Revision 1.6  2004/06/14 16:58:03  gwolf
# Minor modifications for consistency sake (all of them regarding field
# names in templates, or at least something terribly similar to it)
#
# Revision 1.5  2004/04/30 19:43:20  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.4  2004/04/23 22:22:01  gwolf
# Separated the name field into firstname and famname
#
# Revision 1.3  2004/04/17 16:33:34  gwolf
# Sorting authors and proposals should be case-insensitive.
#
# Revision 1.2  2004/04/16 23:54:18  gwolf
# Shows the quantity of proposals submitted by a person in the listing,
# the title for each and a link to it in the person's modification page
#
# Revision 1.1  2004/04/08 02:54:22  gwolf
# Created the administrative interface to list/modify attendees
#
# Revision 1.2  2003/12/20 04:14:51  mig
# - Agrego tags Id y Log que expanda el CVS
#
