package LATMOS::Accounts::Web::Model::AttrForms;

use strict;
use warnings;
use base 'Catalyst::Model';
use HTML::Entities qw(encode_entities);

=head1 NAME

LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model

=head1 DESCRIPTION

Catalyst Model.

=cut

my $attrs = {
    'uid' => 'Login',
    'uidNumber', 'UID',
    gidNumber => 'GID',
};

my $forms = {
    user_main => {
        acl => 'admin',
        attrs => [ qw(uid uidNumber gidNumber gecos homeDirectory shell) ],
    }
};


sub new {
    my ($class) = @_;
    bless({}, $class);
}

sub ACCEPT_CONTEXT {
    my ($self, $c, $form, $object) = @_;
    $self->{form} = $form;
    $self->{object} = $object;
    $self->{c} = $c;
    $self
}

sub attributes {
    my ($self) = @_;
    grep { $self->{object}->get_field_name($_, 'w') } @{ $forms->{$self->{form}}->{attrs} };
}

sub attr_label {
    my ($self, $attr) = @_;
    return encode_entities($attrs->{$attr} || $attr);
}

sub attr_field {
    my ($self, $attr) = @_;
    return sprintf(
        '<input type="text" name="%s" value="%s">',
        $attr,
        encode_entities(
            $self->{c}->req->param($attr) ||
            $self->{object}->get_c_field($attr)
        )
    );
}

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