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

Last change on this file since 1394 was 1394, checked in by nanardon, 9 years ago

Fix form pre-filled with default values

File size: 7.4 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            services
31            create
32            date
33        ) ],
34    },
35    user => {
36        name => 'Statut RH',
37        attrs => [ qw(
38            sn givenName description
39            comment
40            company
41            department
42            managerContact
43            manager
44            contratType
45            expire
46            endcircuit
47            snNative
48            givenNameNative
49            wWWHomePage
50            halReference
51            create
52            date
53        ) ],
54    },
55    useraddress => {
56        name => 'Adresse',
57        attrs => [ qw(
58            initials
59            mail
60        ) ],
61    },
62    usermy => {
63        name => 'My',
64        attrs => [ qw(
65            snNative
66            givenNameNative
67            wWWHomePage
68            halReference
69        ) ],
70    },
71    site => {
72        name => 'Site',
73        attrs => [ qw(
74            description
75            siteNick
76            streetAddress
77            postOfficeBox
78            postalCode
79            l
80            st
81            co
82            facsimileTelephoneNumber
83            create
84            date
85        ) ],
86    },
87    address => {
88        name => 'Adresse',
89        attrs => [ qw(
90            isMainAddress
91            telephoneNumber
92            streetAddress
93            postOfficeBox
94            postalCode
95            l
96            st
97            physicalDeliveryOfficeName
98            site
99            co
100            unexported
101            description
102            facsimileTelephoneNumber
103        ) ],
104    },
105    group => {
106        name => 'SystÚme',
107        attrs => [ qw(
108            gidNumber
109            description label
110            comment
111            managedBy
112            managedAlsoBy
113            autoMemberFilter
114            sutype
115            services
116            create
117            date
118        ) ],
119    },
120    nethost => {
121        name => 'Ordinateur',
122        attrs => [ qw(
123            name
124            description
125            comment
126            related
127            serialNumber
128            encryptKey
129            owner
130            user
131            ip
132            macaddr
133            puppetClass
134            noInheritPuppet
135            noDynamic
136            services
137            cname
138            otherName
139            reverse
140            create
141            date
142            unexported
143            sshfp
144        ) ],
145    },
146    netzone => {
147        name => 'Zone réseau',
148        attrs => [ qw(
149            name
150            description
151            type
152            net
153            netExclude
154            puppetClass
155            outputD
156            templateD
157            site
158            allow_dyn
159            dynFrom
160            domain
161            ipCount
162            freeIPCount
163            create
164            date
165            dnsRevision
166            lastUpdate
167            unexported
168        ) ],
169    },
170    aliases => {
171        name => 'Alias mail',
172        attrs => [ qw(
173            forward
174            expire
175            description
176            comment
177            finalpoint
178            parents
179            create
180            date
181            unexported
182        )],
183    },
184    services => {
185        name => 'Services',
186        attrs => [ qw(
187            type
188            description
189            comment
190            start
191            end
192            manager
193            dependOn
194            create
195            date
196            unexported
197        )],
198    },
199};
200
201sub escape {
202    my ($self, $text) = @_;
203    $text ||= '';
204    for ($text) {
205        s/&/&/g;
206        s/</&lt;/g;
207        s/>/&gt;/g;
208        s/"/&quot;/g;
209    }
210    $text;
211}
212
213sub new {
214    my ($class) = @_;
215    bless({}, $class);
216}
217
218# call either OBJ or type + base
219
220sub ACCEPT_CONTEXT {
221    my ($self, $c, $form, $object, $base) = @_;
222    my $new = {};
223    $new->{c} = $c;
224    $new->{form} = $form;
225    $new->{object} = $object if (ref $object);
226    $new->{base} = $base || (ref $object ? $object->base : undef) or return $self;
227    $new->{otype} = ref $object ? $object->type : $object;
228
229
230    if ($form) {
231        foreach (@{ $forms->{$form}->{attrs} }) {
232            $new->{_attr}{$_} = $c->model('AttrFormsA', $_, ($object
233                    ? ($object)
234                    : ($base, $new->{otype})))
235                or die "Cannot get attribute $_";
236        }
237    }
238
239    bless($new, __PACKAGE__);
240}
241
242sub base {
243    my ( $self ) = @_;
244    $self->{base}
245}
246
247sub otype {
248    my ($self) = @_;
249    $self->{otype};
250}
251
252sub object {
253    my ($self) = @_;
254    $self->{object};
255}
256
257sub label {
258    my ($self) = @_;
259    $forms->{$self->{form}}->{name} || ''
260}
261
262sub attributes {
263    my ($self, $for) = @_;
264    grep { $_ }
265    grep { $self->base->attribute($self->otype, $_) }
266    @{ $forms->{$self->{form}}->{attrs} };
267}
268
269sub _uri_part {
270    my ($self, $ref) = @_;
271    my $uri_part = {
272        user => 'users',
273        group => 'groups',
274        nethost => 'nethosts',
275        netzone => 'netzones',
276        site => 'sites',
277        aliasess => 'aliases',
278    }->{$ref};
279}
280
281sub attr_label {
282    my ($self, $attr, $label, $hint) = @_;
283
284    if ($label) {
285        return $label;
286    } elsif (my $hash = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
287        return $hash->{label};
288    } else {
289       return $attr;
290    } 
291}
292
293sub attr_hint {
294    my ($self, $attr) = @_;
295
296    $self->{_attr}{$attr}->attr_hint;
297}
298
299sub attr_field {
300    my ($self, $attr, $type) = @_;
301
302    $self->{_attr}{$attr}->attr_field($type);
303}
304
305sub field {
306    my ($self, $attr, $type) = @_;
307
308    if (!$self->{_attr}{$attr}) {
309        $self->{_attr}{$attr} = $self->{c}->model('AttrFormsA', $attr, ($self->object
310                ? ($self->object)
311                : ($self->base, $self->otype)))
312            or die "Cannot get attribute $_";
313    }
314
315    $self->{_attr}{$attr};
316}
317
318
319sub write_attributes {
320    my ($self) = @_;
321    my @attrs;
322    foreach (values %{ $self->{_attr} }) {
323        $_->readonly and next;
324        push(@attrs, $_);
325    }
326    @attrs;
327}
328
329sub set_attrs {
330    my ($self, $attrs) = @_;
331    $self->{c}->req->param($self->label) || $attrs or return;
332    my %fields;
333    foreach ($attrs ? @{ $attrs } : $self->write_attributes) {
334        my $attr = ref $_ ? $_ : $self->{_attr}{$_};
335        if ($attr->{multiple}) {
336            $fields{$attr->name} = [ grep { $_ } $self->{c}->req->param($attr->htmlname) ];
337        } else {
338            $fields{$attr->name} = $self->{c}->req->param($attr->htmlname);
339        }
340    }
341    $self->{object}->set_c_fields(%fields) or do {
342        $self->{c}->stash->{page}{error} =
343            LATMOS::Accounts::Log::lastmessage(LA_ERR);
344        $self->{object}->base->rollback;
345        return;
346    };
347    $self->{object}->base->commit;
348}
349
350sub submit {
351    my ($self) = @_;
352    return sprintf(
353        '<input type="submit" name="%s" value="Enregistrer">',
354        $self->escape($self->label),
355    );
356}
357
358
359=head1 AUTHOR
360
361Thauvin Olivier
362
363=head1 LICENSE
364
365This library is free software, you can redistribute it and/or modify
366it under the same terms as Perl itself.
367
368=cut
369
3701;
Note: See TracBrowser for help on using the repository browser.