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

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

Add autoFromSutype attribute: merge group member

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