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

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

Removing useless attribute from form

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