package LATMOS::Accounts::Web::Controller::Aliases;

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

=head1 NAME

LATMOS::Accounts::Web::Controller::Aliases - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

    $c->stash->{ofilter} = $c->model('AttrFilter', 'aliases');
}

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

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

    my $oalias = $db->get_object('aliases', $alias);
    $c->stash->{aliasesname} = $alias;
    $c->stash->{aliases} = $oalias;
    $c->stash->{page}{title} = "Gestion des alias";

    $c->stash->{form} = $c->model('AttrForms', 'aliases', $oalias);

    if ($c->req->param('make_active')) {
        $c->stash->{aliases}->set_c_fields('exported' => 1);
        $db->commit;
    }
    elsif ($c->req->param('make_inactive')) {
        $c->stash->{aliases}->set_c_fields('exported' => 0);
        $db->commit;
    }
    elsif ($c->req->param('delete')) {
        $db->delete_object('aliases', $alias);
        $db->commit;
        $c->res->redirect('/aliases');
        return;
    }
    else {
        $c->stash->{form}->set_attrs;
    }
}

=head1 AUTHOR

Olivier Thauvin

=head1 LICENSE

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

=cut

1;
