package LATMOS::Accounts::Web::Model::AttrForms;
use strict;
use warnings;
use base 'Catalyst::Model';
=head1 NAME
LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model
=head1 DESCRIPTION
Catalyst Model.
=cut
my $attrs = {
uid => [ 'Login', 'text:12' ],
uidNumber => [ 'UID', 'text-U:6' ],
gidNumber => [ 'GID', 'select:group:gidNumber' ],
manager => [ 'Responsable', 'select-N:user' ],
managedBy => [ 'Responsable', 'select-N:user' ],
sn => [ 'Nom' ],
givenName => [ 'Prénom' ],
homeDirectory => [ 'Home', 'text-U:25' ],
loginShell => [ 'Shell', 'text-A' ],
physicalDeliveryOfficeName => [ 'Bureau' ],
telephoneNumber => [ 'Téléphone' ],
otherTelephone => [ 'Téléphone (autre)' ],
company => [ 'Société', 'text-A' ],
l => [ 'Ville' ],
postalCode => [ 'Code postal' ],
postOfficeBox => [ 'BP' ],
department => [ 'Département', 'select-N:group' ],
streetAddress => [ 'Rue', 'textarea' ],
title => [ 'Fonction' ],
expire => [ 'Expire le', 'date' ],
st => [ 'État (US)' ],
sutype => [ 'Structure admin.', 'select-N:sutype' ],
exported => [ 'Propagé', 'label' ],
locked => [ 'Vérouillé', 'checkbox' ],
isMainAddress => [ 'Adresse principale', 'checkbox' ],
site => [ 'Site', 'select-N:site' ],
co => [ 'Pays' ],
mail => [ 'Mail', 'text-U:30' ],
contratType => [ 'Type de contrat', 'text-A' ],
grade => [ 'Grade', 'text-A' ],
facsimileTelephoneNumber => [ 'Fax', 'text' ],
nickname => [ 'Nick', 'text-U:3' ],
cn => [ 'Nom' ],
initials => [ 'Initiales', 'text-U:5' ],
create => [ 'Créer le' ],
date => [ 'Modifié le' ],
};
my $forms = {
user => {
name => 'Système',
acl => 'admin',
attrs => [ qw(
sn givenName description
uid uidNumber gidNumber gecos homeDirectory loginShell
mail initials nickname
locked
exported
create
date
) ],
},
useraddress => {
name => 'Adresse',
attrs => [ qw(
initials
mail
) ],
},
userstatus => {
name => 'Status',
attrs => [ qw(
company
manager
department
grade
contratType
expire
) ],
},
site => {
name => 'Site',
attrs => [ qw(
description
streetAddress
postOfficeBox
postalCode
l
st
co
facsimileTelephoneNumber
) ],
},
address => {
name => 'Adresse',
attrs => [ qw(
isMainAddress
telephoneNumber
streetAddress
postOfficeBox
postalCode
l
st
physicalDeliveryOfficeName
site
co
) ],
},
group => {
name => 'Système',
acl => 'admin',
attrs => [ qw(
gidNumber description
managedBy
sutype exported
) ],
},
};
sub escape {
my ($self, $text) = @_;
$text ||= '';
for ($text) {
s/&/&/g;
s/</g;
s/>/>/g;
s/"/"/g;
}
$text;
}
sub new {
my ($class) = @_;
bless({}, $class);
}
# call either OBJ or type + base
sub ACCEPT_CONTEXT {
my ($self, $c, $form, $object, $base) = @_;
my $new = {};
$new->{c} = $c;
$new->{form} = $form;
$new->{object} = $object if (ref $object);
$new->{base} = $base || ($object ? $object->base : undef) or return $self;
$new->{otype} = ref $object ? $object->type : $object;
bless($new, 'LATMOS::Accounts::Web::Model::AttrForms');
}
sub base {
my ( $self ) = @_;
$self->{base}
}
sub otype {
my ($self) = @_;
$self->{otype};
}
sub label {
my ($self) = @_;
$forms->{$self->{form}}->{name} || ''
}
sub attributes {
my ($self, $for) = @_;
grep { $_ }
grep { $self->base->check_acl($self->{object} || $self->otype, $_, 'r') }
grep { $self->base->get_field_name($self->otype, $_, $for || 'a') }
@{ $forms->{$self->{form}}->{attrs} };
}
sub attr_label {
my ($self, $attr) = @_;
my $htmlname = ($self->{object} ? $self->{object}->id . '_' : '') . $attr;
return sprintf('',
$self->escape($htmlname),
$self->escape($attrs->{$attr}[0] || $attr)
);
}
sub attr_raw_value {
my ($self, $attr) = @_;
return $self->{c}->req->param($attr) ||
($self->{object} ? $self->{object}->get_c_field($attr) : '')
}
sub attr_field {
my ($self, $attr, $type) = @_;
my $modallow = $self->base->check_acl($self->{object}
? ($self->{object}, $attr, 'w')
: ($self->otype, '@CREATE', 'w'));
$type ||= $self->base->get_field_name($self->otype, $attr, 'w') && $modallow
? $attrs->{$attr}[1] || ''
: 'label';
# exception: gidNumber is used also in group, but we don't want
# group list here, really the number !
$type = $modallow ? 'text-U:6' : 'label' if (($self->{form} || '') =~ /^group/ && $attr eq 'gidNumber');
$type ||= 'text';
my $htmlname = $self->escape(($self->{object} ? $self->{object}->id . '_' :
'') . $attr);
for ($type) {
/^textarea$/ and return sprintf(
'',
$self->escape($htmlname),
$self->escape($htmlname),
$self->escape($self->attr_raw_value($attr)),
);
/^label$/ and do {
my $field = $self->escape(
$self->attr_raw_value($attr)
);
$field =~ s/\n/
/g;
return $field;
};
/^date$/ and do {
my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || '');
if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
$date = "$3/$2/$1";
}
my $html = "\n" . q{} . "\n";
$html .= sprintf(
'',
$self->escape($htmlname),
$self->escape($htmlname),
$self->escape($date)
);
$html .= q{