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

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

Add expiration and real last working date to forms

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            endLastEmployment
66            manager
67           
68        ) ]
69    },
70    useraddress => {
71        name => 'Adresse',
72        attrs => [ qw(
73            initials
74            mail
75        ) ],
76    },
77    usermy => {
78        name => 'My',
79        attrs => [ qw(
80            snNative
81            givenNameNative
82            wWWHomePage
83            halReference
84        ) ],
85    },
86    site => {
87        name => 'Site',
88        attrs => [ qw(
89            description
90            siteNick
91            streetAddress
92            postOfficeBox
93            postalCode
94            l
95            st
96            co
97            facsimileTelephoneNumber
98            create
99            date
100        ) ],
101    },
102    address => {
103        name => 'Adresse',
104        attrs => [ qw(
105            isMainAddress
106            telephoneNumber
107            streetAddress
108            postOfficeBox
109            postalCode
110            l
111            st
112            physicalDeliveryOfficeName
113            site
114            co
115            unexported
116            description
117            facsimileTelephoneNumber
118        ) ],
119    },
120    employment => {
121        name => 'Nouvelle période',
122        attrs => [ qw(
123            firstday
124            lastday
125            endcircuit
126            contratType
127            managerContact
128            company
129            department
130        ) ],
131    },
132    group => {
133        name => 'SystÚme',
134        attrs => [ qw(
135            gidNumber
136            description label
137            comment
138            managedBy
139            managedAlsoBy
140            autoMemberFilter
141            sutype
142            services
143            create
144            createdby
145            date
146            modifiedby
147        ) ],
148    },
149    nethost => {
150        name => 'Ordinateur',
151        attrs => [ qw(
152            name
153            description
154            comment
155            related
156            serialNumber
157            encryptKey
158            owner
159            user
160            ip
161            macaddr
162            puppetClass
163            noInheritPuppet
164            noDynamic
165            services
166            cname
167            otherName
168            reverse
169            create
170            createdby
171            date
172            modifiedby
173            unexported
174            sshfp
175        ) ],
176    },
177    netzone => {
178        name => 'Zone réseau',
179        attrs => [ qw(
180            name
181            description
182            type
183            net
184            netExclude
185            puppetClass
186            outputD
187            templateD
188            site
189            allow_dyn
190            dynFrom
191            domain
192            ipCount
193            freeIPCount
194            create
195            createdby
196            date
197            modifiedby
198            dnsRevision
199            lastUpdate
200            unexported
201        ) ],
202    },
203    aliases => {
204        name => 'Alias mail',
205        attrs => [ qw(
206            forward
207            expire
208            description
209            comment
210            autoMemberFilter
211            autoExclude
212            finalpoint
213            parents
214            create
215            createdby
216            date
217            modifiedby
218            unexported
219        )],
220    },
221    services => {
222        name => 'Services',
223        attrs => [ qw(
224            type
225            description
226            comment
227            start
228            end
229            manager
230            dependOn
231            create
232            createdby
233            date
234            modifiedby
235            unexported
236        )],
237    },
238};
239
240sub escape {
241    my ($self, $text) = @_;
242    $text ||= '';
243    for ($text) {
244        s/&/&/g;
245        s/</&lt;/g;
246        s/>/&gt;/g;
247        s/"/&quot;/g;
248    }
249    $text;
250}
251
252sub new {
253    my ($class) = @_;
254    bless({}, $class);
255}
256
257# call either OBJ or type + base
258
259sub ACCEPT_CONTEXT {
260    my ($self, $c, $form, $object, $base) = @_;
261    my $new = {};
262    $new->{c} = $c;
263    $new->{form} = $form;
264    $new->{object} = $object if (ref $object);
265    $new->{base} = $base || (ref $object ? $object->base : undef) or return $self;
266    $new->{otype} = ref $object ? $object->type : $object;
267
268
269    if ($form) {
270        foreach (@{ $forms->{$form}->{attrs} }) {
271            $new->{_attr}{$_} = $c->model('AttrFormsA', $_, (ref $object
272                    ? ($object)
273                    : ($base, $new->{otype})))
274                or die "Cannot get attribute $_";
275        }
276    }
277
278    bless($new, __PACKAGE__);
279}
280
281sub base {
282    my ( $self ) = @_;
283    $self->{base}
284}
285
286sub otype {
287    my ($self) = @_;
288    $self->{otype};
289}
290
291sub object {
292    my ($self) = @_;
293    $self->{object};
294}
295
296sub label {
297    my ($self) = @_;
298    $forms->{$self->{form}}->{name} || ''
299}
300
301sub attributes {
302    my ($self, $for) = @_;
303    grep { $_ }
304    grep { $self->base->attribute($self->otype, $_) }
305    @{ $forms->{$self->{form}}->{attrs} };
306}
307
308sub _uri_part {
309    my ($self, $ref) = @_;
310    my $uri_part = {
311        user => 'users',
312        group => 'groups',
313        nethost => 'nethosts',
314        netzone => 'netzones',
315        site => 'sites',
316        aliasess => 'aliases',
317    }->{$ref};
318}
319
320sub attr_label {
321    my ($self, $attr, $label, $hint) = @_;
322
323    if ($label) {
324        return $label;
325    } elsif (my $hash = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
326        return $hash->{label};
327    } else {
328       return $attr;
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.