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

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

=head1 NAME

LATMOS::Accounts::Web::Controller - Base class Controller for LATMOS::Accounts::Web

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

=head2 default

=cut

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

    if (!$c->user_exists) {
        if ($c->req->path ne 'login') {
            $c->forward('/login');
        }
    } else {
        $c->model('Accounts')->db->connect($c->user->{username},
            $c->user->{password});
    }
    
    my $menu = '';
    my @menus = (
        [ 'users' => 'Utilisateurs' ],
        ($c->model('Accounts')->db->check_acl('user', '@CREATE', 'w')
            ? [ 'create/user' => 'Cr&eacute;er un Utilisateur' ]
            : ()),
        [ 'groups' => 'Groupes' ],
        ($c->model('Accounts')->db->check_acl('group', '@CREATE', 'w')
            ? [ 'create/group' => 'Cr&eacute;er un Groupe' ]
            : ()),
        [ 'sites' => 'Sites' ],
        [ 'aliases' => 'Alias Mail' ],
    );
    foreach(@menus) {
        $menu .= sprintf(
            q{<a href="%s"><span%s>%s</span></a>},
            $c->uri_for('/' . $_->[0]),
            ($c->req->path =~ m:^$_->[0]: ? ' id="actif"' : ''),
            $_->[1],
        );

    }
    $c->stash->{mainmenu} = $menu;
}

=head2 end

Attempt to render a view, if needed.

=cut 

sub end : ActionClass('RenderView') {
    my ($self, $c) = @_;
    $c->forward($c->view('TT'));
    $c->model('Accounts')->db->rollback;
    $c->model('Accounts')->call_batch_sync;
}

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