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

Last change on this file was 2573, checked in by nanardon, 14 months ago

Add manufacturer and model attribute to nethost

File size: 10.9 KB
Line 
1package LATMOS::Accounts::Web::Model::AttrForms;
2
3use strict;
4use warnings;
5use base 'Catalyst::Model';
6use LATMOS::Accounts::Log;
7use HTML::Prototype;
8use utf8;
9
10=head1 NAME
11
12LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model
13
14=head1 DESCRIPTION
15
16Catalyst Model.
17
18=cut
19
20my $forms = {
21    usersys => {
22        name => 'SystÚme',
23        attrs => [ qw(
24            sn givenName
25            description
26            comment
27            managerContact
28            uid uidNumber gidNumber
29            gecos homeDirectory loginShell
30            mail
31            expire
32            endcircuit
33            lastlogin
34            locked
35            sshPublicKey
36            sshPublicKeyFilter
37            delUnknownSshKey
38            createRequestId
39            create
40            createdby
41            date
42            modifiedby
43        ) ],
44    },
45    add => {
46        name => 'Others',
47        attrs => [ qw(
48            sn givenName
49            nationality nativeCountry
50            initials nickname
51            snNative
52            givenNameNative
53            wWWHomePage
54            halReference
55            firstAidTrainingValidity
56        ) ],
57    },
58    user => {
59        name => 'Statut RH',
60        attrs => [ qw(
61            sn givenName
62            nationality nativeCountry
63            description
64            company
65            employer
66            department
67            managerContact
68            manager
69            contratType
70            assigned
71            grade
72            BAP
73            expire
74            endcircuit
75            create
76            createdby
77            date
78            modifiedby
79        ) ],
80    },
81    useremployment => {
82        name => 'Suivi RH',
83        attrs => [ qw(
84            sn givenName
85            nationality nativeCountry
86            expire
87        ) ]
88    },
89    useraddress => {
90        name => 'Adresse',
91        attrs => [ qw(
92            initials
93            mail
94        ) ],
95    },
96    usermy => {
97        name => 'My',
98        attrs => [ qw(
99            snNative
100            givenNameNative
101            wWWHomePage
102            halReference
103        ) ],
104    },
105    site => {
106        name => 'Site',
107        attrs => [ qw(
108            description
109            siteNick
110            streetAddress
111            postOfficeBox
112            postalCode
113            l
114            st
115            co
116            facsimileTelephoneNumber
117            create
118            date
119        ) ],
120    },
121    address => {
122        name => 'Adresse',
123        attrs => [ qw(
124            isMainAddress
125            contactOnly
126            expire
127            telephoneNumber
128            streetAddress
129            postOfficeBox
130            postalCode
131            l
132            st
133            physicalDeliveryOfficeName
134            site
135            co
136            unexported
137            description
138            facsimileTelephoneNumber
139        ) ],
140    },
141    employment => {
142        name => 'Nouvelle période',
143        attrs => [ qw(
144            firstday
145            lastday
146            endcircuit
147            lengthText
148            description
149            company
150            employer
151            contratType
152            assigned
153            grade
154            BAP
155            department
156            managerContact
157            requestId
158        ) ],
159    },
160    group => {
161        name => 'SystÚme',
162        attrs => [ qw(
163            gidNumber
164            description label
165            comment
166            managedBy
167            managedAlsoBy
168            autoMemberFilter
169            autoFromSutype
170            autoInclude
171            autoExclude
172            sutype
173            services
174            create
175            createdby
176            date
177            modifiedby
178        ) ],
179    },
180    nethost => {
181        name => 'Ordinateur',
182        attrs => [ qw(
183            name
184            description
185            manufacturer
186            model
187            hostType
188            comment
189            endOfWarranty
190            serialNumber
191            support
192            inventoryNumber
193            expire
194            netLocked
195            related
196            encryptKey
197            site
198            physicalDeliveryOfficeName
199            owner
200            user
201            ip
202            macaddr
203            puppetClass
204            noInheritPuppet
205            noDynamic
206            services
207            cname
208            otherName
209            reverse
210            unexported
211            vlan
212            radiusParams
213            create
214            createdby
215            date
216            modifiedby
217            sshfp
218        ) ],
219    },
220    netzone => {
221        name => 'Zone réseau',
222        attrs => [ qw(
223            name
224            description
225            type
226            net
227            netExclude
228            puppetClass
229            outputD
230            templateD
231            site
232            allow_dyn
233            dynFrom
234            domain
235            ipCount
236            freeIPCount
237            create
238            createdby
239            date
240            modifiedby
241            dnsRevision
242            lastUpdate
243            unexported
244            vlan
245            hostParams
246        ) ],
247    },
248    aliases => {
249        name => 'Alias mail',
250        attrs => [ qw(
251            expire
252            description
253            comment
254            autoMemberFilter
255            autoFromSutype
256            autoInclude
257            autoExclude
258            autoAddEmail
259            create
260            createdby
261            date
262            modifiedby
263            unexported
264            forward
265            finalpoint
266            parents
267        )],
268    },
269    services => {
270        name => 'Services',
271        attrs => [ qw(
272            type
273            description
274            comment
275            start
276            end
277            manager
278            dependOn
279            create
280            createdby
281            date
282            modifiedby
283            unexported
284        )],
285    },
286    templates => {
287        name => 'Templates',
288        attrs => [ qw(
289            objectType
290            data
291            create
292            createdby
293            date
294            modifiedby
295            unexported
296        )],
297    },
298    sutype => {
299        name => 'Type',
300        attrs => [ qw(
301            description
302            create
303            createdby
304            date
305            modifiedby
306            unexported
307        )],
308    },
309    support => {
310        name => 'Support',
311        attrs => [ qw(
312            description
313            comment
314            phone
315            mail
316            address
317        ) ],
318    },
319};
320
321sub escape {
322    my ($self, $text) = @_;
323    $text ||= '';
324    for ($text) {
325        s/&/&/g;
326        s/</&lt;/g;
327        s/>/&gt;/g;
328        s/"/&quot;/g;
329    }
330    $text;
331}
332
333sub new {
334    my ($class) = @_;
335    bless({}, $class);
336}
337
338# call either OBJ or type + base
339
340sub ACCEPT_CONTEXT {
341    my ($self, $c, $form, $object, $base) = @_;
342    my $new = {};
343    $new->{c} = $c;
344    $new->{form} = $form;
345    $new->{object} = $object if (ref $object);
346    $new->{base} = $base || (ref $object ? $object->base : $c->model('Accounts')->db);
347    $new->{otype} = ref $object ? $object->type : $object;
348
349
350    if ($form) {
351        foreach (@{ $forms->{$form}->{attrs} }) {
352            $new->{_attr}{$_} = $c->model('AttrFormsA', $_, (ref $object
353                    ? ($object)
354                    : ($base, $new->{otype})))
355                or die "Cannot get attribute $_";
356        }
357    }
358
359    bless($new, __PACKAGE__);
360}
361
362sub base {
363    my ( $self ) = @_;
364    $self->{base}
365}
366
367sub otype {
368    my ($self) = @_;
369    $self->{otype};
370}
371
372sub object {
373    my ($self) = @_;
374    $self->{object};
375}
376
377sub label {
378    my ($self) = @_;
379    $forms->{$self->{form}}->{name} || ''
380}
381
382sub attributes {
383    my ($self, $for) = @_;
384    grep { $_ }
385    grep { $self->base->attribute($self->otype, $_) }
386    @{ $forms->{$self->{form}}->{attrs} };
387}
388
389sub _uri_part {
390    my ($self, $ref) = @_;
391    my $uri_part = {
392        user => 'users',
393        group => 'groups',
394        nethost => 'nethosts',
395        netzone => 'netzones',
396        site => 'sites',
397        aliasess => 'aliases',
398    }->{$ref};
399}
400
401sub attr_label {
402    my ($self, $attr, $label, $hint) = @_;
403
404    if ($label) {
405        return $label;
406    } else {
407        my $oattr = $self->base->attribute($self->otype, $attr)
408            or return $attr;
409        my $label = $oattr->label;
410        return $label;
411    }
412}
413
414sub attr_hint {
415    my ($self, $attr) = @_;
416
417    if (my $fattr = $self->{_attr}{$attr}) {
418        return $fattr->attr_hint;
419    } else {
420        return;
421    }
422}
423
424sub attr_field {
425    my ($self, $attr, $type) = @_;
426
427    if (my $fattr = $self->{_attr}{$attr}) {
428        return $fattr->attr_field($type);
429    } else {
430        return;
431    }
432}
433
434sub field {
435    my ($self, $attr, $type) = @_;
436
437    if (!$self->{_attr}{$attr}) {
438        $self->{_attr}{$attr} = $self->{c}->model('AttrFormsA', $attr, ($self->object
439                ? ($self->object)
440                : ($self->base, $self->otype)))
441            or die "Cannot get attribute $_";
442    }
443
444    $self->{_attr}{$attr};
445}
446
447
448sub write_attributes {
449    my ($self) = @_;
450    my @attrs;
451    foreach (values %{ $self->{_attr} }) {
452        $_->readonly and next;
453        push(@attrs, $_);
454    }
455    @attrs;
456}
457
458sub set_attrs {
459    my ($self, $attrs, $id) = @_;
460    $self->{c}->req->param($self->{form}) || $attrs or return;
461    my %fields;
462    foreach ($attrs ? @{ $attrs } : $self->write_attributes) {
463        my $attr = ref $_ ? $_ : $self->{_attr}{$_};
464        $attr ||= $self->{c}->model('AttrFormsA', $_, (
465                ref $self->object
466                    ? ($self->object)
467                    : ($self->base, $self->{otype})
468                )
469        );
470
471        if ($attr->{multiple}) {
472            $fields{$attr->name} = [ grep { $_ } $self->{c}->req->param($attr->htmlname) ];
473        } else {
474            $fields{$attr->name} = $self->{c}->req->param($attr->htmlname) || undef;
475        }
476    }
477    if ($self->object) {
478        $self->object->set_c_fields(%fields) or do {
479            $self->{c}->stash->{page}{error} =
480                LATMOS::Accounts::Log::lastmessage(LA_ERR);
481            $self->object->base->rollback;
482            return;
483        };
484    } else {
485        $id ||= join('', map {('a'..'z')[rand(26)]}(0..8));
486        $self->base->create_c_object($self->otype, $id, %fields) or do {
487            $self->{c}->stash->{page}{error} =
488                LATMOS::Accounts::Log::lastmessage(LA_ERR);
489            $self->base->rollback;
490            return;
491        };
492    }
493    $self->base->commit;
494    $self->reset_param;
495    return $id || $self->object->id;
496   
497}
498
499sub reset_param {
500    my ($self) = @_;
501    foreach (values %{ $self->{_attr} }) {
502        delete($self->{c}->req->params->{$_->htmlname});
503    }
504}
505
506sub submit {
507    my ($self, $hidden) = @_;
508    return sprintf(
509        '<input type="%s" name="%s" value="Enregistrer">',
510        ($hidden ? 'hidden' : 'submit'),
511        $self->escape($self->{form}),
512    );
513}
514
515
516=head1 AUTHOR
517
518Thauvin Olivier
519
520=head1 LICENSE
521
522This library is free software, you can redistribute it and/or modify
523it under the same terms as Perl itself.
524
525=cut
526
5271;
Note: See TracBrowser for help on using the repository browser.