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

Last change on this file since 2478 was 2478, checked in by nanardon, 3 years ago

Add attribute BAP and Grade

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