# $Id: admin_mgr.pm,v 1.6 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::admin_mgr
# Manages administrators and his tasks 
######################################
# Depends on:
#
# Comas::Person - Handles the interaction with a person for Comas
# Comas::Proposal - Handles the interaction with a person 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::admin_mgr;

use strict;
use warnings;
use Carp;

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

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

use Exporter;

our @ISA = qw(Exporter);
our @EXPORT_OK = qw( admin_mgr_menu admin_mgr_edit admin_mgr_create 
		     admin_mgr_delete );
our %EXPORT_TAGS = (all => [@EXPORT_OK]);


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

=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 admin_mgr_menu {
    my ($h, $message, $admin, $template, @admins, %tasks);
    $h = shift;
    $message = shift;

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

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

    $template = $h->new_template('committee/admin_mgr/menu.tmpl');

    # Get the data for each admin, put it in @admins to be passed as param
    # to the template
    for my $adm_id ($admin->admin_list) {
	my %adm_tasks = map {$_=>1} $admin->query_admin_tasks($adm_id->[0]);

	# -not_self: Should not be able to delete myself
	push(@admins, {-id => $adm_id->[0],
		       -name => $adm_id->[1],
		       -login => $adm_id->[2],
		       -tasks => [map {{task_descr => $_,
					active => exists($adm_tasks{$_})}}
				  sort keys %tasks],
		       -not_self => ($adm_id->[0] == $admin->get_id) ? 0 : 1});
    }

    $template->param(admins => \@admins,
		     tasks => [map {{task_descr => $_}} sort keys %tasks]);
    $template->param($message => 1) if defined $message;

    return $template->output;
}

sub admin_mgr_edit {
    my ($h, $adm_id, $admin, %tasks, %usr_tasks);
    $h = shift;
    $adm_id = shift;

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

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

    %tasks = $admin->get_tasks;
    %usr_tasks = map {$_ => 1} @{$h->{-params}{-task}};

    $h->{-db_adm}->begin_work;
    $admin->set_adm_name($adm_id, $h->{-params}{-name}) or
	$h->{-db_adm}->rollback &&
	return $h->admin_mgr_menu('ERROR_USER_MODIF');

    if (my $pass = $h->{-params}{-passwd}) {
	$admin->set_adm_passwd($adm_id, $pass) or
	    $h->{-db_adm}->rollback &&
	    return $h->admin_mgr_menu('ERROR_USER_MODIF');

    }

    for my $task (keys %tasks) {
	if (exists $usr_tasks{$task}) {
	    $admin->grant_task_adm($adm_id, $task) or
		$h->{-db_adm}->rollback &&
		return $h->admin_mgr_menu('ERROR_USER_MODIF');
	} else {
	    $admin->revoke_task_adm($adm_id, $task) or
		$h->{-db_adm}->rollback &&
		return $h->admin_mgr_menu('ERROR_USER_MODIF');
	}
    }

    $h->{-db_adm}->commit;

    return $h->admin_mgr_menu('USER_MODIF');
}

sub admin_mgr_delete {
    my ($h, $adm_id, $admin, $template);
    $h = shift;
    $adm_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/admin_mgr/del.tmpl');

    if ($adm_id == $admin->get_id) {
	return $h->committee_menu('CANNOT_DELETE_SELF');
    }

    if ($h->{-params}{confirm_del}) {
	# Deletion has been confirmed - Remove the user.
	if ($admin->remove_adm($adm_id)) {
	    return $h->committee_menu('USER_DELETED');
	}
    } else {
	# Ask user to confirm deletion
	my (%tasks);

	%tasks = Comas::Admin->get_tasks(-db=>$h->{-db});

	$template->param(not_confirmed => 1,
			 -id => $adm_id,
			 -login => Comas::Admin->get_login(-db=>$h->{-db}, 
							   -id=>$adm_id),
			 -name => Comas::Admin->get_name(-db=>$h->{-db}, 
							  -id=>$adm_id),
			 -tasks => join(', ', sort grep {
			     Comas::Admin->ck_admin_task(-db=>$h->{-db}, 
							 -task=>$_, 
							 -id=>$adm_id)} 
					keys %tasks));
    }

    return $template->output;
}

sub admin_mgr_create {
    my ($h, $admin, $tasks);
    $h = shift;

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

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

    #### PENDING: Convert to a better, Data::FormValidator style
    unless ($h->{-params}{-login} and $h->{-params}{-name} and
	    $h->{-params}{-passwd}) {
	return $h->admin_mgr_menu('MISSING_FIELDS_CREATE');
    }

    if ($admin->get_name(-login=>$h->{-params}{-login})) {
	return $h->admin_mgr_menu('LOGIN_USED');
    }

    if ($admin->create_admin($h->{-params}{-login}, $h->{-params}{-name},
			     $h->{-params}{-passwd}, $h->{-params}{-task})) {
	return $h->admin_mgr_menu('ADM_CREATED');
    }

    return $h->admin_mgr_menu('ADM_NOT_CREATED');
}

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: admin_mgr.pm,v $
# Revision 1.6  2004/11/20 19:33:48  mig
# - PODs are uniformed with the other PODs of Comas::HTML::*, ready to be written.
#
# Revision 1.5  2004/07/24 18:19:52  gwolf
# admin_mgr now working!
#
# Revision 1.4  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.3  2004/04/23 22:40:20  gwolf
# Able to remove administrative users!
#
# Revision 1.2  2003/12/20 04:14:51  mig
# - Agrego tags Id y Log que expanda el CVS
#
