package LATMOS::Accounts::Web::Controller::Groups;

use strict;
use warnings;
use base 'Catalyst::Controller';

=head1 NAME

LATMOS::Accounts::Web::Controller::Groups - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index 

=cut

sub index : Private {
    my ( $self, $c ) = @_;

    $c->stash->{ofilter} = $c->model('AttrFilter', 'group');
    $c->stash->{page}{title} = "Liste des groupes";

}

sub default : LocalPath {
    my ( $self, $c, undef, $groupname, $subform ) = @_;

    my $base = $c->model('Accounts')->db;

    $c->stash->{page}{title} = "Groupe :: $groupname";
    $c->stash->{groupname} = $groupname;
    $c->stash->{group} = $base->get_object('group', $groupname) or do {
        $c->forward('/no_object');
        return;
    };

    if ($c->req->param('make_active')) {
        $c->stash->{group}->set_c_fields('exported' => 1);
        $base->commit;
    }
    if ($c->req->param('make_inactive')) {
        $c->stash->{group}->set_c_fields('exported' => 0);
        $base->commit;
    }
    if ($c->req->param('delete')) {
        $base->delete_object('group', $groupname);
        $base->commit;
        $c->res->redirect('/groups');
    }

    $c->stash->{subform} = $subform || '';

    for ($subform || '') {
        # Exceptions...
        /^users$/ and do {
            $c->stash->{template} = 'groups/users.tt';

            if ($c->req->param('adduser')) {
                $c->stash->{group}->addAttributeValue('memberUID', $c->req->param('adduser'));
                $c->stash->{group}->base->commit;
            } elsif ($c->req->param('deluser')) {
                $c->stash->{group}->delAttributeValue('memberUID', $c->req->param('deluser'));
                $c->stash->{group}->base->commit;
            }

            last;
        };
        /^dpmt$/ and do {
            $c->stash->{template} = 'groups/dpmt.tt';
            my @filter = ();
            push(@filter, 'active=1') if (!$c->req->param('inactive'));
            push(@filter, 'mail=*') if ($c->req->param('mail'));
            push(@filter, 'company=' . $c->req->param('company')) if
                ($c->req->param('company'));
            $c->stash->{dpmt} = $groupname;
            $c->stash->{odpmt} = $c->stash->{group};
            $c->stash->{user} = [ $base->search_objects('user',
                    'department=' . $c->stash->{dpmt},
                    @filter,
                ) ];
            foreach my $u ($base->search_objects('user',
                    'memberOf=' . $c->stash->{dpmt},
                    @filter,
                )) {
                push(@{$c->{stash}->{guser}}, $u)
                unless(grep { $u eq $_ } @{$c->stash->{user}});
            }
            last;
        };

        $c->stash->{form} = $c->model('AttrForms', 'group' . $c->stash->{subform}, $c->stash->{group});
        $c->stash->{form}->set_attrs;
    }
}

=head1 AUTHOR

Thauvin Olivier

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
