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

Last change on this file since 2155 was 2155, checked in by nanardon, 6 years ago

Fix: don't use employment_delay when no eployment apply

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