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

Last change on this file since 1613 was 1613, checked in by nanardon, 8 years ago

Some fix on employment form

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