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

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

Don't display services for user to make form lighter

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