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

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

Add inventory number to nethost

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