source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Model/AttrForms.pm @ 2264

Last change on this file since 2264 was 2264, checked in by nanardon, 5 years ago

Fix otheraddress attribute issue

File size: 9.9 KB
RevLine 
[111]1package LATMOS::Accounts::Web::Model::AttrForms;
2
3use strict;
4use warnings;
5use base 'Catalyst::Model';
[861]6use LATMOS::Accounts::Log;
[955]7use HTML::Prototype;
[2162]8use utf8;
[111]9
10=head1 NAME
11
12LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model
13
14=head1 DESCRIPTION
15
16Catalyst Model.
17
18=cut
19
20my $forms = {
[894]21    usersys => {
[117]22        name => 'SystÚme',
[112]23        attrs => [ qw(
[2148]24            sn givenName
25            description
[1282]26            comment
[1623]27            managerContact
[2201]28            uid uidNumber gidNumber
29            gecos homeDirectory loginShell
30            mail
[894]31            expire
[1348]32            endcircuit
[677]33            locked
[1953]34            sshPublicKey
[2189]35            sshPublicKeyFilter
[2193]36            delUnknownSshKey
[2114]37            createRequestId
[677]38            create
[1458]39            createdby
[677]40            date
[1458]41            modifiedby
[112]42        ) ],
[117]43    },
[2196]44    add => {
45        name => 'Others',
46        attrs => [ qw(
[2201]47            sn givenName
48            nationality nativeCountry
49            initials nickname
[2196]50            snNative
51            givenNameNative
52            wWWHomePage
53            halReference
54            firstAidTrainingValidity
55        ) ],
56    },
[894]57    user => {
[1264]58        name => 'Statut RH',
[129]59        attrs => [ qw(
[1500]60            sn givenName
[2148]61            nationality nativeCountry
[1500]62            description
[129]63            company
[1860]64            employer
[733]65            department
[1365]66            managerContact
[151]67            manager
[431]68            contratType
[160]69            expire
[1348]70            endcircuit
[894]71            create
[1458]72            createdby
[894]73            date
[1458]74            modifiedby
[129]75        ) ],
76    },
[1500]77    useremployment => {
78        name => 'Suivi RH',
79        attrs => [ qw(
80            sn givenName
[2148]81            nationality nativeCountry
[1517]82            expire
[1500]83        ) ]
84    },
[894]85    useraddress => {
86        name => 'Adresse',
87        attrs => [ qw(
88            initials
89            mail
90        ) ],
91    },
[861]92    usermy => {
93        name => 'My',
94        attrs => [ qw(
95            snNative
96            givenNameNative
97            wWWHomePage
98            halReference
99        ) ],
100    },
[134]101    site => {
102        name => 'Site',
103        attrs => [ qw(
[148]104            description
[861]105            siteNick
[134]106            streetAddress
107            postOfficeBox
108            postalCode
[364]109            l
[160]110            st
[364]111            co
[448]112            facsimileTelephoneNumber
[740]113            create
114            date
[364]115        ) ],
116    },
117    address => {
118        name => 'Adresse',
119        attrs => [ qw(
120            isMainAddress
[2078]121            contactOnly
[1865]122            expire
[364]123            telephoneNumber
124            streetAddress
125            postOfficeBox
126            postalCode
[134]127            l
[364]128            st
129            physicalDeliveryOfficeName
130            site
131            co
[861]132            unexported
[714]133            description
[1329]134            facsimileTelephoneNumber
[134]135        ) ],
136    },
[1500]137    employment => {
138        name => 'Nouvelle période',
139        attrs => [ qw(
140            firstday
141            lastday
142            endcircuit
[1623]143            lengthText
144            description
[1530]145            company
[1860]146            employer
[1500]147            contratType
[2155]148            assigned
[1530]149            department
[1500]150            managerContact
[2114]151            requestId
[1500]152        ) ],
153    },
[125]154    group => {
155        name => 'SystÚme',
156        attrs => [ qw(
[1255]157            gidNumber
158            description label
[1282]159            comment
[183]160            managedBy
[1186]161            managedAlsoBy
[1137]162            autoMemberFilter
[1782]163            autoFromSutype
[2256]164            autoInclude
165            autoExclude
[861]166            sutype
[1349]167            services
[740]168            create
[1458]169            createdby
[740]170            date
[1458]171            modifiedby
[125]172        ) ],
173    },
[861]174    nethost => {
175        name => 'Ordinateur',
176        attrs => [ qw(
177            name
178            description
[2098]179            hostType
[1282]180            comment
[2097]181            endOfWarranty
[861]182            serialNumber
[2054]183            inventoryNumber
[2100]184            expire
[2117]185            netLocked
[2100]186            related
[1237]187            encryptKey
[2100]188            site
[2117]189            physicalDeliveryOfficeName
[861]190            owner
[924]191            user
[861]192            ip
193            macaddr
[1064]194            puppetClass
[1087]195            noInheritPuppet
[861]196            noDynamic
[1349]197            services
[861]198            cname
199            otherName
200            reverse
201            create
[1458]202            createdby
[861]203            date
[1458]204            modifiedby
[861]205            unexported
[1225]206            sshfp
[861]207        ) ],
208    },
209    netzone => {
210        name => 'Zone réseau',
211        attrs => [ qw(
212            name
213            description
214            type
215            net
216            netExclude
[1064]217            puppetClass
[861]218            outputD
219            templateD
220            site
221            allow_dyn
222            dynFrom
223            domain
224            ipCount
225            freeIPCount
226            create
[1458]227            createdby
[861]228            date
[1458]229            modifiedby
[861]230            dnsRevision
231            lastUpdate
232            unexported
[2057]233            hostParams
[861]234        ) ],
235    },
[929]236    aliases => {
237        name => 'Alias mail',
238        attrs => [ qw(
[939]239            expire
[1281]240            description
[1282]241            comment
[1490]242            autoMemberFilter
[1782]243            autoFromSutype
[2256]244            autoInclude
[1490]245            autoExclude
[2256]246            autoAddEmail
[1305]247            create
[1458]248            createdby
[929]249            date
[1458]250            modifiedby
[929]251            unexported
[1738]252            forward
253            finalpoint
254            parents
[929]255        )],
256    },
[1305]257    services => {
258        name => 'Services',
259        attrs => [ qw(
[1346]260            type
[1305]261            description
262            comment
[1346]263            start
264            end
[1305]265            manager
266            dependOn
267            create
[1458]268            createdby
[1305]269            date
[1458]270            modifiedby
[1305]271            unexported
272        )],
273    },
[111]274};
275
[116]276sub escape {
277    my ($self, $text) = @_;
[117]278    $text ||= '';
[116]279    for ($text) {
280        s/&/&/g;
281        s/</&lt;/g;
282        s/>/&gt;/g;
283        s/"/&quot;/g;
284    }
285    $text;
286}
[111]287
288sub new {
289    my ($class) = @_;
290    bless({}, $class);
291}
292
[144]293# call either OBJ or type + base
294
[111]295sub ACCEPT_CONTEXT {
[144]296    my ($self, $c, $form, $object, $base) = @_;
297    my $new = {};
298    $new->{c} = $c;
299    $new->{form} = $form;
300    $new->{object} = $object if (ref $object);
[2264]301    $new->{base} = $base || (ref $object ? $object->base : $c->model('Accounts')->db);
[144]302    $new->{otype} = ref $object ? $object->type : $object;
[1329]303
304
[1394]305    if ($form) {
306        foreach (@{ $forms->{$form}->{attrs} }) {
[1500]307            $new->{_attr}{$_} = $c->model('AttrFormsA', $_, (ref $object
[1394]308                    ? ($object)
309                    : ($base, $new->{otype})))
310                or die "Cannot get attribute $_";
311        }
[1329]312    }
313
[927]314    bless($new, __PACKAGE__);
[111]315}
316
[144]317sub base {
318    my ( $self ) = @_;
319    $self->{base}
320}
321
322sub otype {
323    my ($self) = @_;
324    $self->{otype};
325}
326
[1333]327sub object {
328    my ($self) = @_;
329    $self->{object};
330}
331
[117]332sub label {
333    my ($self) = @_;
334    $forms->{$self->{form}}->{name} || ''
335}
336
[111]337sub attributes {
[373]338    my ($self, $for) = @_;
[507]339    grep { $_ }
[861]340    grep { $self->base->attribute($self->otype, $_) }
[143]341    @{ $forms->{$self->{form}}->{attrs} };
[111]342}
343
[955]344sub _uri_part {
345    my ($self, $ref) = @_;
346    my $uri_part = {
347        user => 'users',
348        group => 'groups',
349        nethost => 'nethosts',
350        netzone => 'netzones',
[1080]351        site => 'sites',
352        aliasess => 'aliases',
[955]353    }->{$ref};
354}
355
[1329]356sub attr_label {
357    my ($self, $attr, $label, $hint) = @_;
[955]358
[1329]359    if ($label) {
360        return $label;
361    } else {
[1551]362        my $oattr = $self->base->attribute($self->otype, $attr)
363            or return $attr;
[1555]364        my $label = $oattr->label;
365        return $label;
[1550]366    }
[1329]367}
[955]368
[1329]369sub attr_hint {
370    my ($self, $attr) = @_;
[861]371
[1329]372    $self->{_attr}{$attr}->attr_hint;
[953]373}
[951]374
[953]375sub attr_field {
376    my ($self, $attr, $type) = @_;
377
[1329]378    $self->{_attr}{$attr}->attr_field($type);
[111]379}
380
[1333]381sub field {
382    my ($self, $attr, $type) = @_;
[174]383
[1333]384    if (!$self->{_attr}{$attr}) {
385        $self->{_attr}{$attr} = $self->{c}->model('AttrFormsA', $attr, ($self->object
386                ? ($self->object)
387                : ($self->base, $self->otype)))
388            or die "Cannot get attribute $_";
389    }
390
391    $self->{_attr}{$attr};
392}
393
394
[861]395sub write_attributes {
396    my ($self) = @_;
397    my @attrs;
[1329]398    foreach (values %{ $self->{_attr} }) {
399        $_->readonly and next;
[861]400        push(@attrs, $_);
401    }
402    @attrs;
403}
404
[112]405sub set_attrs {
[1500]406    my ($self, $attrs, $id) = @_;
407    $self->{c}->req->param($self->{form}) || $attrs or return;
[738]408    my %fields;
[927]409    foreach ($attrs ? @{ $attrs } : $self->write_attributes) {
[1329]410        my $attr = ref $_ ? $_ : $self->{_attr}{$_};
[1500]411        $attr ||= $self->{c}->model('AttrFormsA', $_, (
412                ref $self->object
413                    ? ($self->object)
414                    : ($self->base, $self->{otype})
415                )
416        );
417
[899]418        if ($attr->{multiple}) {
[1329]419            $fields{$attr->name} = [ grep { $_ } $self->{c}->req->param($attr->htmlname) ];
[738]420        } else {
[1500]421            $fields{$attr->name} = $self->{c}->req->param($attr->htmlname) || undef;
[738]422        }
423    }
[1500]424    if ($self->object) {
425        $self->object->set_c_fields(%fields) or do {
426            $self->{c}->stash->{page}{error} =
427                LATMOS::Accounts::Log::lastmessage(LA_ERR);
428            $self->object->base->rollback;
429            return;
430        };
431    } else {
432        $id ||= join('', map {('a'..'z')[rand(26)]}(0..8));
[1945]433        $self->base->create_c_object($self->otype, $id, %fields) or do {
[1500]434            $self->{c}->stash->{page}{error} =
435                LATMOS::Accounts::Log::lastmessage(LA_ERR);
436            $self->base->rollback;
437            return;
438        };
439    }
440    $self->base->commit;
441    $self->reset_param;
442    return $id || $self->object->id;
443   
[112]444}
445
[1500]446sub reset_param {
447    my ($self) = @_;
448    foreach (values %{ $self->{_attr} }) {
449        delete($self->{c}->req->params->{$_->htmlname});
450    }
451}
452
[1329]453sub submit {
[1500]454    my ($self, $hidden) = @_;
[1329]455    return sprintf(
[1500]456        '<input type="%s" name="%s" value="Enregistrer">',
457        ($hidden ? 'hidden' : 'submit'),
458        $self->escape($self->{form}),
[1329]459    );
460}
461
462
[111]463=head1 AUTHOR
464
465Thauvin Olivier
466
467=head1 LICENSE
468
469This library is free software, you can redistribute it and/or modify
470it under the same terms as Perl itself.
471
472=cut
473
4741;
Note: See TracBrowser for help on using the repository browser.