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

Last change on this file since 1500 was 1500, checked in by nanardon, 8 years ago
  • Add expiration list to expiration page and Employment things
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            manager
65           
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            contratType
125            managerContact
126            company
127            department
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    } elsif (my $hash = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
324        return $hash->{label};
325    } else {
326       return $attr;
327    } 
328}
329
330sub attr_hint {
331    my ($self, $attr) = @_;
332
333    $self->{_attr}{$attr}->attr_hint;
334}
335
336sub attr_field {
337    my ($self, $attr, $type) = @_;
338
339    $self->{_attr}{$attr}->attr_field($type);
340}
341
342sub field {
343    my ($self, $attr, $type) = @_;
344
345    if (!$self->{_attr}{$attr}) {
346        $self->{_attr}{$attr} = $self->{c}->model('AttrFormsA', $attr, ($self->object
347                ? ($self->object)
348                : ($self->base, $self->otype)))
349            or die "Cannot get attribute $_";
350    }
351
352    $self->{_attr}{$attr};
353}
354
355
356sub write_attributes {
357    my ($self) = @_;
358    my @attrs;
359    foreach (values %{ $self->{_attr} }) {
360        $_->readonly and next;
361        push(@attrs, $_);
362    }
363    @attrs;
364}
365
366sub set_attrs {
367    my ($self, $attrs, $id) = @_;
368    $self->{c}->req->param($self->{form}) || $attrs or return;
369    my %fields;
370    foreach ($attrs ? @{ $attrs } : $self->write_attributes) {
371        my $attr = ref $_ ? $_ : $self->{_attr}{$_};
372        $attr ||= $self->{c}->model('AttrFormsA', $_, (
373                ref $self->object
374                    ? ($self->object)
375                    : ($self->base, $self->{otype})
376                )
377        );
378
379        if ($attr->{multiple}) {
380            $fields{$attr->name} = [ grep { $_ } $self->{c}->req->param($attr->htmlname) ];
381        } else {
382            $fields{$attr->name} = $self->{c}->req->param($attr->htmlname) || undef;
383        }
384    }
385    if ($self->object) {
386        $self->object->set_c_fields(%fields) or do {
387            $self->{c}->stash->{page}{error} =
388                LATMOS::Accounts::Log::lastmessage(LA_ERR);
389            $self->object->base->rollback;
390            return;
391        };
392    } else {
393        $id ||= join('', map {('a'..'z')[rand(26)]}(0..8));
394        $self->base->create_object($self->otype, $id, %fields) or do {
395            $self->{c}->stash->{page}{error} =
396                LATMOS::Accounts::Log::lastmessage(LA_ERR);
397            $self->base->rollback;
398            return;
399        };
400    }
401    $self->base->commit;
402    $self->reset_param;
403    return $id || $self->object->id;
404   
405}
406
407sub reset_param {
408    my ($self) = @_;
409    foreach (values %{ $self->{_attr} }) {
410        delete($self->{c}->req->params->{$_->htmlname});
411    }
412}
413
414sub submit {
415    my ($self, $hidden) = @_;
416    return sprintf(
417        '<input type="%s" name="%s" value="Enregistrer">',
418        ($hidden ? 'hidden' : 'submit'),
419        $self->escape($self->{form}),
420    );
421}
422
423
424=head1 AUTHOR
425
426Thauvin Olivier
427
428=head1 LICENSE
429
430This library is free software, you can redistribute it and/or modify
431it under the same terms as Perl itself.
432
433=cut
434
4351;
Note: See TracBrowser for help on using the repository browser.