Ignore:
Timestamp:
12/02/11 11:42:17 (13 years ago)
Author:
nanardon
Message:
  • reimport missing files from previous svn
File:
1 edited

Legend:

Unmodified
Added
Removed
  • LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Model/AttrForms.pm

    r804 r861  
    44use warnings; 
    55use base 'Catalyst::Model'; 
     6use LATMOS::Accounts::Log; 
    67 
    78=head1 NAME 
     
    3940    sutype => [ 'Structure admin.', 'select-N:sutype' ], 
    4041    exported => [ 'Propagé', 'label' ], 
     42    unexported => [ 'Caché' ], 
    4143    locked => [ 'Vérouillé', 'checkbox:l' ], 
    4244    isMainAddress => [ 'Adresse principale', 'checkbox' ], 
     
    5456    contratType => [ 'Type de contrat', 'select-N:group/sutype=contrattype' ], 
    5557    managerContact => [ 'Contact' ], 
     58    owner => [ 'Propriétaire' ], 
     59    description => [ 'Description' ], 
     60    name => [ 'Nom' ], 
     61    net => [ "Réseau" ], 
     62    macaddr => [ "Adresse Ethernet" ], 
     63    noDynamic => [ "Pas d'adressage dyn." ], 
     64    cname => [ "Alias (CName)" ], 
     65    type => [ "Type" ], 
     66    netExclude => [ "Réseau exclus" ], 
     67    output => [ "Sortie" ], 
     68    template => [ "ModÚle" ], 
     69    outputD => [ "Sortie" ], 
     70    templateD => [ "ModÚle" ], 
     71    group => [ "Groupe" ], 
     72    allow_dyn => [ "DHCP dynamique" ], 
     73    ipCount => [ "Nb d'IP" ], 
     74    freeIPCount => [ "Nb d'IP libres" ], 
     75    dnsRevision => [ "Révision DNS" ], 
     76    lastUpdate => [ "DerniÚre mise à jour "], 
     77    otherName => [ "Autres noms (A)" ], 
     78    'reverse' => [ "Forcer le reverse à" ], 
     79    dynFrom => [ 'Sans Ip depuis' ], 
     80    siteNick => [ 'Acronyme' ], 
     81    serialNumber => [ 'N° de série' ], 
     82    snNative => [ 'Nom d\'origine' ], 
     83    givenNameNative => [ 'Prénom d\'origine' ], 
     84    wWWHomePage => [ 'Site Web' ], 
     85    halReference => [ 'Référence HAL' ], 
    5686}; 
    5787 
     
    6494            mail initials nickname 
    6595            locked 
    66             exported 
    6796            create 
    6897            date 
     
    76105        ) ], 
    77106    }, 
    78     userstatus => { 
    79         name => 'Status', 
     107    userstatut => { 
     108        name => 'Statut', 
    80109        attrs => [ qw( 
    81110            company 
     
    87116        ) ], 
    88117    }, 
     118    usermy => { 
     119        name => 'My', 
     120        attrs => [ qw( 
     121            snNative 
     122            givenNameNative 
     123            wWWHomePage 
     124            halReference 
     125        ) ], 
     126    }, 
    89127    site => { 
    90128        name => 'Site', 
    91129        attrs => [ qw( 
    92130            description 
     131            siteNick 
    93132            streetAddress 
    94133            postOfficeBox 
     
    115154            site 
    116155            co 
    117             exported 
     156            unexported 
    118157            description 
    119158        ) ], 
     
    124163            gidNumber description 
    125164            managedBy 
    126             sutype exported 
     165            sutype 
    127166            create 
    128167            date 
     168        ) ], 
     169    }, 
     170    nethost => { 
     171        name => 'Ordinateur', 
     172        attrs => [ qw( 
     173            name 
     174            description 
     175            serialNumber 
     176            owner 
     177            ip 
     178            macaddr 
     179            noDynamic 
     180            cname 
     181            otherName 
     182            reverse 
     183            create 
     184            date 
     185            unexported 
     186        ) ], 
     187    }, 
     188    netzone => { 
     189        name => 'Zone réseau', 
     190        attrs => [ qw( 
     191            name 
     192            description 
     193            type 
     194            net 
     195            netExclude 
     196            outputD 
     197            templateD 
     198            site 
     199            allow_dyn 
     200            dynFrom 
     201            domain 
     202            ipCount 
     203            freeIPCount 
     204            create 
     205            date 
     206            dnsRevision 
     207            lastUpdate 
     208            unexported 
    129209        ) ], 
    130210    }, 
     
    179259    my ($self, $for) = @_; 
    180260    grep { $_ } 
    181     grep { $self->base->check_acl($self->{object} || $self->otype, $_, 'r') } 
    182     grep { $self->base->get_field_name($self->otype, $_, $for || 'a') } 
     261    grep { $self->base->attribute($self->otype, $_) } 
    183262    @{ $forms->{$self->{form}}->{attrs} }; 
    184263} 
     
    196275    my ($self, $attr) = @_; 
    197276    return $self->{c}->req->param($attr) || 
    198         ($self->{object} ? $self->{object}->get_c_field($attr) : '') 
    199 } 
    200  
    201 sub attr_field { 
    202     my ($self, $attr, $type) = @_; 
    203     my $modallow = $self->base->check_acl($self->{object} 
    204         ? ($self->{object}, $attr, 'w') 
    205         : ($self->otype, '@CREATE', 'w')); 
    206     $type ||= $attrs->{$attr}[1] || ''; 
    207     if (!($self->base->get_field_name($self->otype, $attr, 'w') && $modallow)) { 
    208         $type = 'label'; 
    209     } 
    210     # exception: gidNumber is used also in group, but we don't want 
    211     # group list here, really the number ! 
    212     $type = $modallow ? 'text-U:6' : 'label' if ($self->{otype} eq 'group' && $attr eq 'gidNumber'); 
    213     $type ||= 'text'; 
    214      
    215     my $htmlname = $self->escape(($self->{object}  
     277        ($self->{object} ? $self->{object}->get_attributes($attr) : '') 
     278} 
     279 
     280sub attr_field_name { 
     281    my ($self, $attr) = @_; 
     282    return ($self->{object} 
    216283            ? $self->{object}->id . '_' 
    217284            : '' 
    218285        ) . $attr 
    219     ); 
    220     for ($type) { 
    221         /^textarea$/ and return sprintf( 
    222             '<textarea id="%s" name="%s" cols="40">%s</textarea>', 
    223             $self->escape($htmlname), 
    224             $self->escape($htmlname), 
    225             $self->escape($self->attr_raw_value($attr) || ''), 
    226         ); 
    227         /^label$/ and do { 
    228             my $field = $self->escape( 
    229                 $self->attr_raw_value($attr) 
    230             ); 
    231             $field =~ s/\n/<br>/g; 
    232             return $field . sprintf('<input type="hidden" name="%s" value="%s">', 
    233                 $self->escape($htmlname), ($self->attr_raw_value($attr) || '')); 
    234         }; 
    235         /^date$/ and do { 
    236             my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || ''); 
    237             if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) { 
    238                 $date = "$3/$2/$1"; 
    239             } 
    240             my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13"> 
    241             var cal13 = new CalendarPopup(); 
    242             </SCRIPT>} . "\n"; 
    243             $html .= sprintf( 
    244                 '<input type="text" id="%s" name="%s" value="%s" size="12">', 
    245                 $self->escape($htmlname), 
    246                 $self->escape($htmlname), 
    247                 $self->escape($date) 
    248             ); 
    249             $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>}; 
    250             $html .= qq| 
    251             <A HREF="#" 
    252                 onClick="cal13.select(document.forms[0].$htmlname,'${htmlname}_anc','dd/MM/yyyy');return false;" 
    253                 TITLE="cal13.select(document.forms[0].$htmlname,'${htmlname}_anc','dd/MM/yyyy');return false;" 
    254                 NAME="${htmlname}_anc" ID="${htmlname}_anc"> 
    255                 <img src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png)) 
    256                 . qq{" style="ref"></A> 
    257                 } . "\n"; 
    258             return $html; 
    259         }; 
    260         /^checkbox(?::(\w+))?$/ and do { 
    261             my $options = $1 || ''; 
    262             my $text = sprintf('<input type="checkbox" name="%s"%s>', 
    263                 $self->escape($htmlname), 
    264                 $self->attr_raw_value($attr) ? '  checked="yes"' : '' 
    265             );  
    266             $text .= sprintf('<input type="hidden" name="%s">', 
    267                 $self->escape($htmlname)); 
    268             if ($options =~ /l/) { 
    269                 $text .= $self->attr_raw_value($attr) 
    270                     ? ' ' . $self->attr_raw_value($attr) 
    271                     : '';  
    272             } 
    273             return $text; 
    274         }; 
    275         /^select(-\w+)?:([^:\/]+)(?:\/([^:]+))?(?::(.*))?/ and do { 
    276             my $options = $1 || ''; 
    277             my $otype = $2; 
    278             my $filter = $3; 
    279             my $keyfield = $4; 
    280             my $observe_keyfield = $keyfield || 'displayName'; 
    281             my $select = sprintf('<select id="%s" name="%s">', 
    282                 $self->escape($htmlname), 
    283                 $self->escape($htmlname)) . "\n"; 
    284             $select .= '<option value="">--</option>' . "\n" if ($options =~ /N/); 
    285             my $value = $self->attr_raw_value($attr) || ''; 
    286             my $initial_observed = ''; 
    287             foreach my $id (sort $filter 
    288                 ? $self->base->search_objects($otype, $filter) 
    289                 : $self->base->list_objects($otype)) { 
    290                 my $val = $id; 
    291                 if ($keyfield) { 
    292                     my $obj = $self->base->get_object($otype, $id) or next; 
    293                     $val = $obj->get_c_field($keyfield); 
    294                 } 
    295                 $select .= sprintf( 
    296                     '    <option value="%s"%s>%s</option>', 
    297                     $self->escape($val || ''), 
    298                     $value eq $val ? ' selected="selected"' : '', 
    299                     $self->escape($id || ''), 
     286} 
     287 
     288sub attr_field { 
     289    my ($self, $attr, $type) = @_; 
     290 
     291    my $attribute = ($self->{object} 
     292        ? $self->{object}->attribute($attr) 
     293        : $self->base->attribute($self->otype, $attr)) or return; 
     294 
     295    my $htmlname = $self->escape($self->attr_field_name($attr)); 
     296 
     297    my @html_fields; 
     298    foreach my $attr_raw_value ( 
     299        $attribute->{multiple} 
     300            ? ((grep { $_ } $self->attr_raw_value($attr)), 
     301                $attribute->readonly 
     302                    ? () 
     303                    : ('') 
     304            ) 
     305            : ($self->attr_raw_value($attr))) { 
     306 
     307        my $html_id = $htmlname . 
     308            (scalar(@html_fields) ? scalar(@html_fields) : ''); 
     309 
     310        my $html_field = ''; 
     311        for ($attribute->form_type) { 
     312            /^textarea$/i and do { 
     313                $html_field = sprintf( 
     314                    '<textarea id="%s" name="%s" cols="40">%s</textarea>', 
     315                    $self->escape($html_id), 
     316                    $self->escape($htmlname), 
     317                    $self->escape($attr_raw_value || ''), 
    300318                ); 
    301                 $select .= "\n"; 
    302                 if($value eq $val) { 
    303                     if (my $obj = $self->base->get_object($otype, $id)) { 
    304                         $initial_observed = $obj->get_c_field($observe_keyfield) 
    305                         || ''; 
    306                     } 
    307                 } 
    308             } 
    309             $select .= "</select>\n"; 
    310             $select .= $self->{c}->prototype->observe_field( $htmlname, { 
    311                 update => "${htmlname}_span", 
    312                 url => $self->{c}->uri_for('/ajax', 'rawattr', $otype), 
    313                 frequency => 1, 
    314                 with   => "'attr=" . $observe_keyfield . 
    315                 "&id='+element.options[element.selectedIndex].text", 
    316             }) . 
    317             qq|<span id="${htmlname}_span">$initial_observed</span>|; 
    318             return $select; 
    319         }; 
    320         /^text(-\w+)?(?::(\d+))?/ and do { 
    321             my $flag = $1 || ''; 
    322             if (my @allowed = $self->base->obj_attr_allowed_values( 
    323                     $self->{otype}, $attr)) { 
    324                 my $cvalue = $self->attr_raw_value($attr); 
    325                 my $textf = sprintf('<select  id="%s" name="%s">', 
     319                last; 
     320            }; 
     321            /^label$/i and do { 
     322                $attr_raw_value or last; 
     323                $html_field = $self->escape($attr_raw_value); 
     324                $html_field =~ s/\n/<br>/g; 
     325                $html_field .= sprintf('<input type="hidden" name="%s" value="%s">', 
     326                    $self->escape($htmlname), ($attr_raw_value || '')); 
     327                last; 
     328            }; 
     329            /^date$/i and do { 
     330                my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || ''); 
     331                if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) { 
     332                    $date = "$3/$2/$1"; 
     333                } 
     334                my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13"> 
     335                var cal13 = new CalendarPopup(); 
     336                </SCRIPT>} . "\n"; 
     337                $html .= sprintf( 
     338                    '<input type="text" id="%s" name="%s" value="%s" size="12">', 
     339                    $self->escape($html_id), 
    326340                    $self->escape($htmlname), 
     341                    $self->escape($date) 
     342                ); 
     343                $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>}; 
     344                $html .= qq| 
     345                <A HREF="#" 
     346                    onClick="cal13.select(document.getElementById('$html_id'),'${html_id}_anc','dd/MM/yyyy');return false;" 
     347                    TITLE="Date" 
     348                    NAME="${html_id}_anc" ID="${html_id}_anc"> 
     349                    <img class="attr" src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png)) 
     350                    . qq{" style="ref"></A> 
     351                    } . "\n"; 
     352                $html_field = $html; 
     353                last; 
     354            }; 
     355            /^checkbox(?::(\w+))?$/i and do { 
     356                my $options = $1 || ''; 
     357                $html_field = sprintf('<input type="checkbox" name="%s"%s>', 
     358                    $self->escape($htmlname), 
     359                    $attr_raw_value ? '  checked="yes"' : '' 
     360                );  
     361                $html_field .= sprintf('<input type="hidden" name="%s">', 
     362                    $self->escape($htmlname)); 
     363                if ($attribute->form_option('rawvalue')) { 
     364                    $html_field .= $attr_raw_value 
     365                        ? ' ' . $attr_raw_value 
     366                        : '';  
     367                } 
     368                last; 
     369            }; 
     370            /^LIST/ and do { 
     371                my $options = $1 || ''; 
     372                my $select = sprintf('<select id="%s" name="%s">', 
     373                    $self->escape($html_id), 
    327374                    $self->escape($htmlname)) . "\n"; 
    328                 $textf .= '<option value="">--</option>' . "\n"; 
    329                 foreach (sort @allowed) { 
    330                     $textf .= sprintf('<option value="%s"%s>%s</option>' . "\n", 
    331                         $self->escape($_), 
    332                         (($cvalue || '') eq $_ ? ' selected="selected"' : ''), 
    333                         $self->escape($_), 
     375                $select .= '<option value="">--</option>' . "\n" 
     376                    unless($attribute->mandatory); 
     377                my $value = $attr_raw_value || ''; 
     378                my $initial_observed = ''; 
     379                my @valslist; 
     380                foreach my $val (sort $attribute->can_values) { 
     381                    push(@valslist, { 
     382                        val => $val, 
     383                        disp => $attribute->display($val || ''), 
     384                    }); 
     385                } 
     386                foreach (sort { $a->{disp} cmp $b->{disp} } @valslist) { 
     387                    $select .= sprintf( 
     388                        '    <option value="%s"%s>%s</option>', 
     389                        $self->escape($_->{val} || ''), 
     390                        $value eq $_->{val} ? ' selected="selected"' : '', 
     391                        $self->escape($_->{disp} || ''), 
    334392                    ); 
    335                 } 
    336                 $textf .= "</select>\n"; 
    337                 return $textf; 
    338             } else { 
    339                 my $textf = sprintf( 
     393                    $select .= "\n"; 
     394                } 
     395                $select .= "</select>\n"; 
     396 
     397                $html_field = $select; 
     398                last; 
     399            }; 
     400            /^text(-\w+)?(?::(\d+))?/i and do { 
     401                my $flag = $1 || ''; 
     402                $html_field = sprintf( 
    340403                    '<input type="text" id="%s" name="%s" value="%s" size="%d">', 
     404                    $self->escape($html_id), 
    341405                    $self->escape($htmlname), 
    342                     $self->escape($htmlname), 
    343                     $self->escape($self->attr_raw_value($attr)), 
    344                     $2 || 30, 
     406                    $self->escape($attr_raw_value), 
     407                    $attribute->form_option('length') || 30, 
    345408                ); 
    346409                if ($flag =~ /A/) { 
    347                 $textf .= qq|<span id="${htmlname}_auto_complete"></span>|; 
    348                 $textf .= "\n"; 
    349                 $textf .= $self->{c}->prototype->auto_complete_field( 
    350                     $htmlname, 
    351                     { 
    352                     update => "${htmlname}_auto_complete", 
    353                     url => $self->{c}->uri_for('/ajax', 'attrvalues', $self->otype, $attr), 
    354                     indicator => "${htmlname}_stat", min_chars => 1, 
    355                     with => "'val='+document.getElementById('$htmlname').value", 
    356                     frequency => 2, 
    357                     } 
    358                 ); 
    359                 } 
    360                 if ($flag =~ /U/) { 
    361                 $textf .= qq|<span id="${htmlname}_observer_uniq"></span>|; 
    362                 $textf .= "\n"; 
    363                 $textf .= $self->{c}->prototype->observe_field( 
    364                     $htmlname, 
    365                     { 
    366                     update => "${htmlname}_observer_uniq", 
    367                     url => $self->{c}->uri_for('/ajax', 'objattrexist', 
    368                         $self->otype, $attr), 
    369                     frequency => 2, 
    370                     indicator => "${htmlname}_stat", min_chars => 1, 
    371                     with => "'val='+document.getElementById('$htmlname').value" . 
    372                         ($self->{object} ? "+'&exclude=" . $self->{object}->id . "'" : 
    373                             ''), 
    374                     } 
    375                 ); 
    376                 } 
    377                 $textf .= qq|<span style="display:none" id="${htmlname}_stat">Searching...</span>|; 
    378  
    379                 return $textf; 
     410                    $html_field .= qq|<span id="${html_id}_auto_complete"></span>|; 
     411                    $html_field .= "\n"; 
     412                    $html_field .= $self->{c}->prototype->auto_complete_field( 
     413                        $html_id, 
     414                        { 
     415                            update => "${html_id}_auto_complete", 
     416                            url => $self->{c}->uri_for('/ajax', 'attrvalues', $self->otype, $attr), 
     417                            indicator => "${html_id}_stat", min_chars => 1, 
     418                            with => "'val='+document.getElementById('$html_id').value", 
     419                            frequency => 2, 
     420                        } 
     421                    ); 
     422                } 
     423                if ($attribute->uniq) { 
     424                    $html_field .= qq|<span class="inputvalidate" id="${html_id}_observer_uniq"></span>|; 
     425                    $html_field .= "\n"; 
     426                    $html_field .= $self->{c}->prototype->observe_field( 
     427                        $html_id, 
     428                        { 
     429                            update => "${html_id}_observer_uniq", 
     430                            url => $self->{c}->uri_for('/ajax', 'objattrexist', 
     431                                $self->otype, $attr), 
     432                            frequency => 2, 
     433                            indicator => "${html_id}_stat", min_chars => 1, 
     434                            with => "'val='+document.getElementById('$html_id').value" . 
     435                            ($self->{object} ? "+'&exclude=" . $self->{object}->id . "'" : 
     436                                ''), 
     437                        } 
     438                    ); 
     439                    $html_field .= qq|<span style="display:none" id="${html_id}_stat">Searching...</span>|; 
     440                } 
     441                last; 
     442            }; 
     443        } 
     444        if (my $ref = $attribute->reference) { 
     445            my $uri_part= { 
     446                user => 'users', 
     447                group => 'groups', 
     448                nethost => 'nethosts', 
     449                netzone => 'netzones', 
     450                site => 'sites' 
     451            }->{$ref}; 
     452            my $text; 
     453            if ($self->base->attribute($ref, 'displayName')) { 
     454                if ($attr_raw_value && 
     455                    (my $obj = $self->base->get_object($ref, $attr_raw_value))) 
     456                { 
     457                    $text = $obj->get_attributes('displayName'); 
     458                } 
     459                 
     460                $html_field .= $self->{c}->prototype->observe_field( $html_id, { 
     461                        update => "${html_id}_span", 
     462                        url => $self->{c}->uri_for('/ajax', 'rawattr', $ref), 
     463                        frequency => 1, 
     464                        with   => "'attr=displayName" . 
     465                        "&id=' + element.options[element.selectedIndex].text", 
     466                    }) . "\n" if ($attribute->form_type =~ /list/i); 
     467            } elsif($attr_raw_value && $uri_part) { 
     468                $text = sprintf( 
     469                    '<img class="attr" src="%s" title="%s">', 
     470                    $self->{c}->uri_for('/static', 'icons', 'arrow-right.png'), 
     471                    $attr_raw_value, 
     472                ) 
    380473            } 
    381         }; 
     474            $html_field .= sprintf(qq{<span id="%s" style="margin-left: 1em">}, 
     475                "${html_id}_span"); 
     476 
     477            if (defined($text)) { 
     478                $html_field .= $uri_part 
     479                    ? sprintf('<a href="%s">%s</a>', 
     480                        $self->{c}->uri_for("/$uri_part", $attribute->display($attr_raw_value)), 
     481                        $text,) 
     482                    : $text; 
     483            } 
     484 
     485            $html_field .= "</span>\n"; 
     486        } 
     487        push(@html_fields, $html_field); 
    382488    } 
     489    return join("<br>\n", @html_fields); 
    383490} 
    384491 
     
    391498} 
    392499 
     500sub write_attributes { 
     501    my ($self) = @_; 
     502    my @attrs; 
     503    foreach ($self->attributes) { 
     504        my $attr = ($self->{object} 
     505            ? $self->{object}->attribute($_) 
     506            : $self->base->attribute($self->otype, $_)) or next; 
     507        $attr->readonly and next; 
     508        push(@attrs, $_); 
     509    } 
     510    @attrs; 
     511} 
     512 
    393513sub set_attrs { 
    394514    my ($self) = @_; 
     
    396516    my $prefix = $self->{object}->id . '_'; 
    397517    my %fields; 
    398     foreach ( 
    399         grep { $self->base->get_field_name($self->otype, $_, 'w') } 
    400         $self->attributes) { 
     518    foreach ($self->write_attributes) { 
     519        my $attr = ($self->{object} 
     520            ? $self->{object}->attribute($_) 
     521            : $self->base->attribute($self->otype, $_)) or next; 
    401522        if (($attrs->{$_}[1] || '') eq 'checkbox') { 
    402523            $fields{$_} = $self->{c}->req->param("$prefix$_") ? 1 : 0; 
     524        } elsif ($attr->{multiple}) { 
     525            $fields{$_} = [ grep { $_ } $self->{c}->req->param("$prefix$_") ]; 
    403526        } else { 
    404527            $fields{$_} = $self->{c}->req->param("$prefix$_"); 
    405528        } 
    406529    } 
    407     $self->{object}->set_c_fields(%fields) or return; 
     530    $self->{object}->set_c_fields(%fields) or do { 
     531        $self->{c}->stash->{page}{error} = 
     532            LATMOS::Accounts::Log::lastmessage(LA_ERR); 
     533        $self->{object}->base->rollback; 
     534        return; 
     535    }; 
    408536    $self->{object}->base->commit; 
    409537} 
Note: See TracChangeset for help on using the changeset viewer.