package LATMOS::Accounts::Web::Controller::OAliases;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

LATMOS::Accounts::Web::Controller::OAliases - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

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

}

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

    my $otype = $c->req->param('otype');
    my $name  = $c->req->param('name');

    if (my $for = $c->req->param('oalias')) {
        if ($c->model('Accounts')->db->CreateAlias($otype, $name, $for)) {
            $c->res->redirect($c->uri_for('/oaliases', $otype, $name));
            $c->model('Accounts')->db->commit;
        } else {
            $c->model('Accounts')->db->rollback;
        }
    }
}

sub Edit :Path :Args(2) {
    my ( $self, $c, $otype, $name ) = @_;

    my $oalias = $c->model('Accounts')->db->GetAlias($otype, $name) or do {
        $c->go('/no_object');
        return;
    };

    if ($c->req->param('delete')) {
        if ($c->model('Accounts')->db->RemoveAlias($otype, $name)) {
            $c->res->redirect($c->uri_for('/oaliases'));
            $c->model('Accounts')->db->commit;
            return 1;
        }
    } elsif (my $for = $c->req->param('oalias')) {
        my $exported = $c->req->param('exported') || 0;
        if($oalias->set_c_fields(oalias => $for, exported => $exported)) {
            $oalias->base->commit;

        }
    }

    $c->stash->{objectalias} = $oalias;

}

=encoding utf8

=head1 AUTHOR

Olivier Thauvin,Guyancourt - B1428,+33 1 80285052,

=head1 LICENSE

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

=cut

__PACKAGE__->meta->make_immutable;

1;
