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

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

add employer attribute to employment/user

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