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

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

Add firstAidTrainingValidity attribute

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