package LATMOS::Accounts::Web::Model::AttrForms;
use strict;
use warnings;
use base 'Catalyst::Model';
use LATMOS::Accounts::Log;
use HTML::Prototype;
=head1 NAME
LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model
=head1 DESCRIPTION
Catalyst Model.
=cut
my $forms = {
usersys => {
name => 'Système',
attrs => [ qw(
sn givenName description
uid uidNumber gidNumber gecos homeDirectory loginShell
mail initials nickname
expire
locked
create
date
) ],
},
user => {
name => 'Statut',
attrs => [ qw(
sn givenName description
company
department
manager
managerContact
contratType
expire
snNative
givenNameNative
wWWHomePage
halReference
create
date
) ],
},
useraddress => {
name => 'Adresse',
attrs => [ qw(
initials
mail
) ],
},
usermy => {
name => 'My',
attrs => [ qw(
snNative
givenNameNative
wWWHomePage
halReference
) ],
},
site => {
name => 'Site',
attrs => [ qw(
description
siteNick
streetAddress
postOfficeBox
postalCode
l
st
co
facsimileTelephoneNumber
create
date
) ],
},
address => {
name => 'Adresse',
attrs => [ qw(
isMainAddress
telephoneNumber
streetAddress
postOfficeBox
postalCode
l
st
physicalDeliveryOfficeName
site
co
unexported
description
) ],
},
group => {
name => 'Système',
attrs => [ qw(
gidNumber description
managedBy
autoMemberFilter
sutype
create
date
) ],
},
nethost => {
name => 'Ordinateur',
attrs => [ qw(
name
description
serialNumber
owner
user
ip
macaddr
puppetClass
noInheritPuppet
noDynamic
cname
otherName
reverse
create
date
unexported
) ],
},
netzone => {
name => 'Zone réseau',
attrs => [ qw(
name
description
type
net
netExclude
puppetClass
outputD
templateD
site
allow_dyn
dynFrom
domain
ipCount
freeIPCount
create
date
dnsRevision
lastUpdate
unexported
) ],
},
aliases => {
name => 'Alias mail',
attrs => [ qw(
forward
expire
finalpoint
parents
created
date
unexported
)],
},
};
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, __PACKAGE__);
}
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->attribute($self->otype, $_) }
@{ $forms->{$self->{form}}->{attrs} };
}
sub attr_label {
my ($self, $attr, $label, $hint) = @_;
$label ||= '';
$hint ||= '';
LATMOS::Accounts::Web->config->{attrs} ||= {};
my $attribute = $self->base->attribute($self->otype, $attr);
my $htmlname = $self->escape($self->attr_field_name($attr));
if (my $attr_config = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
$label ||= $attr_config->{label} || '';
#utf8::is_utf8($label) && utf8::encode($label);
$hint ||= $attr_config->{hint} || '';
#utf8::is_utf8($hint) && utf8::encode($hint);
}
return sprintf('',
($hint ? sprintf('title="%s"', $self->escape($hint)) : ''),
$self->escape($htmlname),
$self->escape($label || $attr),
);
}
sub attr_hint {
my ($self, $attr) = @_;
LATMOS::Accounts::Web->config->{attrs} ||= {};
if (my $attr_config = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
return $attr_config->{hint} || '';
}
return;
}
sub attr_raw_value {
my ($self, $attribute) = @_;
my @val = grep { $_ }
($self->{c}->req->param($attribute->name))
|| ($self->{object}
? ($self->{object}->get_attributes($attribute->name))
: ());
if (@val) {
return @val;
} else {
return (grep { $_ } ($attribute->default_value))
}
}
sub attr_field_name {
my ($self, $attr) = @_;
my $id = $self->{object}
? ($self->{object}->id || '')
: '';
$id .= '_' if ($id);
$id =~ s/\./_/g;
$id =~ s/-/_/g;
$id . $attr;
}
sub attr_item {
my ($self, $attribute, $value, $htmlid) = @_;
my $html = $self->_attr_item($attribute, $value, $htmlid);
if ($attribute->uniq) {
$html .= qq||;
}
if (my $ref = $attribute->reference) {
my $uri_part = $self->_uri_part($ref);
if ($self->base->attribute($ref, 'displayName')) {
my $text = '';
if ($value &&
(my $obj = $self->base->get_object($ref, $value))) {
$text = $obj->get_attributes('displayName');
}
$html .= sprintf(
qq|$text|,
$self->escape($text),
);
} elsif($value && $uri_part) {
$html .= sprintf(
'',
$self->{c}->uri_for('/', $uri_part, $value),
$self->{c}->uri_for('/static', 'icons', 'arrow-right.png'),
$value,
);
}
}
$html
}
sub _attr_item {
my ($self, $attribute, $value, $htmlid) = @_;
my $htmlname = $self->escape($self->attr_field_name($attribute->name));
$htmlid ||= $htmlname;
for ($attribute->form_type) {
/^textarea$/i and do {
return sprintf(
'',
$self->escape($htmlid),
$self->escape($htmlname),
$self->escape($value || ''),
);
last;
};
/^label$/i and do {
$value or last;
my $html_field = $self->escape($value);
$html_field =~ s/\n/
/g;
$html_field .= sprintf('',
$self->escape($htmlname), ($value || ''));
return $html_field;
last;
};
/^date$/i and do {
my ($date, $time) = split(/ /, $value || '');
if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
$date = "$3/$2/$1";
}
my $html = "\n" . q{} . "\n";
$html .= sprintf(
'',
$self->escape($htmlid),
$self->escape($htmlname),
$self->escape($date)
);
$html .= q{