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

use strict;
use warnings;
use base 'LATMOS::Accounts::Web::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 ) = @_;

    my $db = $c->model('Accounts')->db;
    my @al = $c->req->param('al');
    my @name = $c->req->param('name');
    my @forward = $c->req->param('forward');
    foreach my $idx (0 .. scalar(@name) - 1) {

        if ($c->req->param('del' . ($idx+1))) {
            $db->delete_object('aliases', $al[$idx]);
        } elsif ($al[$idx] ne $name[$idx]) {
            # delete old, create new
            $db->delete_c_object('aliases', $al[$idx]) if($al[$idx]);
            $db->create_c_object('aliases', $name[$idx],
                forward => [ split(/\s*,\s*/, $forward[$idx]) ],
            );
        } else {
            my $obj = $db->get_object('aliases', $al[$idx]);
            $obj->set_c_fields(
                'forward' => [ split(/\s*,\s*/, $forward[$idx]) ],
            );
        }
    }

    $db->commit;
}


=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;
