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