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

Last change on this file since 2148 was 2148, checked in by nanardon, 6 years ago

Show new attribute into web interface

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