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; } $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{}; $html .= qq| } . "\n"; return $html; last; }; /^checkbox/i and do { my $options = $1 || ''; my $html = sprintf('', $htmlid, $self->escape($htmlname), $value ? ' checked="yes"' : '' ); $html .= sprintf('', $self->escape($htmlname)); if ($attribute->form_option('rawvalue')) { $html .= $value ? ' ' . $value : ''; } return $html; last; }; /^LIST/i and do { my $select = sprintf('\n"; return $select; last; }; /^text/i and do { return sprintf( '', $self->escape($htmlid), $self->escape($htmlname), $self->escape($value), $attribute->form_option('length') || 30, ); last; }; } return ''; } sub _uri_part { my ($self, $ref) = @_; my $uri_part = { user => 'users', group => 'groups', nethost => 'nethosts', netzone => 'netzones', site => 'sites', aliasess => 'aliases', }->{$ref}; } sub attr_js { my ($self, $attribute, $value, $htmlid) = @_; my (@js); if (my $ref = $attribute->reference) { my $uri_part = $self->_uri_part($ref); if ($self->base->attribute($ref, 'displayName')) { push(@js, $self->{c}->prototype->observe_field( $htmlid, { update => "${htmlid}_ref", url => $self->{c}->uri_for('/ajax', 'rawattr', $ref), frequency => 1, with => "'attr=displayName&id=' + element.options[element.selectedIndex].text", } ) ) if ($attribute->form_type =~ /list/i); } } if ($attribute->uniq) { push(@js, $self->{c}->prototype->observe_field( $htmlid, { update => "${htmlid}_uniq", url => $self->{c}->uri_for('/ajax', 'objattrexist', $self->otype, $attribute->name), frequency => 1, with => "'val='+element.value" . ($self->{object} ? "+'&exclude=" . ($self->{object}->id || '') . "'" : ''), } ) ); } return(join("\n", @js)); } sub attr_field { my ($self, $attr, $type) = @_; my $attribute; if ($type) { if (ref $type) { $attribute = $self->base->attribute( $self->otype, { %$type, name => $attr }, ); } else { $attribute = $self->base->attribute( $self->otype, { formtype => $type , name => $attr }, ); } } else { if ($self->{object}) { $attribute = $self->{object}->attribute($attr); } else { $attribute = $self->base->attribute( $self->otype, $attr, { formtype => $type } ); } } $attribute ||= $self->base->attribute($self->otype, { name => $attr }); my $htmlname = $self->escape($self->attr_field_name($attr)); my @html_fields; # Force Array context: my @attr_raw_value = (); if ($attribute->multiple) { @attr_raw_value = grep { $_ } $self->attr_raw_value($attribute); } else { my ($val) = $self->attr_raw_value($attribute); @attr_raw_value = ($val || ''); } foreach my $attr_raw_value (@attr_raw_value) { my $html_id = $htmlname . (scalar(@html_fields) ? scalar(@html_fields) : ''); push(@html_fields, join("\n", $self->attr_item( $attribute, $attr_raw_value, $html_id ), $self->attr_js( $attribute, $attr_raw_value, $html_id ) ) ); } if ($attribute->{multiple} && !$attribute->readonly) { my $html_id = $htmlname . '_m_SUBID'; my $attr = $self->attr_item($attribute, '', $html_id); # JS: string are on the line only $attr =~ s/\n//mg; $attr =~ s/'/\'/g; my $js = $self->attr_js($attribute, '', 'NANAR'); $js =~ s!'NANAR([_\w]*)'!('$html_id$1'.replace(/SUBID/g, cn))!gm; $js =~ s!]+>!!gm; $js =~ s!!!gm; $js =~ s!-->!!gm; $js =~ s/