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

Last change on this file since 2097 was 2097, checked in by nanardon, 7 years ago

Add hostType and endOfWarranty attribute

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