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

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

Fix object creation after the jquery switch

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