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

Last change on this file since 2078 was 2078, checked in by nanardon, 7 years ago

Add attribute 'contactOnly' on addresses objects

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